Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 2f6766d

Browse files
committed
valid: Restore ID lookup
Revert a change from d025cfb and don't overwrite ID table entries, so that the first attribute will be returned if there are duplicate IDs. This requires two other changes: - Attributes in entity content are never added to the ID table. This seems reasonable. - Remove the optimization to skip ID lookup when copying and the target document has an empty ID table. This also seems more correct since the document could have ID declarations nevertheless or we could be copying xml:ids into the document for the first time. Fixes #757.
1 parent fc3c432 commit 2f6766d

4 files changed

Lines changed: 8 additions & 14 deletions

File tree

SAX2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,7 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
12551255
} else
12561256
#endif /* LIBXML_VALID_ENABLED */
12571257
if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1258+
(ctxt->input->entity == NULL) &&
12581259
/* Don't create IDs containing entity references */
12591260
(ret->children != NULL) &&
12601261
(ret->children->type == XML_TEXT_NODE) &&
@@ -1987,6 +1988,7 @@ xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
19871988
} else
19881989
#endif /* LIBXML_VALID_ENABLED */
19891990
if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1991+
(ctxt->input->entity == NULL) &&
19901992
/* Don't create IDs containing entity references */
19911993
(ret->children != NULL) &&
19921994
(ret->children->type == XML_TEXT_NODE) &&

result/xmlid/id_tst4.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Object is a Node Set :
22
Set contains 1 nodes:
3-
1 ELEMENT err
3+
1 ELEMENT foo
44
ATTRIBUTE id
55
TEXT
66
content=bar

tree.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4059,7 +4059,6 @@ xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
40594059
*/
40604060
if ((target != NULL) && (cur != NULL) &&
40614061
(target->doc != NULL) && (cur->doc != NULL) &&
4062-
(cur->doc->ids != NULL) &&
40634062
(cur->parent != NULL) &&
40644063
(cur->children != NULL)) {
40654064
int res = xmlIsID(cur->doc, cur->parent, cur);

valid.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,9 +2336,6 @@ xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) {
23362336
if (doc == NULL)
23372337
return(0);
23382338

2339-
if (attr->id != NULL)
2340-
xmlRemoveID(doc, attr);
2341-
23422339
/*
23432340
* Create the ID table if needed.
23442341
*/
@@ -2349,14 +2346,8 @@ xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) {
23492346
return(-1);
23502347
} else {
23512348
id = xmlHashLookup(table, value);
2352-
if (id != NULL) {
2353-
if (id->attr != NULL) {
2354-
id->attr->id = NULL;
2355-
id->attr->atype = 0;
2356-
}
2357-
ret = 0;
2358-
goto done;
2359-
}
2349+
if (id != NULL)
2350+
return(0);
23602351
}
23612352

23622353
id = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
@@ -2374,6 +2365,9 @@ xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) {
23742365
return(-1);
23752366
}
23762367

2368+
if (attr->id != NULL)
2369+
xmlRemoveID(doc, attr);
2370+
23772371
if (xmlHashAddEntry(table, value, id) < 0) {
23782372
xmlFreeID(id);
23792373
return(-1);
@@ -2383,7 +2377,6 @@ xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) {
23832377
if (idPtr != NULL)
23842378
*idPtr = id;
23852379

2386-
done:
23872380
id->attr = attr;
23882381
id->lineno = xmlGetLineNo(attr->parent);
23892382
attr->atype = XML_ATTRIBUTE_ID;

0 commit comments

Comments
 (0)