Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * tree.c : implementation of access function for an XML tree. |
3 | | * |
4 | | * References: |
5 | | * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/ |
6 | | * |
7 | | * See Copyright for the status of this software. |
8 | | * |
9 | | * Author: Daniel Veillard |
10 | | * |
11 | | */ |
12 | | |
13 | | /* To avoid EBCDIC trouble when parsing on zOS */ |
14 | | #if defined(__MVS__) |
15 | | #pragma convert("ISO8859-1") |
16 | | #endif |
17 | | |
18 | | #define IN_LIBXML |
19 | | #include "libxml.h" |
20 | | |
21 | | #include <string.h> /* for memset() only ! */ |
22 | | #include <stddef.h> |
23 | | #include <limits.h> |
24 | | #include <ctype.h> |
25 | | #include <stdlib.h> |
26 | | |
27 | | #ifdef LIBXML_ZLIB_ENABLED |
28 | | #include <zlib.h> |
29 | | #endif |
30 | | |
31 | | #include <libxml/tree.h> |
32 | | #include <libxml/xmlmemory.h> |
33 | | #include <libxml/parser.h> |
34 | | #include <libxml/uri.h> |
35 | | #include <libxml/entities.h> |
36 | | #include <libxml/xmlerror.h> |
37 | | #include <libxml/parserInternals.h> |
38 | | #ifdef LIBXML_HTML_ENABLED |
39 | | #include <libxml/HTMLtree.h> |
40 | | #endif |
41 | | |
42 | | #include "private/buf.h" |
43 | | #include "private/entities.h" |
44 | | #include "private/error.h" |
45 | | #include "private/memory.h" |
46 | | #include "private/io.h" |
47 | | #include "private/parser.h" |
48 | | #include "private/tree.h" |
49 | | |
50 | | #ifndef SIZE_MAX |
51 | | #define SIZE_MAX ((size_t) -1) |
52 | | #endif |
53 | | |
54 | | /* |
55 | | * Internal variable indicating whether a callback has been registered |
56 | | * for node creation/destruction. This avoids looking up thread-local |
57 | | * data if no callback was ever registered. |
58 | | */ |
59 | | int xmlRegisterCallbacks = 0; |
60 | | |
61 | | /************************************************************************ |
62 | | * * |
63 | | * Forward declarations * |
64 | | * * |
65 | | ************************************************************************/ |
66 | | |
67 | | static xmlNodePtr |
68 | | xmlNewEntityRef(xmlDocPtr doc, xmlChar *name); |
69 | | |
70 | | static xmlNsPtr |
71 | | xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns); |
72 | | |
73 | | static xmlAttrPtr |
74 | | xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name, |
75 | | const xmlChar *nsName, int useDTD); |
76 | | |
77 | | static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop); |
78 | | |
79 | | static void |
80 | | xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree); |
81 | | |
82 | | static void |
83 | | xmlUnlinkNodeInternal(xmlNodePtr cur); |
84 | | |
85 | | /************************************************************************ |
86 | | * * |
87 | | * A few static variables and macros * |
88 | | * * |
89 | | ************************************************************************/ |
90 | | /* #undef xmlStringText */ |
91 | | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
92 | | /* #undef xmlStringTextNoenc */ |
93 | | const xmlChar xmlStringTextNoenc[] = |
94 | | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
95 | | /* #undef xmlStringComment */ |
96 | | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
97 | | |
98 | | static int xmlCompressMode = 0; |
99 | | |
100 | 4.53M | #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \ |
101 | 4.53M | (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0)) |
102 | | |
103 | | /************************************************************************ |
104 | | * * |
105 | | * Functions to move to entities.c once the * |
106 | | * API freeze is smoothen and they can be made public. * |
107 | | * * |
108 | | ************************************************************************/ |
109 | | #include <libxml/hash.h> |
110 | | |
111 | | /** |
112 | | * Do an entity lookup in the DTD entity hash table and |
113 | | * |
114 | | * @param dtd A pointer to the DTD to search |
115 | | * @param name The entity name |
116 | | * @returns the corresponding entity, if found. |
117 | | */ |
118 | | static xmlEntityPtr |
119 | 0 | xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) { |
120 | 0 | xmlEntitiesTablePtr table; |
121 | |
|
122 | 0 | if((dtd != NULL) && (dtd->entities != NULL)) { |
123 | 0 | table = (xmlEntitiesTablePtr) dtd->entities; |
124 | 0 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
125 | | /* return(xmlGetEntityFromTable(table, name)); */ |
126 | 0 | } |
127 | 0 | return(NULL); |
128 | 0 | } |
129 | | /** |
130 | | * Do an entity lookup in the DTD parameter entity hash table and |
131 | | * |
132 | | * @param dtd A pointer to the DTD to search |
133 | | * @param name The entity name |
134 | | * @returns the corresponding entity, if found. |
135 | | */ |
136 | | static xmlEntityPtr |
137 | 0 | xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) { |
138 | 0 | xmlEntitiesTablePtr table; |
139 | |
|
140 | 0 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
141 | 0 | table = (xmlEntitiesTablePtr) dtd->pentities; |
142 | 0 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
143 | | /* return(xmlGetEntityFromTable(table, name)); */ |
144 | 0 | } |
145 | 0 | return(NULL); |
146 | 0 | } |
147 | | |
148 | | /************************************************************************ |
149 | | * * |
150 | | * QName handling helper * |
151 | | * * |
152 | | ************************************************************************/ |
153 | | |
154 | | /** |
155 | | * Build a QName from prefix and local name. |
156 | | * |
157 | | * Builds the QName `prefix:ncname` in `memory` if there is enough space |
158 | | * and prefix is not NULL nor empty, otherwise allocate a new string. |
159 | | * If prefix is NULL or empty it returns ncname. |
160 | | * |
161 | | * @param ncname the Name |
162 | | * @param prefix the prefix |
163 | | * @param memory preallocated memory |
164 | | * @param len preallocated memory length |
165 | | * @returns the new string which must be freed by the caller if different from |
166 | | * `memory` and `ncname` or NULL in case of error |
167 | | */ |
168 | | xmlChar * |
169 | | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
170 | 52.8k | xmlChar *memory, int len) { |
171 | 52.8k | size_t lenn, lenp; |
172 | 52.8k | xmlChar *ret; |
173 | | |
174 | 52.8k | if ((ncname == NULL) || (len < 0)) return(NULL); |
175 | 52.8k | if (prefix == NULL) return((xmlChar *) ncname); |
176 | | |
177 | 52.8k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
178 | | /* Make allocation more likely */ |
179 | 52.8k | if (len > 8) |
180 | 43.2k | len = 8; |
181 | 52.8k | #endif |
182 | | |
183 | 52.8k | lenn = strlen((char *) ncname); |
184 | 52.8k | lenp = strlen((char *) prefix); |
185 | 52.8k | if (lenn >= SIZE_MAX - lenp - 1) |
186 | 0 | return(NULL); |
187 | | |
188 | 52.8k | if ((memory == NULL) || ((size_t) len < lenn + lenp + 2)) { |
189 | 50.2k | ret = xmlMalloc(lenn + lenp + 2); |
190 | 50.2k | if (ret == NULL) |
191 | 150 | return(NULL); |
192 | 50.2k | } else { |
193 | 2.54k | ret = memory; |
194 | 2.54k | } |
195 | 52.6k | memcpy(&ret[0], prefix, lenp); |
196 | 52.6k | ret[lenp] = ':'; |
197 | 52.6k | memcpy(&ret[lenp + 1], ncname, lenn); |
198 | 52.6k | ret[lenn + lenp + 1] = 0; |
199 | 52.6k | return(ret); |
200 | 52.8k | } |
201 | | |
202 | | /** |
203 | | * Parse an XML qualified name. |
204 | | * |
205 | | * @deprecated This function doesn't report malloc failures. |
206 | | * |
207 | | * [NS 5] QName ::= (Prefix ':')? LocalPart |
208 | | * |
209 | | * [NS 6] Prefix ::= NCName |
210 | | * |
211 | | * [NS 7] LocalPart ::= NCName |
212 | | * |
213 | | * @param name the full QName |
214 | | * @param prefix a xmlChar ** |
215 | | * @returns NULL if the name doesn't have a prefix. Otherwise, returns the |
216 | | * local part, and prefix is updated to get the Prefix. Both the return value |
217 | | * and the prefix must be freed by the caller. |
218 | | */ |
219 | | xmlChar * |
220 | 44.7k | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
221 | 44.7k | int len = 0; |
222 | 44.7k | xmlChar *ret = NULL; |
223 | | |
224 | 44.7k | if (prefix == NULL) return(NULL); |
225 | 44.7k | *prefix = NULL; |
226 | 44.7k | if (name == NULL) return(NULL); |
227 | | |
228 | | /* nasty but valid */ |
229 | 44.0k | if (name[0] == ':') |
230 | 729 | return(NULL); |
231 | | |
232 | | /* |
233 | | * we are not trying to validate but just to cut, and yes it will |
234 | | * work even if this is as set of UTF-8 encoded chars |
235 | | */ |
236 | 923k | while ((name[len] != 0) && (name[len] != ':')) |
237 | 880k | len++; |
238 | | |
239 | 43.3k | if ((name[len] == 0) || (name[len+1] == 0)) |
240 | 8.07k | return(NULL); |
241 | | |
242 | 35.2k | *prefix = xmlStrndup(name, len); |
243 | 35.2k | if (*prefix == NULL) |
244 | 6 | return(NULL); |
245 | 35.2k | ret = xmlStrdup(&name[len + 1]); |
246 | 35.2k | if (ret == NULL) { |
247 | 5 | if (*prefix != NULL) { |
248 | 5 | xmlFree(*prefix); |
249 | 5 | *prefix = NULL; |
250 | 5 | } |
251 | 5 | return(NULL); |
252 | 5 | } |
253 | | |
254 | 35.2k | return(ret); |
255 | 35.2k | } |
256 | | |
257 | | /** |
258 | | * Parse an XML qualified name. |
259 | | * |
260 | | * @param name the full QName |
261 | | * @param len an int * |
262 | | * @returns NULL if it is not a Qualified Name, otherwise, update len |
263 | | * with the length in byte of the prefix and return a pointer |
264 | | * to the start of the name without the prefix |
265 | | */ |
266 | | |
267 | | const xmlChar * |
268 | 170k | xmlSplitQName3(const xmlChar *name, int *len) { |
269 | 170k | int l = 0; |
270 | | |
271 | 170k | if (name == NULL) return(NULL); |
272 | 170k | if (len == NULL) return(NULL); |
273 | | |
274 | | /* nasty but valid */ |
275 | 170k | if (name[0] == ':') |
276 | 1.34k | return(NULL); |
277 | | |
278 | | /* |
279 | | * we are not trying to validate but just to cut, and yes it will |
280 | | * work even if this is as set of UTF-8 encoded chars |
281 | | */ |
282 | 921k | while ((name[l] != 0) && (name[l] != ':')) |
283 | 751k | l++; |
284 | | |
285 | 169k | if ((name[l] == 0) || (name[l+1] == 0)) |
286 | 140k | return(NULL); |
287 | | |
288 | 28.9k | *len = l; |
289 | | |
290 | 28.9k | return(&name[l+1]); |
291 | 169k | } |
292 | | |
293 | | /** |
294 | | * Parse a QName. |
295 | | * |
296 | | * The return value points to the start of the local |
297 | | * name in the input string. If the QName has a prefix, it will be |
298 | | * allocated and stored in `prefixPtr`. This string must be freed by |
299 | | * the caller. If there's no prefix, `prefixPtr` is set to NULL. |
300 | | * |
301 | | * @param name the full QName |
302 | | * @param prefixPtr pointer to resulting prefix |
303 | | * @returns the local name or NULL if a memory allocation failed. |
304 | | */ |
305 | | const xmlChar * |
306 | 141k | xmlSplitQName4(const xmlChar *name, xmlChar **prefixPtr) { |
307 | 141k | xmlChar *prefix; |
308 | 141k | int l = 0; |
309 | | |
310 | 141k | if ((name == NULL) || (prefixPtr == NULL)) |
311 | 0 | return(NULL); |
312 | | |
313 | 141k | *prefixPtr = NULL; |
314 | | |
315 | | /* nasty but valid */ |
316 | 141k | if (name[0] == ':') |
317 | 654 | return(name); |
318 | | |
319 | | /* |
320 | | * we are not trying to validate but just to cut, and yes it will |
321 | | * work even if this is as set of UTF-8 encoded chars |
322 | | */ |
323 | 917k | while ((name[l] != 0) && (name[l] != ':')) |
324 | 777k | l++; |
325 | | |
326 | | /* |
327 | | * TODO: What about names with multiple colons? |
328 | | */ |
329 | 140k | if ((name[l] == 0) || (name[l+1] == 0)) |
330 | 125k | return(name); |
331 | | |
332 | 15.4k | prefix = xmlStrndup(name, l); |
333 | 15.4k | if (prefix == NULL) |
334 | 6 | return(NULL); |
335 | | |
336 | 15.4k | *prefixPtr = prefix; |
337 | 15.4k | return(&name[l+1]); |
338 | 15.4k | } |
339 | | |
340 | | /************************************************************************ |
341 | | * * |
342 | | * Check Name, NCName and QName strings * |
343 | | * * |
344 | | ************************************************************************/ |
345 | | |
346 | | /** |
347 | | * Check that a value conforms to the lexical space of NCName |
348 | | * |
349 | | * @param value the value to check |
350 | | * @param space allow spaces in front and end of the string |
351 | | * @returns 0 if this validates, a positive error code number otherwise |
352 | | * and -1 in case of internal or API error. |
353 | | */ |
354 | | int |
355 | 16.6k | xmlValidateNCName(const xmlChar *value, int space) { |
356 | 16.6k | const xmlChar *cur; |
357 | | |
358 | 16.6k | if (value == NULL) |
359 | 0 | return(-1); |
360 | | |
361 | 16.6k | cur = value; |
362 | | |
363 | 16.6k | if (space) { |
364 | 9.60k | while (IS_BLANK_CH(*cur)) |
365 | 5.42k | cur++; |
366 | 9.60k | } |
367 | | |
368 | 16.6k | value = cur; |
369 | 16.6k | cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC); |
370 | 16.6k | if ((cur == NULL) || (cur == value)) |
371 | 2.49k | return(1); |
372 | | |
373 | 14.1k | if (space) { |
374 | 8.11k | while (IS_BLANK_CH(*cur)) |
375 | 8.81k | cur++; |
376 | 8.11k | } |
377 | | |
378 | 14.1k | return(*cur != 0); |
379 | 16.6k | } |
380 | | |
381 | | /** |
382 | | * Check that a value conforms to the lexical space of QName |
383 | | * |
384 | | * @param value the value to check |
385 | | * @param space allow spaces in front and end of the string |
386 | | * @returns 0 if this validates, a positive error code number otherwise |
387 | | * and -1 in case of internal or API error. |
388 | | */ |
389 | | int |
390 | 195k | xmlValidateQName(const xmlChar *value, int space) { |
391 | 195k | const xmlChar *cur; |
392 | | |
393 | 195k | if (value == NULL) |
394 | 0 | return(-1); |
395 | | |
396 | 195k | cur = value; |
397 | | |
398 | 195k | if (space) { |
399 | 0 | while (IS_BLANK_CH(*cur)) |
400 | 0 | cur++; |
401 | 0 | } |
402 | | |
403 | 195k | value = cur; |
404 | 195k | cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC); |
405 | 195k | if ((cur == NULL) || (cur == value)) |
406 | 24.6k | return(1); |
407 | | |
408 | 170k | if (*cur == ':') { |
409 | 17.5k | cur += 1; |
410 | 17.5k | value = cur; |
411 | 17.5k | cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC); |
412 | 17.5k | if ((cur == NULL) || (cur == value)) |
413 | 4.68k | return(1); |
414 | 17.5k | } |
415 | | |
416 | 165k | if (space) { |
417 | 0 | while (IS_BLANK_CH(*cur)) |
418 | 0 | cur++; |
419 | 0 | } |
420 | | |
421 | 165k | return(*cur != 0); |
422 | 170k | } |
423 | | |
424 | | /** |
425 | | * Check that a value conforms to the lexical space of Name |
426 | | * |
427 | | * @param value the value to check |
428 | | * @param space allow spaces in front and end of the string |
429 | | * @returns 0 if this validates, a positive error code number otherwise |
430 | | * and -1 in case of internal or API error. |
431 | | */ |
432 | | int |
433 | 0 | xmlValidateName(const xmlChar *value, int space) { |
434 | 0 | const xmlChar *cur; |
435 | |
|
436 | 0 | if (value == NULL) |
437 | 0 | return(-1); |
438 | | |
439 | 0 | cur = value; |
440 | |
|
441 | 0 | if (space) { |
442 | 0 | while (IS_BLANK_CH(*cur)) |
443 | 0 | cur++; |
444 | 0 | } |
445 | |
|
446 | 0 | value = cur; |
447 | 0 | cur = xmlScanName(value, SIZE_MAX, 0); |
448 | 0 | if ((cur == NULL) || (cur == value)) |
449 | 0 | return(1); |
450 | | |
451 | 0 | if (space) { |
452 | 0 | while (IS_BLANK_CH(*cur)) |
453 | 0 | cur++; |
454 | 0 | } |
455 | |
|
456 | 0 | return(*cur != 0); |
457 | 0 | } |
458 | | |
459 | | /** |
460 | | * Check that a value conforms to the lexical space of NMToken |
461 | | * |
462 | | * @param value the value to check |
463 | | * @param space allow spaces in front and end of the string |
464 | | * @returns 0 if this validates, a positive error code number otherwise |
465 | | * and -1 in case of internal or API error. |
466 | | */ |
467 | | int |
468 | 0 | xmlValidateNMToken(const xmlChar *value, int space) { |
469 | 0 | const xmlChar *cur; |
470 | |
|
471 | 0 | if (value == NULL) |
472 | 0 | return(-1); |
473 | | |
474 | 0 | cur = value; |
475 | |
|
476 | 0 | if (space) { |
477 | 0 | while (IS_BLANK_CH(*cur)) |
478 | 0 | cur++; |
479 | 0 | } |
480 | |
|
481 | 0 | value = cur; |
482 | 0 | cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NMTOKEN); |
483 | 0 | if ((cur == NULL) || (cur == value)) |
484 | 0 | return(1); |
485 | | |
486 | 0 | if (space) { |
487 | 0 | while (IS_BLANK_CH(*cur)) |
488 | 0 | cur++; |
489 | 0 | } |
490 | |
|
491 | 0 | return(*cur != 0); |
492 | 0 | } |
493 | | |
494 | | /************************************************************************ |
495 | | * * |
496 | | * Allocation and deallocation of basic structures * |
497 | | * * |
498 | | ************************************************************************/ |
499 | | |
500 | | /** |
501 | | * Create a new namespace. For a default namespace, `prefix` should be |
502 | | * NULL. The namespace URI in `href` is not checked. You should make sure |
503 | | * to pass a valid URI. |
504 | | * |
505 | | * If `node` is provided, it must be an element node. The namespace will |
506 | | * be appended to the node's namespace declarations. It is an error if |
507 | | * the node already has a definition for the prefix or default |
508 | | * namespace. |
509 | | * |
510 | | * @param node the element carrying the namespace (optional) |
511 | | * @param href the URI associated |
512 | | * @param prefix the prefix for the namespace (optional) |
513 | | * @returns a new namespace pointer or NULL if arguments are invalid, |
514 | | * the prefix is already in use or a memory allocation failed. |
515 | | */ |
516 | | xmlNs * |
517 | 4.06M | xmlNewNs(xmlNode *node, const xmlChar *href, const xmlChar *prefix) { |
518 | 4.06M | xmlNsPtr cur; |
519 | | |
520 | 4.06M | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
521 | 0 | return(NULL); |
522 | | |
523 | | /* |
524 | | * Allocate a new Namespace and fill the fields. |
525 | | */ |
526 | 4.06M | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
527 | 4.06M | if (cur == NULL) |
528 | 3.55k | return(NULL); |
529 | 4.06M | memset(cur, 0, sizeof(xmlNs)); |
530 | 4.06M | cur->type = XML_LOCAL_NAMESPACE; |
531 | | |
532 | 4.06M | if (href != NULL) { |
533 | 4.06M | cur->href = xmlStrdup(href); |
534 | 4.06M | if (cur->href == NULL) |
535 | 217 | goto error; |
536 | 4.06M | } |
537 | 4.06M | if (prefix != NULL) { |
538 | 3.88M | cur->prefix = xmlStrdup(prefix); |
539 | 3.88M | if (cur->prefix == NULL) |
540 | 233 | goto error; |
541 | 3.88M | } |
542 | | |
543 | | /* |
544 | | * Add it at the end to preserve parsing order ... |
545 | | * and checks for existing use of the prefix |
546 | | */ |
547 | 4.06M | if (node != NULL) { |
548 | 3.31M | if (node->nsDef == NULL) { |
549 | 1.46M | node->nsDef = cur; |
550 | 1.85M | } else { |
551 | 1.85M | xmlNsPtr prev = node->nsDef; |
552 | | |
553 | 1.85M | if ((xmlStrEqual(prev->prefix, cur->prefix)) && |
554 | 1.85M | (prev->href != NULL)) |
555 | 5.98k | goto error; |
556 | 7.58M | while (prev->next != NULL) { |
557 | 5.73M | prev = prev->next; |
558 | 5.73M | if ((xmlStrEqual(prev->prefix, cur->prefix)) && |
559 | 5.73M | (prev->href != NULL)) |
560 | 2.95k | goto error; |
561 | 5.73M | } |
562 | 1.84M | prev->next = cur; |
563 | 1.84M | } |
564 | 3.31M | } |
565 | 4.05M | return(cur); |
566 | | |
567 | 9.39k | error: |
568 | 9.39k | xmlFreeNs(cur); |
569 | 9.39k | return(NULL); |
570 | 4.06M | } |
571 | | |
572 | | /** |
573 | | * Set the namespace of an element or attribute node. Passing a NULL |
574 | | * namespace unsets the namespace. |
575 | | * |
576 | | * @param node a node in the document |
577 | | * @param ns a namespace pointer (optional) |
578 | | */ |
579 | | void |
580 | 0 | xmlSetNs(xmlNode *node, xmlNs *ns) { |
581 | 0 | if (node == NULL) { |
582 | 0 | return; |
583 | 0 | } |
584 | 0 | if ((node->type == XML_ELEMENT_NODE) || |
585 | 0 | (node->type == XML_ATTRIBUTE_NODE)) |
586 | 0 | node->ns = ns; |
587 | 0 | } |
588 | | |
589 | | /** |
590 | | * Free an xmlNs object. |
591 | | * |
592 | | * @param cur the namespace pointer |
593 | | */ |
594 | | void |
595 | 4.12M | xmlFreeNs(xmlNs *cur) { |
596 | 4.12M | if (cur == NULL) { |
597 | 0 | return; |
598 | 0 | } |
599 | 4.12M | if (cur->href != NULL) xmlFree((char *) cur->href); |
600 | 4.12M | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
601 | 4.12M | xmlFree(cur); |
602 | 4.12M | } |
603 | | |
604 | | /** |
605 | | * Free a list of xmlNs objects. |
606 | | * |
607 | | * @param cur the first namespace pointer |
608 | | */ |
609 | | void |
610 | 2.15M | xmlFreeNsList(xmlNs *cur) { |
611 | 2.15M | xmlNsPtr next; |
612 | 2.15M | if (cur == NULL) { |
613 | 3 | return; |
614 | 3 | } |
615 | 6.27M | while (cur != NULL) { |
616 | 4.11M | next = cur->next; |
617 | 4.11M | xmlFreeNs(cur); |
618 | 4.11M | cur = next; |
619 | 4.11M | } |
620 | 2.15M | } |
621 | | |
622 | | /** |
623 | | * Create a DTD node. |
624 | | * |
625 | | * If a document is provided, it is an error if it already has an |
626 | | * external subset. If the document has no external subset, it |
627 | | * will be set to the created DTD. |
628 | | * |
629 | | * To create an internal subset, use #xmlCreateIntSubset. |
630 | | * |
631 | | * @param doc the document pointer (optional) |
632 | | * @param name the DTD name (optional) |
633 | | * @param publicId public identifier of the DTD (optional) |
634 | | * @param systemId system identifier (URL) of the DTD (optional) |
635 | | * @returns a pointer to the new DTD object or NULL if arguments are |
636 | | * invalid or a memory allocation failed. |
637 | | */ |
638 | | xmlDtd * |
639 | | xmlNewDtd(xmlDoc *doc, const xmlChar *name, const xmlChar *publicId, |
640 | 30.7k | const xmlChar *systemId) { |
641 | 30.7k | xmlDtdPtr cur; |
642 | | |
643 | 30.7k | if ((doc != NULL) && (doc->extSubset != NULL)) { |
644 | 0 | return(NULL); |
645 | 0 | } |
646 | | |
647 | | /* |
648 | | * Allocate a new DTD and fill the fields. |
649 | | */ |
650 | 30.7k | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
651 | 30.7k | if (cur == NULL) |
652 | 2 | return(NULL); |
653 | 30.7k | memset(cur, 0 , sizeof(xmlDtd)); |
654 | 30.7k | cur->type = XML_DTD_NODE; |
655 | | |
656 | 30.7k | if (name != NULL) { |
657 | 30.7k | cur->name = xmlStrdup(name); |
658 | 30.7k | if (cur->name == NULL) |
659 | 7 | goto error; |
660 | 30.7k | } |
661 | 30.7k | if (publicId != NULL) { |
662 | 1.21k | cur->ExternalID = xmlStrdup(publicId); |
663 | 1.21k | if (cur->ExternalID == NULL) |
664 | 2 | goto error; |
665 | 1.21k | } |
666 | 30.7k | if (systemId != NULL) { |
667 | 14.7k | cur->SystemID = xmlStrdup(systemId); |
668 | 14.7k | if (cur->SystemID == NULL) |
669 | 1 | goto error; |
670 | 14.7k | } |
671 | 30.7k | if (doc != NULL) |
672 | 30.7k | doc->extSubset = cur; |
673 | 30.7k | cur->doc = doc; |
674 | | |
675 | 30.7k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
676 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
677 | 30.7k | return(cur); |
678 | | |
679 | 10 | error: |
680 | 10 | xmlFreeDtd(cur); |
681 | 10 | return(NULL); |
682 | 30.7k | } |
683 | | |
684 | | /** |
685 | | * Get the internal subset of a document. |
686 | | * |
687 | | * @param doc the document pointer |
688 | | * @returns a pointer to the DTD object or NULL if not found. |
689 | | */ |
690 | | xmlDtd * |
691 | 290k | xmlGetIntSubset(const xmlDoc *doc) { |
692 | 290k | xmlNodePtr cur; |
693 | | |
694 | 290k | if (doc == NULL) |
695 | 0 | return(NULL); |
696 | 290k | cur = doc->children; |
697 | 354k | while (cur != NULL) { |
698 | 64.2k | if (cur->type == XML_DTD_NODE) |
699 | 388 | return((xmlDtdPtr) cur); |
700 | 63.8k | cur = cur->next; |
701 | 63.8k | } |
702 | 290k | return((xmlDtdPtr) doc->intSubset); |
703 | 290k | } |
704 | | |
705 | | /** |
706 | | * Create a DTD node. |
707 | | * |
708 | | * If a document is provided and it already has an internal subset, |
709 | | * the existing DTD object is returned without creating a new object. |
710 | | * If the document has no internal subset, it will be set to the |
711 | | * created DTD. |
712 | | * |
713 | | * @param doc the document pointer (optional) |
714 | | * @param name the DTD name (optional) |
715 | | * @param publicId public identifier of the DTD (optional) |
716 | | * @param systemId system identifier (URL) of the DTD (optional) |
717 | | * @returns a pointer to the new or existing DTD object or NULL if |
718 | | * arguments are invalid or a memory allocation failed. |
719 | | */ |
720 | | xmlDtd * |
721 | | xmlCreateIntSubset(xmlDoc *doc, const xmlChar *name, const xmlChar *publicId, |
722 | 108k | const xmlChar *systemId) { |
723 | 108k | xmlDtdPtr cur; |
724 | | |
725 | 108k | if (doc != NULL) { |
726 | 108k | cur = xmlGetIntSubset(doc); |
727 | 108k | if (cur != NULL) |
728 | 0 | return(cur); |
729 | 108k | } |
730 | | |
731 | | /* |
732 | | * Allocate a new DTD and fill the fields. |
733 | | */ |
734 | 108k | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
735 | 108k | if (cur == NULL) |
736 | 10 | return(NULL); |
737 | 108k | memset(cur, 0, sizeof(xmlDtd)); |
738 | 108k | cur->type = XML_DTD_NODE; |
739 | | |
740 | 108k | if (name != NULL) { |
741 | 108k | cur->name = xmlStrdup(name); |
742 | 108k | if (cur->name == NULL) |
743 | 7 | goto error; |
744 | 108k | } |
745 | 108k | if (publicId != NULL) { |
746 | 2.47k | cur->ExternalID = xmlStrdup(publicId); |
747 | 2.47k | if (cur->ExternalID == NULL) |
748 | 2 | goto error; |
749 | 2.47k | } |
750 | 108k | if (systemId != NULL) { |
751 | 33.1k | cur->SystemID = xmlStrdup(systemId); |
752 | 33.1k | if (cur->SystemID == NULL) |
753 | 3 | goto error; |
754 | 33.1k | } |
755 | 108k | if (doc != NULL) { |
756 | 108k | doc->intSubset = cur; |
757 | 108k | cur->parent = doc; |
758 | 108k | cur->doc = doc; |
759 | 108k | if (doc->children == NULL) { |
760 | 77.8k | doc->children = (xmlNodePtr) cur; |
761 | 77.8k | doc->last = (xmlNodePtr) cur; |
762 | 77.8k | } else { |
763 | 30.8k | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
764 | 0 | xmlNodePtr prev; |
765 | |
|
766 | 0 | prev = doc->children; |
767 | 0 | prev->prev = (xmlNodePtr) cur; |
768 | 0 | cur->next = prev; |
769 | 0 | doc->children = (xmlNodePtr) cur; |
770 | 30.8k | } else { |
771 | 30.8k | xmlNodePtr next; |
772 | | |
773 | 30.8k | next = doc->children; |
774 | 62.7k | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
775 | 31.9k | next = next->next; |
776 | 30.8k | if (next == NULL) { |
777 | 30.8k | cur->prev = doc->last; |
778 | 30.8k | cur->prev->next = (xmlNodePtr) cur; |
779 | 30.8k | cur->next = NULL; |
780 | 30.8k | doc->last = (xmlNodePtr) cur; |
781 | 30.8k | } else { |
782 | 0 | cur->next = next; |
783 | 0 | cur->prev = next->prev; |
784 | 0 | if (cur->prev == NULL) |
785 | 0 | doc->children = (xmlNodePtr) cur; |
786 | 0 | else |
787 | 0 | cur->prev->next = (xmlNodePtr) cur; |
788 | 0 | next->prev = (xmlNodePtr) cur; |
789 | 0 | } |
790 | 30.8k | } |
791 | 30.8k | } |
792 | 108k | } |
793 | | |
794 | 108k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
795 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
796 | 108k | return(cur); |
797 | | |
798 | 12 | error: |
799 | 12 | xmlFreeDtd(cur); |
800 | 12 | return(NULL); |
801 | 108k | } |
802 | | |
803 | | /** |
804 | | * Free a string if it is not owned by the "dict" dictionary in the |
805 | | * current scope |
806 | | * |
807 | | * @param str a string |
808 | | */ |
809 | | #define DICT_FREE(str) \ |
810 | 26.2M | if ((str) && ((!dict) || \ |
811 | 25.4M | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
812 | 8.98M | xmlFree((char *)(str)); |
813 | | |
814 | | /** |
815 | | * Free a DTD structure. |
816 | | * |
817 | | * @param cur the DTD structure to free up |
818 | | */ |
819 | | void |
820 | 139k | xmlFreeDtd(xmlDtd *cur) { |
821 | 139k | xmlDictPtr dict = NULL; |
822 | | |
823 | 139k | if (cur == NULL) { |
824 | 0 | return; |
825 | 0 | } |
826 | 139k | if (cur->doc != NULL) dict = cur->doc->dict; |
827 | | |
828 | 139k | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
829 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
830 | | |
831 | 139k | if (cur->children != NULL) { |
832 | 90.7k | xmlNodePtr next, c = cur->children; |
833 | | |
834 | | /* |
835 | | * Cleanup all nodes which are not part of the specific lists |
836 | | * of notations, elements, attributes and entities. |
837 | | */ |
838 | 337k | while (c != NULL) { |
839 | 246k | next = c->next; |
840 | 246k | if ((c->type != XML_ELEMENT_DECL) && |
841 | 246k | (c->type != XML_ATTRIBUTE_DECL) && |
842 | 246k | (c->type != XML_ENTITY_DECL)) { |
843 | 38.6k | xmlUnlinkNodeInternal(c); |
844 | 38.6k | xmlFreeNode(c); |
845 | 38.6k | } |
846 | 246k | c = next; |
847 | 246k | } |
848 | 90.7k | } |
849 | | |
850 | 139k | DICT_FREE(cur->name) |
851 | | |
852 | 139k | if (cur->SystemID != NULL) |
853 | 47.9k | xmlFree(cur->SystemID); |
854 | 139k | if (cur->ExternalID != NULL) |
855 | 3.68k | xmlFree(cur->ExternalID); |
856 | | |
857 | | /* TODO !!! */ |
858 | 139k | if (cur->notations != NULL) |
859 | 3.24k | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
860 | | |
861 | 139k | if (cur->elements != NULL) |
862 | 30.1k | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
863 | 139k | if (cur->attributes != NULL) |
864 | 13.2k | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
865 | 139k | if (cur->entities != NULL) |
866 | 34.3k | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
867 | 139k | if (cur->pentities != NULL) |
868 | 32.4k | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
869 | | |
870 | 139k | xmlFree(cur); |
871 | 139k | } |
872 | | |
873 | | /** |
874 | | * Creates a new XML document. If version is NULL, `"1.0"` is used. |
875 | | * |
876 | | * @param version XML version string like `"1.0"` (optional) |
877 | | * @returns a new document or NULL if a memory allocation failed. |
878 | | */ |
879 | | xmlDoc * |
880 | 1.10M | xmlNewDoc(const xmlChar *version) { |
881 | 1.10M | xmlDocPtr cur; |
882 | | |
883 | 1.10M | if (version == NULL) |
884 | 316k | version = (const xmlChar *) "1.0"; |
885 | | |
886 | | /* |
887 | | * Allocate a new document and fill the fields. |
888 | | */ |
889 | 1.10M | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
890 | 1.10M | if (cur == NULL) |
891 | 109 | return(NULL); |
892 | 1.10M | memset(cur, 0, sizeof(xmlDoc)); |
893 | 1.10M | cur->type = XML_DOCUMENT_NODE; |
894 | | |
895 | 1.10M | cur->version = xmlStrdup(version); |
896 | 1.10M | if (cur->version == NULL) { |
897 | 82 | xmlFree(cur); |
898 | 82 | return(NULL); |
899 | 82 | } |
900 | 1.10M | cur->standalone = -1; |
901 | 1.10M | cur->compression = -1; /* not initialized */ |
902 | 1.10M | cur->doc = cur; |
903 | 1.10M | cur->parseFlags = 0; |
904 | 1.10M | cur->properties = XML_DOC_USERBUILT; |
905 | | /* |
906 | | * The in memory encoding is always UTF8 |
907 | | * This field will never change and would |
908 | | * be obsolete if not for binary compatibility. |
909 | | */ |
910 | 1.10M | cur->charset = XML_CHAR_ENCODING_UTF8; |
911 | | |
912 | 1.10M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
913 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
914 | 1.10M | return(cur); |
915 | 1.10M | } |
916 | | |
917 | | /** |
918 | | * Free a document including all children and associated DTDs. |
919 | | * |
920 | | * @param cur pointer to the document |
921 | | */ |
922 | | void |
923 | 1.24M | xmlFreeDoc(xmlDoc *cur) { |
924 | 1.24M | xmlDtdPtr extSubset, intSubset; |
925 | 1.24M | xmlDictPtr dict = NULL; |
926 | | |
927 | 1.24M | if (cur == NULL) { |
928 | 133k | return; |
929 | 133k | } |
930 | | |
931 | 1.10M | dict = cur->dict; |
932 | | |
933 | 1.10M | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
934 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
935 | | |
936 | | /* |
937 | | * Do this before freeing the children list to avoid ID lookups |
938 | | */ |
939 | 1.10M | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
940 | 1.10M | cur->ids = NULL; |
941 | 1.10M | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
942 | 1.10M | cur->refs = NULL; |
943 | 1.10M | extSubset = cur->extSubset; |
944 | 1.10M | intSubset = cur->intSubset; |
945 | 1.10M | if (intSubset == extSubset) |
946 | 1.00M | extSubset = NULL; |
947 | 1.10M | if (extSubset != NULL) { |
948 | 14.7k | xmlUnlinkNodeInternal((xmlNodePtr) cur->extSubset); |
949 | 14.7k | cur->extSubset = NULL; |
950 | 14.7k | xmlFreeDtd(extSubset); |
951 | 14.7k | } |
952 | 1.10M | if (intSubset != NULL) { |
953 | 124k | xmlUnlinkNodeInternal((xmlNodePtr) cur->intSubset); |
954 | 124k | cur->intSubset = NULL; |
955 | 124k | xmlFreeDtd(intSubset); |
956 | 124k | } |
957 | | |
958 | 1.10M | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
959 | 1.10M | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
960 | | |
961 | 1.10M | DICT_FREE(cur->name) |
962 | | |
963 | 1.10M | if (cur->version != NULL) |
964 | 1.10M | xmlFree(cur->version); |
965 | 1.10M | if (cur->encoding != NULL) |
966 | 107k | xmlFree(cur->encoding); |
967 | 1.10M | if (cur->URL != NULL) |
968 | 64.5k | xmlFree(cur->URL); |
969 | | |
970 | 1.10M | xmlFree(cur); |
971 | 1.10M | if (dict) xmlDictFree(dict); |
972 | 1.10M | } |
973 | | |
974 | | /** |
975 | | * See xmlNodeParseContent. |
976 | | * |
977 | | * @param doc a document (optional) |
978 | | * @param parent an element or attribute (optional) |
979 | | * @param value an attribute value |
980 | | * @param len maximum length of the attribute value |
981 | | * @param listPtr pointer to the resulting node list (optional) |
982 | | * @returns 0 on success, -1 if a memory allocation failed. |
983 | | */ |
984 | | static int |
985 | | xmlNodeParseContentInternal(const xmlDoc *doc, xmlNodePtr parent, |
986 | | const xmlChar *value, int len, |
987 | 7.94k | xmlNodePtr *listPtr) { |
988 | 7.94k | xmlNodePtr head = NULL, last = NULL; |
989 | 7.94k | xmlNodePtr node; |
990 | 7.94k | xmlChar *val = NULL; |
991 | 7.94k | const xmlChar *cur; |
992 | 7.94k | const xmlChar *q; |
993 | 7.94k | xmlEntityPtr ent; |
994 | 7.94k | xmlBufPtr buf; |
995 | 7.94k | int remaining; |
996 | | |
997 | 7.94k | if (listPtr != NULL) |
998 | 0 | *listPtr = NULL; |
999 | | |
1000 | 7.94k | if (len < 0) |
1001 | 37 | remaining = INT_MAX; |
1002 | 7.90k | else |
1003 | 7.90k | remaining = len; |
1004 | | |
1005 | 7.94k | if ((value == NULL) || (value[0] == 0)) |
1006 | 1 | goto done; |
1007 | | |
1008 | 7.94k | cur = value; |
1009 | | |
1010 | 7.94k | buf = xmlBufCreate(50); |
1011 | 7.94k | if (buf == NULL) |
1012 | 0 | return(-1); |
1013 | | |
1014 | 7.94k | q = cur; |
1015 | 23.3M | while ((remaining > 0) && (*cur != 0)) { |
1016 | 23.2M | if (cur[0] == '&') { |
1017 | 21.6k | int charval = 0; |
1018 | | |
1019 | | /* |
1020 | | * Save the current text. |
1021 | | */ |
1022 | 21.6k | if (cur != q) { |
1023 | 7.90k | if (xmlBufAdd(buf, q, cur - q)) |
1024 | 0 | goto out; |
1025 | 7.90k | q = cur; |
1026 | 7.90k | } |
1027 | | |
1028 | 21.6k | if ((remaining > 2) && (cur[1] == '#') && (cur[2] == 'x')) { |
1029 | 0 | int tmp = 0; |
1030 | |
|
1031 | 0 | cur += 3; |
1032 | 0 | remaining -= 3; |
1033 | 0 | while ((remaining > 0) && ((tmp = *cur) != ';')) { |
1034 | 0 | if ((tmp >= '0') && (tmp <= '9')) |
1035 | 0 | charval = charval * 16 + (tmp - '0'); |
1036 | 0 | else if ((tmp >= 'a') && (tmp <= 'f')) |
1037 | 0 | charval = charval * 16 + (tmp - 'a') + 10; |
1038 | 0 | else if ((tmp >= 'A') && (tmp <= 'F')) |
1039 | 0 | charval = charval * 16 + (tmp - 'A') + 10; |
1040 | 0 | else { |
1041 | 0 | charval = 0; |
1042 | 0 | break; |
1043 | 0 | } |
1044 | 0 | if (charval > 0x110000) |
1045 | 0 | charval = 0x110000; |
1046 | 0 | cur++; |
1047 | 0 | remaining--; |
1048 | 0 | } |
1049 | 0 | if (tmp == ';') { |
1050 | 0 | cur++; |
1051 | 0 | remaining--; |
1052 | 0 | } |
1053 | 0 | q = cur; |
1054 | 21.6k | } else if ((remaining > 1) && (cur[1] == '#')) { |
1055 | 552 | int tmp = 0; |
1056 | | |
1057 | 552 | cur += 2; |
1058 | 552 | remaining -= 2; |
1059 | 1.65k | while ((remaining > 0) && ((tmp = *cur) != ';')) { |
1060 | 1.10k | if ((tmp >= '0') && (tmp <= '9')) |
1061 | 1.10k | charval = charval * 10 + (tmp - '0'); |
1062 | 0 | else { |
1063 | 0 | charval = 0; |
1064 | 0 | break; |
1065 | 0 | } |
1066 | 1.10k | if (charval > 0x110000) |
1067 | 0 | charval = 0x110000; |
1068 | 1.10k | cur++; |
1069 | 1.10k | remaining--; |
1070 | 1.10k | } |
1071 | 552 | if (tmp == ';') { |
1072 | 552 | cur++; |
1073 | 552 | remaining--; |
1074 | 552 | } |
1075 | 552 | q = cur; |
1076 | 21.1k | } else { |
1077 | | /* |
1078 | | * Read the entity string |
1079 | | */ |
1080 | 21.1k | cur++; |
1081 | 21.1k | remaining--; |
1082 | 21.1k | q = cur; |
1083 | 42.3k | while ((remaining > 0) && (*cur != 0) && (*cur != ';')) { |
1084 | 21.1k | cur++; |
1085 | 21.1k | remaining--; |
1086 | 21.1k | } |
1087 | 21.1k | if ((remaining <= 0) || (*cur == 0)) |
1088 | 0 | break; |
1089 | 21.1k | if (cur != q) { |
1090 | 21.1k | val = xmlStrndup(q, cur - q); |
1091 | 21.1k | if (val == NULL) |
1092 | 0 | goto out; |
1093 | 21.1k | ent = xmlGetDocEntity(doc, val); |
1094 | 21.1k | if ((ent != NULL) && |
1095 | 21.1k | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
1096 | | /* |
1097 | | * Predefined entities don't generate nodes |
1098 | | */ |
1099 | 0 | if (xmlBufCat(buf, ent->content)) |
1100 | 0 | goto out; |
1101 | 21.1k | } else { |
1102 | | /* |
1103 | | * Flush buffer so far |
1104 | | */ |
1105 | 21.1k | if (!xmlBufIsEmpty(buf)) { |
1106 | 7.55k | node = xmlNewDocText(doc, NULL); |
1107 | 7.55k | if (node == NULL) |
1108 | 0 | goto out; |
1109 | 7.55k | node->content = xmlBufDetach(buf); |
1110 | 7.55k | node->parent = parent; |
1111 | | |
1112 | 7.55k | if (last == NULL) { |
1113 | 274 | head = node; |
1114 | 7.28k | } else { |
1115 | 7.28k | last->next = node; |
1116 | 7.28k | node->prev = last; |
1117 | 7.28k | } |
1118 | 7.55k | last = node; |
1119 | 7.55k | } |
1120 | | |
1121 | 21.1k | if ((ent != NULL) && |
1122 | 21.1k | ((ent->flags & XML_ENT_PARSED) == 0) && |
1123 | 21.1k | ((ent->flags & XML_ENT_EXPANDING) == 0) && |
1124 | 21.1k | (ent->content != NULL)) { |
1125 | 37 | int res; |
1126 | | |
1127 | 37 | ent->flags |= XML_ENT_EXPANDING; |
1128 | 37 | res = xmlNodeParseContentInternal(doc, |
1129 | 37 | (xmlNodePtr) ent, ent->content, -1, NULL); |
1130 | 37 | ent->flags &= ~XML_ENT_EXPANDING; |
1131 | 37 | if (res < 0) |
1132 | 0 | goto out; |
1133 | 37 | ent->flags |= XML_ENT_PARSED; |
1134 | 37 | } |
1135 | | |
1136 | | /* |
1137 | | * Create a new REFERENCE_REF node |
1138 | | */ |
1139 | 21.1k | node = xmlNewEntityRef((xmlDocPtr) doc, val); |
1140 | 21.1k | val = NULL; |
1141 | 21.1k | if (node == NULL) |
1142 | 0 | goto out; |
1143 | 21.1k | node->parent = parent; |
1144 | 21.1k | node->last = (xmlNodePtr) ent; |
1145 | 21.1k | if (ent != NULL) { |
1146 | 21.1k | node->children = (xmlNodePtr) ent; |
1147 | 21.1k | node->content = ent->content; |
1148 | 21.1k | } |
1149 | | |
1150 | 21.1k | if (last == NULL) { |
1151 | 286 | head = node; |
1152 | 20.8k | } else { |
1153 | 20.8k | last->next = node; |
1154 | 20.8k | node->prev = last; |
1155 | 20.8k | } |
1156 | 21.1k | last = node; |
1157 | 21.1k | } |
1158 | 21.1k | xmlFree(val); |
1159 | 21.1k | val = NULL; |
1160 | 21.1k | } |
1161 | 21.1k | cur++; |
1162 | 21.1k | remaining--; |
1163 | 21.1k | q = cur; |
1164 | 21.1k | } |
1165 | 21.6k | if (charval != 0) { |
1166 | 552 | xmlChar buffer[10]; |
1167 | 552 | int l; |
1168 | | |
1169 | 552 | if (charval >= 0x110000) |
1170 | 0 | charval = 0xFFFD; /* replacement character */ |
1171 | | |
1172 | 552 | l = xmlCopyCharMultiByte(buffer, charval); |
1173 | 552 | buffer[l] = 0; |
1174 | | |
1175 | 552 | if (xmlBufCat(buf, buffer)) |
1176 | 0 | goto out; |
1177 | 552 | } |
1178 | 23.2M | } else { |
1179 | 23.2M | cur++; |
1180 | 23.2M | remaining--; |
1181 | 23.2M | } |
1182 | 23.2M | } |
1183 | | |
1184 | 7.94k | if (cur != q) { |
1185 | | /* |
1186 | | * Handle the last piece of text. |
1187 | | */ |
1188 | 7.66k | if (xmlBufAdd(buf, q, cur - q)) |
1189 | 0 | goto out; |
1190 | 7.66k | } |
1191 | | |
1192 | 7.94k | if (!xmlBufIsEmpty(buf)) { |
1193 | 7.66k | node = xmlNewDocText(doc, NULL); |
1194 | 7.66k | if (node == NULL) |
1195 | 0 | goto out; |
1196 | 7.66k | node->parent = parent; |
1197 | 7.66k | node->content = xmlBufDetach(buf); |
1198 | | |
1199 | 7.66k | if (last == NULL) { |
1200 | 7.38k | head = node; |
1201 | 7.38k | } else { |
1202 | 281 | last->next = node; |
1203 | 281 | node->prev = last; |
1204 | 281 | } |
1205 | 7.66k | last = node; |
1206 | 7.66k | } else if (head == NULL) { |
1207 | 0 | head = xmlNewDocText(doc, BAD_CAST ""); |
1208 | 0 | if (head == NULL) |
1209 | 0 | goto out; |
1210 | 0 | head->parent = parent; |
1211 | 0 | last = head; |
1212 | 0 | } |
1213 | | |
1214 | 7.94k | xmlBufFree(buf); |
1215 | | |
1216 | 7.94k | done: |
1217 | 7.94k | if (parent != NULL) { |
1218 | 7.94k | if (parent->children != NULL) |
1219 | 0 | xmlFreeNodeList(parent->children); |
1220 | 7.94k | parent->children = head; |
1221 | 7.94k | parent->last = last; |
1222 | 7.94k | } |
1223 | | |
1224 | 7.94k | if (listPtr != NULL) |
1225 | 0 | *listPtr = head; |
1226 | | |
1227 | 7.94k | return(0); |
1228 | | |
1229 | 0 | out: |
1230 | 0 | xmlBufFree(buf); |
1231 | 0 | if (val != NULL) |
1232 | 0 | xmlFree(val); |
1233 | 0 | if (head != NULL) |
1234 | 0 | xmlFreeNodeList(head); |
1235 | 0 | return(-1); |
1236 | 7.94k | } |
1237 | | |
1238 | | /** |
1239 | | * Parse content and replace the node's children with the resulting |
1240 | | * node list. |
1241 | | * |
1242 | | * `content` is expected to be a valid XML attribute value possibly |
1243 | | * containing character and entity references. Syntax errors |
1244 | | * and references to undeclared entities are ignored silently. |
1245 | | * Only references are handled, nested elements, comments or PIs are |
1246 | | * not. |
1247 | | * |
1248 | | * @param node an element or attribute |
1249 | | * @param content text content with XML references |
1250 | | * @param len maximum length of content |
1251 | | * @returns 0 on success, -1 if a memory allocation failed. |
1252 | | */ |
1253 | | int |
1254 | 7.90k | xmlNodeParseContent(xmlNode *node, const xmlChar *content, int len) { |
1255 | 7.90k | return(xmlNodeParseContentInternal(node->doc, node, content, len, NULL)); |
1256 | 7.90k | } |
1257 | | |
1258 | | /** |
1259 | | * See #xmlStringGetNodeList. |
1260 | | * |
1261 | | * @deprecated Use #xmlNodeSetContentLen. |
1262 | | * |
1263 | | * @param doc a document (optional) |
1264 | | * @param value an attribute value |
1265 | | * @param len maximum length of the attribute value |
1266 | | * @returns a pointer to the first child or NULL if the value if empty |
1267 | | * or a memory allocation failed. |
1268 | | */ |
1269 | | xmlNode * |
1270 | 0 | xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) { |
1271 | 0 | xmlNodePtr ret; |
1272 | |
|
1273 | 0 | xmlNodeParseContentInternal(doc, NULL, value, len, &ret); |
1274 | 0 | return(ret); |
1275 | 0 | } |
1276 | | |
1277 | | /** |
1278 | | * Parse an attribute value and build a node list with |
1279 | | * text and entity reference nodes. The resulting nodes will be |
1280 | | * associated with the document if provided. The document is also |
1281 | | * used to look up entities. |
1282 | | * |
1283 | | * @deprecated Use #xmlNodeSetContent. |
1284 | | * |
1285 | | * The input is not validated. Syntax errors or references to |
1286 | | * undeclared entities will be ignored silently with unspecified |
1287 | | * results. |
1288 | | * |
1289 | | * @param doc a document (optional) |
1290 | | * @param value an attribute value |
1291 | | * @returns a pointer to the first child or NULL if the value if empty |
1292 | | * or a memory allocation failed. |
1293 | | */ |
1294 | | xmlNode * |
1295 | 0 | xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) { |
1296 | 0 | xmlNodePtr ret; |
1297 | |
|
1298 | 0 | xmlNodeParseContentInternal(doc, NULL, value, -1, &ret); |
1299 | 0 | return(ret); |
1300 | 0 | } |
1301 | | |
1302 | | /** |
1303 | | * @param node a node list |
1304 | | * @param escape whether to escape characters and keep entity refs |
1305 | | * @param flags escape flags |
1306 | | * @returns a pointer to the string. |
1307 | | */ |
1308 | | xmlChar * |
1309 | 260k | xmlNodeListGetStringInternal(const xmlNode *node, int escape, int flags) { |
1310 | 260k | xmlBufPtr buf; |
1311 | 260k | xmlChar *ret; |
1312 | | |
1313 | 260k | if (node == NULL) |
1314 | 0 | return(xmlStrdup(BAD_CAST "")); |
1315 | | |
1316 | 260k | if ((escape == 0) && |
1317 | 260k | ((node->type == XML_TEXT_NODE) || |
1318 | 260k | (node->type == XML_CDATA_SECTION_NODE)) && |
1319 | 260k | (node->next == NULL)) { |
1320 | 260k | if (node->content == NULL) |
1321 | 0 | return(xmlStrdup(BAD_CAST "")); |
1322 | 260k | return(xmlStrdup(node->content)); |
1323 | 260k | } |
1324 | | |
1325 | 0 | buf = xmlBufCreate(50); |
1326 | 0 | if (buf == NULL) |
1327 | 0 | return(NULL); |
1328 | | |
1329 | 0 | while (node != NULL) { |
1330 | 0 | if ((node->type == XML_TEXT_NODE) || |
1331 | 0 | (node->type == XML_CDATA_SECTION_NODE)) { |
1332 | 0 | if (node->content != NULL) { |
1333 | 0 | if (escape == 0) { |
1334 | 0 | xmlBufCat(buf, node->content); |
1335 | 0 | } else { |
1336 | 0 | xmlChar *encoded; |
1337 | |
|
1338 | 0 | encoded = xmlEscapeText(node->content, flags); |
1339 | 0 | if (encoded == NULL) |
1340 | 0 | goto error; |
1341 | 0 | xmlBufCat(buf, encoded); |
1342 | 0 | xmlFree(encoded); |
1343 | 0 | } |
1344 | 0 | } |
1345 | 0 | } else if (node->type == XML_ENTITY_REF_NODE) { |
1346 | 0 | if (escape == 0) { |
1347 | 0 | xmlBufGetNodeContent(buf, node); |
1348 | 0 | } else { |
1349 | 0 | xmlBufAdd(buf, BAD_CAST "&", 1); |
1350 | 0 | xmlBufCat(buf, node->name); |
1351 | 0 | xmlBufAdd(buf, BAD_CAST ";", 1); |
1352 | 0 | } |
1353 | 0 | } |
1354 | | |
1355 | 0 | node = node->next; |
1356 | 0 | } |
1357 | | |
1358 | 0 | ret = xmlBufDetach(buf); |
1359 | 0 | xmlBufFree(buf); |
1360 | 0 | return(ret); |
1361 | | |
1362 | 0 | error: |
1363 | 0 | xmlBufFree(buf); |
1364 | 0 | return(NULL); |
1365 | 0 | } |
1366 | | |
1367 | | /** |
1368 | | * Serializes attribute children (text and entity reference nodes) |
1369 | | * into a string. |
1370 | | * |
1371 | | * If `inLine` is true, entity references will be substituted. |
1372 | | * Otherwise, entity references will be kept and special characters |
1373 | | * like `&` as well as non-ASCII chars will be escaped. See |
1374 | | * #xmlEncodeEntitiesReentrant for details. If `list` is the child |
1375 | | * of an attribute, escaping rules apply are adjusted. |
1376 | | * |
1377 | | * See #xmlNodeListGetRawString for an alternative option. |
1378 | | * |
1379 | | * @param doc a document (optional) |
1380 | | * @param list a node list of attribute children |
1381 | | * @param inLine whether entity references are substituted |
1382 | | * @returns a string or NULL if a memory allocation failed. |
1383 | | */ |
1384 | | xmlChar * |
1385 | | xmlNodeListGetString(xmlDoc *doc, const xmlNode *list, int inLine) |
1386 | 260k | { |
1387 | 260k | int flags = 0; |
1388 | 260k | int escape = 0; |
1389 | | |
1390 | | /* backward compatibility */ |
1391 | 260k | if (list == NULL) |
1392 | 0 | return(NULL); |
1393 | | |
1394 | 260k | if (!inLine) { |
1395 | 0 | escape = 1; |
1396 | |
|
1397 | 0 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) |
1398 | 0 | flags |= XML_ESCAPE_HTML; |
1399 | 0 | else if ((doc == NULL) || (doc->encoding == NULL)) |
1400 | 0 | flags |= XML_ESCAPE_NON_ASCII; |
1401 | |
|
1402 | 0 | if ((list->parent != NULL) && |
1403 | 0 | (list->parent->type == XML_ATTRIBUTE_NODE)) |
1404 | 0 | flags |= XML_ESCAPE_ATTR; |
1405 | 0 | } |
1406 | | |
1407 | 260k | return(xmlNodeListGetStringInternal(list, escape, flags)); |
1408 | 260k | } |
1409 | | |
1410 | | /** |
1411 | | * Serializes attribute children (text and entity reference nodes) |
1412 | | * into a string. |
1413 | | * |
1414 | | * If `inLine` is true, entity references will be substituted. |
1415 | | * Otherwise, entity references will be kept and special characters |
1416 | | * like `&` will be escaped. See #xmlEncodeSpecialChars for |
1417 | | * details. |
1418 | | * |
1419 | | * @param doc a document (unused) |
1420 | | * @param list a node list of attribute children |
1421 | | * @param inLine whether entity references are substituted |
1422 | | * @returns a string or NULL if a memory allocation failed. |
1423 | | */ |
1424 | | xmlChar * |
1425 | | xmlNodeListGetRawString(const xmlDoc *doc ATTRIBUTE_UNUSED, |
1426 | | const xmlNode *list, int inLine) |
1427 | 0 | { |
1428 | 0 | int escape = 0; |
1429 | 0 | int flags = 0; |
1430 | | |
1431 | | /* backward compatibility */ |
1432 | 0 | if (list == NULL) |
1433 | 0 | return(NULL); |
1434 | | |
1435 | 0 | if (!inLine) { |
1436 | 0 | escape = 1; |
1437 | 0 | flags = XML_ESCAPE_QUOT; |
1438 | 0 | } |
1439 | |
|
1440 | 0 | return(xmlNodeListGetStringInternal(list, escape, flags)); |
1441 | 0 | } |
1442 | | |
1443 | | static xmlAttrPtr |
1444 | | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
1445 | | const xmlChar * name, const xmlChar * value, |
1446 | | int eatname) |
1447 | 594k | { |
1448 | 594k | xmlAttrPtr cur; |
1449 | 594k | xmlDocPtr doc = NULL; |
1450 | | |
1451 | 594k | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
1452 | 0 | if ((eatname == 1) && |
1453 | 0 | ((node->doc == NULL) || (node->doc->dict == NULL) || |
1454 | 0 | (!(xmlDictOwns(node->doc->dict, name))))) |
1455 | 0 | xmlFree((xmlChar *) name); |
1456 | 0 | return (NULL); |
1457 | 0 | } |
1458 | | |
1459 | | /* |
1460 | | * Allocate a new property and fill the fields. |
1461 | | */ |
1462 | 594k | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
1463 | 594k | if (cur == NULL) { |
1464 | 3.47k | if ((eatname == 1) && |
1465 | 3.47k | ((node == NULL) || (node->doc == NULL) || |
1466 | 0 | (node->doc->dict == NULL) || |
1467 | 0 | (!(xmlDictOwns(node->doc->dict, name))))) |
1468 | 0 | xmlFree((xmlChar *) name); |
1469 | 3.47k | return (NULL); |
1470 | 3.47k | } |
1471 | 590k | memset(cur, 0, sizeof(xmlAttr)); |
1472 | 590k | cur->type = XML_ATTRIBUTE_NODE; |
1473 | | |
1474 | 590k | cur->parent = node; |
1475 | 590k | if (node != NULL) { |
1476 | 590k | doc = node->doc; |
1477 | 590k | cur->doc = doc; |
1478 | 590k | } |
1479 | 590k | cur->ns = ns; |
1480 | | |
1481 | 590k | if (eatname == 0) { |
1482 | 590k | if ((doc != NULL) && (doc->dict != NULL)) |
1483 | 590k | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
1484 | 0 | else |
1485 | 0 | cur->name = xmlStrdup(name); |
1486 | 590k | if (cur->name == NULL) |
1487 | 1 | goto error; |
1488 | 590k | } else |
1489 | 0 | cur->name = name; |
1490 | | |
1491 | 590k | if (value != NULL) { |
1492 | 582k | xmlNodePtr tmp; |
1493 | | |
1494 | 582k | cur->children = xmlNewDocText(doc, value); |
1495 | 582k | if (cur->children == NULL) |
1496 | 21 | goto error; |
1497 | 582k | cur->last = NULL; |
1498 | 582k | tmp = cur->children; |
1499 | 1.16M | while (tmp != NULL) { |
1500 | 582k | tmp->parent = (xmlNodePtr) cur; |
1501 | 582k | if (tmp->next == NULL) |
1502 | 582k | cur->last = tmp; |
1503 | 582k | tmp = tmp->next; |
1504 | 582k | } |
1505 | | |
1506 | 582k | if (doc != NULL) { |
1507 | 582k | int res = xmlIsID(doc, node, cur); |
1508 | | |
1509 | 582k | if (res < 0) |
1510 | 0 | goto error; |
1511 | 582k | if ((res == 1) && (xmlAddIDSafe(cur, value) < 0)) |
1512 | 1 | goto error; |
1513 | 582k | } |
1514 | 582k | } |
1515 | | |
1516 | | /* |
1517 | | * Add it at the end to preserve parsing order ... |
1518 | | */ |
1519 | 590k | if (node != NULL) { |
1520 | 590k | if (node->properties == NULL) { |
1521 | 427k | node->properties = cur; |
1522 | 427k | } else { |
1523 | 163k | xmlAttrPtr prev = node->properties; |
1524 | | |
1525 | 178k | while (prev->next != NULL) |
1526 | 15.5k | prev = prev->next; |
1527 | 163k | prev->next = cur; |
1528 | 163k | cur->prev = prev; |
1529 | 163k | } |
1530 | 590k | } |
1531 | | |
1532 | 590k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1533 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
1534 | 590k | return (cur); |
1535 | | |
1536 | 23 | error: |
1537 | 23 | xmlFreeProp(cur); |
1538 | 23 | return(NULL); |
1539 | 590k | } |
1540 | | |
1541 | | /** |
1542 | | * Create an attribute node. |
1543 | | * |
1544 | | * If provided, `value` should be a raw, unescaped string. |
1545 | | * |
1546 | | * If `node` is provided, the created attribute will be appended without |
1547 | | * checking for duplicate names. It is an error if `node` is not an |
1548 | | * element. |
1549 | | * |
1550 | | * @param node the parent node (optional) |
1551 | | * @param name the name of the attribute |
1552 | | * @param value the value of the attribute (optional) |
1553 | | * @returns a pointer to the attribute or NULL if arguments are invalid |
1554 | | * or a memory allocation failed. |
1555 | | */ |
1556 | | xmlAttr * |
1557 | 45 | xmlNewProp(xmlNode *node, const xmlChar *name, const xmlChar *value) { |
1558 | | |
1559 | 45 | if (name == NULL) { |
1560 | 0 | return(NULL); |
1561 | 0 | } |
1562 | | |
1563 | 45 | return xmlNewPropInternal(node, NULL, name, value, 0); |
1564 | 45 | } |
1565 | | |
1566 | | /** |
1567 | | * Create an attribute node. |
1568 | | * |
1569 | | * If provided, `value` should be a raw, unescaped string. |
1570 | | * |
1571 | | * If `node` is provided, the created attribute will be appended without |
1572 | | * checking for duplicate names. It is an error if `node` is not an |
1573 | | * element. |
1574 | | * |
1575 | | * @param node the parent node (optional) |
1576 | | * @param ns the namespace (optional) |
1577 | | * @param name the local name of the attribute |
1578 | | * @param value the value of the attribute (optional) |
1579 | | * @returns a pointer to the attribute or NULL if arguments are invalid |
1580 | | * or a memory allocation failed. |
1581 | | */ |
1582 | | xmlAttr * |
1583 | | xmlNewNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name, |
1584 | 570k | const xmlChar *value) { |
1585 | | |
1586 | 570k | if (name == NULL) { |
1587 | 0 | return(NULL); |
1588 | 0 | } |
1589 | | |
1590 | 570k | return xmlNewPropInternal(node, ns, name, value, 0); |
1591 | 570k | } |
1592 | | |
1593 | | /** |
1594 | | * Create an attribute node. |
1595 | | * |
1596 | | * Like #xmlNewNsProp, but the `name` string will be used directly |
1597 | | * without making a copy. Takes ownership of `name` which will also |
1598 | | * be freed on error. |
1599 | | * |
1600 | | * @param node the parent node (optional) |
1601 | | * @param ns the namespace (optional) |
1602 | | * @param name the local name of the attribute |
1603 | | * @param value the value of the attribute (optional) |
1604 | | * @returns a pointer to the attribute or NULL if arguments are invalid |
1605 | | * or a memory allocation failed. |
1606 | | */ |
1607 | | xmlAttr * |
1608 | | xmlNewNsPropEatName(xmlNode *node, xmlNs *ns, xmlChar *name, |
1609 | 0 | const xmlChar *value) { |
1610 | |
|
1611 | 0 | if (name == NULL) { |
1612 | 0 | return(NULL); |
1613 | 0 | } |
1614 | | |
1615 | 0 | return xmlNewPropInternal(node, ns, name, value, 1); |
1616 | 0 | } |
1617 | | |
1618 | | /** |
1619 | | * Create an attribute node. |
1620 | | * |
1621 | | * If provided, `value` is expected to be a valid XML attribute value |
1622 | | * possibly containing character and entity references. Syntax errors |
1623 | | * and references to undeclared entities are ignored silently. |
1624 | | * If you want to pass a raw string, see #xmlNewProp. |
1625 | | * |
1626 | | * @param doc the target document (optional) |
1627 | | * @param name the name of the attribute |
1628 | | * @param value attribute value with XML references (optional) |
1629 | | * @returns a pointer to the attribute or NULL if arguments are invalid |
1630 | | * or a memory allocation failed. |
1631 | | */ |
1632 | | xmlAttr * |
1633 | 174k | xmlNewDocProp(xmlDoc *doc, const xmlChar *name, const xmlChar *value) { |
1634 | 174k | xmlAttrPtr cur; |
1635 | | |
1636 | 174k | if (name == NULL) { |
1637 | 0 | return(NULL); |
1638 | 0 | } |
1639 | | |
1640 | | /* |
1641 | | * Allocate a new property and fill the fields. |
1642 | | */ |
1643 | 174k | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
1644 | 174k | if (cur == NULL) |
1645 | 315 | return(NULL); |
1646 | 173k | memset(cur, 0, sizeof(xmlAttr)); |
1647 | 173k | cur->type = XML_ATTRIBUTE_NODE; |
1648 | | |
1649 | 173k | if ((doc != NULL) && (doc->dict != NULL)) |
1650 | 173k | cur->name = xmlDictLookup(doc->dict, name, -1); |
1651 | 0 | else |
1652 | 0 | cur->name = xmlStrdup(name); |
1653 | 173k | if (cur->name == NULL) |
1654 | 0 | goto error; |
1655 | 173k | cur->doc = doc; |
1656 | 173k | if (value != NULL) { |
1657 | 0 | if (xmlNodeParseContent((xmlNodePtr) cur, value, -1) < 0) |
1658 | 0 | goto error; |
1659 | 0 | } |
1660 | | |
1661 | 173k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1662 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
1663 | 173k | return(cur); |
1664 | | |
1665 | 0 | error: |
1666 | 0 | xmlFreeProp(cur); |
1667 | 0 | return(NULL); |
1668 | 173k | } |
1669 | | |
1670 | | /** |
1671 | | * Free an attribute list including all children. |
1672 | | * |
1673 | | * @param cur the first attribute in the list |
1674 | | */ |
1675 | | void |
1676 | 3.91M | xmlFreePropList(xmlAttr *cur) { |
1677 | 3.91M | xmlAttrPtr next; |
1678 | 3.91M | if (cur == NULL) return; |
1679 | 8.54M | while (cur != NULL) { |
1680 | 4.62M | next = cur->next; |
1681 | 4.62M | xmlFreeProp(cur); |
1682 | 4.62M | cur = next; |
1683 | 4.62M | } |
1684 | 3.91M | } |
1685 | | |
1686 | | /** |
1687 | | * Free an attribute including all children. |
1688 | | * |
1689 | | * @param cur an attribute |
1690 | | */ |
1691 | | void |
1692 | 4.62M | xmlFreeProp(xmlAttr *cur) { |
1693 | 4.62M | xmlDictPtr dict = NULL; |
1694 | 4.62M | if (cur == NULL) return; |
1695 | | |
1696 | 4.62M | if (cur->doc != NULL) dict = cur->doc->dict; |
1697 | | |
1698 | 4.62M | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
1699 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
1700 | | |
1701 | | /* Check for ID removal -> leading to invalid references ! */ |
1702 | 4.62M | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
1703 | 487 | xmlRemoveID(cur->doc, cur); |
1704 | 487 | } |
1705 | 4.62M | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
1706 | 4.62M | DICT_FREE(cur->name) |
1707 | 4.62M | xmlFree(cur); |
1708 | 4.62M | } |
1709 | | |
1710 | | /** |
1711 | | * Unlink and free an attribute including all children. |
1712 | | * |
1713 | | * Note this doesn't work for namespace declarations. |
1714 | | * |
1715 | | * The attribute must have a non-NULL parent pointer. |
1716 | | * |
1717 | | * @param cur an attribute |
1718 | | * @returns 0 on success or -1 if the attribute was not found or |
1719 | | * arguments are invalid. |
1720 | | */ |
1721 | | int |
1722 | 0 | xmlRemoveProp(xmlAttr *cur) { |
1723 | 0 | xmlAttrPtr tmp; |
1724 | 0 | if (cur == NULL) { |
1725 | 0 | return(-1); |
1726 | 0 | } |
1727 | 0 | if (cur->parent == NULL) { |
1728 | 0 | return(-1); |
1729 | 0 | } |
1730 | 0 | tmp = cur->parent->properties; |
1731 | 0 | if (tmp == cur) { |
1732 | 0 | cur->parent->properties = cur->next; |
1733 | 0 | if (cur->next != NULL) |
1734 | 0 | cur->next->prev = NULL; |
1735 | 0 | xmlFreeProp(cur); |
1736 | 0 | return(0); |
1737 | 0 | } |
1738 | 0 | while (tmp != NULL) { |
1739 | 0 | if (tmp->next == cur) { |
1740 | 0 | tmp->next = cur->next; |
1741 | 0 | if (tmp->next != NULL) |
1742 | 0 | tmp->next->prev = tmp; |
1743 | 0 | xmlFreeProp(cur); |
1744 | 0 | return(0); |
1745 | 0 | } |
1746 | 0 | tmp = tmp->next; |
1747 | 0 | } |
1748 | 0 | return(-1); |
1749 | 0 | } |
1750 | | |
1751 | | /** |
1752 | | * Create a processing instruction object. |
1753 | | * |
1754 | | * @param doc the target document (optional) |
1755 | | * @param name the processing instruction target |
1756 | | * @param content the PI content (optional) |
1757 | | * @returns a pointer to the new node object or NULL if arguments are |
1758 | | * invalid or a memory allocation failed. |
1759 | | */ |
1760 | | xmlNode * |
1761 | 102k | xmlNewDocPI(xmlDoc *doc, const xmlChar *name, const xmlChar *content) { |
1762 | 102k | xmlNodePtr cur; |
1763 | | |
1764 | 102k | if (name == NULL) { |
1765 | 0 | return(NULL); |
1766 | 0 | } |
1767 | | |
1768 | | /* |
1769 | | * Allocate a new node and fill the fields. |
1770 | | */ |
1771 | 102k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
1772 | 102k | if (cur == NULL) |
1773 | 38 | return(NULL); |
1774 | 102k | memset(cur, 0, sizeof(xmlNode)); |
1775 | 102k | cur->type = XML_PI_NODE; |
1776 | 102k | cur->doc = doc; |
1777 | | |
1778 | 102k | if ((doc != NULL) && (doc->dict != NULL)) |
1779 | 102k | cur->name = xmlDictLookup(doc->dict, name, -1); |
1780 | 0 | else |
1781 | 0 | cur->name = xmlStrdup(name); |
1782 | 102k | if (cur->name == NULL) |
1783 | 1 | goto error; |
1784 | 102k | if (content != NULL) { |
1785 | 56.6k | cur->content = xmlStrdup(content); |
1786 | 56.6k | if (cur->content == NULL) |
1787 | 9 | goto error; |
1788 | 56.6k | } |
1789 | | |
1790 | 102k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1791 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
1792 | 102k | return(cur); |
1793 | | |
1794 | 10 | error: |
1795 | 10 | xmlFreeNode(cur); |
1796 | 10 | return(NULL); |
1797 | 102k | } |
1798 | | |
1799 | | /** |
1800 | | * Create a processing instruction node. |
1801 | | * |
1802 | | * Use of this function is DISCOURAGED in favor of #xmlNewDocPI. |
1803 | | * |
1804 | | * @param name the processing instruction target |
1805 | | * @param content the PI content (optional) |
1806 | | * @returns a pointer to the new node object or NULL if arguments are |
1807 | | * invalid or a memory allocation failed. |
1808 | | */ |
1809 | | xmlNode * |
1810 | 0 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
1811 | 0 | return(xmlNewDocPI(NULL, name, content)); |
1812 | 0 | } |
1813 | | |
1814 | | /** |
1815 | | * Create an element node. |
1816 | | * |
1817 | | * Use of this function is DISCOURAGED in favor of #xmlNewDocNode. |
1818 | | * |
1819 | | * @param ns namespace (optional) |
1820 | | * @param name the node name |
1821 | | * @returns a pointer to the new node object or NULL if arguments are |
1822 | | * invalid or a memory allocation failed. |
1823 | | */ |
1824 | | xmlNode * |
1825 | 0 | xmlNewNode(xmlNs *ns, const xmlChar *name) { |
1826 | 0 | return(xmlNewDocNode(NULL, ns, name, NULL)); |
1827 | 0 | } |
1828 | | |
1829 | | /** |
1830 | | * Create an element node. |
1831 | | * |
1832 | | * Use of this function is DISCOURAGED in favor of #xmlNewDocNodeEatName. |
1833 | | * |
1834 | | * Like #xmlNewNode, but the `name` string will be used directly |
1835 | | * without making a copy. Takes ownership of `name` which will also |
1836 | | * be freed on error. |
1837 | | * |
1838 | | * @param ns namespace (optional) |
1839 | | * @param name the node name |
1840 | | * @returns a pointer to the new node object or NULL if arguments are |
1841 | | * invalid or a memory allocation failed. |
1842 | | */ |
1843 | | xmlNode * |
1844 | 0 | xmlNewNodeEatName(xmlNs *ns, xmlChar *name) { |
1845 | 0 | return(xmlNewDocNodeEatName(NULL, ns, name, NULL)); |
1846 | 0 | } |
1847 | | |
1848 | | static xmlNodePtr |
1849 | | xmlNewElem(xmlDocPtr doc, xmlNsPtr ns, const xmlChar *name, |
1850 | 7.66M | const xmlChar *content) { |
1851 | 7.66M | xmlNodePtr cur; |
1852 | | |
1853 | 7.66M | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
1854 | 7.66M | if (cur == NULL) |
1855 | 1.76k | return(NULL); |
1856 | 7.65M | memset(cur, 0, sizeof(xmlNode)); |
1857 | | |
1858 | 7.65M | cur->type = XML_ELEMENT_NODE; |
1859 | 7.65M | cur->doc = doc; |
1860 | 7.65M | cur->name = name; |
1861 | 7.65M | cur->ns = ns; |
1862 | | |
1863 | 7.65M | if (content != NULL) { |
1864 | 0 | if (xmlNodeParseContent(cur, content, -1) < 0) { |
1865 | | /* Don't free name on error */ |
1866 | 0 | xmlFree(cur); |
1867 | 0 | return(NULL); |
1868 | 0 | } |
1869 | 0 | } |
1870 | | |
1871 | 7.65M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1872 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
1873 | | |
1874 | 7.65M | return(cur); |
1875 | 7.65M | } |
1876 | | |
1877 | | /** |
1878 | | * Create an element node. |
1879 | | * |
1880 | | * If provided, `content` is expected to be a valid XML attribute value |
1881 | | * possibly containing character and entity references. Syntax errors |
1882 | | * and references to undeclared entities are ignored silently. |
1883 | | * Only references are handled, nested elements, comments or PIs are |
1884 | | * not. See #xmlNewDocRawNode for an alternative. |
1885 | | * |
1886 | | * General notes on object creation: |
1887 | | * |
1888 | | * Each node and all its children are associated with the same |
1889 | | * document. The document should be provided when creating nodes to |
1890 | | * avoid a performance penalty when adding the node to a document |
1891 | | * tree. Note that a document only owns nodes reachable from the root |
1892 | | * node. Unlinked subtrees must be freed manually. |
1893 | | * |
1894 | | * @param doc the target document |
1895 | | * @param ns namespace (optional) |
1896 | | * @param name the node name |
1897 | | * @param content text content with XML references (optional) |
1898 | | * @returns a pointer to the new node object or NULL if arguments are |
1899 | | * invalid or a memory allocation failed. |
1900 | | */ |
1901 | | xmlNode * |
1902 | | xmlNewDocNode(xmlDoc *doc, xmlNs *ns, |
1903 | 1.08M | const xmlChar *name, const xmlChar *content) { |
1904 | 1.08M | xmlNodePtr cur; |
1905 | 1.08M | xmlChar *copy; |
1906 | | |
1907 | 1.08M | if (name == NULL) |
1908 | 0 | return(NULL); |
1909 | | |
1910 | 1.08M | if ((doc != NULL) && (doc->dict != NULL)) { |
1911 | 1.08M | const xmlChar *dictName = xmlDictLookup(doc->dict, name, -1); |
1912 | | |
1913 | 1.08M | if (dictName == NULL) |
1914 | 1.18k | return(NULL); |
1915 | 1.08M | return(xmlNewElem(doc, ns, dictName, content)); |
1916 | 1.08M | } |
1917 | | |
1918 | 0 | copy = xmlStrdup(name); |
1919 | 0 | if (copy == NULL) |
1920 | 0 | return(NULL); |
1921 | | |
1922 | 0 | cur = xmlNewElem(doc, ns, copy, content); |
1923 | 0 | if (cur == NULL) { |
1924 | 0 | xmlFree(copy); |
1925 | 0 | return(NULL); |
1926 | 0 | } |
1927 | | |
1928 | 0 | return(cur); |
1929 | 0 | } |
1930 | | |
1931 | | /** |
1932 | | * Create an element node. |
1933 | | * |
1934 | | * Like #xmlNewDocNode, but the `name` string will be used directly |
1935 | | * without making a copy. Takes ownership of `name` which will also |
1936 | | * be freed on error. |
1937 | | * |
1938 | | * @param doc the target document |
1939 | | * @param ns namespace (optional) |
1940 | | * @param name the node name |
1941 | | * @param content text content with XML references (optional) |
1942 | | * @returns a pointer to the new node object or NULL if arguments are |
1943 | | * invalid or a memory allocation failed. |
1944 | | */ |
1945 | | xmlNode * |
1946 | | xmlNewDocNodeEatName(xmlDoc *doc, xmlNs *ns, |
1947 | 6.57M | xmlChar *name, const xmlChar *content) { |
1948 | 6.57M | xmlNodePtr cur; |
1949 | | |
1950 | 6.57M | if (name == NULL) |
1951 | 5 | return(NULL); |
1952 | | |
1953 | 6.57M | cur = xmlNewElem(doc, ns, name, content); |
1954 | 6.57M | if (cur == NULL) { |
1955 | | /* if name doesn't come from the doc dictionary free it here */ |
1956 | 392 | if ((doc == NULL) || |
1957 | 392 | (doc->dict == NULL) || |
1958 | 392 | (!xmlDictOwns(doc->dict, name))) |
1959 | 0 | xmlFree(name); |
1960 | 392 | return(NULL); |
1961 | 392 | } |
1962 | | |
1963 | 6.57M | return(cur); |
1964 | 6.57M | } |
1965 | | |
1966 | | /** |
1967 | | * Create an element node. |
1968 | | * |
1969 | | * If provided, `value` should be a raw, unescaped string. |
1970 | | * |
1971 | | * @param doc the target document |
1972 | | * @param ns namespace (optional) |
1973 | | * @param name the node name |
1974 | | * @param content raw text content (optional) |
1975 | | * @returns a pointer to the new node object or NULL if arguments are |
1976 | | * invalid or a memory allocation failed. |
1977 | | */ |
1978 | | xmlNode * |
1979 | | xmlNewDocRawNode(xmlDoc *doc, xmlNs *ns, |
1980 | 1.04M | const xmlChar *name, const xmlChar *content) { |
1981 | 1.04M | xmlNodePtr cur; |
1982 | | |
1983 | 1.04M | cur = xmlNewDocNode(doc, ns, name, NULL); |
1984 | 1.04M | if (cur != NULL) { |
1985 | 1.04M | cur->doc = doc; |
1986 | 1.04M | if (content != NULL) { |
1987 | 1.04M | xmlNodePtr text; |
1988 | | |
1989 | 1.04M | text = xmlNewDocText(doc, content); |
1990 | 1.04M | if (text == NULL) { |
1991 | 48 | xmlFreeNode(cur); |
1992 | 48 | return(NULL); |
1993 | 48 | } |
1994 | | |
1995 | 1.04M | cur->children = text; |
1996 | 1.04M | cur->last = text; |
1997 | 1.04M | text->parent = cur; |
1998 | 1.04M | } |
1999 | 1.04M | } |
2000 | 1.04M | return(cur); |
2001 | 1.04M | } |
2002 | | |
2003 | | /** |
2004 | | * Create a document fragment node. |
2005 | | * |
2006 | | * @param doc the target document (optional) |
2007 | | * @returns a pointer to the new node object or NULL if a memory |
2008 | | * allocation failed. |
2009 | | */ |
2010 | | xmlNode * |
2011 | 0 | xmlNewDocFragment(xmlDoc *doc) { |
2012 | 0 | xmlNodePtr cur; |
2013 | | |
2014 | | /* |
2015 | | * Allocate a new DocumentFragment node and fill the fields. |
2016 | | */ |
2017 | 0 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2018 | 0 | if (cur == NULL) |
2019 | 0 | return(NULL); |
2020 | 0 | memset(cur, 0, sizeof(xmlNode)); |
2021 | 0 | cur->type = XML_DOCUMENT_FRAG_NODE; |
2022 | |
|
2023 | 0 | cur->doc = doc; |
2024 | |
|
2025 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2026 | 0 | xmlRegisterNodeDefaultValue(cur); |
2027 | 0 | return(cur); |
2028 | 0 | } |
2029 | | |
2030 | | /** |
2031 | | * Create a text node. |
2032 | | * |
2033 | | * Use of this function is DISCOURAGED in favor of #xmlNewDocText. |
2034 | | * |
2035 | | * @param content raw text content (optional) |
2036 | | * @returns a pointer to the new node object or NULL if a memory |
2037 | | * allocation failed. |
2038 | | */ |
2039 | | xmlNode * |
2040 | 1.85M | xmlNewText(const xmlChar *content) { |
2041 | 1.85M | xmlNodePtr cur; |
2042 | | |
2043 | | /* |
2044 | | * Allocate a new node and fill the fields. |
2045 | | */ |
2046 | 1.85M | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2047 | 1.85M | if (cur == NULL) |
2048 | 1.22k | return(NULL); |
2049 | 1.85M | memset(cur, 0, sizeof(xmlNode)); |
2050 | 1.85M | cur->type = XML_TEXT_NODE; |
2051 | | |
2052 | 1.85M | cur->name = xmlStringText; |
2053 | 1.85M | if (content != NULL) { |
2054 | 1.66M | cur->content = xmlStrdup(content); |
2055 | 1.66M | if (cur->content == NULL) |
2056 | 45 | goto error; |
2057 | 1.66M | } |
2058 | | |
2059 | 1.85M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2060 | 0 | xmlRegisterNodeDefaultValue(cur); |
2061 | 1.85M | return(cur); |
2062 | | |
2063 | 45 | error: |
2064 | 45 | xmlFreeNode(cur); |
2065 | 45 | return(NULL); |
2066 | 1.85M | } |
2067 | | |
2068 | | /** |
2069 | | * Create a new child element and append it to a parent element. |
2070 | | * |
2071 | | * If `ns` is NULL, the newly created element inherits the namespace |
2072 | | * of the parent. |
2073 | | * |
2074 | | * If `content` is provided, a text node will be added to the child |
2075 | | * element, see #xmlNewDocRawNode. |
2076 | | * |
2077 | | * @param parent the parent node |
2078 | | * @param ns a namespace (optional) |
2079 | | * @param name the name of the child |
2080 | | * @param content raw text content of the child (optional) |
2081 | | * @returns a pointer to the new node object or NULL if arguments |
2082 | | * are invalid or a memory allocation failed. |
2083 | | */ |
2084 | | xmlNode * |
2085 | | xmlNewTextChild(xmlNode *parent, xmlNs *ns, |
2086 | 1.02M | const xmlChar *name, const xmlChar *content) { |
2087 | 1.02M | xmlNodePtr cur, prev; |
2088 | | |
2089 | 1.02M | if ((parent == NULL) || (name == NULL)) |
2090 | 0 | return(NULL); |
2091 | | |
2092 | 1.02M | switch (parent->type) { |
2093 | 1.02M | case XML_DOCUMENT_NODE: |
2094 | 1.02M | case XML_HTML_DOCUMENT_NODE: |
2095 | 1.02M | case XML_DOCUMENT_FRAG_NODE: |
2096 | 1.02M | break; |
2097 | | |
2098 | 0 | case XML_ELEMENT_NODE: |
2099 | 0 | if (ns == NULL) |
2100 | 0 | ns = parent->ns; |
2101 | 0 | break; |
2102 | | |
2103 | 0 | default: |
2104 | 0 | return(NULL); |
2105 | 1.02M | } |
2106 | | |
2107 | 1.02M | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
2108 | 1.02M | if (cur == NULL) |
2109 | 1.43k | return(NULL); |
2110 | | |
2111 | | /* |
2112 | | * add the new element at the end of the children list. |
2113 | | */ |
2114 | 1.02M | cur->parent = parent; |
2115 | 1.02M | if (parent->children == NULL) { |
2116 | 82.0k | parent->children = cur; |
2117 | 82.0k | parent->last = cur; |
2118 | 941k | } else { |
2119 | 941k | prev = parent->last; |
2120 | 941k | prev->next = cur; |
2121 | 941k | cur->prev = prev; |
2122 | 941k | parent->last = cur; |
2123 | 941k | } |
2124 | | |
2125 | 1.02M | return(cur); |
2126 | 1.02M | } |
2127 | | |
2128 | | /** |
2129 | | * Create an empty entity reference node. This function doesn't attempt |
2130 | | * to look up the entity in `doc`. |
2131 | | * |
2132 | | * `name` is consumed. |
2133 | | * |
2134 | | * @param doc the target document (optional) |
2135 | | * @param name the entity name |
2136 | | * @returns a pointer to the new node object or NULL if arguments are |
2137 | | * invalid or a memory allocation failed. |
2138 | | */ |
2139 | | static xmlNodePtr |
2140 | 21.1k | xmlNewEntityRef(xmlDocPtr doc, xmlChar *name) { |
2141 | 21.1k | xmlNodePtr cur; |
2142 | | |
2143 | | /* |
2144 | | * Allocate a new node and fill the fields. |
2145 | | */ |
2146 | 21.1k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2147 | 21.1k | if (cur == NULL) { |
2148 | 0 | xmlFree(name); |
2149 | 0 | return(NULL); |
2150 | 0 | } |
2151 | 21.1k | memset(cur, 0, sizeof(xmlNode)); |
2152 | 21.1k | cur->type = XML_ENTITY_REF_NODE; |
2153 | 21.1k | cur->doc = doc; |
2154 | 21.1k | cur->name = name; |
2155 | | |
2156 | 21.1k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2157 | 0 | xmlRegisterNodeDefaultValue(cur); |
2158 | | |
2159 | 21.1k | return(cur); |
2160 | 21.1k | } |
2161 | | |
2162 | | /** |
2163 | | * Create an empty entity reference node. |
2164 | | * |
2165 | | * This function is MISNAMED. It doesn't create a character reference |
2166 | | * but an entity reference. |
2167 | | * |
2168 | | * This function doesn't attempt to look up the entity in `doc`. |
2169 | | * |
2170 | | * Entity names like `&entity;` are handled as well. |
2171 | | * |
2172 | | * @param doc the target document (optional) |
2173 | | * @param name the entity name |
2174 | | * @returns a pointer to the new node object or NULL if arguments are |
2175 | | * invalid or a memory allocation failed. |
2176 | | */ |
2177 | | xmlNode * |
2178 | 0 | xmlNewCharRef(xmlDoc *doc, const xmlChar *name) { |
2179 | 0 | xmlChar *copy; |
2180 | |
|
2181 | 0 | if (name == NULL) |
2182 | 0 | return(NULL); |
2183 | | |
2184 | 0 | if (name[0] == '&') { |
2185 | 0 | int len; |
2186 | 0 | name++; |
2187 | 0 | len = xmlStrlen(name); |
2188 | 0 | if (name[len - 1] == ';') |
2189 | 0 | copy = xmlStrndup(name, len - 1); |
2190 | 0 | else |
2191 | 0 | copy = xmlStrndup(name, len); |
2192 | 0 | } else |
2193 | 0 | copy = xmlStrdup(name); |
2194 | 0 | if (copy == NULL) |
2195 | 0 | return(NULL); |
2196 | | |
2197 | 0 | return(xmlNewEntityRef(doc, copy)); |
2198 | 0 | } |
2199 | | |
2200 | | /** |
2201 | | * Create a new entity reference node, linking the result with the |
2202 | | * entity in `doc` if found. |
2203 | | * |
2204 | | * Entity names like `&entity;` are handled as well. |
2205 | | * |
2206 | | * @param doc the target document (optional) |
2207 | | * @param name the entity name |
2208 | | * @returns a pointer to the new node object or NULL if arguments are |
2209 | | * invalid or a memory allocation failed. |
2210 | | */ |
2211 | | xmlNode * |
2212 | 3.75k | xmlNewReference(const xmlDoc *doc, const xmlChar *name) { |
2213 | 3.75k | xmlNodePtr cur; |
2214 | 3.75k | xmlEntityPtr ent; |
2215 | | |
2216 | 3.75k | if (name == NULL) |
2217 | 0 | return(NULL); |
2218 | | |
2219 | | /* |
2220 | | * Allocate a new node and fill the fields. |
2221 | | */ |
2222 | 3.75k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2223 | 3.75k | if (cur == NULL) |
2224 | 0 | return(NULL); |
2225 | 3.75k | memset(cur, 0, sizeof(xmlNode)); |
2226 | 3.75k | cur->type = XML_ENTITY_REF_NODE; |
2227 | | |
2228 | 3.75k | cur->doc = (xmlDoc *)doc; |
2229 | 3.75k | if (name[0] == '&') { |
2230 | 0 | int len; |
2231 | 0 | name++; |
2232 | 0 | len = xmlStrlen(name); |
2233 | 0 | if (name[len - 1] == ';') |
2234 | 0 | cur->name = xmlStrndup(name, len - 1); |
2235 | 0 | else |
2236 | 0 | cur->name = xmlStrndup(name, len); |
2237 | 0 | } else |
2238 | 3.75k | cur->name = xmlStrdup(name); |
2239 | 3.75k | if (cur->name == NULL) |
2240 | 0 | goto error; |
2241 | | |
2242 | 3.75k | ent = xmlGetDocEntity(doc, cur->name); |
2243 | 3.75k | if (ent != NULL) { |
2244 | 1 | cur->content = ent->content; |
2245 | | /* |
2246 | | * The parent pointer in entity is a DTD pointer and thus is NOT |
2247 | | * updated. Not sure if this is 100% correct. |
2248 | | * -George |
2249 | | */ |
2250 | 1 | cur->children = (xmlNodePtr) ent; |
2251 | 1 | cur->last = (xmlNodePtr) ent; |
2252 | 1 | } |
2253 | | |
2254 | 3.75k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2255 | 0 | xmlRegisterNodeDefaultValue(cur); |
2256 | 3.75k | return(cur); |
2257 | | |
2258 | 0 | error: |
2259 | 0 | xmlFreeNode(cur); |
2260 | 0 | return(NULL); |
2261 | 3.75k | } |
2262 | | |
2263 | | /** |
2264 | | * Create a new text node. |
2265 | | * |
2266 | | * @param doc the target document |
2267 | | * @param content raw text content (optional) |
2268 | | * @returns a pointer to the new node object or NULL if a memory |
2269 | | * allocation failed. |
2270 | | */ |
2271 | | xmlNode * |
2272 | 1.67M | xmlNewDocText(const xmlDoc *doc, const xmlChar *content) { |
2273 | 1.67M | xmlNodePtr cur; |
2274 | | |
2275 | 1.67M | cur = xmlNewText(content); |
2276 | 1.67M | if (cur != NULL) cur->doc = (xmlDoc *)doc; |
2277 | 1.67M | return(cur); |
2278 | 1.67M | } |
2279 | | |
2280 | | /** |
2281 | | * Create a new text node. |
2282 | | * |
2283 | | * Use of this function is DISCOURAGED in favor of #xmlNewDocTextLen. |
2284 | | * |
2285 | | * @param content raw text content (optional) |
2286 | | * @param len size of text content |
2287 | | * @returns a pointer to the new node object or NULL if a memory |
2288 | | * allocation failed. |
2289 | | */ |
2290 | | xmlNode * |
2291 | 1.08M | xmlNewTextLen(const xmlChar *content, int len) { |
2292 | 1.08M | xmlNodePtr cur; |
2293 | | |
2294 | | /* |
2295 | | * Allocate a new node and fill the fields. |
2296 | | */ |
2297 | 1.08M | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2298 | 1.08M | if (cur == NULL) |
2299 | 4.88k | return(NULL); |
2300 | 1.08M | memset(cur, 0, sizeof(xmlNode)); |
2301 | 1.08M | cur->type = XML_TEXT_NODE; |
2302 | | |
2303 | 1.08M | cur->name = xmlStringText; |
2304 | 1.08M | if (content != NULL) { |
2305 | 662k | cur->content = xmlStrndup(content, len); |
2306 | 662k | if (cur->content == NULL) { |
2307 | 41 | xmlFreeNode(cur); |
2308 | 41 | return(NULL); |
2309 | 41 | } |
2310 | 662k | } |
2311 | | |
2312 | 1.08M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2313 | 0 | xmlRegisterNodeDefaultValue(cur); |
2314 | 1.08M | return(cur); |
2315 | 1.08M | } |
2316 | | |
2317 | | /** |
2318 | | * Create a new text node. |
2319 | | * |
2320 | | * @param doc the target document |
2321 | | * @param content raw text content (optional) |
2322 | | * @param len size of text content |
2323 | | * @returns a pointer to the new node object or NULL if a memory |
2324 | | * allocation failed. |
2325 | | */ |
2326 | | xmlNode * |
2327 | 0 | xmlNewDocTextLen(xmlDoc *doc, const xmlChar *content, int len) { |
2328 | 0 | xmlNodePtr cur; |
2329 | |
|
2330 | 0 | cur = xmlNewTextLen(content, len); |
2331 | 0 | if (cur != NULL) cur->doc = doc; |
2332 | 0 | return(cur); |
2333 | 0 | } |
2334 | | |
2335 | | /** |
2336 | | * Use of this function is DISCOURAGED in favor of #xmlNewDocComment. |
2337 | | * |
2338 | | * Create a comment node. |
2339 | | * |
2340 | | * @param content the comment content (optional) |
2341 | | * @returns a pointer to the new node object or NULL if a memory |
2342 | | * allocation failed. |
2343 | | */ |
2344 | | xmlNode * |
2345 | 102k | xmlNewComment(const xmlChar *content) { |
2346 | 102k | xmlNodePtr cur; |
2347 | | |
2348 | | /* |
2349 | | * Allocate a new node and fill the fields. |
2350 | | */ |
2351 | 102k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2352 | 102k | if (cur == NULL) |
2353 | 201 | return(NULL); |
2354 | 102k | memset(cur, 0, sizeof(xmlNode)); |
2355 | 102k | cur->type = XML_COMMENT_NODE; |
2356 | | |
2357 | 102k | cur->name = xmlStringComment; |
2358 | 102k | if (content != NULL) { |
2359 | 102k | cur->content = xmlStrdup(content); |
2360 | 102k | if (cur->content == NULL) |
2361 | 8 | goto error; |
2362 | 102k | } |
2363 | | |
2364 | 102k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2365 | 0 | xmlRegisterNodeDefaultValue(cur); |
2366 | 102k | return(cur); |
2367 | | |
2368 | 8 | error: |
2369 | 8 | xmlFreeNode(cur); |
2370 | 8 | return(NULL); |
2371 | 102k | } |
2372 | | |
2373 | | /** |
2374 | | * Create a CDATA section node. |
2375 | | * |
2376 | | * @param doc the target document (optional) |
2377 | | * @param content raw text content (optional) |
2378 | | * @param len size of text content |
2379 | | * @returns a pointer to the new node object or NULL if a memory |
2380 | | * allocation failed. |
2381 | | */ |
2382 | | xmlNode * |
2383 | 9.00k | xmlNewCDataBlock(xmlDoc *doc, const xmlChar *content, int len) { |
2384 | 9.00k | xmlNodePtr cur; |
2385 | | |
2386 | | /* |
2387 | | * Allocate a new node and fill the fields. |
2388 | | */ |
2389 | 9.00k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2390 | 9.00k | if (cur == NULL) |
2391 | 116 | return(NULL); |
2392 | 8.88k | memset(cur, 0, sizeof(xmlNode)); |
2393 | 8.88k | cur->type = XML_CDATA_SECTION_NODE; |
2394 | 8.88k | cur->doc = doc; |
2395 | | |
2396 | 8.88k | if (content != NULL) { |
2397 | 8.88k | cur->content = xmlStrndup(content, len); |
2398 | 8.88k | if (cur->content == NULL) { |
2399 | 7 | xmlFree(cur); |
2400 | 7 | return(NULL); |
2401 | 7 | } |
2402 | 8.88k | } |
2403 | | |
2404 | 8.87k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2405 | 0 | xmlRegisterNodeDefaultValue(cur); |
2406 | 8.87k | return(cur); |
2407 | 8.88k | } |
2408 | | |
2409 | | /** |
2410 | | * Create a comment node. |
2411 | | * |
2412 | | * @param doc the document |
2413 | | * @param content the comment content |
2414 | | * @returns a pointer to the new node object or NULL if a memory |
2415 | | * allocation failed. |
2416 | | */ |
2417 | | xmlNode * |
2418 | 99.7k | xmlNewDocComment(xmlDoc *doc, const xmlChar *content) { |
2419 | 99.7k | xmlNodePtr cur; |
2420 | | |
2421 | 99.7k | cur = xmlNewComment(content); |
2422 | 99.7k | if (cur != NULL) cur->doc = doc; |
2423 | 99.7k | return(cur); |
2424 | 99.7k | } |
2425 | | |
2426 | | static void |
2427 | 0 | xmlRemoveEntity(xmlEntityPtr ent) { |
2428 | 0 | xmlDocPtr doc = ent->doc; |
2429 | 0 | xmlDtdPtr intSubset, extSubset; |
2430 | |
|
2431 | 0 | if (doc == NULL) |
2432 | 0 | return; |
2433 | 0 | intSubset = doc->intSubset; |
2434 | 0 | extSubset = doc->extSubset; |
2435 | |
|
2436 | 0 | if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
2437 | 0 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
2438 | 0 | (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) { |
2439 | 0 | if (intSubset != NULL) { |
2440 | 0 | if (xmlHashLookup(intSubset->entities, ent->name) == ent) |
2441 | 0 | xmlHashRemoveEntry(intSubset->entities, ent->name, NULL); |
2442 | 0 | } |
2443 | 0 | if (extSubset != NULL) { |
2444 | 0 | if (xmlHashLookup(extSubset->entities, ent->name) == ent) |
2445 | 0 | xmlHashRemoveEntry(extSubset->entities, ent->name, NULL); |
2446 | 0 | } |
2447 | 0 | } else if ((ent->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
2448 | 0 | (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
2449 | 0 | if (intSubset != NULL) { |
2450 | 0 | if (xmlHashLookup(intSubset->pentities, ent->name) == ent) |
2451 | 0 | xmlHashRemoveEntry(intSubset->entities, ent->name, NULL); |
2452 | 0 | } |
2453 | 0 | if (extSubset != NULL) { |
2454 | 0 | if (xmlHashLookup(extSubset->pentities, ent->name) == ent) |
2455 | 0 | xmlHashRemoveEntry(extSubset->entities, ent->name, NULL); |
2456 | 0 | } |
2457 | 0 | } |
2458 | 0 | } |
2459 | | |
2460 | | static int |
2461 | 626k | xmlNodeSetDoc(xmlNodePtr node, xmlDocPtr doc) { |
2462 | 626k | xmlDocPtr oldDoc; |
2463 | 626k | xmlDictPtr oldDict, newDict; |
2464 | 626k | int ret = 0; |
2465 | | |
2466 | | /* |
2467 | | * Remove name and content from old dictionary |
2468 | | */ |
2469 | | |
2470 | 626k | oldDoc = node->doc; |
2471 | 626k | oldDict = oldDoc ? oldDoc->dict : NULL; |
2472 | 626k | newDict = doc ? doc->dict : NULL; |
2473 | | |
2474 | 626k | if ((oldDict != NULL) && (oldDict != newDict)) { |
2475 | 0 | if ((node->name != NULL) && |
2476 | 0 | ((node->type == XML_ELEMENT_NODE) || |
2477 | 0 | (node->type == XML_ATTRIBUTE_NODE) || |
2478 | 0 | (node->type == XML_PI_NODE) || |
2479 | 0 | (node->type == XML_ENTITY_REF_NODE)) && |
2480 | 0 | (xmlDictOwns(oldDict, node->name))) { |
2481 | 0 | if (newDict) |
2482 | 0 | node->name = xmlDictLookup(newDict, node->name, -1); |
2483 | 0 | else |
2484 | 0 | node->name = xmlStrdup(node->name); |
2485 | 0 | if (node->name == NULL) |
2486 | 0 | ret = -1; |
2487 | 0 | } |
2488 | |
|
2489 | 0 | if ((node->content != NULL) && |
2490 | 0 | ((node->type == XML_TEXT_NODE) || |
2491 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
2492 | 0 | (xmlDictOwns(oldDict, node->content))) { |
2493 | 0 | node->content = xmlStrdup(node->content); |
2494 | 0 | if (node->content == NULL) |
2495 | 0 | ret = -1; |
2496 | 0 | } |
2497 | 0 | } |
2498 | | |
2499 | 626k | switch (node->type) { |
2500 | 0 | case XML_ATTRIBUTE_NODE: { |
2501 | 0 | xmlAttrPtr attr = (xmlAttrPtr) node; |
2502 | | |
2503 | | /* |
2504 | | * Handle IDs |
2505 | | * |
2506 | | * TODO: ID attributes should also be added to the new |
2507 | | * document, but it's not clear how to handle clashes. |
2508 | | */ |
2509 | 0 | if (attr->atype == XML_ATTRIBUTE_ID) |
2510 | 0 | xmlRemoveID(oldDoc, attr); |
2511 | |
|
2512 | 0 | break; |
2513 | 0 | } |
2514 | | |
2515 | 0 | case XML_ENTITY_REF_NODE: |
2516 | | /* |
2517 | | * Handle entity references |
2518 | | */ |
2519 | 0 | node->children = NULL; |
2520 | 0 | node->last = NULL; |
2521 | 0 | node->content = NULL; |
2522 | |
|
2523 | 0 | if ((doc != NULL) && |
2524 | 0 | ((doc->intSubset != NULL) || (doc->extSubset != NULL))) { |
2525 | 0 | xmlEntityPtr ent; |
2526 | | |
2527 | | /* |
2528 | | * Assign new entity node if available |
2529 | | */ |
2530 | 0 | ent = xmlGetDocEntity(doc, node->name); |
2531 | 0 | if (ent != NULL) { |
2532 | 0 | node->children = (xmlNodePtr) ent; |
2533 | 0 | node->last = (xmlNodePtr) ent; |
2534 | 0 | node->content = ent->content; |
2535 | 0 | } |
2536 | 0 | } |
2537 | |
|
2538 | 0 | break; |
2539 | | |
2540 | 0 | case XML_DTD_NODE: |
2541 | 0 | if (oldDoc != NULL) { |
2542 | 0 | if (oldDoc->intSubset == (xmlDtdPtr) node) |
2543 | 0 | oldDoc->intSubset = NULL; |
2544 | 0 | if (oldDoc->extSubset == (xmlDtdPtr) node) |
2545 | 0 | oldDoc->extSubset = NULL; |
2546 | 0 | } |
2547 | |
|
2548 | 0 | break; |
2549 | | |
2550 | 0 | case XML_ENTITY_DECL: |
2551 | 0 | xmlRemoveEntity((xmlEntityPtr) node); |
2552 | 0 | break; |
2553 | | |
2554 | | /* |
2555 | | * TODO: |
2556 | | * - Remove element decls from doc->elements |
2557 | | * - Remove attribtue decls form doc->attributes |
2558 | | */ |
2559 | | |
2560 | 626k | default: |
2561 | 626k | break; |
2562 | 626k | } |
2563 | | |
2564 | | /* |
2565 | | * Set new document |
2566 | | */ |
2567 | 626k | node->doc = doc; |
2568 | | |
2569 | 626k | return(ret); |
2570 | 626k | } |
2571 | | |
2572 | | /** |
2573 | | * Associate all nodes in a tree with a new document. |
2574 | | * |
2575 | | * This is an internal function which shouldn't be used. It is |
2576 | | * invoked by functions like #xmlAddChild, #xmlAddSibling or |
2577 | | * #xmlReplaceNode. `tree` must be the root node of an unlinked |
2578 | | * subtree. |
2579 | | * |
2580 | | * Also copy strings from the old document's dictionary and |
2581 | | * remove ID attributes from the old ID table. |
2582 | | * |
2583 | | * @param tree root of a subtree |
2584 | | * @param doc new document |
2585 | | * @returns 0 on success. If a memory allocation fails, returns -1. |
2586 | | * The whole tree will be updated on failure but some strings |
2587 | | * may be lost. |
2588 | | */ |
2589 | | int |
2590 | 626k | xmlSetTreeDoc(xmlNode *tree, xmlDoc *doc) { |
2591 | 626k | int ret = 0; |
2592 | | |
2593 | 626k | if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL)) |
2594 | 0 | return(0); |
2595 | 626k | if (tree->doc == doc) |
2596 | 0 | return(0); |
2597 | | |
2598 | 626k | if (tree->type == XML_ELEMENT_NODE) { |
2599 | 2.43k | xmlAttrPtr prop = tree->properties; |
2600 | | |
2601 | 2.43k | while (prop != NULL) { |
2602 | 0 | if (prop->children != NULL) { |
2603 | 0 | if (xmlSetListDoc(prop->children, doc) < 0) |
2604 | 0 | ret = -1; |
2605 | 0 | } |
2606 | |
|
2607 | 0 | if (xmlNodeSetDoc((xmlNodePtr) prop, doc) < 0) |
2608 | 0 | ret = -1; |
2609 | |
|
2610 | 0 | prop = prop->next; |
2611 | 0 | } |
2612 | 2.43k | } |
2613 | | |
2614 | 626k | if ((tree->children != NULL) && |
2615 | 626k | (tree->type != XML_ENTITY_REF_NODE)) { |
2616 | 0 | if (xmlSetListDoc(tree->children, doc) < 0) |
2617 | 0 | ret = -1; |
2618 | 0 | } |
2619 | | |
2620 | 626k | if (xmlNodeSetDoc(tree, doc) < 0) |
2621 | 0 | ret = -1; |
2622 | | |
2623 | 626k | return(ret); |
2624 | 626k | } |
2625 | | |
2626 | | /** |
2627 | | * Associate all subtrees in `list` with a new document. |
2628 | | * |
2629 | | * Internal function, see #xmlSetTreeDoc. |
2630 | | * |
2631 | | * @param list a node list |
2632 | | * @param doc new document |
2633 | | * @returns 0 on success. If a memory allocation fails, returns -1. |
2634 | | * All subtrees will be updated on failure but some strings |
2635 | | * may be lost. |
2636 | | */ |
2637 | | int |
2638 | 0 | xmlSetListDoc(xmlNode *list, xmlDoc *doc) { |
2639 | 0 | xmlNodePtr cur; |
2640 | 0 | int ret = 0; |
2641 | |
|
2642 | 0 | if ((list == NULL) || (list->type == XML_NAMESPACE_DECL)) |
2643 | 0 | return(0); |
2644 | | |
2645 | 0 | cur = list; |
2646 | 0 | while (cur != NULL) { |
2647 | 0 | if (cur->doc != doc) { |
2648 | 0 | if (xmlSetTreeDoc(cur, doc) < 0) |
2649 | 0 | ret = -1; |
2650 | 0 | } |
2651 | 0 | cur = cur->next; |
2652 | 0 | } |
2653 | |
|
2654 | 0 | return(ret); |
2655 | 0 | } |
2656 | | |
2657 | | /** |
2658 | | * Create a new child element and append it to a parent element. |
2659 | | * |
2660 | | * If `ns` is NULL, the newly created element inherits the namespace |
2661 | | * of the parent. |
2662 | | * |
2663 | | * If provided, `content` is expected to be a valid XML attribute |
2664 | | * value possibly containing character and entity references. Text |
2665 | | * and entity reference node will be added to the child element, |
2666 | | * see #xmlNewDocNode. |
2667 | | * |
2668 | | * @param parent the parent node |
2669 | | * @param ns a namespace (optional) |
2670 | | * @param name the name of the child |
2671 | | * @param content text content with XML references (optional) |
2672 | | * @returns a pointer to the new node object or NULL if arguments |
2673 | | * are invalid or a memory allocation failed. |
2674 | | */ |
2675 | | xmlNode * |
2676 | | xmlNewChild(xmlNode *parent, xmlNs *ns, |
2677 | 0 | const xmlChar *name, const xmlChar *content) { |
2678 | 0 | xmlNodePtr cur, prev; |
2679 | |
|
2680 | 0 | if ((parent == NULL) || (name == NULL)) |
2681 | 0 | return(NULL); |
2682 | | |
2683 | 0 | switch (parent->type) { |
2684 | 0 | case XML_DOCUMENT_NODE: |
2685 | 0 | case XML_HTML_DOCUMENT_NODE: |
2686 | 0 | case XML_DOCUMENT_FRAG_NODE: |
2687 | 0 | break; |
2688 | | |
2689 | 0 | case XML_ELEMENT_NODE: |
2690 | 0 | if (ns == NULL) |
2691 | 0 | ns = parent->ns; |
2692 | 0 | break; |
2693 | | |
2694 | 0 | default: |
2695 | 0 | return(NULL); |
2696 | 0 | } |
2697 | | |
2698 | 0 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
2699 | 0 | if (cur == NULL) |
2700 | 0 | return(NULL); |
2701 | | |
2702 | | /* |
2703 | | * add the new element at the end of the children list. |
2704 | | */ |
2705 | 0 | cur->parent = parent; |
2706 | 0 | if (parent->children == NULL) { |
2707 | 0 | parent->children = cur; |
2708 | 0 | parent->last = cur; |
2709 | 0 | } else { |
2710 | 0 | prev = parent->last; |
2711 | 0 | prev->next = cur; |
2712 | 0 | cur->prev = prev; |
2713 | 0 | parent->last = cur; |
2714 | 0 | } |
2715 | |
|
2716 | 0 | return(cur); |
2717 | 0 | } |
2718 | | |
2719 | | static void |
2720 | 3.89M | xmlTextSetContent(xmlNodePtr text, xmlChar *content) { |
2721 | 3.89M | if ((text->content != NULL) && |
2722 | 3.89M | (text->content != (xmlChar *) &text->properties)) { |
2723 | 3.89M | xmlDocPtr doc = text->doc; |
2724 | | |
2725 | 3.89M | if ((doc == NULL) || |
2726 | 3.89M | (doc->dict == NULL) || |
2727 | 3.89M | (!xmlDictOwns(doc->dict, text->content))) |
2728 | 3.77M | xmlFree(text->content); |
2729 | 3.89M | } |
2730 | | |
2731 | 3.89M | text->content = content; |
2732 | 3.89M | text->properties = NULL; |
2733 | 3.89M | } |
2734 | | |
2735 | | static int |
2736 | 126k | xmlTextAddContent(xmlNodePtr text, const xmlChar *content, int len) { |
2737 | 126k | xmlChar *merged; |
2738 | | |
2739 | 126k | if (content == NULL) |
2740 | 0 | return(0); |
2741 | | |
2742 | 126k | merged = xmlStrncatNew(text->content, content, len); |
2743 | 126k | if (merged == NULL) |
2744 | 371 | return(-1); |
2745 | | |
2746 | 125k | xmlTextSetContent(text, merged); |
2747 | 125k | return(0); |
2748 | 126k | } |
2749 | | |
2750 | | static xmlNodePtr |
2751 | | xmlInsertProp(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent, |
2752 | 0 | xmlNodePtr prev, xmlNodePtr next) { |
2753 | 0 | xmlAttrPtr attr; |
2754 | |
|
2755 | 0 | if (((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)) || |
2756 | 0 | ((next != NULL) && (next->type != XML_ATTRIBUTE_NODE))) |
2757 | 0 | return(NULL); |
2758 | | |
2759 | | /* check if an attribute with the same name exists */ |
2760 | 0 | attr = xmlGetPropNodeInternal(parent, cur->name, |
2761 | 0 | cur->ns ? cur->ns->href : NULL, 0); |
2762 | |
|
2763 | 0 | xmlUnlinkNodeInternal(cur); |
2764 | |
|
2765 | 0 | if (cur->doc != doc) { |
2766 | 0 | if (xmlSetTreeDoc(cur, doc) < 0) |
2767 | 0 | return(NULL); |
2768 | 0 | } |
2769 | | |
2770 | 0 | cur->parent = parent; |
2771 | 0 | cur->prev = prev; |
2772 | 0 | cur->next = next; |
2773 | |
|
2774 | 0 | if (prev == NULL) { |
2775 | 0 | if (parent != NULL) |
2776 | 0 | parent->properties = (xmlAttrPtr) cur; |
2777 | 0 | } else { |
2778 | 0 | prev->next = cur; |
2779 | 0 | } |
2780 | 0 | if (next != NULL) { |
2781 | 0 | next->prev = cur; |
2782 | 0 | } |
2783 | |
|
2784 | 0 | if ((attr != NULL) && (attr != (xmlAttrPtr) cur)) { |
2785 | | /* different instance, destroy it (attributes must be unique) */ |
2786 | 0 | xmlRemoveProp((xmlAttrPtr) attr); |
2787 | 0 | } |
2788 | |
|
2789 | 0 | return cur; |
2790 | 0 | } |
2791 | | |
2792 | | static xmlNodePtr |
2793 | | xmlInsertNode(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent, |
2794 | 2.36M | xmlNodePtr prev, xmlNodePtr next, int coalesce) { |
2795 | 2.36M | xmlNodePtr oldParent; |
2796 | | |
2797 | 2.36M | if (cur->type == XML_ATTRIBUTE_NODE) |
2798 | 0 | return xmlInsertProp(doc, cur, parent, prev, next); |
2799 | | |
2800 | | /* |
2801 | | * Coalesce text nodes |
2802 | | */ |
2803 | 2.36M | if ((coalesce) && (cur->type == XML_TEXT_NODE)) { |
2804 | 1.08M | if ((prev != NULL) && (prev->type == XML_TEXT_NODE) && |
2805 | 1.08M | (prev->name == cur->name)) { |
2806 | 0 | if (xmlTextAddContent(prev, cur->content, -1) < 0) |
2807 | 0 | return(NULL); |
2808 | 0 | xmlUnlinkNodeInternal(cur); |
2809 | 0 | xmlFreeNode(cur); |
2810 | 0 | return(prev); |
2811 | 0 | } |
2812 | | |
2813 | 1.08M | if ((next != NULL) && (next->type == XML_TEXT_NODE) && |
2814 | 1.08M | (next->name == cur->name)) { |
2815 | 0 | if (cur->content != NULL) { |
2816 | 0 | xmlChar *merged; |
2817 | |
|
2818 | 0 | merged = xmlStrncatNew(cur->content, next->content, -1); |
2819 | 0 | if (merged == NULL) |
2820 | 0 | return(NULL); |
2821 | 0 | xmlTextSetContent(next, merged); |
2822 | 0 | } |
2823 | | |
2824 | 0 | xmlUnlinkNodeInternal(cur); |
2825 | 0 | xmlFreeNode(cur); |
2826 | 0 | return(next); |
2827 | 0 | } |
2828 | 1.08M | } |
2829 | | |
2830 | | /* Unlink */ |
2831 | 2.36M | oldParent = cur->parent; |
2832 | 2.36M | if (oldParent != NULL) { |
2833 | 0 | if (oldParent->children == cur) |
2834 | 0 | oldParent->children = cur->next; |
2835 | 0 | if (oldParent->last == cur) |
2836 | 0 | oldParent->last = cur->prev; |
2837 | 0 | } |
2838 | 2.36M | if (cur->next != NULL) |
2839 | 0 | cur->next->prev = cur->prev; |
2840 | 2.36M | if (cur->prev != NULL) |
2841 | 0 | cur->prev->next = cur->next; |
2842 | | |
2843 | 2.36M | if (cur->doc != doc) { |
2844 | 626k | if (xmlSetTreeDoc(cur, doc) < 0) { |
2845 | | /* |
2846 | | * We shouldn't make any modifications to the inserted |
2847 | | * tree if a memory allocation fails, but that's hard to |
2848 | | * implement. The tree has been moved to the target |
2849 | | * document now but some contents are corrupted. |
2850 | | * Unlinking is the best we can do. |
2851 | | */ |
2852 | 0 | cur->parent = NULL; |
2853 | 0 | cur->prev = NULL; |
2854 | 0 | cur->next = NULL; |
2855 | 0 | return(NULL); |
2856 | 0 | } |
2857 | 626k | } |
2858 | | |
2859 | 2.36M | cur->parent = parent; |
2860 | 2.36M | cur->prev = prev; |
2861 | 2.36M | cur->next = next; |
2862 | | |
2863 | 2.36M | if (prev == NULL) { |
2864 | 628k | if (parent != NULL) |
2865 | 628k | parent->children = cur; |
2866 | 1.73M | } else { |
2867 | 1.73M | prev->next = cur; |
2868 | 1.73M | } |
2869 | 2.36M | if (next == NULL) { |
2870 | 2.35M | if (parent != NULL) |
2871 | 2.35M | parent->last = cur; |
2872 | 2.35M | } else { |
2873 | 8.79k | next->prev = cur; |
2874 | 8.79k | } |
2875 | | |
2876 | 2.36M | return(cur); |
2877 | 2.36M | } |
2878 | | |
2879 | | /** |
2880 | | * Unlinks `cur` and inserts it as next sibling after `prev`. |
2881 | | * |
2882 | | * Unlike #xmlAddChild this function does not merge text nodes. |
2883 | | * |
2884 | | * If `cur` is an attribute node, it is inserted after attribute |
2885 | | * `prev`. If the attribute list contains an attribute with a name |
2886 | | * matching `cur`, the old attribute is destroyed. |
2887 | | * |
2888 | | * See the notes in #xmlAddChild. |
2889 | | * |
2890 | | * @param prev the target node |
2891 | | * @param cur the new node |
2892 | | * @returns `cur` or a sibling if `cur` was merged. Returns NULL |
2893 | | * if arguments are invalid or a memory allocation failed. |
2894 | | */ |
2895 | | xmlNode * |
2896 | 0 | xmlAddNextSibling(xmlNode *prev, xmlNode *cur) { |
2897 | 0 | if ((prev == NULL) || (prev->type == XML_NAMESPACE_DECL) || |
2898 | 0 | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
2899 | 0 | (cur == prev)) |
2900 | 0 | return(NULL); |
2901 | | |
2902 | 0 | if (cur == prev->next) |
2903 | 0 | return(cur); |
2904 | | |
2905 | 0 | return(xmlInsertNode(prev->doc, cur, prev->parent, prev, prev->next, 0)); |
2906 | 0 | } |
2907 | | |
2908 | | /** |
2909 | | * Unlinks `cur` and inserts it as previous sibling before `next`. |
2910 | | * |
2911 | | * Unlike #xmlAddChild this function does not merge text nodes. |
2912 | | * |
2913 | | * If `cur` is an attribute node, it is inserted before attribute |
2914 | | * `next`. If the attribute list contains an attribute with a name |
2915 | | * matching `cur`, the old attribute is destroyed. |
2916 | | * |
2917 | | * See the notes in #xmlAddChild. |
2918 | | * |
2919 | | * @param next the target node |
2920 | | * @param cur the new node |
2921 | | * @returns `cur` or a sibling if `cur` was merged. Returns NULL |
2922 | | * if arguments are invalid or a memory allocation failed. |
2923 | | */ |
2924 | | xmlNode * |
2925 | 8.79k | xmlAddPrevSibling(xmlNode *next, xmlNode *cur) { |
2926 | 8.79k | if ((next == NULL) || (next->type == XML_NAMESPACE_DECL) || |
2927 | 8.79k | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
2928 | 8.79k | (cur == next)) |
2929 | 0 | return(NULL); |
2930 | | |
2931 | 8.79k | if (cur == next->prev) |
2932 | 0 | return(cur); |
2933 | | |
2934 | 8.79k | return(xmlInsertNode(next->doc, cur, next->parent, next->prev, next, 0)); |
2935 | 8.79k | } |
2936 | | |
2937 | | /** |
2938 | | * Unlinks `cur` and inserts it as last sibling of `node`. |
2939 | | * |
2940 | | * If `cur` is a text node, it may be merged with an adjacent text |
2941 | | * node and freed. In this case the text node containing the merged |
2942 | | * content is returned. |
2943 | | * |
2944 | | * If `cur` is an attribute node, it is appended to the attribute |
2945 | | * list containing `node`. If the attribute list contains an attribute |
2946 | | * with a name matching `cur`, the old attribute is destroyed. |
2947 | | * |
2948 | | * See the notes in #xmlAddChild. |
2949 | | * |
2950 | | * @param node the target node |
2951 | | * @param cur the new node |
2952 | | * @returns `cur` or a sibling if `cur` was merged. Returns NULL |
2953 | | * if arguments are invalid or a memory allocation failed. |
2954 | | */ |
2955 | | xmlNode * |
2956 | 0 | xmlAddSibling(xmlNode *node, xmlNode *cur) { |
2957 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || |
2958 | 0 | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
2959 | 0 | (cur == node)) |
2960 | 0 | return(NULL); |
2961 | | |
2962 | | /* |
2963 | | * Constant time is we can rely on the ->parent->last to find |
2964 | | * the last sibling. |
2965 | | */ |
2966 | 0 | if ((node->type != XML_ATTRIBUTE_NODE) && (node->parent != NULL)) { |
2967 | 0 | if (node->parent->last != NULL) |
2968 | 0 | node = node->parent->last; |
2969 | 0 | } else { |
2970 | 0 | while (node->next != NULL) |
2971 | 0 | node = node->next; |
2972 | 0 | } |
2973 | |
|
2974 | 0 | if (cur == node) |
2975 | 0 | return(cur); |
2976 | | |
2977 | 0 | return(xmlInsertNode(node->doc, cur, node->parent, node, NULL, 1)); |
2978 | 0 | } |
2979 | | |
2980 | | /** |
2981 | | * Append a node list to another node. |
2982 | | * |
2983 | | * See #xmlAddChild. |
2984 | | * |
2985 | | * @param parent the parent node |
2986 | | * @param cur the first node in the list |
2987 | | * @returns the last child or NULL in case of error. |
2988 | | */ |
2989 | | xmlNode * |
2990 | 0 | xmlAddChildList(xmlNode *parent, xmlNode *cur) { |
2991 | 0 | xmlNodePtr iter; |
2992 | 0 | xmlNodePtr prev; |
2993 | 0 | int oom; |
2994 | |
|
2995 | 0 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
2996 | 0 | return(NULL); |
2997 | 0 | } |
2998 | | |
2999 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
3000 | 0 | return(NULL); |
3001 | 0 | } |
3002 | | |
3003 | 0 | oom = 0; |
3004 | 0 | for (iter = cur; iter != NULL; iter = iter->next) { |
3005 | 0 | if (iter->doc != parent->doc) { |
3006 | 0 | if (xmlSetTreeDoc(iter, parent->doc) < 0) |
3007 | 0 | oom = 1; |
3008 | 0 | } |
3009 | 0 | } |
3010 | 0 | if (oom) |
3011 | 0 | return(NULL); |
3012 | | |
3013 | | /* |
3014 | | * add the first element at the end of the children list. |
3015 | | */ |
3016 | | |
3017 | 0 | if (parent->children == NULL) { |
3018 | 0 | parent->children = cur; |
3019 | 0 | } else { |
3020 | 0 | prev = parent->last; |
3021 | | |
3022 | | /* |
3023 | | * If cur and parent->last both are TEXT nodes, then merge them. |
3024 | | */ |
3025 | 0 | if ((cur->type == XML_TEXT_NODE) && |
3026 | 0 | (prev->type == XML_TEXT_NODE) && |
3027 | 0 | (cur->name == prev->name)) { |
3028 | 0 | xmlNodePtr next; |
3029 | |
|
3030 | 0 | if (xmlTextAddContent(prev, cur->content, -1) < 0) |
3031 | 0 | return(NULL); |
3032 | 0 | next = cur->next; |
3033 | 0 | xmlFreeNode(cur); |
3034 | | /* |
3035 | | * if it's the only child, nothing more to be done. |
3036 | | */ |
3037 | 0 | if (next == NULL) |
3038 | 0 | return(prev); |
3039 | 0 | cur = next; |
3040 | 0 | } |
3041 | | |
3042 | 0 | prev->next = cur; |
3043 | 0 | cur->prev = prev; |
3044 | 0 | } |
3045 | 0 | while (cur->next != NULL) { |
3046 | 0 | cur->parent = parent; |
3047 | 0 | cur = cur->next; |
3048 | 0 | } |
3049 | 0 | cur->parent = parent; |
3050 | 0 | parent->last = cur; |
3051 | |
|
3052 | 0 | return(cur); |
3053 | 0 | } |
3054 | | |
3055 | | /** |
3056 | | * Unlink `cur` and append it to the children of `parent`. |
3057 | | * |
3058 | | * If `cur` is a text node, it may be merged with an adjacent text |
3059 | | * node and freed. In this case the text node containing the merged |
3060 | | * content is returned. |
3061 | | * |
3062 | | * If `cur` is an attribute node, it is appended to the attributes of |
3063 | | * `parent`. If the attribute list contains an attribute with a name |
3064 | | * matching `cur`, the old attribute is destroyed. |
3065 | | * |
3066 | | * General notes: |
3067 | | * |
3068 | | * Move operations like #xmlAddChild can cause element or attribute |
3069 | | * nodes to reference namespaces that aren't declared in one of |
3070 | | * their ancestors. This can lead to use-after-free errors if the |
3071 | | * elements containing the declarations are freed later, especially |
3072 | | * when moving nodes from one document to another. You should |
3073 | | * consider calling #xmlReconciliateNs after a move operation to |
3074 | | * normalize namespaces. Another option is to call |
3075 | | * #xmlDOMWrapAdoptNode with the target parent before moving a node. |
3076 | | * |
3077 | | * For the most part, move operations don't check whether the |
3078 | | * resulting tree structure is valid. Users must make sure that |
3079 | | * parent nodes only receive children of valid types. Inserted |
3080 | | * child nodes must never be an ancestor of the parent node to |
3081 | | * avoid cycles in the tree structure. In general, only |
3082 | | * document, document fragments, elements and attributes |
3083 | | * should be used as parent nodes. |
3084 | | * |
3085 | | * When moving a node between documents and a memory allocation |
3086 | | * fails, the node's content will be corrupted and it will be |
3087 | | * unlinked. In this case, the node must be freed manually. |
3088 | | * |
3089 | | * Moving DTDs between documents isn't supported. |
3090 | | * |
3091 | | * @param parent the parent node |
3092 | | * @param cur the child node |
3093 | | * @returns `cur` or a sibling if `cur` was merged. Returns NULL |
3094 | | * if arguments are invalid or a memory allocation failed. |
3095 | | */ |
3096 | | xmlNode * |
3097 | 2.35M | xmlAddChild(xmlNode *parent, xmlNode *cur) { |
3098 | 2.35M | xmlNodePtr prev; |
3099 | | |
3100 | 2.35M | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL) || |
3101 | 2.35M | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
3102 | 2.35M | (parent == cur)) |
3103 | 570 | return(NULL); |
3104 | | |
3105 | | /* |
3106 | | * If parent is a text node, call xmlTextAddContent. This |
3107 | | * undocumented quirk should probably be removed. |
3108 | | */ |
3109 | 2.35M | if (parent->type == XML_TEXT_NODE) { |
3110 | 0 | if (xmlTextAddContent(parent, cur->content, -1) < 0) |
3111 | 0 | return(NULL); |
3112 | 0 | xmlFreeNode(cur); |
3113 | 0 | return(parent); |
3114 | 0 | } |
3115 | | |
3116 | 2.35M | if (cur->type == XML_ATTRIBUTE_NODE) { |
3117 | 0 | prev = (xmlNodePtr) parent->properties; |
3118 | 0 | if (prev != NULL) { |
3119 | 0 | while (prev->next != NULL) |
3120 | 0 | prev = prev->next; |
3121 | 0 | } |
3122 | 2.35M | } else { |
3123 | 2.35M | prev = parent->last; |
3124 | 2.35M | } |
3125 | | |
3126 | 2.35M | if (cur == prev) |
3127 | 0 | return(cur); |
3128 | | |
3129 | 2.35M | return(xmlInsertNode(parent->doc, cur, parent, prev, NULL, 1)); |
3130 | 2.35M | } |
3131 | | |
3132 | | /** |
3133 | | * Find the last child of a node. |
3134 | | * |
3135 | | * @param parent the parent node |
3136 | | * @returns the last child or NULL if parent has no children. |
3137 | | */ |
3138 | | xmlNode * |
3139 | 0 | xmlGetLastChild(const xmlNode *parent) { |
3140 | 0 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
3141 | 0 | return(NULL); |
3142 | 0 | } |
3143 | 0 | return(parent->last); |
3144 | 0 | } |
3145 | | |
3146 | | /* |
3147 | | * 5 interfaces from DOM ElementTraversal |
3148 | | */ |
3149 | | |
3150 | | /** |
3151 | | * Count the number of child nodes which are elements. |
3152 | | * |
3153 | | * Note that entity references are not expanded. |
3154 | | * |
3155 | | * @param parent the parent node |
3156 | | * @returns the number of element children or 0 if arguments are |
3157 | | * invalid. |
3158 | | */ |
3159 | | unsigned long |
3160 | 0 | xmlChildElementCount(xmlNode *parent) { |
3161 | 0 | unsigned long ret = 0; |
3162 | 0 | xmlNodePtr cur = NULL; |
3163 | |
|
3164 | 0 | if (parent == NULL) |
3165 | 0 | return(0); |
3166 | 0 | switch (parent->type) { |
3167 | 0 | case XML_ELEMENT_NODE: |
3168 | 0 | case XML_DOCUMENT_NODE: |
3169 | 0 | case XML_DOCUMENT_FRAG_NODE: |
3170 | 0 | case XML_HTML_DOCUMENT_NODE: |
3171 | 0 | case XML_ENTITY_DECL: |
3172 | 0 | cur = parent->children; |
3173 | 0 | break; |
3174 | 0 | default: |
3175 | 0 | return(0); |
3176 | 0 | } |
3177 | 0 | while (cur != NULL) { |
3178 | 0 | if (cur->type == XML_ELEMENT_NODE) |
3179 | 0 | ret++; |
3180 | 0 | cur = cur->next; |
3181 | 0 | } |
3182 | 0 | return(ret); |
3183 | 0 | } |
3184 | | |
3185 | | /** |
3186 | | * Find the first child node which is an element. |
3187 | | * |
3188 | | * Note that entity references are not expanded. |
3189 | | * |
3190 | | * @param parent the parent node |
3191 | | * @returns the first element or NULL if parent has no children. |
3192 | | */ |
3193 | | xmlNode * |
3194 | 0 | xmlFirstElementChild(xmlNode *parent) { |
3195 | 0 | xmlNodePtr cur = NULL; |
3196 | |
|
3197 | 0 | if (parent == NULL) |
3198 | 0 | return(NULL); |
3199 | 0 | switch (parent->type) { |
3200 | 0 | case XML_ELEMENT_NODE: |
3201 | 0 | case XML_DOCUMENT_NODE: |
3202 | 0 | case XML_DOCUMENT_FRAG_NODE: |
3203 | 0 | case XML_HTML_DOCUMENT_NODE: |
3204 | 0 | case XML_ENTITY_DECL: |
3205 | 0 | cur = parent->children; |
3206 | 0 | break; |
3207 | 0 | default: |
3208 | 0 | return(NULL); |
3209 | 0 | } |
3210 | 0 | while (cur != NULL) { |
3211 | 0 | if (cur->type == XML_ELEMENT_NODE) |
3212 | 0 | return(cur); |
3213 | 0 | cur = cur->next; |
3214 | 0 | } |
3215 | 0 | return(NULL); |
3216 | 0 | } |
3217 | | |
3218 | | /** |
3219 | | * Find the last child node which is an element. |
3220 | | * |
3221 | | * Note that entity references are not expanded. |
3222 | | * |
3223 | | * @param parent the parent node |
3224 | | * @returns the last element or NULL if parent has no children. |
3225 | | */ |
3226 | | xmlNode * |
3227 | 0 | xmlLastElementChild(xmlNode *parent) { |
3228 | 0 | xmlNodePtr cur = NULL; |
3229 | |
|
3230 | 0 | if (parent == NULL) |
3231 | 0 | return(NULL); |
3232 | 0 | switch (parent->type) { |
3233 | 0 | case XML_ELEMENT_NODE: |
3234 | 0 | case XML_DOCUMENT_NODE: |
3235 | 0 | case XML_DOCUMENT_FRAG_NODE: |
3236 | 0 | case XML_HTML_DOCUMENT_NODE: |
3237 | 0 | case XML_ENTITY_DECL: |
3238 | 0 | cur = parent->last; |
3239 | 0 | break; |
3240 | 0 | default: |
3241 | 0 | return(NULL); |
3242 | 0 | } |
3243 | 0 | while (cur != NULL) { |
3244 | 0 | if (cur->type == XML_ELEMENT_NODE) |
3245 | 0 | return(cur); |
3246 | 0 | cur = cur->prev; |
3247 | 0 | } |
3248 | 0 | return(NULL); |
3249 | 0 | } |
3250 | | |
3251 | | /** |
3252 | | * Find the closest preceding sibling which is a element. |
3253 | | * |
3254 | | * Note that entity references are not expanded. |
3255 | | * |
3256 | | * @param node the current node |
3257 | | * @returns the sibling or NULL if no sibling was found. |
3258 | | */ |
3259 | | xmlNode * |
3260 | 0 | xmlPreviousElementSibling(xmlNode *node) { |
3261 | 0 | if (node == NULL) |
3262 | 0 | return(NULL); |
3263 | 0 | switch (node->type) { |
3264 | 0 | case XML_ELEMENT_NODE: |
3265 | 0 | case XML_TEXT_NODE: |
3266 | 0 | case XML_CDATA_SECTION_NODE: |
3267 | 0 | case XML_ENTITY_REF_NODE: |
3268 | 0 | case XML_PI_NODE: |
3269 | 0 | case XML_COMMENT_NODE: |
3270 | 0 | case XML_XINCLUDE_START: |
3271 | 0 | case XML_XINCLUDE_END: |
3272 | 0 | node = node->prev; |
3273 | 0 | break; |
3274 | 0 | default: |
3275 | 0 | return(NULL); |
3276 | 0 | } |
3277 | 0 | while (node != NULL) { |
3278 | 0 | if (node->type == XML_ELEMENT_NODE) |
3279 | 0 | return(node); |
3280 | 0 | node = node->prev; |
3281 | 0 | } |
3282 | 0 | return(NULL); |
3283 | 0 | } |
3284 | | |
3285 | | /** |
3286 | | * Find the closest following sibling which is a element. |
3287 | | * |
3288 | | * Note that entity references are not expanded. |
3289 | | * |
3290 | | * @param node the current node |
3291 | | * @returns the sibling or NULL if no sibling was found. |
3292 | | */ |
3293 | | xmlNode * |
3294 | 0 | xmlNextElementSibling(xmlNode *node) { |
3295 | 0 | if (node == NULL) |
3296 | 0 | return(NULL); |
3297 | 0 | switch (node->type) { |
3298 | 0 | case XML_ELEMENT_NODE: |
3299 | 0 | case XML_TEXT_NODE: |
3300 | 0 | case XML_CDATA_SECTION_NODE: |
3301 | 0 | case XML_ENTITY_REF_NODE: |
3302 | 0 | case XML_PI_NODE: |
3303 | 0 | case XML_COMMENT_NODE: |
3304 | 0 | case XML_DTD_NODE: |
3305 | 0 | case XML_XINCLUDE_START: |
3306 | 0 | case XML_XINCLUDE_END: |
3307 | 0 | node = node->next; |
3308 | 0 | break; |
3309 | 0 | default: |
3310 | 0 | return(NULL); |
3311 | 0 | } |
3312 | 0 | while (node != NULL) { |
3313 | 0 | if (node->type == XML_ELEMENT_NODE) |
3314 | 0 | return(node); |
3315 | 0 | node = node->next; |
3316 | 0 | } |
3317 | 0 | return(NULL); |
3318 | 0 | } |
3319 | | |
3320 | | /** |
3321 | | * Free a node list including all children. |
3322 | | * |
3323 | | * @param cur the first node in the list |
3324 | | */ |
3325 | | void |
3326 | 5.50M | xmlFreeNodeList(xmlNode *cur) { |
3327 | 5.50M | xmlNodePtr next; |
3328 | 5.50M | xmlNodePtr parent; |
3329 | 5.50M | xmlDictPtr dict = NULL; |
3330 | 5.50M | size_t depth = 0; |
3331 | | |
3332 | 5.50M | if (cur == NULL) return; |
3333 | 5.49M | if (cur->type == XML_NAMESPACE_DECL) { |
3334 | 0 | xmlFreeNsList((xmlNsPtr) cur); |
3335 | 0 | return; |
3336 | 0 | } |
3337 | 5.49M | if (cur->doc != NULL) dict = cur->doc->dict; |
3338 | 17.5M | while (1) { |
3339 | 20.5M | while ((cur->children != NULL) && |
3340 | 20.5M | (cur->type != XML_DOCUMENT_NODE) && |
3341 | 20.5M | (cur->type != XML_HTML_DOCUMENT_NODE) && |
3342 | 20.5M | (cur->type != XML_DTD_NODE) && |
3343 | 20.5M | (cur->type != XML_ENTITY_REF_NODE)) { |
3344 | 3.02M | cur = cur->children; |
3345 | 3.02M | depth += 1; |
3346 | 3.02M | } |
3347 | | |
3348 | 17.5M | next = cur->next; |
3349 | 17.5M | parent = cur->parent; |
3350 | 17.5M | if ((cur->type == XML_DOCUMENT_NODE) || |
3351 | 17.5M | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
3352 | 0 | xmlFreeDoc((xmlDocPtr) cur); |
3353 | 17.5M | } else if (cur->type == XML_DTD_NODE) { |
3354 | | /* |
3355 | | * TODO: We should consider freeing the DTD if it isn't |
3356 | | * referenced from doc->intSubset or doc->extSubset. |
3357 | | */ |
3358 | 0 | cur->prev = NULL; |
3359 | 0 | cur->next = NULL; |
3360 | 17.5M | } else { |
3361 | 17.5M | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
3362 | 0 | xmlDeregisterNodeDefaultValue(cur); |
3363 | | |
3364 | 17.5M | if (((cur->type == XML_ELEMENT_NODE) || |
3365 | 17.5M | (cur->type == XML_XINCLUDE_START) || |
3366 | 17.5M | (cur->type == XML_XINCLUDE_END)) && |
3367 | 17.5M | (cur->properties != NULL)) |
3368 | 3.91M | xmlFreePropList(cur->properties); |
3369 | 17.5M | if ((cur->type != XML_ELEMENT_NODE) && |
3370 | 17.5M | (cur->type != XML_XINCLUDE_START) && |
3371 | 17.5M | (cur->type != XML_XINCLUDE_END) && |
3372 | 17.5M | (cur->type != XML_ENTITY_REF_NODE) && |
3373 | 17.5M | (cur->content != (xmlChar *) &(cur->properties))) { |
3374 | 8.67M | DICT_FREE(cur->content) |
3375 | 8.67M | } |
3376 | 17.5M | if (((cur->type == XML_ELEMENT_NODE) || |
3377 | 17.5M | (cur->type == XML_XINCLUDE_START) || |
3378 | 17.5M | (cur->type == XML_XINCLUDE_END)) && |
3379 | 17.5M | (cur->nsDef != NULL)) |
3380 | 2.08M | xmlFreeNsList(cur->nsDef); |
3381 | | |
3382 | | /* |
3383 | | * When a node is a text node or a comment, it uses a global static |
3384 | | * variable for the name of the node. |
3385 | | * Otherwise the node name might come from the document's |
3386 | | * dictionary |
3387 | | */ |
3388 | 17.5M | if ((cur->name != NULL) && |
3389 | 17.5M | (cur->type != XML_TEXT_NODE) && |
3390 | 17.5M | (cur->type != XML_COMMENT_NODE)) |
3391 | 8.90M | DICT_FREE(cur->name) |
3392 | 17.5M | xmlFree(cur); |
3393 | 17.5M | } |
3394 | | |
3395 | 17.5M | if (next != NULL) { |
3396 | 8.98M | cur = next; |
3397 | 8.98M | } else { |
3398 | 8.52M | if ((depth == 0) || (parent == NULL)) |
3399 | 5.49M | break; |
3400 | 3.02M | depth -= 1; |
3401 | 3.02M | cur = parent; |
3402 | 3.02M | cur->children = NULL; |
3403 | 3.02M | } |
3404 | 17.5M | } |
3405 | 5.49M | } |
3406 | | |
3407 | | /** |
3408 | | * Free a node including all the children. |
3409 | | * |
3410 | | * This doesn't unlink the node from the tree. Call #xmlUnlinkNode first |
3411 | | * unless `cur` is a root node. |
3412 | | * |
3413 | | * @param cur the node |
3414 | | */ |
3415 | | void |
3416 | 2.80M | xmlFreeNode(xmlNode *cur) { |
3417 | 2.80M | xmlDictPtr dict = NULL; |
3418 | | |
3419 | 2.80M | if (cur == NULL) return; |
3420 | | |
3421 | | /* use xmlFreeDtd for DTD nodes */ |
3422 | 2.80M | if (cur->type == XML_DTD_NODE) { |
3423 | 0 | xmlFreeDtd((xmlDtdPtr) cur); |
3424 | 0 | return; |
3425 | 0 | } |
3426 | 2.80M | if (cur->type == XML_NAMESPACE_DECL) { |
3427 | 0 | xmlFreeNs((xmlNsPtr) cur); |
3428 | 0 | return; |
3429 | 0 | } |
3430 | 2.80M | if (cur->type == XML_ATTRIBUTE_NODE) { |
3431 | 0 | xmlFreeProp((xmlAttrPtr) cur); |
3432 | 0 | return; |
3433 | 0 | } |
3434 | 2.80M | if (cur->type == XML_ENTITY_DECL) { |
3435 | 0 | xmlFreeEntity((xmlEntityPtr) cur); |
3436 | 0 | return; |
3437 | 0 | } |
3438 | | |
3439 | 2.80M | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
3440 | 0 | xmlDeregisterNodeDefaultValue(cur); |
3441 | | |
3442 | 2.80M | if (cur->doc != NULL) dict = cur->doc->dict; |
3443 | | |
3444 | 2.80M | if ((cur->children != NULL) && |
3445 | 2.80M | (cur->type != XML_ENTITY_REF_NODE)) |
3446 | 15.7k | xmlFreeNodeList(cur->children); |
3447 | | |
3448 | 2.80M | if ((cur->type == XML_ELEMENT_NODE) || |
3449 | 2.80M | (cur->type == XML_XINCLUDE_START) || |
3450 | 2.80M | (cur->type == XML_XINCLUDE_END)) { |
3451 | 42.4k | if (cur->properties != NULL) |
3452 | 2.51k | xmlFreePropList(cur->properties); |
3453 | 42.4k | if (cur->nsDef != NULL) |
3454 | 761 | xmlFreeNsList(cur->nsDef); |
3455 | 2.75M | } else if ((cur->content != NULL) && |
3456 | 2.75M | (cur->type != XML_ENTITY_REF_NODE) && |
3457 | 2.75M | (cur->content != (xmlChar *) &(cur->properties))) { |
3458 | 2.72M | DICT_FREE(cur->content) |
3459 | 2.72M | } |
3460 | | |
3461 | | /* |
3462 | | * When a node is a text node or a comment, it uses a global static |
3463 | | * variable for the name of the node. |
3464 | | * Otherwise the node name might come from the document's dictionary |
3465 | | */ |
3466 | 2.80M | if ((cur->name != NULL) && |
3467 | 2.80M | (cur->type != XML_TEXT_NODE) && |
3468 | 2.80M | (cur->type != XML_COMMENT_NODE)) |
3469 | 73.2k | DICT_FREE(cur->name) |
3470 | | |
3471 | 2.80M | xmlFree(cur); |
3472 | 2.80M | } |
3473 | | |
3474 | | /** |
3475 | | * Unlink a node from its tree. |
3476 | | * |
3477 | | * This function only unlinks the node from the tree. It doesn't |
3478 | | * clear references to DTD nodes. |
3479 | | * |
3480 | | * @param cur the node |
3481 | | */ |
3482 | | static void |
3483 | 3.01M | xmlUnlinkNodeInternal(xmlNodePtr cur) { |
3484 | 3.01M | if (cur->parent != NULL) { |
3485 | 2.96M | xmlNodePtr parent; |
3486 | 2.96M | parent = cur->parent; |
3487 | 2.96M | if (cur->type == XML_ATTRIBUTE_NODE) { |
3488 | 0 | if (parent->properties == (xmlAttrPtr) cur) |
3489 | 0 | parent->properties = ((xmlAttrPtr) cur)->next; |
3490 | 2.96M | } else { |
3491 | 2.96M | if (parent->children == cur) |
3492 | 833k | parent->children = cur->next; |
3493 | 2.96M | if (parent->last == cur) |
3494 | 749k | parent->last = cur->prev; |
3495 | 2.96M | } |
3496 | 2.96M | cur->parent = NULL; |
3497 | 2.96M | } |
3498 | | |
3499 | 3.01M | if (cur->next != NULL) |
3500 | 2.21M | cur->next->prev = cur->prev; |
3501 | 3.01M | if (cur->prev != NULL) |
3502 | 2.13M | cur->prev->next = cur->next; |
3503 | 3.01M | cur->next = NULL; |
3504 | 3.01M | cur->prev = NULL; |
3505 | 3.01M | } |
3506 | | |
3507 | | /** |
3508 | | * Unlink a node from its tree. |
3509 | | * |
3510 | | * The node is not freed. Unless it is reinserted, it must be managed |
3511 | | * manually and freed eventually by calling #xmlFreeNode. |
3512 | | * |
3513 | | * @param cur the node |
3514 | | */ |
3515 | | void |
3516 | 2.83M | xmlUnlinkNode(xmlNode *cur) { |
3517 | 2.83M | if (cur == NULL) |
3518 | 0 | return; |
3519 | | |
3520 | 2.83M | if (cur->type == XML_NAMESPACE_DECL) |
3521 | 0 | return; |
3522 | | |
3523 | 2.83M | if (cur->type == XML_DTD_NODE) { |
3524 | 388 | xmlDocPtr doc = cur->doc; |
3525 | | |
3526 | 388 | if (doc != NULL) { |
3527 | 388 | if (doc->intSubset == (xmlDtdPtr) cur) |
3528 | 388 | doc->intSubset = NULL; |
3529 | 388 | if (doc->extSubset == (xmlDtdPtr) cur) |
3530 | 0 | doc->extSubset = NULL; |
3531 | 388 | } |
3532 | 388 | } |
3533 | | |
3534 | 2.83M | if (cur->type == XML_ENTITY_DECL) |
3535 | 0 | xmlRemoveEntity((xmlEntityPtr) cur); |
3536 | | |
3537 | 2.83M | xmlUnlinkNodeInternal(cur); |
3538 | 2.83M | } |
3539 | | |
3540 | | /** |
3541 | | * Unlink the old node. If `cur` is provided, it is unlinked and |
3542 | | * inserted in place of `old`. |
3543 | | * |
3544 | | * It is an error if `old` has no parent. |
3545 | | * |
3546 | | * Unlike #xmlAddChild, this function doesn't merge text nodes or |
3547 | | * delete duplicate attributes. |
3548 | | * |
3549 | | * See the notes in #xmlAddChild. |
3550 | | * |
3551 | | * @param old the old node |
3552 | | * @param cur the node (optional) |
3553 | | * @returns `old` or NULL if arguments are invalid or a memory |
3554 | | * allocation failed. |
3555 | | */ |
3556 | | xmlNode * |
3557 | 0 | xmlReplaceNode(xmlNode *old, xmlNode *cur) { |
3558 | 0 | if (old == cur) return(NULL); |
3559 | 0 | if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) || |
3560 | 0 | (old->parent == NULL)) { |
3561 | 0 | return(NULL); |
3562 | 0 | } |
3563 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
3564 | | /* Don't call xmlUnlinkNodeInternal to handle DTDs. */ |
3565 | 0 | xmlUnlinkNode(old); |
3566 | 0 | return(old); |
3567 | 0 | } |
3568 | 0 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
3569 | 0 | return(old); |
3570 | 0 | } |
3571 | 0 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
3572 | 0 | return(old); |
3573 | 0 | } |
3574 | 0 | xmlUnlinkNodeInternal(cur); |
3575 | 0 | if (xmlSetTreeDoc(cur, old->doc) < 0) |
3576 | 0 | return(NULL); |
3577 | 0 | cur->parent = old->parent; |
3578 | 0 | cur->next = old->next; |
3579 | 0 | if (cur->next != NULL) |
3580 | 0 | cur->next->prev = cur; |
3581 | 0 | cur->prev = old->prev; |
3582 | 0 | if (cur->prev != NULL) |
3583 | 0 | cur->prev->next = cur; |
3584 | 0 | if (cur->parent != NULL) { |
3585 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) { |
3586 | 0 | if (cur->parent->properties == (xmlAttrPtr)old) |
3587 | 0 | cur->parent->properties = ((xmlAttrPtr) cur); |
3588 | 0 | } else { |
3589 | 0 | if (cur->parent->children == old) |
3590 | 0 | cur->parent->children = cur; |
3591 | 0 | if (cur->parent->last == old) |
3592 | 0 | cur->parent->last = cur; |
3593 | 0 | } |
3594 | 0 | } |
3595 | 0 | old->next = old->prev = NULL; |
3596 | 0 | old->parent = NULL; |
3597 | 0 | return(old); |
3598 | 0 | } |
3599 | | |
3600 | | /************************************************************************ |
3601 | | * * |
3602 | | * Copy operations * |
3603 | | * * |
3604 | | ************************************************************************/ |
3605 | | |
3606 | | /** |
3607 | | * Copy a namespace. |
3608 | | * |
3609 | | * @param cur the namespace |
3610 | | * @returns the copied namespace or NULL if a memory allocation |
3611 | | * failed. |
3612 | | */ |
3613 | | xmlNs * |
3614 | 2.04k | xmlCopyNamespace(xmlNs *cur) { |
3615 | 2.04k | xmlNsPtr ret; |
3616 | | |
3617 | 2.04k | if (cur == NULL) return(NULL); |
3618 | 2.04k | switch (cur->type) { |
3619 | 2.04k | case XML_LOCAL_NAMESPACE: |
3620 | 2.04k | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
3621 | 2.04k | break; |
3622 | 0 | default: |
3623 | 0 | return(NULL); |
3624 | 2.04k | } |
3625 | 2.04k | return(ret); |
3626 | 2.04k | } |
3627 | | |
3628 | | /** |
3629 | | * Copy a namespace list. |
3630 | | * |
3631 | | * @param cur the first namespace |
3632 | | * @returns the head of the copied list or NULL if a memory |
3633 | | * allocation failed. |
3634 | | */ |
3635 | | xmlNs * |
3636 | 1.41k | xmlCopyNamespaceList(xmlNs *cur) { |
3637 | 1.41k | xmlNsPtr ret = NULL; |
3638 | 1.41k | xmlNsPtr p = NULL,q; |
3639 | | |
3640 | 3.45k | while (cur != NULL) { |
3641 | 2.04k | q = xmlCopyNamespace(cur); |
3642 | 2.04k | if (q == NULL) { |
3643 | 7 | xmlFreeNsList(ret); |
3644 | 7 | return(NULL); |
3645 | 7 | } |
3646 | 2.04k | if (p == NULL) { |
3647 | 1.40k | ret = p = q; |
3648 | 1.40k | } else { |
3649 | 632 | p->next = q; |
3650 | 632 | p = q; |
3651 | 632 | } |
3652 | 2.04k | cur = cur->next; |
3653 | 2.04k | } |
3654 | 1.40k | return(ret); |
3655 | 1.41k | } |
3656 | | |
3657 | | static xmlAttrPtr |
3658 | 12.5k | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
3659 | 12.5k | xmlAttrPtr ret = NULL; |
3660 | | |
3661 | 12.5k | if (cur == NULL) return(NULL); |
3662 | 12.5k | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
3663 | 0 | return(NULL); |
3664 | 12.5k | if (target != NULL) |
3665 | 12.5k | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
3666 | 0 | else if (doc != NULL) |
3667 | 0 | ret = xmlNewDocProp(doc, cur->name, NULL); |
3668 | 0 | else if (cur->parent != NULL) |
3669 | 0 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
3670 | 0 | else if (cur->children != NULL) |
3671 | 0 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
3672 | 0 | else |
3673 | 0 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
3674 | 12.5k | if (ret == NULL) return(NULL); |
3675 | 12.4k | ret->parent = target; |
3676 | | |
3677 | 12.4k | if ((cur->ns != NULL) && (target != NULL)) { |
3678 | 414 | xmlNsPtr ns; |
3679 | 414 | int res; |
3680 | | |
3681 | 414 | res = xmlSearchNsSafe(target, cur->ns->prefix, &ns); |
3682 | 414 | if (res < 0) |
3683 | 0 | goto error; |
3684 | 414 | if (ns == NULL) { |
3685 | | /* |
3686 | | * Humm, we are copying an element whose namespace is defined |
3687 | | * out of the new tree scope. Search it in the original tree |
3688 | | * and add it at the top of the new tree |
3689 | | */ |
3690 | 0 | res = xmlSearchNsSafe(cur->parent, cur->ns->prefix, &ns); |
3691 | 0 | if (res < 0) |
3692 | 0 | goto error; |
3693 | 0 | if (ns != NULL) { |
3694 | 0 | xmlNodePtr root = target; |
3695 | 0 | xmlNodePtr pred = NULL; |
3696 | |
|
3697 | 0 | while (root->parent != NULL) { |
3698 | 0 | pred = root; |
3699 | 0 | root = root->parent; |
3700 | 0 | } |
3701 | 0 | if (root == (xmlNodePtr) target->doc) { |
3702 | | /* correct possibly cycling above the document elt */ |
3703 | 0 | root = pred; |
3704 | 0 | } |
3705 | 0 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
3706 | 0 | if (ret->ns == NULL) |
3707 | 0 | goto error; |
3708 | 0 | } |
3709 | 414 | } else { |
3710 | | /* |
3711 | | * we have to find something appropriate here since |
3712 | | * we can't be sure, that the namespace we found is identified |
3713 | | * by the prefix |
3714 | | */ |
3715 | 414 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
3716 | | /* this is the nice case */ |
3717 | 414 | ret->ns = ns; |
3718 | 414 | } else { |
3719 | | /* |
3720 | | * we are in trouble: we need a new reconciled namespace. |
3721 | | * This is expensive |
3722 | | */ |
3723 | 0 | ret->ns = xmlNewReconciledNs(target, cur->ns); |
3724 | 0 | if (ret->ns == NULL) |
3725 | 0 | goto error; |
3726 | 0 | } |
3727 | 414 | } |
3728 | | |
3729 | 414 | } else |
3730 | 12.0k | ret->ns = NULL; |
3731 | | |
3732 | 12.4k | if (cur->children != NULL) { |
3733 | 12.4k | xmlNodePtr tmp; |
3734 | | |
3735 | 12.4k | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
3736 | 12.4k | if (ret->children == NULL) |
3737 | 4 | goto error; |
3738 | 12.4k | ret->last = NULL; |
3739 | 12.4k | tmp = ret->children; |
3740 | 24.9k | while (tmp != NULL) { |
3741 | | /* tmp->parent = (xmlNodePtr)ret; */ |
3742 | 12.4k | if (tmp->next == NULL) |
3743 | 12.4k | ret->last = tmp; |
3744 | 12.4k | tmp = tmp->next; |
3745 | 12.4k | } |
3746 | 12.4k | } |
3747 | | /* |
3748 | | * Try to handle IDs |
3749 | | */ |
3750 | 12.4k | if ((target != NULL) && (cur != NULL) && |
3751 | 12.4k | (target->doc != NULL) && (cur->doc != NULL) && |
3752 | 12.4k | (cur->parent != NULL) && |
3753 | 12.4k | (cur->children != NULL)) { |
3754 | 12.4k | int res = xmlIsID(cur->doc, cur->parent, cur); |
3755 | | |
3756 | 12.4k | if (res < 0) |
3757 | 2 | goto error; |
3758 | 12.4k | if (res != 0) { |
3759 | 226 | xmlChar *id; |
3760 | | |
3761 | 226 | id = xmlNodeGetContent((xmlNodePtr) cur); |
3762 | 226 | if (id == NULL) |
3763 | 1 | goto error; |
3764 | 225 | res = xmlAddIDSafe(ret, id); |
3765 | 225 | xmlFree(id); |
3766 | 225 | if (res < 0) |
3767 | 1 | goto error; |
3768 | 225 | } |
3769 | 12.4k | } |
3770 | 12.4k | return(ret); |
3771 | | |
3772 | 8 | error: |
3773 | 8 | xmlFreeProp(ret); |
3774 | 8 | return(NULL); |
3775 | 12.4k | } |
3776 | | |
3777 | | /** |
3778 | | * Create a copy of the attribute. This function sets the parent |
3779 | | * pointer of the copy to `target` but doesn't set the attribute on |
3780 | | * the target element. Users should consider to set the attribute |
3781 | | * by calling #xmlAddChild afterwards or reset the parent pointer to |
3782 | | * NULL. |
3783 | | * |
3784 | | * @param target the element where the attribute will be grafted |
3785 | | * @param cur the attribute |
3786 | | * @returns the copied attribute or NULL if a memory allocation |
3787 | | * failed. |
3788 | | */ |
3789 | | xmlAttr * |
3790 | 12.5k | xmlCopyProp(xmlNode *target, xmlAttr *cur) { |
3791 | 12.5k | return xmlCopyPropInternal(NULL, target, cur); |
3792 | 12.5k | } |
3793 | | |
3794 | | /** |
3795 | | * Create a copy of an attribute list. This function sets the |
3796 | | * parent pointers of the copied attributes to `target` but doesn't |
3797 | | * set the attributes on the target element. |
3798 | | * |
3799 | | * @param target the element where the attributes will be grafted |
3800 | | * @param cur the first attribute |
3801 | | * @returns the head of the copied list or NULL if a memory |
3802 | | * allocation failed. |
3803 | | */ |
3804 | | xmlAttr * |
3805 | 9.99k | xmlCopyPropList(xmlNode *target, xmlAttr *cur) { |
3806 | 9.99k | xmlAttrPtr ret = NULL; |
3807 | 9.99k | xmlAttrPtr p = NULL,q; |
3808 | | |
3809 | 9.99k | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
3810 | 0 | return(NULL); |
3811 | 22.4k | while (cur != NULL) { |
3812 | 12.5k | q = xmlCopyProp(target, cur); |
3813 | 12.5k | if (q == NULL) { |
3814 | 12 | xmlFreePropList(ret); |
3815 | 12 | return(NULL); |
3816 | 12 | } |
3817 | 12.4k | if (p == NULL) { |
3818 | 9.98k | ret = p = q; |
3819 | 9.98k | } else { |
3820 | 2.50k | p->next = q; |
3821 | 2.50k | q->prev = p; |
3822 | 2.50k | p = q; |
3823 | 2.50k | } |
3824 | 12.4k | cur = cur->next; |
3825 | 12.4k | } |
3826 | 9.98k | return(ret); |
3827 | 9.99k | } |
3828 | | |
3829 | | /* |
3830 | | * NOTE about the CopyNode operations ! |
3831 | | * |
3832 | | * They are split into external and internal parts for one |
3833 | | * tricky reason: namespaces. Doing a direct copy of a node |
3834 | | * say RPM:Copyright without changing the namespace pointer to |
3835 | | * something else can produce stale links. One way to do it is |
3836 | | * to keep a reference counter but this doesn't work as soon |
3837 | | * as one moves the element or the subtree out of the scope of |
3838 | | * the existing namespace. The actual solution seems to be to add |
3839 | | * a copy of the namespace at the top of the copied tree if |
3840 | | * not available in the subtree. |
3841 | | * Hence two functions, the public front-end call the inner ones |
3842 | | * The argument "recursive" normally indicates a recursive copy |
3843 | | * of the node with values 0 (no) and 1 (yes). For XInclude, |
3844 | | * however, we allow a value of 2 to indicate copy properties and |
3845 | | * namespace info, but don't recurse on children. |
3846 | | */ |
3847 | | |
3848 | | /** |
3849 | | * Copy a node. |
3850 | | * |
3851 | | * @param node source node |
3852 | | * @param doc target document |
3853 | | * @param parent target parent |
3854 | | * @param extended flags |
3855 | | * @returns the copy or NULL if a memory allocation failed. |
3856 | | */ |
3857 | | xmlNode * |
3858 | | xmlStaticCopyNode(xmlNode *node, xmlDoc *doc, xmlNode *parent, |
3859 | 1.24M | int extended) { |
3860 | 1.24M | xmlNodePtr ret; |
3861 | | |
3862 | 1.24M | if (node == NULL) return(NULL); |
3863 | 1.24M | switch (node->type) { |
3864 | 26.8k | case XML_TEXT_NODE: |
3865 | 26.8k | case XML_CDATA_SECTION_NODE: |
3866 | 1.22M | case XML_ELEMENT_NODE: |
3867 | 1.22M | case XML_DOCUMENT_FRAG_NODE: |
3868 | 1.22M | case XML_ENTITY_REF_NODE: |
3869 | 1.22M | case XML_PI_NODE: |
3870 | 1.24M | case XML_COMMENT_NODE: |
3871 | 1.24M | case XML_XINCLUDE_START: |
3872 | 1.24M | case XML_XINCLUDE_END: |
3873 | 1.24M | break; |
3874 | 0 | case XML_ATTRIBUTE_NODE: |
3875 | 0 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
3876 | 0 | case XML_NAMESPACE_DECL: |
3877 | 0 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
3878 | | |
3879 | 0 | case XML_DOCUMENT_NODE: |
3880 | 0 | case XML_HTML_DOCUMENT_NODE: |
3881 | 0 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
3882 | 0 | default: |
3883 | 0 | return(NULL); |
3884 | 1.24M | } |
3885 | | |
3886 | | /* |
3887 | | * Allocate a new node and fill the fields. |
3888 | | */ |
3889 | 1.24M | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
3890 | 1.24M | if (ret == NULL) |
3891 | 5.81k | return(NULL); |
3892 | 1.24M | memset(ret, 0, sizeof(xmlNode)); |
3893 | 1.24M | ret->type = node->type; |
3894 | | |
3895 | 1.24M | ret->doc = doc; |
3896 | 1.24M | ret->parent = parent; |
3897 | 1.24M | if (node->name == xmlStringText) |
3898 | 26.8k | ret->name = xmlStringText; |
3899 | 1.21M | else if (node->name == xmlStringTextNoenc) |
3900 | 0 | ret->name = xmlStringTextNoenc; |
3901 | 1.21M | else if (node->name == xmlStringComment) |
3902 | 19.5k | ret->name = xmlStringComment; |
3903 | 1.19M | else if (node->name != NULL) { |
3904 | 1.19M | if ((doc != NULL) && (doc->dict != NULL)) |
3905 | 1.19M | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
3906 | 0 | else |
3907 | 0 | ret->name = xmlStrdup(node->name); |
3908 | 1.19M | if (ret->name == NULL) |
3909 | 2 | goto error; |
3910 | 1.19M | } |
3911 | 1.24M | if ((node->type != XML_ELEMENT_NODE) && |
3912 | 1.24M | (node->content != NULL) && |
3913 | 1.24M | (node->type != XML_ENTITY_REF_NODE) && |
3914 | 1.24M | (node->type != XML_XINCLUDE_END) && |
3915 | 1.24M | (node->type != XML_XINCLUDE_START)) { |
3916 | 47.2k | ret->content = xmlStrdup(node->content); |
3917 | 47.2k | if (ret->content == NULL) |
3918 | 3 | goto error; |
3919 | 1.19M | }else{ |
3920 | 1.19M | if (node->type == XML_ELEMENT_NODE) |
3921 | 1.18M | ret->line = node->line; |
3922 | 1.19M | } |
3923 | | |
3924 | 1.24M | if (!extended) |
3925 | 1.20M | goto out; |
3926 | 41.3k | if (((node->type == XML_ELEMENT_NODE) || |
3927 | 41.3k | (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) { |
3928 | 1.41k | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
3929 | 1.41k | if (ret->nsDef == NULL) |
3930 | 7 | goto error; |
3931 | 1.41k | } |
3932 | | |
3933 | 41.3k | if ((node->type == XML_ELEMENT_NODE) && (node->ns != NULL)) { |
3934 | 8.52k | xmlNsPtr ns = NULL; |
3935 | 8.52k | int res; |
3936 | | |
3937 | 8.52k | res = xmlSearchNsSafe(ret, node->ns->prefix, &ns); |
3938 | 8.52k | if (res < 0) |
3939 | 0 | goto error; |
3940 | 8.52k | if (ns == NULL) { |
3941 | | /* |
3942 | | * Humm, we are copying an element whose namespace is defined |
3943 | | * out of the new tree scope. Search it in the original tree |
3944 | | * and add it at the top of the new tree. |
3945 | | * |
3946 | | * TODO: Searching the original tree seems unnecessary. We |
3947 | | * already have a namespace URI. |
3948 | | */ |
3949 | 0 | res = xmlSearchNsSafe(node, node->ns->prefix, &ns); |
3950 | 0 | if (res < 0) |
3951 | 0 | goto error; |
3952 | 0 | if (ns != NULL) { |
3953 | 0 | xmlNodePtr root = ret; |
3954 | |
|
3955 | 0 | while (root->parent != NULL) root = root->parent; |
3956 | 0 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
3957 | 0 | } else { |
3958 | 0 | ret->ns = xmlNewReconciledNs(ret, node->ns); |
3959 | 0 | } |
3960 | 0 | if (ret->ns == NULL) |
3961 | 0 | goto error; |
3962 | 8.52k | } else { |
3963 | | /* |
3964 | | * reference the existing namespace definition in our own tree. |
3965 | | */ |
3966 | 8.52k | ret->ns = ns; |
3967 | 8.52k | } |
3968 | 8.52k | } |
3969 | 41.3k | if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) { |
3970 | 9.99k | ret->properties = xmlCopyPropList(ret, node->properties); |
3971 | 9.99k | if (ret->properties == NULL) |
3972 | 12 | goto error; |
3973 | 9.99k | } |
3974 | 41.3k | if (node->type == XML_ENTITY_REF_NODE) { |
3975 | 0 | if ((doc == NULL) || (node->doc != doc)) { |
3976 | | /* |
3977 | | * The copied node will go into a separate document, so |
3978 | | * to avoid dangling references to the ENTITY_DECL node |
3979 | | * we cannot keep the reference. Try to find it in the |
3980 | | * target document. |
3981 | | */ |
3982 | 0 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
3983 | 0 | } else { |
3984 | 0 | ret->children = node->children; |
3985 | 0 | } |
3986 | 0 | ret->last = ret->children; |
3987 | 41.3k | } else if ((node->children != NULL) && (extended != 2)) { |
3988 | 1.61k | xmlNodePtr cur, insert; |
3989 | | |
3990 | 1.61k | cur = node->children; |
3991 | 1.61k | insert = ret; |
3992 | 22.6k | while (cur != NULL) { |
3993 | 21.0k | xmlNodePtr copy = xmlStaticCopyNode(cur, doc, insert, 2); |
3994 | 21.0k | if (copy == NULL) |
3995 | 10 | goto error; |
3996 | | |
3997 | | /* Check for coalesced text nodes */ |
3998 | 21.0k | if (insert->last != copy) { |
3999 | 21.0k | if (insert->last == NULL) { |
4000 | 3.99k | insert->children = copy; |
4001 | 17.0k | } else { |
4002 | 17.0k | copy->prev = insert->last; |
4003 | 17.0k | insert->last->next = copy; |
4004 | 17.0k | } |
4005 | 21.0k | insert->last = copy; |
4006 | 21.0k | } |
4007 | | |
4008 | 21.0k | if ((cur->type != XML_ENTITY_REF_NODE) && |
4009 | 21.0k | (cur->children != NULL)) { |
4010 | 2.38k | cur = cur->children; |
4011 | 2.38k | insert = copy; |
4012 | 2.38k | continue; |
4013 | 2.38k | } |
4014 | | |
4015 | 21.0k | while (1) { |
4016 | 21.0k | if (cur->next != NULL) { |
4017 | 17.0k | cur = cur->next; |
4018 | 17.0k | break; |
4019 | 17.0k | } |
4020 | | |
4021 | 3.98k | cur = cur->parent; |
4022 | 3.98k | insert = insert->parent; |
4023 | 3.98k | if (cur == node) { |
4024 | 1.60k | cur = NULL; |
4025 | 1.60k | break; |
4026 | 1.60k | } |
4027 | 3.98k | } |
4028 | 18.6k | } |
4029 | 1.61k | } |
4030 | | |
4031 | 1.24M | out: |
4032 | 1.24M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
4033 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
4034 | 1.24M | return(ret); |
4035 | | |
4036 | 34 | error: |
4037 | 34 | xmlFreeNode(ret); |
4038 | 34 | return(NULL); |
4039 | 41.3k | } |
4040 | | |
4041 | | /** |
4042 | | * Copy a node list. If `parent` is provided, sets the parent pointer |
4043 | | * of the copied nodes, but doesn't update the children and last |
4044 | | * pointer of `parent`. |
4045 | | * |
4046 | | * @param node node to copy |
4047 | | * @param doc target document |
4048 | | * @param parent target node (optional) |
4049 | | * @returns a the copy or NULL in case of error. |
4050 | | */ |
4051 | | xmlNode * |
4052 | 12.4k | xmlStaticCopyNodeList(xmlNode *node, xmlDoc *doc, xmlNode *parent) { |
4053 | 12.4k | xmlNodePtr ret = NULL; |
4054 | 12.4k | xmlNodePtr p = NULL,q; |
4055 | 12.4k | xmlDtdPtr newSubset = NULL; |
4056 | 12.4k | int linkedSubset = 0; |
4057 | | |
4058 | 24.9k | while (node != NULL) { |
4059 | 12.4k | xmlNodePtr next = node->next; |
4060 | | |
4061 | 12.4k | if (node->type == XML_DTD_NODE ) { |
4062 | 0 | if (doc == NULL) { |
4063 | 0 | node = next; |
4064 | 0 | continue; |
4065 | 0 | } |
4066 | 0 | if ((doc->intSubset == NULL) && (newSubset == NULL)) { |
4067 | 0 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
4068 | 0 | if (q == NULL) goto error; |
4069 | | /* Can't fail on DTD */ |
4070 | 0 | xmlSetTreeDoc(q, doc); |
4071 | 0 | q->parent = parent; |
4072 | 0 | newSubset = (xmlDtdPtr) q; |
4073 | 0 | } else { |
4074 | | /* |
4075 | | * We don't allow multiple internal subsets in a document, |
4076 | | * so we move the DTD instead of creating a copy. |
4077 | | */ |
4078 | 0 | linkedSubset = 1; |
4079 | 0 | q = (xmlNodePtr) doc->intSubset; |
4080 | | /* Unlink */ |
4081 | 0 | if (q->prev == NULL) { |
4082 | 0 | if (q->parent != NULL) |
4083 | 0 | q->parent->children = q->next; |
4084 | 0 | } else { |
4085 | 0 | q->prev->next = q->next; |
4086 | 0 | } |
4087 | 0 | if (q->next == NULL) { |
4088 | 0 | if (q->parent != NULL) |
4089 | 0 | q->parent->last = q->prev; |
4090 | 0 | } else { |
4091 | 0 | q->next->prev = q->prev; |
4092 | 0 | } |
4093 | 0 | q->parent = parent; |
4094 | 0 | q->next = NULL; |
4095 | 0 | q->prev = NULL; |
4096 | 0 | } |
4097 | 0 | } else |
4098 | 12.4k | q = xmlStaticCopyNode(node, doc, parent, 1); |
4099 | 12.4k | if (q == NULL) goto error; |
4100 | 12.4k | if (ret == NULL) { |
4101 | 12.4k | q->prev = NULL; |
4102 | 12.4k | ret = p = q; |
4103 | 12.4k | } else if (p != q) { |
4104 | | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
4105 | 0 | p->next = q; |
4106 | 0 | q->prev = p; |
4107 | 0 | p = q; |
4108 | 0 | } |
4109 | 12.4k | node = next; |
4110 | 12.4k | } |
4111 | 12.4k | if ((doc != NULL) && (newSubset != NULL)) |
4112 | 0 | doc->intSubset = newSubset; |
4113 | 12.4k | return(ret); |
4114 | 4 | error: |
4115 | 4 | xmlFreeNodeList(ret); |
4116 | 4 | if (newSubset != NULL) |
4117 | 0 | xmlFreeDtd(newSubset); |
4118 | 4 | if (linkedSubset != 0) { |
4119 | 0 | doc->intSubset->next = NULL; |
4120 | 0 | doc->intSubset->prev = NULL; |
4121 | 0 | } |
4122 | 4 | return(NULL); |
4123 | 12.4k | } |
4124 | | |
4125 | | /** |
4126 | | * Copy a node. |
4127 | | * |
4128 | | * If `extended` is 0, make a shallow copy. |
4129 | | * |
4130 | | * If `extended` is 1, make a deep copy (properties, namespaces |
4131 | | * and children when applicable). |
4132 | | * |
4133 | | * If `extended` is 2, make a shallow copy including properties and |
4134 | | * namespaces of elements. |
4135 | | * |
4136 | | * Use of this function is DISCOURAGED in favor of #xmlDocCopyNode. |
4137 | | * |
4138 | | * @param node the node |
4139 | | * @param extended mode of operation |
4140 | | * @returns the copied node or NULL if a memory allocation failed. |
4141 | | */ |
4142 | | xmlNode * |
4143 | 0 | xmlCopyNode(xmlNode *node, int extended) { |
4144 | 0 | xmlNodePtr ret; |
4145 | |
|
4146 | 0 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
4147 | 0 | return(ret); |
4148 | 0 | } |
4149 | | |
4150 | | /** |
4151 | | * Copy a node into another document. |
4152 | | * |
4153 | | * If `extended` is 0, make a shallow copy. |
4154 | | * |
4155 | | * If `extended` is 1, make a deep copy (properties, namespaces |
4156 | | * and children when applicable). |
4157 | | * |
4158 | | * If `extended` is 2, make a shallow copy including properties and |
4159 | | * namespaces of elements. |
4160 | | * |
4161 | | * @param node the node |
4162 | | * @param doc the document |
4163 | | * @param extended mode of operation |
4164 | | * @returns the copied node or NULL if a memory allocation failed. |
4165 | | */ |
4166 | | xmlNode * |
4167 | 1.21M | xmlDocCopyNode(xmlNode *node, xmlDoc *doc, int extended) { |
4168 | 1.21M | xmlNodePtr ret; |
4169 | | |
4170 | 1.21M | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
4171 | 1.21M | return(ret); |
4172 | 1.21M | } |
4173 | | |
4174 | | /** |
4175 | | * Copy a node list and all children into a new document. |
4176 | | * |
4177 | | * @param doc the target document |
4178 | | * @param node the first node in the list. |
4179 | | * @returns the head of the copied list or NULL if a memory |
4180 | | * allocation failed. |
4181 | | */ |
4182 | 0 | xmlNode *xmlDocCopyNodeList(xmlDoc *doc, xmlNode *node) { |
4183 | 0 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
4184 | 0 | return(ret); |
4185 | 0 | } |
4186 | | |
4187 | | /** |
4188 | | * Copy a node list and all children. |
4189 | | * |
4190 | | * Use of this function is DISCOURAGED in favor of #xmlDocCopyNodeList. |
4191 | | * |
4192 | | * @param node the first node in the list. |
4193 | | * @returns the head of the copied list or NULL if a memory |
4194 | | * allocation failed. |
4195 | | */ |
4196 | 0 | xmlNode *xmlCopyNodeList(xmlNode *node) { |
4197 | 0 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
4198 | 0 | return(ret); |
4199 | 0 | } |
4200 | | |
4201 | | /** |
4202 | | * Copy a DTD. |
4203 | | * |
4204 | | * @param dtd the DTD |
4205 | | * @returns the copied DTD or NULL if a memory allocation failed. |
4206 | | */ |
4207 | | xmlDtd * |
4208 | 0 | xmlCopyDtd(xmlDtd *dtd) { |
4209 | 0 | xmlDtdPtr ret; |
4210 | 0 | xmlNodePtr cur, p = NULL, q; |
4211 | |
|
4212 | 0 | if (dtd == NULL) return(NULL); |
4213 | 0 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
4214 | 0 | if (ret == NULL) return(NULL); |
4215 | 0 | if (dtd->entities != NULL) { |
4216 | 0 | ret->entities = (void *) xmlCopyEntitiesTable( |
4217 | 0 | (xmlEntitiesTablePtr) dtd->entities); |
4218 | 0 | if (ret->entities == NULL) |
4219 | 0 | goto error; |
4220 | 0 | } |
4221 | 0 | if (dtd->notations != NULL) { |
4222 | 0 | ret->notations = (void *) xmlCopyNotationTable( |
4223 | 0 | (xmlNotationTablePtr) dtd->notations); |
4224 | 0 | if (ret->notations == NULL) |
4225 | 0 | goto error; |
4226 | 0 | } |
4227 | 0 | if (dtd->elements != NULL) { |
4228 | 0 | ret->elements = (void *) xmlCopyElementTable( |
4229 | 0 | (xmlElementTablePtr) dtd->elements); |
4230 | 0 | if (ret->elements == NULL) |
4231 | 0 | goto error; |
4232 | 0 | } |
4233 | 0 | if (dtd->attributes != NULL) { |
4234 | 0 | ret->attributes = (void *) xmlCopyAttributeTable( |
4235 | 0 | (xmlAttributeTablePtr) dtd->attributes); |
4236 | 0 | if (ret->attributes == NULL) |
4237 | 0 | goto error; |
4238 | 0 | } |
4239 | 0 | if (dtd->pentities != NULL) { |
4240 | 0 | ret->pentities = (void *) xmlCopyEntitiesTable( |
4241 | 0 | (xmlEntitiesTablePtr) dtd->pentities); |
4242 | 0 | if (ret->pentities == NULL) |
4243 | 0 | goto error; |
4244 | 0 | } |
4245 | | |
4246 | 0 | cur = dtd->children; |
4247 | 0 | while (cur != NULL) { |
4248 | 0 | q = NULL; |
4249 | |
|
4250 | 0 | if (cur->type == XML_ENTITY_DECL) { |
4251 | 0 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
4252 | 0 | switch (tmp->etype) { |
4253 | 0 | case XML_INTERNAL_GENERAL_ENTITY: |
4254 | 0 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
4255 | 0 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
4256 | 0 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
4257 | 0 | break; |
4258 | 0 | case XML_INTERNAL_PARAMETER_ENTITY: |
4259 | 0 | case XML_EXTERNAL_PARAMETER_ENTITY: |
4260 | 0 | q = (xmlNodePtr) |
4261 | 0 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
4262 | 0 | break; |
4263 | 0 | case XML_INTERNAL_PREDEFINED_ENTITY: |
4264 | 0 | break; |
4265 | 0 | } |
4266 | 0 | } else if (cur->type == XML_ELEMENT_DECL) { |
4267 | 0 | xmlElementPtr tmp = (xmlElementPtr) cur; |
4268 | 0 | q = (xmlNodePtr) |
4269 | 0 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
4270 | 0 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
4271 | 0 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
4272 | 0 | q = (xmlNodePtr) |
4273 | 0 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
4274 | 0 | } else if (cur->type == XML_COMMENT_NODE) { |
4275 | 0 | q = xmlCopyNode(cur, 0); |
4276 | 0 | if (q == NULL) |
4277 | 0 | goto error; |
4278 | 0 | } |
4279 | | |
4280 | 0 | if (q == NULL) { |
4281 | 0 | cur = cur->next; |
4282 | 0 | continue; |
4283 | 0 | } |
4284 | | |
4285 | 0 | if (p == NULL) |
4286 | 0 | ret->children = q; |
4287 | 0 | else |
4288 | 0 | p->next = q; |
4289 | |
|
4290 | 0 | q->prev = p; |
4291 | 0 | q->parent = (xmlNodePtr) ret; |
4292 | 0 | q->next = NULL; |
4293 | 0 | ret->last = q; |
4294 | 0 | p = q; |
4295 | 0 | cur = cur->next; |
4296 | 0 | } |
4297 | | |
4298 | 0 | return(ret); |
4299 | | |
4300 | 0 | error: |
4301 | 0 | xmlFreeDtd(ret); |
4302 | 0 | return(NULL); |
4303 | 0 | } |
4304 | | |
4305 | | /** |
4306 | | * Copy a document. If recursive, the content tree will |
4307 | | * be copied too as well as DTD, namespaces and entities. |
4308 | | * |
4309 | | * @param doc the document |
4310 | | * @param recursive if not zero do a recursive copy. |
4311 | | * @returns the copied document or NULL if a memory allocation |
4312 | | * failed. |
4313 | | */ |
4314 | | xmlDoc * |
4315 | 0 | xmlCopyDoc(xmlDoc *doc, int recursive) { |
4316 | 0 | xmlDocPtr ret; |
4317 | |
|
4318 | 0 | if (doc == NULL) return(NULL); |
4319 | 0 | ret = xmlNewDoc(doc->version); |
4320 | 0 | if (ret == NULL) return(NULL); |
4321 | 0 | ret->type = doc->type; |
4322 | 0 | if (doc->name != NULL) { |
4323 | 0 | ret->name = xmlMemStrdup(doc->name); |
4324 | 0 | if (ret->name == NULL) |
4325 | 0 | goto error; |
4326 | 0 | } |
4327 | 0 | if (doc->encoding != NULL) { |
4328 | 0 | ret->encoding = xmlStrdup(doc->encoding); |
4329 | 0 | if (ret->encoding == NULL) |
4330 | 0 | goto error; |
4331 | 0 | } |
4332 | 0 | if (doc->URL != NULL) { |
4333 | 0 | ret->URL = xmlStrdup(doc->URL); |
4334 | 0 | if (ret->URL == NULL) |
4335 | 0 | goto error; |
4336 | 0 | } |
4337 | 0 | ret->charset = doc->charset; |
4338 | 0 | ret->compression = doc->compression; |
4339 | 0 | ret->standalone = doc->standalone; |
4340 | 0 | if (!recursive) return(ret); |
4341 | | |
4342 | 0 | ret->last = NULL; |
4343 | 0 | ret->children = NULL; |
4344 | 0 | if (doc->intSubset != NULL) { |
4345 | 0 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
4346 | 0 | if (ret->intSubset == NULL) |
4347 | 0 | goto error; |
4348 | | /* Can't fail on DTD */ |
4349 | 0 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
4350 | 0 | } |
4351 | 0 | if (doc->oldNs != NULL) { |
4352 | 0 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
4353 | 0 | if (ret->oldNs == NULL) |
4354 | 0 | goto error; |
4355 | 0 | } |
4356 | 0 | if (doc->children != NULL) { |
4357 | 0 | xmlNodePtr tmp; |
4358 | |
|
4359 | 0 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
4360 | 0 | (xmlNodePtr)ret); |
4361 | 0 | if (ret->children == NULL) |
4362 | 0 | goto error; |
4363 | 0 | ret->last = NULL; |
4364 | 0 | tmp = ret->children; |
4365 | 0 | while (tmp != NULL) { |
4366 | 0 | if (tmp->next == NULL) |
4367 | 0 | ret->last = tmp; |
4368 | 0 | tmp = tmp->next; |
4369 | 0 | } |
4370 | 0 | } |
4371 | 0 | return(ret); |
4372 | | |
4373 | 0 | error: |
4374 | 0 | xmlFreeDoc(ret); |
4375 | 0 | return(NULL); |
4376 | 0 | } |
4377 | | |
4378 | | /************************************************************************ |
4379 | | * * |
4380 | | * Content access functions * |
4381 | | * * |
4382 | | ************************************************************************/ |
4383 | | |
4384 | | /** |
4385 | | * Get line number of `node`. Try to work around the limitation of |
4386 | | * line numbers being stored as 16 bits ints. Requires xmlParserOption |
4387 | | * XML_PARSE_BIG_LINES to be set when parsing. |
4388 | | * |
4389 | | * @param node valid node |
4390 | | * @param depth used to limit any risk of recursion |
4391 | | * @returns the line number if successful, -1 otherwise |
4392 | | */ |
4393 | | static long |
4394 | | xmlGetLineNoInternal(const xmlNode *node, int depth) |
4395 | 8.28M | { |
4396 | 8.28M | long result = -1; |
4397 | | |
4398 | 8.28M | if (depth >= 5) |
4399 | 12.6k | return(-1); |
4400 | | |
4401 | 8.27M | if (!node) |
4402 | 0 | return result; |
4403 | 8.27M | if ((node->type == XML_ELEMENT_NODE) || |
4404 | 8.27M | (node->type == XML_TEXT_NODE) || |
4405 | 8.27M | (node->type == XML_COMMENT_NODE) || |
4406 | 8.27M | (node->type == XML_PI_NODE)) { |
4407 | 8.27M | if (node->line == 65535) { |
4408 | 80.6k | if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL)) |
4409 | 0 | result = XML_PTR_TO_INT(node->psvi); |
4410 | 80.6k | else if ((node->type == XML_ELEMENT_NODE) && |
4411 | 80.6k | (node->children != NULL)) |
4412 | 10.6k | result = xmlGetLineNoInternal(node->children, depth + 1); |
4413 | 69.9k | else if (node->next != NULL) |
4414 | 58.1k | result = xmlGetLineNoInternal(node->next, depth + 1); |
4415 | 11.8k | else if (node->prev != NULL) |
4416 | 6.16k | result = xmlGetLineNoInternal(node->prev, depth + 1); |
4417 | 80.6k | } |
4418 | 8.27M | if ((result == -1) || (result == 65535)) |
4419 | 8.27M | result = node->line; |
4420 | 8.27M | } else if ((node->prev != NULL) && |
4421 | 3.36k | ((node->prev->type == XML_ELEMENT_NODE) || |
4422 | 2.59k | (node->prev->type == XML_TEXT_NODE) || |
4423 | 2.59k | (node->prev->type == XML_COMMENT_NODE) || |
4424 | 2.59k | (node->prev->type == XML_PI_NODE))) |
4425 | 1.02k | result = xmlGetLineNoInternal(node->prev, depth + 1); |
4426 | 2.34k | else if ((node->parent != NULL) && |
4427 | 2.34k | (node->parent->type == XML_ELEMENT_NODE)) |
4428 | 2.03k | result = xmlGetLineNoInternal(node->parent, depth + 1); |
4429 | | |
4430 | 8.27M | return result; |
4431 | 8.27M | } |
4432 | | |
4433 | | /** |
4434 | | * Get line number of `node`. Try to work around the limitation of |
4435 | | * line numbers being stored as 16 bits ints. Requires xmlParserOption |
4436 | | * XML_PARSE_BIG_LINES to be set when parsing. |
4437 | | * |
4438 | | * @param node valid node |
4439 | | * @returns the line number if successful, -1 otherwise |
4440 | | */ |
4441 | | long |
4442 | | xmlGetLineNo(const xmlNode *node) |
4443 | 8.20M | { |
4444 | 8.20M | return(xmlGetLineNoInternal(node, 0)); |
4445 | 8.20M | } |
4446 | | |
4447 | | /** |
4448 | | * Build a structure based Path for the given node |
4449 | | * |
4450 | | * @param node a node |
4451 | | * @returns the new path or NULL in case of error. The caller must free |
4452 | | * the returned string |
4453 | | */ |
4454 | | xmlChar * |
4455 | | xmlGetNodePath(const xmlNode *node) |
4456 | 0 | { |
4457 | 0 | const xmlNode *cur, *tmp; |
4458 | 0 | const xmlNode **nodes = NULL; |
4459 | 0 | xmlChar *ret = NULL; |
4460 | 0 | xmlBuf *buf; |
4461 | 0 | size_t numNodes, i; |
4462 | |
|
4463 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
4464 | 0 | return(NULL); |
4465 | | |
4466 | 0 | buf = xmlBufCreate(50); |
4467 | 0 | if (buf == NULL) |
4468 | 0 | return(NULL); |
4469 | | |
4470 | | /* |
4471 | | * Get list of ancestors |
4472 | | */ |
4473 | 0 | numNodes = 0; |
4474 | 0 | for (cur = node; cur != NULL; cur = cur->parent) |
4475 | 0 | numNodes += 1; |
4476 | 0 | if (numNodes > SIZE_MAX / sizeof(nodes[0])) |
4477 | 0 | goto error; |
4478 | 0 | nodes = xmlMalloc(numNodes * sizeof(nodes[0])); |
4479 | 0 | if (nodes == NULL) |
4480 | 0 | goto error; |
4481 | 0 | i = 0; |
4482 | 0 | for (cur = node; cur != NULL && i < numNodes; cur = cur->parent) |
4483 | 0 | nodes[i++] = cur; |
4484 | | |
4485 | | /* |
4486 | | * Iterate in reverse to start at root |
4487 | | */ |
4488 | 0 | while (i > 0) { |
4489 | 0 | int occur = 0; |
4490 | |
|
4491 | 0 | i -= 1; |
4492 | 0 | cur = nodes[i]; |
4493 | |
|
4494 | 0 | if ((cur->type == XML_DOCUMENT_NODE) || |
4495 | 0 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
4496 | 0 | if (i == 0) |
4497 | 0 | xmlBufCat(buf, BAD_CAST "/"); |
4498 | 0 | } else if (cur->type == XML_ELEMENT_NODE) { |
4499 | 0 | int generic = 0; |
4500 | |
|
4501 | 0 | xmlBufCat(buf, BAD_CAST "/"); |
4502 | |
|
4503 | 0 | if (cur->ns) { |
4504 | 0 | if (cur->ns->prefix != NULL) { |
4505 | 0 | xmlBufCat(buf, cur->ns->prefix); |
4506 | 0 | xmlBufCat(buf, BAD_CAST ":"); |
4507 | 0 | xmlBufCat(buf, cur->name); |
4508 | 0 | } else { |
4509 | | /* |
4510 | | * We cannot express named elements in the default |
4511 | | * namespace, so use "*". |
4512 | | */ |
4513 | 0 | generic = 1; |
4514 | 0 | xmlBufCat(buf, BAD_CAST "*"); |
4515 | 0 | } |
4516 | 0 | } else { |
4517 | 0 | xmlBufCat(buf, cur->name); |
4518 | 0 | } |
4519 | | |
4520 | | /* |
4521 | | * Thumbler index computation |
4522 | | * TODO: the occurrence test seems bogus for namespaced names |
4523 | | */ |
4524 | 0 | tmp = cur->prev; |
4525 | 0 | while (tmp != NULL) { |
4526 | 0 | if ((tmp->type == XML_ELEMENT_NODE) && |
4527 | 0 | (generic || |
4528 | 0 | (xmlStrEqual(cur->name, tmp->name) && |
4529 | 0 | ((tmp->ns == cur->ns) || |
4530 | 0 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
4531 | 0 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
4532 | 0 | occur++; |
4533 | 0 | tmp = tmp->prev; |
4534 | 0 | } |
4535 | 0 | if (occur == 0) { |
4536 | 0 | tmp = cur->next; |
4537 | 0 | while (tmp != NULL && occur == 0) { |
4538 | 0 | if ((tmp->type == XML_ELEMENT_NODE) && |
4539 | 0 | (generic || |
4540 | 0 | (xmlStrEqual(cur->name, tmp->name) && |
4541 | 0 | ((tmp->ns == cur->ns) || |
4542 | 0 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
4543 | 0 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
4544 | 0 | occur++; |
4545 | 0 | tmp = tmp->next; |
4546 | 0 | } |
4547 | 0 | if (occur != 0) |
4548 | 0 | occur = 1; |
4549 | 0 | } else |
4550 | 0 | occur++; |
4551 | 0 | } else if (cur->type == XML_COMMENT_NODE) { |
4552 | 0 | xmlBufCat(buf, BAD_CAST "/comment()"); |
4553 | | |
4554 | | /* |
4555 | | * Thumbler index computation |
4556 | | */ |
4557 | 0 | tmp = cur->prev; |
4558 | 0 | while (tmp != NULL) { |
4559 | 0 | if (tmp->type == XML_COMMENT_NODE) |
4560 | 0 | occur++; |
4561 | 0 | tmp = tmp->prev; |
4562 | 0 | } |
4563 | 0 | if (occur == 0) { |
4564 | 0 | tmp = cur->next; |
4565 | 0 | while (tmp != NULL && occur == 0) { |
4566 | 0 | if (tmp->type == XML_COMMENT_NODE) |
4567 | 0 | occur++; |
4568 | 0 | tmp = tmp->next; |
4569 | 0 | } |
4570 | 0 | if (occur != 0) |
4571 | 0 | occur = 1; |
4572 | 0 | } else |
4573 | 0 | occur++; |
4574 | 0 | } else if ((cur->type == XML_TEXT_NODE) || |
4575 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
4576 | 0 | xmlBufCat(buf, BAD_CAST "/text()"); |
4577 | | |
4578 | | /* |
4579 | | * Thumbler index computation |
4580 | | */ |
4581 | 0 | tmp = cur->prev; |
4582 | 0 | while (tmp != NULL) { |
4583 | 0 | if ((tmp->type == XML_TEXT_NODE) || |
4584 | 0 | (tmp->type == XML_CDATA_SECTION_NODE)) |
4585 | 0 | occur++; |
4586 | 0 | tmp = tmp->prev; |
4587 | 0 | } |
4588 | | /* |
4589 | | * Evaluate if this is the only text- or CDATA-section-node; |
4590 | | * if yes, then we'll get "text()", otherwise "text()[1]". |
4591 | | */ |
4592 | 0 | if (occur == 0) { |
4593 | 0 | tmp = cur->next; |
4594 | 0 | while (tmp != NULL) { |
4595 | 0 | if ((tmp->type == XML_TEXT_NODE) || |
4596 | 0 | (tmp->type == XML_CDATA_SECTION_NODE)) |
4597 | 0 | { |
4598 | 0 | occur = 1; |
4599 | 0 | break; |
4600 | 0 | } |
4601 | 0 | tmp = tmp->next; |
4602 | 0 | } |
4603 | 0 | } else |
4604 | 0 | occur++; |
4605 | 0 | } else if (cur->type == XML_PI_NODE) { |
4606 | 0 | xmlBufCat(buf, BAD_CAST "/processing-instruction('"); |
4607 | 0 | xmlBufCat(buf, cur->name); |
4608 | 0 | xmlBufCat(buf, BAD_CAST "')"); |
4609 | | |
4610 | | /* |
4611 | | * Thumbler index computation |
4612 | | */ |
4613 | 0 | tmp = cur->prev; |
4614 | 0 | while (tmp != NULL) { |
4615 | 0 | if ((tmp->type == XML_PI_NODE) && |
4616 | 0 | (xmlStrEqual(cur->name, tmp->name))) |
4617 | 0 | occur++; |
4618 | 0 | tmp = tmp->prev; |
4619 | 0 | } |
4620 | 0 | if (occur == 0) { |
4621 | 0 | tmp = cur->next; |
4622 | 0 | while (tmp != NULL && occur == 0) { |
4623 | 0 | if ((tmp->type == XML_PI_NODE) && |
4624 | 0 | (xmlStrEqual(cur->name, tmp->name))) |
4625 | 0 | occur++; |
4626 | 0 | tmp = tmp->next; |
4627 | 0 | } |
4628 | 0 | if (occur != 0) |
4629 | 0 | occur = 1; |
4630 | 0 | } else |
4631 | 0 | occur++; |
4632 | |
|
4633 | 0 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
4634 | 0 | xmlBufCat(buf, BAD_CAST "/@"); |
4635 | 0 | if (cur->ns && cur->ns->prefix != NULL) { |
4636 | 0 | xmlBufCat(buf, cur->ns->prefix); |
4637 | 0 | xmlBufCat(buf, BAD_CAST ":"); |
4638 | 0 | } |
4639 | 0 | xmlBufCat(buf, cur->name); |
4640 | 0 | } else { |
4641 | 0 | goto error; |
4642 | 0 | } |
4643 | | |
4644 | 0 | if (occur > 0) { |
4645 | 0 | char tmpbuf[30]; |
4646 | |
|
4647 | 0 | snprintf(tmpbuf, sizeof(tmpbuf), "[%d]", occur); |
4648 | 0 | xmlBufCat(buf, BAD_CAST tmpbuf); |
4649 | 0 | } |
4650 | 0 | } |
4651 | | |
4652 | 0 | ret = xmlBufDetach(buf); |
4653 | |
|
4654 | 0 | error: |
4655 | 0 | xmlBufFree(buf); |
4656 | 0 | xmlFree(nodes); |
4657 | 0 | return(ret); |
4658 | 0 | } |
4659 | | |
4660 | | /** |
4661 | | * Get the root element of the document. |
4662 | | * |
4663 | | * Searches the document's children for the root element. The first |
4664 | | * child is not necessarily the root element, but could also be a |
4665 | | * DTD, comment or PI. |
4666 | | * |
4667 | | * @param doc the document |
4668 | | * @returns the root element or NULL if no element was found. |
4669 | | */ |
4670 | | xmlNode * |
4671 | 1.98M | xmlDocGetRootElement(const xmlDoc *doc) { |
4672 | 1.98M | xmlNodePtr ret; |
4673 | | |
4674 | 1.98M | if (doc == NULL) return(NULL); |
4675 | 1.43M | ret = doc->children; |
4676 | 1.56M | while (ret != NULL) { |
4677 | 1.53M | if (ret->type == XML_ELEMENT_NODE) |
4678 | 1.40M | return(ret); |
4679 | 123k | ret = ret->next; |
4680 | 123k | } |
4681 | 30.6k | return(ret); |
4682 | 1.43M | } |
4683 | | |
4684 | | /** |
4685 | | * If `root` is NULL no action is taken. To remove a node from a |
4686 | | * document, use #xmlUnlinkNode instead. |
4687 | | * |
4688 | | * Set the root element of the document (`doc->children` is a list |
4689 | | * containing possibly comments, PIs, etc ...). |
4690 | | * |
4691 | | * `root` must be an element node. It is unlinked before insertion. |
4692 | | * |
4693 | | * @param doc the document |
4694 | | * @param root the new document root element |
4695 | | * @returns the unlinked old root element or NULL if the document |
4696 | | * didn't have a root element or a memory allocation failed. |
4697 | | */ |
4698 | | xmlNode * |
4699 | 0 | xmlDocSetRootElement(xmlDoc *doc, xmlNode *root) { |
4700 | 0 | xmlNodePtr old = NULL; |
4701 | |
|
4702 | 0 | if (doc == NULL) return(NULL); |
4703 | 0 | if ((root == NULL) || (root->type == XML_NAMESPACE_DECL)) |
4704 | 0 | return(NULL); |
4705 | 0 | old = doc->children; |
4706 | 0 | while (old != NULL) { |
4707 | 0 | if (old->type == XML_ELEMENT_NODE) |
4708 | 0 | break; |
4709 | 0 | old = old->next; |
4710 | 0 | } |
4711 | 0 | if (old == root) |
4712 | 0 | return(old); |
4713 | 0 | xmlUnlinkNodeInternal(root); |
4714 | 0 | if (xmlSetTreeDoc(root, doc) < 0) |
4715 | 0 | return(NULL); |
4716 | 0 | root->parent = (xmlNodePtr) doc; |
4717 | 0 | if (old == NULL) { |
4718 | 0 | if (doc->children == NULL) { |
4719 | 0 | doc->children = root; |
4720 | 0 | doc->last = root; |
4721 | 0 | } else { |
4722 | 0 | xmlAddSibling(doc->children, root); |
4723 | 0 | } |
4724 | 0 | } else { |
4725 | 0 | xmlReplaceNode(old, root); |
4726 | 0 | } |
4727 | 0 | return(old); |
4728 | 0 | } |
4729 | | |
4730 | | /** |
4731 | | * Set the `xml:lang` attribute of a node. |
4732 | | * |
4733 | | * @param cur the node being changed |
4734 | | * @param lang the language description |
4735 | | * @returns 0 on success, 1 if arguments are invalid, -1 if a |
4736 | | * memory allocation failed. |
4737 | | */ |
4738 | | int |
4739 | 0 | xmlNodeSetLang(xmlNode *cur, const xmlChar *lang) { |
4740 | 0 | xmlNsPtr ns; |
4741 | 0 | xmlAttrPtr attr; |
4742 | 0 | int res; |
4743 | |
|
4744 | 0 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
4745 | 0 | return(1); |
4746 | | |
4747 | 0 | res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns); |
4748 | 0 | if (res != 0) |
4749 | 0 | return(res); |
4750 | 0 | attr = xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
4751 | 0 | if (attr == NULL) |
4752 | 0 | return(-1); |
4753 | | |
4754 | 0 | return(0); |
4755 | 0 | } |
4756 | | |
4757 | | /** |
4758 | | * Find the `xml:lang` of a node. |
4759 | | * |
4760 | | * Look up the value of the `xml:lang` attribute or the one carried |
4761 | | * by the nearest ancestor. |
4762 | | * |
4763 | | * @param cur the node being checked |
4764 | | * @returns a pointer to the lang value, or NULL if not found |
4765 | | * It's up to the caller to free the memory with #xmlFree. |
4766 | | */ |
4767 | | xmlChar * |
4768 | 0 | xmlNodeGetLang(const xmlNode *cur) { |
4769 | 0 | xmlChar *lang; |
4770 | 0 | int res; |
4771 | |
|
4772 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) |
4773 | 0 | return(NULL); |
4774 | | |
4775 | 0 | while (cur != NULL) { |
4776 | 0 | res = xmlNodeGetAttrValue(cur, BAD_CAST "lang", XML_XML_NAMESPACE, |
4777 | 0 | &lang); |
4778 | 0 | if (res < 0) |
4779 | 0 | return(NULL); |
4780 | 0 | if (lang != NULL) |
4781 | 0 | return(lang); |
4782 | | |
4783 | 0 | cur = cur->parent; |
4784 | 0 | } |
4785 | | |
4786 | 0 | return(NULL); |
4787 | 0 | } |
4788 | | |
4789 | | |
4790 | | /** |
4791 | | * Set the `xml:space` attribute of a node. |
4792 | | * |
4793 | | * @param cur the node being changed |
4794 | | * @param val the xml:space value ("0": default, 1: "preserve") |
4795 | | * @returns 0 on success, 1 if arguments are invalid, -1 if a |
4796 | | * memory allocation failed. |
4797 | | */ |
4798 | | int |
4799 | 0 | xmlNodeSetSpacePreserve(xmlNode *cur, int val) { |
4800 | 0 | xmlNsPtr ns; |
4801 | 0 | xmlAttrPtr attr; |
4802 | 0 | const char *string; |
4803 | 0 | int res; |
4804 | |
|
4805 | 0 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
4806 | 0 | return(1); |
4807 | | |
4808 | 0 | res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns); |
4809 | 0 | if (res != 0) |
4810 | 0 | return(res); |
4811 | | |
4812 | 0 | if (val == 0) |
4813 | 0 | string = "default"; |
4814 | 0 | else |
4815 | 0 | string = "preserve"; |
4816 | |
|
4817 | 0 | attr = xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST string); |
4818 | 0 | if (attr == NULL) |
4819 | 0 | return(-1); |
4820 | | |
4821 | 0 | return(0); |
4822 | 0 | } |
4823 | | |
4824 | | /** |
4825 | | * Find the `xml:space` of a node. |
4826 | | * |
4827 | | * Look up the value of the `xml:space` attribute or the one carried |
4828 | | * by the nearest ancestor. |
4829 | | * |
4830 | | * @param cur the node being checked |
4831 | | * @returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve" |
4832 | | */ |
4833 | | int |
4834 | 2.64M | xmlNodeGetSpacePreserve(const xmlNode *cur) { |
4835 | 2.64M | xmlChar *space; |
4836 | 2.64M | int res; |
4837 | | |
4838 | 2.64M | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
4839 | 0 | return(-1); |
4840 | | |
4841 | 8.70M | while (cur != NULL) { |
4842 | 6.06M | res = xmlNodeGetAttrValue(cur, BAD_CAST "space", XML_XML_NAMESPACE, |
4843 | 6.06M | &space); |
4844 | 6.06M | if (res < 0) |
4845 | 17 | return(-1); |
4846 | 6.06M | if (space != NULL) { |
4847 | 2.62k | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
4848 | 1.20k | xmlFree(space); |
4849 | 1.20k | return(1); |
4850 | 1.20k | } |
4851 | 1.41k | if (xmlStrEqual(space, BAD_CAST "default")) { |
4852 | 328 | xmlFree(space); |
4853 | 328 | return(0); |
4854 | 328 | } |
4855 | 1.08k | xmlFree(space); |
4856 | 1.08k | } |
4857 | | |
4858 | 6.06M | cur = cur->parent; |
4859 | 6.06M | } |
4860 | | |
4861 | 2.64M | return(-1); |
4862 | 2.64M | } |
4863 | | |
4864 | | /** |
4865 | | * Set (or reset) the name of a node. |
4866 | | * |
4867 | | * @param cur the node being changed |
4868 | | * @param name the new tag name |
4869 | | */ |
4870 | | void |
4871 | 0 | xmlNodeSetName(xmlNode *cur, const xmlChar *name) { |
4872 | 0 | xmlDocPtr doc; |
4873 | 0 | xmlDictPtr dict; |
4874 | 0 | const xmlChar *copy; |
4875 | 0 | const xmlChar *oldName; |
4876 | |
|
4877 | 0 | if (cur == NULL) return; |
4878 | 0 | if (name == NULL) return; |
4879 | 0 | switch(cur->type) { |
4880 | 0 | case XML_ELEMENT_NODE: |
4881 | 0 | case XML_ATTRIBUTE_NODE: |
4882 | 0 | case XML_PI_NODE: |
4883 | 0 | case XML_ENTITY_REF_NODE: |
4884 | 0 | break; |
4885 | 0 | default: |
4886 | 0 | return; |
4887 | 0 | } |
4888 | | |
4889 | 0 | doc = cur->doc; |
4890 | 0 | if (doc != NULL) |
4891 | 0 | dict = doc->dict; |
4892 | 0 | else |
4893 | 0 | dict = NULL; |
4894 | |
|
4895 | 0 | if (dict != NULL) |
4896 | 0 | copy = xmlDictLookup(dict, name, -1); |
4897 | 0 | else |
4898 | 0 | copy = xmlStrdup(name); |
4899 | 0 | if (copy == NULL) |
4900 | 0 | return; |
4901 | | |
4902 | 0 | oldName = cur->name; |
4903 | 0 | cur->name = copy; |
4904 | 0 | if ((oldName != NULL) && |
4905 | 0 | ((dict == NULL) || (!xmlDictOwns(dict, oldName)))) |
4906 | 0 | xmlFree((xmlChar *) oldName); |
4907 | 0 | } |
4908 | | |
4909 | | /** |
4910 | | * Set (or reset) the base URI of a node, i.e. the value of the |
4911 | | * `xml:base` attribute. |
4912 | | * |
4913 | | * @param cur the node being changed |
4914 | | * @param uri the new base URI |
4915 | | * @returns 0 on success, -1 on error. |
4916 | | */ |
4917 | | int |
4918 | 0 | xmlNodeSetBase(xmlNode *cur, const xmlChar* uri) { |
4919 | 0 | xmlNsPtr ns; |
4920 | 0 | xmlChar* fixed; |
4921 | |
|
4922 | 0 | if (cur == NULL) |
4923 | 0 | return(-1); |
4924 | 0 | switch(cur->type) { |
4925 | 0 | case XML_ELEMENT_NODE: |
4926 | 0 | case XML_ATTRIBUTE_NODE: |
4927 | 0 | break; |
4928 | 0 | case XML_DOCUMENT_NODE: |
4929 | 0 | case XML_HTML_DOCUMENT_NODE: { |
4930 | 0 | xmlDocPtr doc = (xmlDocPtr) cur; |
4931 | |
|
4932 | 0 | if (doc->URL != NULL) |
4933 | 0 | xmlFree(doc->URL); |
4934 | 0 | if (uri == NULL) { |
4935 | 0 | doc->URL = NULL; |
4936 | 0 | } else { |
4937 | 0 | doc->URL = xmlPathToURI(uri); |
4938 | 0 | if (doc->URL == NULL) |
4939 | 0 | return(-1); |
4940 | 0 | } |
4941 | 0 | return(0); |
4942 | 0 | } |
4943 | 0 | default: |
4944 | 0 | return(-1); |
4945 | 0 | } |
4946 | | |
4947 | 0 | xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns); |
4948 | 0 | if (ns == NULL) |
4949 | 0 | return(-1); |
4950 | 0 | fixed = xmlPathToURI(uri); |
4951 | 0 | if (fixed == NULL) |
4952 | 0 | return(-1); |
4953 | 0 | if (xmlSetNsProp(cur, ns, BAD_CAST "base", fixed) == NULL) { |
4954 | 0 | xmlFree(fixed); |
4955 | 0 | return(-1); |
4956 | 0 | } |
4957 | 0 | xmlFree(fixed); |
4958 | |
|
4959 | 0 | return(0); |
4960 | 0 | } |
4961 | | |
4962 | | /** |
4963 | | * Searches for the base URI. The code should work on both XML |
4964 | | * and HTML document even if base mechanisms are completely different. |
4965 | | * It returns the base as defined in RFC 2396 sections "5.1.1. Base |
4966 | | * URI within Document Content" and "5.1.2. Base URI from the |
4967 | | * Encapsulating Entity". However it does not return the document base |
4968 | | * (5.1.3), use `doc->URL` in this case. |
4969 | | * |
4970 | | * @since 2.13.0 |
4971 | | * |
4972 | | * @param doc the document the node pertains to |
4973 | | * @param cur the node being checked |
4974 | | * @param baseOut pointer to base |
4975 | | * @returns 0 in case of success, 1 if a URI or argument is invalid, -1 if a |
4976 | | * memory allocation failed. |
4977 | | */ |
4978 | | int |
4979 | 2.79M | xmlNodeGetBaseSafe(const xmlDoc *doc, const xmlNode *cur, xmlChar **baseOut) { |
4980 | 2.79M | xmlChar *ret = NULL; |
4981 | 2.79M | xmlChar *base, *newbase; |
4982 | 2.79M | int res; |
4983 | | |
4984 | 2.79M | if (baseOut == NULL) |
4985 | 0 | return(1); |
4986 | 2.79M | *baseOut = NULL; |
4987 | 2.79M | if ((cur == NULL) && (doc == NULL)) |
4988 | 0 | return(1); |
4989 | 2.79M | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
4990 | 0 | return(1); |
4991 | 2.79M | if (doc == NULL) |
4992 | 0 | doc = cur->doc; |
4993 | | |
4994 | 2.79M | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
4995 | 0 | cur = doc->children; |
4996 | 0 | while ((cur != NULL) && (cur->name != NULL)) { |
4997 | 0 | if (cur->type != XML_ELEMENT_NODE) { |
4998 | 0 | cur = cur->next; |
4999 | 0 | continue; |
5000 | 0 | } |
5001 | 0 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
5002 | 0 | cur = cur->children; |
5003 | 0 | continue; |
5004 | 0 | } |
5005 | 0 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
5006 | 0 | cur = cur->children; |
5007 | 0 | continue; |
5008 | 0 | } |
5009 | 0 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
5010 | 0 | if (xmlNodeGetAttrValue(cur, BAD_CAST "href", NULL, &ret) < 0) |
5011 | 0 | return(-1); |
5012 | 0 | if (ret == NULL) |
5013 | 0 | return(1); |
5014 | 0 | goto found; |
5015 | 0 | } |
5016 | 0 | cur = cur->next; |
5017 | 0 | } |
5018 | 0 | return(0); |
5019 | 0 | } |
5020 | | |
5021 | 20.4M | while (cur != NULL) { |
5022 | 17.6M | if (cur->type == XML_ENTITY_DECL) { |
5023 | 0 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
5024 | |
|
5025 | 0 | if (ent->URI == NULL) |
5026 | 0 | break; |
5027 | 0 | xmlFree(ret); |
5028 | 0 | ret = xmlStrdup(ent->URI); |
5029 | 0 | if (ret == NULL) |
5030 | 0 | return(-1); |
5031 | 0 | goto found; |
5032 | 0 | } |
5033 | 17.6M | if (cur->type == XML_ELEMENT_NODE) { |
5034 | 14.8M | if (xmlNodeGetAttrValue(cur, BAD_CAST "base", XML_XML_NAMESPACE, |
5035 | 14.8M | &base) < 0) { |
5036 | 5 | xmlFree(ret); |
5037 | 5 | return(-1); |
5038 | 5 | } |
5039 | 14.8M | if (base != NULL) { |
5040 | 80.6k | if (ret != NULL) { |
5041 | 237 | res = xmlBuildURISafe(ret, base, &newbase); |
5042 | 237 | xmlFree(ret); |
5043 | 237 | xmlFree(base); |
5044 | 237 | if (res != 0) |
5045 | 173 | return(res); |
5046 | 64 | ret = newbase; |
5047 | 80.3k | } else { |
5048 | 80.3k | ret = base; |
5049 | 80.3k | } |
5050 | 80.4k | if ((!xmlStrncmp(ret, BAD_CAST "http://", 7)) || |
5051 | 80.4k | (!xmlStrncmp(ret, BAD_CAST "ftp://", 6)) || |
5052 | 80.4k | (!xmlStrncmp(ret, BAD_CAST "urn:", 4))) |
5053 | 19.4k | goto found; |
5054 | 80.4k | } |
5055 | 14.8M | } |
5056 | 17.6M | cur = cur->parent; |
5057 | 17.6M | } |
5058 | | |
5059 | 2.77M | if ((doc != NULL) && (doc->URL != NULL)) { |
5060 | 520k | if (ret == NULL) { |
5061 | 512k | ret = xmlStrdup(doc->URL); |
5062 | 512k | if (ret == NULL) |
5063 | 27 | return(-1); |
5064 | 512k | } else { |
5065 | 8.43k | res = xmlBuildURISafe(ret, doc->URL, &newbase); |
5066 | 8.43k | xmlFree(ret); |
5067 | 8.43k | if (res != 0) |
5068 | 2.63k | return(res); |
5069 | 5.80k | ret = newbase; |
5070 | 5.80k | } |
5071 | 520k | } |
5072 | | |
5073 | 2.78M | found: |
5074 | 2.78M | *baseOut = ret; |
5075 | 2.78M | return(0); |
5076 | 2.77M | } |
5077 | | |
5078 | | /** |
5079 | | * See #xmlNodeGetBaseSafe. This function doesn't allow to distinguish |
5080 | | * memory allocation failures from a non-existing base. |
5081 | | * |
5082 | | * @param doc the document the node pertains to |
5083 | | * @param cur the node being checked |
5084 | | * @returns a pointer to the base URL, or NULL if not found |
5085 | | * It's up to the caller to free the memory with #xmlFree. |
5086 | | */ |
5087 | | xmlChar * |
5088 | 2.79M | xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) { |
5089 | 2.79M | xmlChar *base; |
5090 | | |
5091 | 2.79M | xmlNodeGetBaseSafe(doc, cur, &base); |
5092 | 2.79M | return(base); |
5093 | 2.79M | } |
5094 | | |
5095 | | /** |
5096 | | * Append the string value of a node to `buffer`. For text nodes, |
5097 | | * the string value is the text content. Otherwise, the string value |
5098 | | * is the concatenation of the string values of the node's |
5099 | | * descendants. |
5100 | | * |
5101 | | * Entity references are substituted. |
5102 | | * |
5103 | | * @param buffer a buffer |
5104 | | * @param cur the node being read |
5105 | | * @returns 0 in case of success and -1 in case of error. |
5106 | | */ |
5107 | | int |
5108 | | xmlNodeBufGetContent(xmlBuffer *buffer, const xmlNode *cur) |
5109 | 0 | { |
5110 | 0 | xmlBufPtr buf; |
5111 | 0 | int ret1, ret2; |
5112 | |
|
5113 | 0 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
5114 | 0 | buf = xmlBufFromBuffer(buffer); |
5115 | 0 | ret1 = xmlBufGetNodeContent(buf, cur); |
5116 | 0 | ret2 = xmlBufBackToBuffer(buf, buffer); |
5117 | 0 | if ((ret1 < 0) || (ret2 < 0)) |
5118 | 0 | return(-1); |
5119 | 0 | return(0); |
5120 | 0 | } |
5121 | | |
5122 | | static void |
5123 | 33.7k | xmlBufGetEntityRefContent(xmlBufPtr buf, const xmlNode *ref) { |
5124 | 33.7k | xmlEntityPtr ent; |
5125 | | |
5126 | 33.7k | if (ref->children != NULL) { |
5127 | 0 | ent = (xmlEntityPtr) ref->children; |
5128 | 33.7k | } else { |
5129 | | /* lookup entity declaration */ |
5130 | 33.7k | ent = xmlGetDocEntity(ref->doc, ref->name); |
5131 | 33.7k | if (ent == NULL) |
5132 | 33.7k | return; |
5133 | 33.7k | } |
5134 | | |
5135 | | /* |
5136 | | * The parser should always expand predefined entities but it's |
5137 | | * possible to create references to predefined entities using |
5138 | | * the tree API. |
5139 | | */ |
5140 | 0 | if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) { |
5141 | 0 | xmlBufCat(buf, ent->content); |
5142 | 0 | return; |
5143 | 0 | } |
5144 | | |
5145 | 0 | if (ent->flags & XML_ENT_EXPANDING) |
5146 | 0 | return; |
5147 | | |
5148 | 0 | ent->flags |= XML_ENT_EXPANDING; |
5149 | 0 | xmlBufGetChildContent(buf, (xmlNodePtr) ent); |
5150 | 0 | ent->flags &= ~XML_ENT_EXPANDING; |
5151 | 0 | } |
5152 | | |
5153 | | static void |
5154 | 1.16M | xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree) { |
5155 | 1.16M | const xmlNode *cur = tree->children; |
5156 | | |
5157 | 49.5M | while (cur != NULL) { |
5158 | 49.5M | switch (cur->type) { |
5159 | 14.9M | case XML_TEXT_NODE: |
5160 | 14.9M | case XML_CDATA_SECTION_NODE: |
5161 | 14.9M | xmlBufCat(buf, cur->content); |
5162 | 14.9M | break; |
5163 | | |
5164 | 33.7k | case XML_ENTITY_REF_NODE: |
5165 | 33.7k | xmlBufGetEntityRefContent(buf, cur); |
5166 | 33.7k | break; |
5167 | | |
5168 | 34.5M | default: |
5169 | 34.5M | if (cur->children != NULL) { |
5170 | 13.1M | cur = cur->children; |
5171 | 13.1M | continue; |
5172 | 13.1M | } |
5173 | 21.3M | break; |
5174 | 49.5M | } |
5175 | | |
5176 | 49.5M | while (cur->next == NULL) { |
5177 | 14.3M | cur = cur->parent; |
5178 | 14.3M | if (cur == tree) |
5179 | 1.15M | return; |
5180 | 14.3M | } |
5181 | 35.2M | cur = cur->next; |
5182 | 35.2M | } |
5183 | 1.16M | } |
5184 | | |
5185 | | /** |
5186 | | * Append the string value of a node to `buf`. For text nodes, |
5187 | | * the string value is the text content. Otherwise, the string value |
5188 | | * is the concatenation of the string values of the node's |
5189 | | * descendants. |
5190 | | * |
5191 | | * Entity references are substituted. |
5192 | | * |
5193 | | * @param buf a buffer xmlBuf |
5194 | | * @param cur the node being read |
5195 | | * @returns 0 in case of success and -1 in case of error. |
5196 | | */ |
5197 | | int |
5198 | | xmlBufGetNodeContent(xmlBuf *buf, const xmlNode *cur) |
5199 | 1.16M | { |
5200 | 1.16M | if ((cur == NULL) || (buf == NULL)) |
5201 | 0 | return(-1); |
5202 | | |
5203 | 1.16M | switch (cur->type) { |
5204 | 244k | case XML_DOCUMENT_NODE: |
5205 | 244k | case XML_HTML_DOCUMENT_NODE: |
5206 | 244k | case XML_DOCUMENT_FRAG_NODE: |
5207 | 1.16M | case XML_ELEMENT_NODE: |
5208 | 1.16M | case XML_ATTRIBUTE_NODE: |
5209 | 1.16M | case XML_ENTITY_DECL: |
5210 | 1.16M | xmlBufGetChildContent(buf, cur); |
5211 | 1.16M | break; |
5212 | | |
5213 | 0 | case XML_CDATA_SECTION_NODE: |
5214 | 0 | case XML_TEXT_NODE: |
5215 | 0 | case XML_COMMENT_NODE: |
5216 | 0 | case XML_PI_NODE: |
5217 | 0 | xmlBufCat(buf, cur->content); |
5218 | 0 | break; |
5219 | | |
5220 | 0 | case XML_ENTITY_REF_NODE: |
5221 | 0 | xmlBufGetEntityRefContent(buf, cur); |
5222 | 0 | break; |
5223 | | |
5224 | 0 | case XML_NAMESPACE_DECL: |
5225 | 0 | xmlBufCat(buf, ((xmlNsPtr) cur)->href); |
5226 | 0 | break; |
5227 | | |
5228 | 0 | default: |
5229 | 0 | break; |
5230 | 1.16M | } |
5231 | | |
5232 | 1.16M | return(0); |
5233 | 1.16M | } |
5234 | | |
5235 | | /** |
5236 | | * Returns the string value of a node. For text nodes, the string |
5237 | | * value is the text content. Otherwise, the string value is the |
5238 | | * concatenation of the string values of the node's descendants. |
5239 | | * |
5240 | | * Entity references are substituted. |
5241 | | * |
5242 | | * It's up to the caller to free the result with #xmlFree. |
5243 | | * |
5244 | | * @param cur the node being read |
5245 | | * @returns a new string or NULL if arguments are invalid or a |
5246 | | * memory allocation failed. |
5247 | | */ |
5248 | | xmlChar * |
5249 | | xmlNodeGetContent(const xmlNode *cur) |
5250 | 6.34M | { |
5251 | 6.34M | xmlBufPtr buf; |
5252 | 6.34M | xmlChar *ret; |
5253 | | |
5254 | 6.34M | if (cur == NULL) |
5255 | 0 | return (NULL); |
5256 | | |
5257 | 6.34M | switch (cur->type) { |
5258 | 245k | case XML_DOCUMENT_NODE: |
5259 | 245k | case XML_HTML_DOCUMENT_NODE: |
5260 | 245k | case XML_ENTITY_REF_NODE: |
5261 | 245k | break; |
5262 | | |
5263 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5264 | 2.87M | case XML_ELEMENT_NODE: |
5265 | 5.16M | case XML_ATTRIBUTE_NODE: |
5266 | 5.16M | case XML_ENTITY_DECL: { |
5267 | 5.16M | xmlNodePtr children = cur->children; |
5268 | | |
5269 | 5.16M | if (children == NULL) |
5270 | 1.59M | return(xmlStrdup(BAD_CAST "")); |
5271 | | |
5272 | | /* Optimization for single text children */ |
5273 | 3.57M | if (((children->type == XML_TEXT_NODE) || |
5274 | 3.57M | (children->type == XML_CDATA_SECTION_NODE)) && |
5275 | 3.57M | (children->next == NULL)) { |
5276 | 2.65M | if (children->content == NULL) |
5277 | 69 | return(xmlStrdup(BAD_CAST "")); |
5278 | 2.65M | return(xmlStrdup(children->content)); |
5279 | 2.65M | } |
5280 | | |
5281 | 918k | break; |
5282 | 3.57M | } |
5283 | | |
5284 | 918k | case XML_CDATA_SECTION_NODE: |
5285 | 754k | case XML_TEXT_NODE: |
5286 | 791k | case XML_COMMENT_NODE: |
5287 | 813k | case XML_PI_NODE: |
5288 | 813k | if (cur->content != NULL) |
5289 | 792k | return(xmlStrdup(cur->content)); |
5290 | 20.9k | else |
5291 | 20.9k | return(xmlStrdup(BAD_CAST "")); |
5292 | | |
5293 | 114k | case XML_NAMESPACE_DECL: |
5294 | 114k | return(xmlStrdup(((xmlNsPtr) cur)->href)); |
5295 | | |
5296 | 0 | default: |
5297 | 0 | return(NULL); |
5298 | 6.34M | } |
5299 | | |
5300 | 1.16M | buf = xmlBufCreate(50); |
5301 | 1.16M | if (buf == NULL) |
5302 | 4.73k | return (NULL); |
5303 | 1.16M | xmlBufGetNodeContent(buf, cur); |
5304 | 1.16M | ret = xmlBufDetach(buf); |
5305 | 1.16M | xmlBufFree(buf); |
5306 | | |
5307 | 1.16M | return(ret); |
5308 | 1.16M | } |
5309 | | |
5310 | | static int |
5311 | 3.77M | xmlNodeSetContentInternal(xmlNodePtr cur, const xmlChar *content, int len) { |
5312 | 3.77M | if (cur == NULL) { |
5313 | 0 | return(1); |
5314 | 0 | } |
5315 | 3.77M | switch (cur->type) { |
5316 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5317 | 0 | case XML_ELEMENT_NODE: |
5318 | 0 | case XML_ATTRIBUTE_NODE: |
5319 | 0 | if (xmlNodeParseContent(cur, content, len) < 0) |
5320 | 0 | return(-1); |
5321 | 0 | break; |
5322 | | |
5323 | 3.76M | case XML_TEXT_NODE: |
5324 | 3.76M | case XML_CDATA_SECTION_NODE: |
5325 | 3.76M | case XML_PI_NODE: |
5326 | 3.77M | case XML_COMMENT_NODE: { |
5327 | 3.77M | xmlChar *copy = NULL; |
5328 | | |
5329 | 3.77M | if (content != NULL) { |
5330 | 0 | if (len < 0) |
5331 | 0 | copy = xmlStrdup(content); |
5332 | 0 | else |
5333 | 0 | copy = xmlStrndup(content, len); |
5334 | 0 | if (copy == NULL) |
5335 | 0 | return(-1); |
5336 | 0 | } |
5337 | | |
5338 | 3.77M | xmlTextSetContent(cur, copy); |
5339 | 3.77M | break; |
5340 | 3.77M | } |
5341 | | |
5342 | 0 | default: |
5343 | 0 | break; |
5344 | 3.77M | } |
5345 | | |
5346 | 3.77M | return(0); |
5347 | 3.77M | } |
5348 | | |
5349 | | /** |
5350 | | * Replace the text content of a node. |
5351 | | * |
5352 | | * Sets the raw text content of text, CDATA, comment or PI nodes. |
5353 | | * |
5354 | | * For element and attribute nodes, removes all children and |
5355 | | * replaces them by parsing `content` which is expected to be a |
5356 | | * valid XML attribute value possibly containing character and |
5357 | | * entity references. Syntax errors and references to undeclared |
5358 | | * entities are ignored silently. Unfortunately, there isn't an |
5359 | | * API to pass raw content directly. An inefficient work-around |
5360 | | * is to escape the content with #xmlEncodeSpecialChars before |
5361 | | * passing it. A better trick is clearing the old content |
5362 | | * with `xmlNodeSetContent(node, NULL)` first and then calling |
5363 | | * `xmlNodeAddContent(node, content)`. Unlike this function, |
5364 | | * #xmlNodeAddContent accepts raw text. |
5365 | | * |
5366 | | * @param cur the node being modified |
5367 | | * @param content the new value of the content |
5368 | | * @returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5369 | | */ |
5370 | | int |
5371 | 3.77M | xmlNodeSetContent(xmlNode *cur, const xmlChar *content) { |
5372 | 3.77M | return(xmlNodeSetContentInternal(cur, content, -1)); |
5373 | 3.77M | } |
5374 | | |
5375 | | /** |
5376 | | * See #xmlNodeSetContent. |
5377 | | * |
5378 | | * @param cur the node being modified |
5379 | | * @param content the new value of the content |
5380 | | * @param len the size of `content` |
5381 | | * @returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5382 | | */ |
5383 | | int |
5384 | 0 | xmlNodeSetContentLen(xmlNode *cur, const xmlChar *content, int len) { |
5385 | 0 | return(xmlNodeSetContentInternal(cur, content, len)); |
5386 | 0 | } |
5387 | | |
5388 | | /** |
5389 | | * Append the extra substring to the node content. |
5390 | | * |
5391 | | * NOTE: In contrast to #xmlNodeSetContentLen, `content` is supposed |
5392 | | * to be raw text, so unescaped XML special chars are allowed, entity |
5393 | | * references are not supported. |
5394 | | * |
5395 | | * @param cur the node being modified |
5396 | | * @param content extra content |
5397 | | * @param len the size of `content` |
5398 | | * @returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5399 | | */ |
5400 | | int |
5401 | 126k | xmlNodeAddContentLen(xmlNode *cur, const xmlChar *content, int len) { |
5402 | 126k | if (cur == NULL) |
5403 | 0 | return(1); |
5404 | 126k | if ((content == NULL) || (len <= 0)) |
5405 | 0 | return(0); |
5406 | | |
5407 | 126k | switch (cur->type) { |
5408 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5409 | 0 | case XML_ELEMENT_NODE: |
5410 | 0 | case XML_ATTRIBUTE_NODE: { |
5411 | 0 | xmlNodePtr newNode, tmp; |
5412 | |
|
5413 | 0 | newNode = xmlNewDocTextLen(cur->doc, content, len); |
5414 | 0 | if (newNode == NULL) |
5415 | 0 | return(-1); |
5416 | 0 | tmp = xmlAddChild(cur, newNode); |
5417 | 0 | if (tmp == NULL) { |
5418 | 0 | xmlFreeNode(newNode); |
5419 | 0 | return(-1); |
5420 | 0 | } |
5421 | 0 | break; |
5422 | 0 | } |
5423 | 0 | break; |
5424 | 126k | case XML_TEXT_NODE: |
5425 | 126k | case XML_CDATA_SECTION_NODE: |
5426 | 126k | case XML_PI_NODE: |
5427 | 126k | case XML_COMMENT_NODE: |
5428 | 126k | return(xmlTextAddContent(cur, content, len)); |
5429 | 0 | default: |
5430 | 0 | break; |
5431 | 126k | } |
5432 | | |
5433 | 0 | return(0); |
5434 | 126k | } |
5435 | | |
5436 | | /** |
5437 | | * Append the extra substring to the node content. |
5438 | | * |
5439 | | * NOTE: In contrast to #xmlNodeSetContent, `content` is supposed |
5440 | | * to be raw text, so unescaped XML special chars are allowed, entity |
5441 | | * references are not supported. |
5442 | | * |
5443 | | * @param cur the node being modified |
5444 | | * @param content extra content |
5445 | | * @returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5446 | | */ |
5447 | | int |
5448 | 126k | xmlNodeAddContent(xmlNode *cur, const xmlChar *content) { |
5449 | 126k | return(xmlNodeAddContentLen(cur, content, xmlStrlen(content))); |
5450 | 126k | } |
5451 | | |
5452 | | /** |
5453 | | * Merge the second text node into the first. If `first` is NULL, |
5454 | | * `second` is returned. Otherwise, the second node is unlinked and |
5455 | | * freed. |
5456 | | * |
5457 | | * @param first the first text node |
5458 | | * @param second the second text node being merged |
5459 | | * @returns the first text node augmented or NULL in case of error. |
5460 | | */ |
5461 | | xmlNode * |
5462 | 0 | xmlTextMerge(xmlNode *first, xmlNode *second) { |
5463 | 0 | if (first == NULL) |
5464 | 0 | return(second); |
5465 | 0 | if (second == NULL) |
5466 | 0 | return(first); |
5467 | | |
5468 | 0 | if ((first->type != XML_TEXT_NODE) || |
5469 | 0 | (second->type != XML_TEXT_NODE) || |
5470 | 0 | (first == second) || |
5471 | 0 | (first->name != second->name)) |
5472 | 0 | return(NULL); |
5473 | | |
5474 | 0 | if (xmlTextAddContent(first, second->content, -1) < 0) |
5475 | 0 | return(NULL); |
5476 | | |
5477 | 0 | xmlUnlinkNodeInternal(second); |
5478 | 0 | xmlFreeNode(second); |
5479 | 0 | return(first); |
5480 | 0 | } |
5481 | | |
5482 | | /** |
5483 | | * Find all in-scope namespaces of a node. `out` returns a NULL |
5484 | | * terminated array of namespace pointers that must be freed by |
5485 | | * the caller. |
5486 | | * |
5487 | | * @since 2.13.0 |
5488 | | * |
5489 | | * @param doc the document |
5490 | | * @param node the current node |
5491 | | * @param out the returned namespace array |
5492 | | * @returns 0 on success, 1 if no namespaces were found, -1 if a |
5493 | | * memory allocation failed. |
5494 | | */ |
5495 | | int |
5496 | | xmlGetNsListSafe(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node, |
5497 | | xmlNs ***out) |
5498 | 1.00M | { |
5499 | 1.00M | xmlNsPtr cur; |
5500 | 1.00M | xmlNsPtr *namespaces = NULL; |
5501 | 1.00M | int nbns = 0; |
5502 | 1.00M | int maxns = 0; |
5503 | 1.00M | int i; |
5504 | | |
5505 | 1.00M | if (out == NULL) |
5506 | 0 | return(1); |
5507 | 1.00M | *out = NULL; |
5508 | 1.00M | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
5509 | 0 | return(1); |
5510 | | |
5511 | 5.04M | while (node != NULL) { |
5512 | 4.04M | if (node->type == XML_ELEMENT_NODE) { |
5513 | 3.04M | cur = node->nsDef; |
5514 | 6.77M | while (cur != NULL) { |
5515 | 15.0M | for (i = 0; i < nbns; i++) { |
5516 | 11.3M | if ((cur->prefix == namespaces[i]->prefix) || |
5517 | 11.3M | (xmlStrEqual(cur->prefix, namespaces[i]->prefix))) |
5518 | 71.1k | break; |
5519 | 11.3M | } |
5520 | 3.73M | if (i >= nbns) { |
5521 | 3.66M | if (nbns >= maxns) { |
5522 | 2.14M | xmlNsPtr *tmp; |
5523 | 2.14M | int newSize; |
5524 | | |
5525 | 2.14M | newSize = xmlGrowCapacity(maxns, sizeof(tmp[0]), |
5526 | 2.14M | 10, XML_MAX_ITEMS); |
5527 | 2.14M | if (newSize < 0) { |
5528 | 0 | xmlFree(namespaces); |
5529 | 0 | return(-1); |
5530 | 0 | } |
5531 | 2.14M | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
5532 | 2.14M | if (newSize < 2) |
5533 | 993k | newSize = 2; |
5534 | 2.14M | #endif |
5535 | 2.14M | tmp = xmlRealloc(namespaces, |
5536 | 2.14M | (newSize + 1) * sizeof(tmp[0])); |
5537 | 2.14M | if (tmp == NULL) { |
5538 | 441 | xmlFree(namespaces); |
5539 | 441 | return(-1); |
5540 | 441 | } |
5541 | 2.14M | namespaces = tmp; |
5542 | 2.14M | maxns = newSize; |
5543 | 2.14M | } |
5544 | 3.66M | namespaces[nbns++] = cur; |
5545 | 3.66M | namespaces[nbns] = NULL; |
5546 | 3.66M | } |
5547 | | |
5548 | 3.73M | cur = cur->next; |
5549 | 3.73M | } |
5550 | 3.04M | } |
5551 | 4.04M | node = node->parent; |
5552 | 4.04M | } |
5553 | | |
5554 | 1.00M | *out = namespaces; |
5555 | 1.00M | return((namespaces == NULL) ? 1 : 0); |
5556 | 1.00M | } |
5557 | | |
5558 | | /** |
5559 | | * Find all in-scope namespaces of a node. |
5560 | | * |
5561 | | * Use #xmlGetNsListSafe for better error reporting. |
5562 | | * |
5563 | | * @param doc the document |
5564 | | * @param node the current node |
5565 | | * @returns a NULL terminated array of namespace pointers that must |
5566 | | * be freed by the caller or NULL if no namespaces were found or |
5567 | | * a memory allocation failed. |
5568 | | */ |
5569 | | xmlNs ** |
5570 | | xmlGetNsList(const xmlDoc *doc, const xmlNode *node) |
5571 | 644k | { |
5572 | 644k | xmlNsPtr *ret; |
5573 | | |
5574 | 644k | xmlGetNsListSafe(doc, node, &ret); |
5575 | 644k | return(ret); |
5576 | 644k | } |
5577 | | |
5578 | | static xmlNsPtr |
5579 | 65.2k | xmlNewXmlNs(void) { |
5580 | 65.2k | xmlNsPtr ns; |
5581 | | |
5582 | 65.2k | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
5583 | 65.2k | if (ns == NULL) |
5584 | 359 | return(NULL); |
5585 | 64.8k | memset(ns, 0, sizeof(xmlNs)); |
5586 | 64.8k | ns->type = XML_LOCAL_NAMESPACE; |
5587 | 64.8k | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
5588 | 64.8k | if (ns->href == NULL) { |
5589 | 5 | xmlFreeNs(ns); |
5590 | 5 | return(NULL); |
5591 | 5 | } |
5592 | 64.8k | ns->prefix = xmlStrdup(BAD_CAST "xml"); |
5593 | 64.8k | if (ns->prefix == NULL) { |
5594 | 3 | xmlFreeNs(ns); |
5595 | 3 | return(NULL); |
5596 | 3 | } |
5597 | | |
5598 | 64.8k | return(ns); |
5599 | 64.8k | } |
5600 | | |
5601 | | /* |
5602 | | * xmlTreeEnsureXMLDecl: |
5603 | | * @doc: the doc |
5604 | | * |
5605 | | * Ensures that there is an XML namespace declaration on the doc. |
5606 | | * |
5607 | | * Returns the XML ns-struct or NULL if a memory allocation failed. |
5608 | | */ |
5609 | | static xmlNsPtr |
5610 | | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
5611 | 145k | { |
5612 | 145k | xmlNsPtr ns; |
5613 | | |
5614 | 145k | ns = doc->oldNs; |
5615 | 145k | if (ns != NULL) |
5616 | 80.3k | return (ns); |
5617 | | |
5618 | 65.2k | ns = xmlNewXmlNs(); |
5619 | 65.2k | doc->oldNs = ns; |
5620 | | |
5621 | 65.2k | return(ns); |
5622 | 145k | } |
5623 | | |
5624 | | /** |
5625 | | * Search for a namespace with `prefix` in scope of `node`. |
5626 | | * |
5627 | | * @param node a node |
5628 | | * @param prefix a namespace prefix |
5629 | | * @param out pointer to resulting namespace |
5630 | | * @returns 0 on success, -1 if a memory allocation failed, 1 on |
5631 | | * other errors. |
5632 | | */ |
5633 | | int |
5634 | | xmlSearchNsSafe(xmlNode *node, const xmlChar *prefix, |
5635 | 4.53M | xmlNs **out) { |
5636 | 4.53M | xmlNsPtr cur; |
5637 | 4.53M | xmlDocPtr doc; |
5638 | 4.53M | xmlNodePtr orig = node; |
5639 | 4.53M | xmlNodePtr parent; |
5640 | | |
5641 | 4.53M | if (out == NULL) |
5642 | 0 | return(1); |
5643 | 4.53M | *out = NULL; |
5644 | 4.53M | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
5645 | 0 | return(1); |
5646 | | |
5647 | 4.53M | doc = node->doc; |
5648 | | |
5649 | 4.53M | if ((doc != NULL) && (IS_STR_XML(prefix))) { |
5650 | 145k | cur = xmlTreeEnsureXMLDecl(doc); |
5651 | 145k | if (cur == NULL) |
5652 | 367 | return(-1); |
5653 | 145k | *out = cur; |
5654 | 145k | return(0); |
5655 | 145k | } |
5656 | | |
5657 | 4.38M | while (node->type != XML_ELEMENT_NODE) { |
5658 | 4.78k | node = node->parent; |
5659 | 4.78k | if (node == NULL) |
5660 | 4.61k | return(0); |
5661 | 4.78k | } |
5662 | | |
5663 | 4.38M | parent = node; |
5664 | | |
5665 | 32.8M | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
5666 | 30.7M | cur = node->nsDef; |
5667 | 47.6M | while (cur != NULL) { |
5668 | 19.0M | if ((xmlStrEqual(cur->prefix, prefix)) && |
5669 | 19.0M | (cur->href != NULL)) { |
5670 | 2.25M | *out = cur; |
5671 | 2.25M | return(0); |
5672 | 2.25M | } |
5673 | 16.8M | cur = cur->next; |
5674 | 16.8M | } |
5675 | 28.5M | if (orig != node) { |
5676 | 24.7M | cur = node->ns; |
5677 | 24.7M | if ((cur != NULL) && |
5678 | 24.7M | (xmlStrEqual(cur->prefix, prefix)) && |
5679 | 24.7M | (cur->href != NULL)) { |
5680 | 34.2k | *out = cur; |
5681 | 34.2k | return(0); |
5682 | 34.2k | } |
5683 | 24.7M | } |
5684 | | |
5685 | 28.4M | node = node->parent; |
5686 | 28.4M | } |
5687 | | |
5688 | | /* |
5689 | | * The XML-1.0 namespace is normally held on the document |
5690 | | * element. In this case exceptionally create it on the |
5691 | | * node element. |
5692 | | */ |
5693 | 2.09M | if ((doc == NULL) && (IS_STR_XML(prefix))) { |
5694 | 0 | cur = xmlNewXmlNs(); |
5695 | 0 | if (cur == NULL) |
5696 | 0 | return(-1); |
5697 | 0 | cur->next = parent->nsDef; |
5698 | 0 | parent->nsDef = cur; |
5699 | 0 | *out = cur; |
5700 | 0 | } |
5701 | | |
5702 | 2.09M | return(0); |
5703 | 2.09M | } |
5704 | | |
5705 | | /** |
5706 | | * Search for a namespace with `prefix` in scope of `node`. |
5707 | | * |
5708 | | * Recurse on the parents until it finds the defined namespace |
5709 | | * or return NULL otherwise. |
5710 | | * |
5711 | | * If `nameSpace` is NULL, the default namespace is looked up. |
5712 | | * |
5713 | | * Namespace search doesn't cross entity boundaries. |
5714 | | * |
5715 | | * @param doc the document |
5716 | | * @param node the current node |
5717 | | * @param nameSpace the namespace prefix |
5718 | | * @returns the namespace pointer or NULL if no namespace was found or |
5719 | | * a memory allocation failed. Allocations can only fail if the `xml` |
5720 | | * namespace is queried. |
5721 | | */ |
5722 | | xmlNs * |
5723 | | xmlSearchNs(xmlDoc *doc ATTRIBUTE_UNUSED, xmlNode *node, |
5724 | 4.40M | const xmlChar *nameSpace) { |
5725 | 4.40M | xmlNsPtr cur; |
5726 | | |
5727 | 4.40M | xmlSearchNsSafe(node, nameSpace, &cur); |
5728 | 4.40M | return(cur); |
5729 | 4.40M | } |
5730 | | |
5731 | | /** |
5732 | | * Verify that the given namespace held on `ancestor` is still in scope |
5733 | | * on node. |
5734 | | * |
5735 | | * @param doc the document |
5736 | | * @param node the current node |
5737 | | * @param ancestor the ancestor carrying the namespace |
5738 | | * @param prefix the namespace prefix |
5739 | | * @returns 1 if true, 0 if false and -1 in case of error. |
5740 | | */ |
5741 | | static int |
5742 | | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
5743 | | xmlNodePtr ancestor, const xmlChar * prefix) |
5744 | 15.6k | { |
5745 | 15.6k | xmlNsPtr tst; |
5746 | | |
5747 | 60.1k | while ((node != NULL) && (node != ancestor)) { |
5748 | 57.6k | if ((node->type == XML_ENTITY_REF_NODE) || |
5749 | 57.6k | (node->type == XML_ENTITY_DECL)) |
5750 | 0 | return (-1); |
5751 | 57.6k | if (node->type == XML_ELEMENT_NODE) { |
5752 | 57.6k | tst = node->nsDef; |
5753 | 69.4k | while (tst != NULL) { |
5754 | 24.9k | if ((tst->prefix == NULL) |
5755 | 24.9k | && (prefix == NULL)) |
5756 | 7.79k | return (0); |
5757 | 17.1k | if ((tst->prefix != NULL) |
5758 | 17.1k | && (prefix != NULL) |
5759 | 17.1k | && (xmlStrEqual(tst->prefix, prefix))) |
5760 | 5.30k | return (0); |
5761 | 11.8k | tst = tst->next; |
5762 | 11.8k | } |
5763 | 57.6k | } |
5764 | 44.5k | node = node->parent; |
5765 | 44.5k | } |
5766 | 2.53k | if (node != ancestor) |
5767 | 0 | return (-1); |
5768 | 2.53k | return (1); |
5769 | 2.53k | } |
5770 | | |
5771 | | /** |
5772 | | * Search for a namespace matching `URI` in scope of `node`. |
5773 | | * |
5774 | | * @param node a node |
5775 | | * @param href a namespace URI |
5776 | | * @param out pointer to resulting namespace |
5777 | | * @returns 0 on success, -1 if a memory allocation failed, 1 on |
5778 | | * other errors. |
5779 | | */ |
5780 | | int |
5781 | | xmlSearchNsByHrefSafe(xmlNode *node, const xmlChar *href, |
5782 | 5.25k | xmlNs **out) { |
5783 | 5.25k | xmlNsPtr cur; |
5784 | 5.25k | xmlDocPtr doc; |
5785 | 5.25k | xmlNodePtr orig = node; |
5786 | 5.25k | xmlNodePtr parent; |
5787 | 5.25k | int is_attr; |
5788 | | |
5789 | 5.25k | if (out == NULL) |
5790 | 0 | return(1); |
5791 | 5.25k | *out = NULL; |
5792 | 5.25k | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
5793 | 0 | return(1); |
5794 | | |
5795 | 5.25k | doc = node->doc; |
5796 | | |
5797 | 5.25k | if ((doc != NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) { |
5798 | 0 | cur = xmlTreeEnsureXMLDecl(doc); |
5799 | 0 | if (cur == NULL) |
5800 | 0 | return(-1); |
5801 | 0 | *out = cur; |
5802 | 0 | return(0); |
5803 | 0 | } |
5804 | | |
5805 | 5.25k | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
5806 | | |
5807 | 5.25k | while (node->type != XML_ELEMENT_NODE) { |
5808 | 0 | node = node->parent; |
5809 | 0 | if (node == NULL) |
5810 | 0 | return(0); |
5811 | 0 | } |
5812 | | |
5813 | 5.25k | parent = node; |
5814 | | |
5815 | 57.7k | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
5816 | 55.0k | cur = node->nsDef; |
5817 | 88.3k | while (cur != NULL) { |
5818 | 34.6k | if (xmlStrEqual(cur->href, href)) { |
5819 | 6.50k | if (((!is_attr) || (cur->prefix != NULL)) && |
5820 | 6.50k | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) { |
5821 | 1.35k | *out = cur; |
5822 | 1.35k | return(0); |
5823 | 1.35k | } |
5824 | 6.50k | } |
5825 | 33.2k | cur = cur->next; |
5826 | 33.2k | } |
5827 | 53.6k | if (orig != node) { |
5828 | 48.6k | cur = node->ns; |
5829 | 48.6k | if (cur != NULL) { |
5830 | 39.7k | if (xmlStrEqual(cur->href, href)) { |
5831 | 9.12k | if (((!is_attr) || (cur->prefix != NULL)) && |
5832 | 9.12k | (xmlNsInScope(doc, orig, node, |
5833 | 9.12k | cur->prefix) == 1)) { |
5834 | 1.18k | *out = cur; |
5835 | 1.18k | return(0); |
5836 | 1.18k | } |
5837 | 9.12k | } |
5838 | 39.7k | } |
5839 | 48.6k | } |
5840 | | |
5841 | 52.4k | node = node->parent; |
5842 | 52.4k | } |
5843 | | |
5844 | | /* |
5845 | | * The XML-1.0 namespace is normally held on the document |
5846 | | * element. In this case exceptionally create it on the |
5847 | | * node element. |
5848 | | */ |
5849 | 2.72k | if ((doc == NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) { |
5850 | 0 | cur = xmlNewXmlNs(); |
5851 | 0 | if (cur == NULL) |
5852 | 0 | return(-1); |
5853 | 0 | cur->next = parent->nsDef; |
5854 | 0 | parent->nsDef = cur; |
5855 | 0 | *out = cur; |
5856 | 0 | } |
5857 | | |
5858 | 2.72k | return(0); |
5859 | 2.72k | } |
5860 | | |
5861 | | /** |
5862 | | * Search for a namespace matching `URI` in scope of `node`. |
5863 | | * |
5864 | | * @param doc the document |
5865 | | * @param node the current node |
5866 | | * @param href the namespace value |
5867 | | * @returns the namespace pointer or NULL if no namespace was found or |
5868 | | * a memory allocation failed. Allocations can only fail if the `xml` |
5869 | | * namespace is queried. |
5870 | | */ |
5871 | | xmlNs * |
5872 | | xmlSearchNsByHref(xmlDoc *doc ATTRIBUTE_UNUSED, xmlNode *node, |
5873 | 5.25k | const xmlChar * href) { |
5874 | 5.25k | xmlNsPtr cur; |
5875 | | |
5876 | 5.25k | xmlSearchNsByHrefSafe(node, href, &cur); |
5877 | 5.25k | return(cur); |
5878 | 5.25k | } |
5879 | | |
5880 | | /** |
5881 | | * Fix up namespace declarations. |
5882 | | * |
5883 | | * This function tries to locate a namespace definition in a tree |
5884 | | * ancestors, or create a new namespace definition node similar to |
5885 | | * `ns` trying to reuse the same prefix. However if the given prefix is |
5886 | | * null (default namespace) or reused within the subtree defined by |
5887 | | * `tree` or on one of its ancestors then a new prefix is generated. |
5888 | | * |
5889 | | * @param tree a node expected to hold the new namespace |
5890 | | * @param ns the original namespace |
5891 | | * @returns the (new) namespace definition or NULL in case of error |
5892 | | */ |
5893 | | static xmlNsPtr |
5894 | 0 | xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns) { |
5895 | 0 | xmlNsPtr def; |
5896 | 0 | xmlChar prefix[50]; |
5897 | 0 | int counter = 1; |
5898 | 0 | int res; |
5899 | |
|
5900 | 0 | if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) { |
5901 | 0 | return(NULL); |
5902 | 0 | } |
5903 | 0 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
5904 | 0 | return(NULL); |
5905 | 0 | } |
5906 | | /* |
5907 | | * Search an existing namespace definition inherited. |
5908 | | */ |
5909 | 0 | res = xmlSearchNsByHrefSafe(tree, ns->href, &def); |
5910 | 0 | if (res < 0) |
5911 | 0 | return(NULL); |
5912 | 0 | if (def != NULL) |
5913 | 0 | return(def); |
5914 | | |
5915 | | /* |
5916 | | * Find a close prefix which is not already in use. |
5917 | | * Let's strip namespace prefixes longer than 20 chars ! |
5918 | | */ |
5919 | 0 | if (ns->prefix == NULL) |
5920 | 0 | snprintf((char *) prefix, sizeof(prefix), "default"); |
5921 | 0 | else |
5922 | 0 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
5923 | |
|
5924 | 0 | res = xmlSearchNsSafe(tree, prefix, &def); |
5925 | 0 | if (res < 0) |
5926 | 0 | return(NULL); |
5927 | 0 | while (def != NULL) { |
5928 | 0 | if (counter > 1000) return(NULL); |
5929 | 0 | if (ns->prefix == NULL) |
5930 | 0 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
5931 | 0 | else |
5932 | 0 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
5933 | 0 | (char *)ns->prefix, counter++); |
5934 | 0 | res = xmlSearchNsSafe(tree, prefix, &def); |
5935 | 0 | if (res < 0) |
5936 | 0 | return(NULL); |
5937 | 0 | } |
5938 | | |
5939 | | /* |
5940 | | * OK, now we are ready to create a new one. |
5941 | | */ |
5942 | 0 | def = xmlNewNs(tree, ns->href, prefix); |
5943 | 0 | return(def); |
5944 | 0 | } |
5945 | | |
5946 | | typedef struct { |
5947 | | xmlNsPtr oldNs; |
5948 | | xmlNsPtr newNs; |
5949 | | } xmlNsCache; |
5950 | | |
5951 | | static int |
5952 | 0 | xmlGrowNsCache(xmlNsCache **cache, int *capacity) { |
5953 | 0 | xmlNsCache *tmp; |
5954 | 0 | int newSize; |
5955 | |
|
5956 | 0 | newSize = xmlGrowCapacity(*capacity, sizeof(tmp[0]), |
5957 | 0 | 10, XML_MAX_ITEMS); |
5958 | 0 | if (newSize < 0) |
5959 | 0 | return(-1); |
5960 | 0 | tmp = xmlRealloc(*cache, newSize * sizeof(tmp[0])); |
5961 | 0 | if (tmp == NULL) |
5962 | 0 | return(-1); |
5963 | 0 | *cache = tmp; |
5964 | 0 | *capacity = newSize; |
5965 | |
|
5966 | 0 | return(0); |
5967 | 0 | } |
5968 | | |
5969 | | /** |
5970 | | * This function checks that all the namespaces declared within the given |
5971 | | * tree are properly declared. This is needed for example after copy or cut |
5972 | | * and then paste operations. The subtree may still hold pointers to |
5973 | | * namespace declarations outside the subtree or invalid/masked. As much |
5974 | | * as possible the function tries to reuse the existing namespaces found in |
5975 | | * the new environment. If not possible the new namespaces are redeclared |
5976 | | * on `tree` at the top of the given subtree. |
5977 | | * |
5978 | | * @param doc the document |
5979 | | * @param tree a node defining the subtree to reconciliate |
5980 | | * @returns 0 on success or -1 in case of error. |
5981 | | */ |
5982 | | int |
5983 | 0 | xmlReconciliateNs(xmlDoc *doc, xmlNode *tree) { |
5984 | 0 | xmlNsCache *cache = NULL; |
5985 | 0 | int sizeCache = 0; |
5986 | 0 | int nbCache = 0; |
5987 | |
|
5988 | 0 | xmlNsPtr n; |
5989 | 0 | xmlNodePtr node = tree; |
5990 | 0 | xmlAttrPtr attr; |
5991 | 0 | int ret = 0, i; |
5992 | |
|
5993 | 0 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
5994 | 0 | if (node->doc != doc) return(-1); |
5995 | 0 | while (node != NULL) { |
5996 | | /* |
5997 | | * Reconciliate the node namespace |
5998 | | */ |
5999 | 0 | if (node->ns != NULL) { |
6000 | 0 | for (i = 0; i < nbCache; i++) { |
6001 | 0 | if (cache[i].oldNs == node->ns) { |
6002 | 0 | node->ns = cache[i].newNs; |
6003 | 0 | break; |
6004 | 0 | } |
6005 | 0 | } |
6006 | 0 | if (i == nbCache) { |
6007 | | /* |
6008 | | * OK we need to recreate a new namespace definition |
6009 | | */ |
6010 | 0 | n = xmlNewReconciledNs(tree, node->ns); |
6011 | 0 | if (n == NULL) { |
6012 | 0 | ret = -1; |
6013 | 0 | } else { |
6014 | | /* |
6015 | | * check if we need to grow the cache buffers. |
6016 | | */ |
6017 | 0 | if ((sizeCache <= nbCache) && |
6018 | 0 | (xmlGrowNsCache(&cache, &sizeCache) < 0)) { |
6019 | 0 | ret = -1; |
6020 | 0 | } else { |
6021 | 0 | cache[nbCache].newNs = n; |
6022 | 0 | cache[nbCache++].oldNs = node->ns; |
6023 | 0 | } |
6024 | 0 | } |
6025 | 0 | node->ns = n; |
6026 | 0 | } |
6027 | 0 | } |
6028 | | /* |
6029 | | * now check for namespace held by attributes on the node. |
6030 | | */ |
6031 | 0 | if (node->type == XML_ELEMENT_NODE) { |
6032 | 0 | attr = node->properties; |
6033 | 0 | while (attr != NULL) { |
6034 | 0 | if (attr->ns != NULL) { |
6035 | 0 | for (i = 0; i < nbCache; i++) { |
6036 | 0 | if (cache[i].oldNs == attr->ns) { |
6037 | 0 | attr->ns = cache[i].newNs; |
6038 | 0 | break; |
6039 | 0 | } |
6040 | 0 | } |
6041 | 0 | if (i == nbCache) { |
6042 | | /* |
6043 | | * OK we need to recreate a new namespace definition |
6044 | | */ |
6045 | 0 | n = xmlNewReconciledNs(tree, attr->ns); |
6046 | 0 | if (n == NULL) { |
6047 | 0 | ret = -1; |
6048 | 0 | } else { |
6049 | | /* |
6050 | | * check if we need to grow the cache buffers. |
6051 | | */ |
6052 | 0 | if ((sizeCache <= nbCache) && |
6053 | 0 | (xmlGrowNsCache(&cache, &sizeCache) < 0)) { |
6054 | 0 | ret = -1; |
6055 | 0 | } else { |
6056 | 0 | cache[nbCache].newNs = n; |
6057 | 0 | cache[nbCache++].oldNs = attr->ns; |
6058 | 0 | } |
6059 | 0 | } |
6060 | 0 | attr->ns = n; |
6061 | 0 | } |
6062 | 0 | } |
6063 | 0 | attr = attr->next; |
6064 | 0 | } |
6065 | 0 | } |
6066 | | |
6067 | | /* |
6068 | | * Browse the full subtree, deep first |
6069 | | */ |
6070 | 0 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
6071 | | /* deep first */ |
6072 | 0 | node = node->children; |
6073 | 0 | } else if ((node != tree) && (node->next != NULL)) { |
6074 | | /* then siblings */ |
6075 | 0 | node = node->next; |
6076 | 0 | } else if (node != tree) { |
6077 | | /* go up to parents->next if needed */ |
6078 | 0 | while (node != tree) { |
6079 | 0 | if (node->parent != NULL) |
6080 | 0 | node = node->parent; |
6081 | 0 | if ((node != tree) && (node->next != NULL)) { |
6082 | 0 | node = node->next; |
6083 | 0 | break; |
6084 | 0 | } |
6085 | 0 | if (node->parent == NULL) { |
6086 | 0 | node = NULL; |
6087 | 0 | break; |
6088 | 0 | } |
6089 | 0 | } |
6090 | | /* exit condition */ |
6091 | 0 | if (node == tree) |
6092 | 0 | node = NULL; |
6093 | 0 | } else |
6094 | 0 | break; |
6095 | 0 | } |
6096 | 0 | if (cache != NULL) |
6097 | 0 | xmlFree(cache); |
6098 | 0 | return(ret); |
6099 | 0 | } |
6100 | | |
6101 | | static xmlAttrPtr |
6102 | | xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name, |
6103 | | const xmlChar *nsName, int useDTD) |
6104 | 25.6M | { |
6105 | 25.6M | xmlAttrPtr prop; |
6106 | | |
6107 | | /* Avoid unused variable warning if features are disabled. */ |
6108 | 25.6M | (void) useDTD; |
6109 | | |
6110 | 25.6M | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
6111 | 2.66M | return(NULL); |
6112 | | |
6113 | 23.0M | if (node->properties != NULL) { |
6114 | 11.2M | prop = node->properties; |
6115 | 11.2M | if (nsName == NULL) { |
6116 | | /* |
6117 | | * We want the attr to be in no namespace. |
6118 | | */ |
6119 | 4.72M | do { |
6120 | 4.72M | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
6121 | 1.83M | return(prop); |
6122 | 1.83M | } |
6123 | 2.89M | prop = prop->next; |
6124 | 2.89M | } while (prop != NULL); |
6125 | 7.11M | } else { |
6126 | | /* |
6127 | | * We want the attr to be in the specified namespace. |
6128 | | */ |
6129 | 8.11M | do { |
6130 | 8.11M | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
6131 | 8.11M | ((prop->ns->href == nsName) || |
6132 | 119k | xmlStrEqual(prop->ns->href, nsName))) |
6133 | 99.7k | { |
6134 | 99.7k | return(prop); |
6135 | 99.7k | } |
6136 | 8.01M | prop = prop->next; |
6137 | 8.01M | } while (prop != NULL); |
6138 | 7.11M | } |
6139 | 11.2M | } |
6140 | | |
6141 | 21.0M | if (! useDTD) |
6142 | 18.2M | return(NULL); |
6143 | | /* |
6144 | | * Check if there is a default/fixed attribute declaration in |
6145 | | * the internal or external subset. |
6146 | | */ |
6147 | 2.82M | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
6148 | 29.9k | xmlDocPtr doc = node->doc; |
6149 | 29.9k | xmlAttributePtr attrDecl = NULL; |
6150 | 29.9k | xmlChar *elemQName, *tmpstr = NULL; |
6151 | | |
6152 | | /* |
6153 | | * We need the QName of the element for the DTD-lookup. |
6154 | | */ |
6155 | 29.9k | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
6156 | 25.0k | tmpstr = xmlStrdup(node->ns->prefix); |
6157 | 25.0k | if (tmpstr == NULL) |
6158 | 192 | return(NULL); |
6159 | 24.9k | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
6160 | 24.9k | if (tmpstr == NULL) |
6161 | 5 | return(NULL); |
6162 | 24.9k | tmpstr = xmlStrcat(tmpstr, node->name); |
6163 | 24.9k | if (tmpstr == NULL) |
6164 | 6 | return(NULL); |
6165 | 24.8k | elemQName = tmpstr; |
6166 | 24.8k | } else |
6167 | 4.80k | elemQName = (xmlChar *) node->name; |
6168 | 29.7k | if (nsName == NULL) { |
6169 | | /* |
6170 | | * The common and nice case: Attr in no namespace. |
6171 | | */ |
6172 | 22.9k | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
6173 | 22.9k | elemQName, name, NULL); |
6174 | 22.9k | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
6175 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
6176 | 0 | elemQName, name, NULL); |
6177 | 0 | } |
6178 | 22.9k | } else if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
6179 | | /* |
6180 | | * The XML namespace must be bound to prefix 'xml'. |
6181 | | */ |
6182 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
6183 | 0 | elemQName, name, BAD_CAST "xml"); |
6184 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
6185 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
6186 | 0 | elemQName, name, BAD_CAST "xml"); |
6187 | 0 | } |
6188 | 6.71k | } else { |
6189 | 6.71k | xmlNsPtr *nsList, *cur; |
6190 | | |
6191 | | /* |
6192 | | * The ugly case: Search using the prefixes of in-scope |
6193 | | * ns-decls corresponding to @nsName. |
6194 | | */ |
6195 | 6.71k | nsList = xmlGetNsList(node->doc, node); |
6196 | 6.71k | if (nsList == NULL) { |
6197 | 2.23k | if (tmpstr != NULL) |
6198 | 264 | xmlFree(tmpstr); |
6199 | 2.23k | return(NULL); |
6200 | 2.23k | } |
6201 | 4.47k | cur = nsList; |
6202 | 25.1k | while (*cur != NULL) { |
6203 | 20.6k | if (xmlStrEqual((*cur)->href, nsName)) { |
6204 | 4.28k | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
6205 | 4.28k | name, (*cur)->prefix); |
6206 | 4.28k | if (attrDecl) |
6207 | 0 | break; |
6208 | 4.28k | if (doc->extSubset != NULL) { |
6209 | 10 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
6210 | 10 | name, (*cur)->prefix); |
6211 | 10 | if (attrDecl) |
6212 | 0 | break; |
6213 | 10 | } |
6214 | 4.28k | } |
6215 | 20.6k | cur++; |
6216 | 20.6k | } |
6217 | 4.47k | xmlFree(nsList); |
6218 | 4.47k | } |
6219 | 27.4k | if (tmpstr != NULL) |
6220 | 24.6k | xmlFree(tmpstr); |
6221 | | /* |
6222 | | * Only default/fixed attrs are relevant. |
6223 | | */ |
6224 | 27.4k | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
6225 | 0 | return((xmlAttrPtr) attrDecl); |
6226 | 27.4k | } |
6227 | | |
6228 | 2.81M | return(NULL); |
6229 | 2.82M | } |
6230 | | |
6231 | | static xmlChar* |
6232 | | xmlGetPropNodeValueInternal(const xmlAttr *prop) |
6233 | 1.97M | { |
6234 | 1.97M | if (prop == NULL) |
6235 | 0 | return(NULL); |
6236 | 1.97M | if (prop->type == XML_ATTRIBUTE_NODE) { |
6237 | 1.97M | return(xmlNodeGetContent((xmlNodePtr) prop)); |
6238 | 1.97M | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
6239 | 0 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
6240 | 0 | } |
6241 | 0 | return(NULL); |
6242 | 1.97M | } |
6243 | | |
6244 | | /** |
6245 | | * Search for an attribute of an element. |
6246 | | * |
6247 | | * This function also looks in DTD attribute declaration for \#FIXED or |
6248 | | * default declaration values. |
6249 | | * |
6250 | | * @param node the element |
6251 | | * @param name the attribute name |
6252 | | * @returns the attribute or the attribute declaration or NULL if |
6253 | | * neither was found. Also returns NULL if a memory allocation failed, |
6254 | | * making this function unreliable. |
6255 | | */ |
6256 | | xmlAttr * |
6257 | 169k | xmlHasProp(const xmlNode *node, const xmlChar *name) { |
6258 | 169k | xmlAttrPtr prop; |
6259 | 169k | xmlDocPtr doc; |
6260 | | |
6261 | 169k | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
6262 | 0 | return(NULL); |
6263 | | /* |
6264 | | * Check on the properties attached to the node |
6265 | | */ |
6266 | 169k | prop = node->properties; |
6267 | 285k | while (prop != NULL) { |
6268 | 194k | if (xmlStrEqual(prop->name, name)) { |
6269 | 77.3k | return(prop); |
6270 | 77.3k | } |
6271 | 116k | prop = prop->next; |
6272 | 116k | } |
6273 | | |
6274 | | /* |
6275 | | * Check if there is a default declaration in the internal |
6276 | | * or external subsets |
6277 | | */ |
6278 | 91.8k | doc = node->doc; |
6279 | 91.8k | if (doc != NULL) { |
6280 | 91.8k | xmlAttributePtr attrDecl; |
6281 | 91.8k | if (doc->intSubset != NULL) { |
6282 | 1.07k | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
6283 | 1.07k | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
6284 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
6285 | 1.07k | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
6286 | | /* return attribute declaration only if a default value is given |
6287 | | (that includes #FIXED declarations) */ |
6288 | 0 | return((xmlAttrPtr) attrDecl); |
6289 | 1.07k | } |
6290 | 91.8k | } |
6291 | 91.8k | return(NULL); |
6292 | 91.8k | } |
6293 | | |
6294 | | /** |
6295 | | * Search for an attribute of an element. |
6296 | | * |
6297 | | * The attribute has to match the specified namespace. A namespace of |
6298 | | * NULL means that the attribute must have no namespace. |
6299 | | * |
6300 | | * This function also looks in DTD attribute declaration for \#FIXED or |
6301 | | * default declaration values. |
6302 | | * |
6303 | | * @param node the element |
6304 | | * @param name the attribute name |
6305 | | * @param nameSpace the URI of the namespace |
6306 | | * @returns the attribute or the attribute declaration or NULL if |
6307 | | * neither was found. Also returns NULL if a memory allocation failed |
6308 | | * making this function unreliable. |
6309 | | */ |
6310 | | xmlAttr * |
6311 | 0 | xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) { |
6312 | |
|
6313 | 0 | return(xmlGetPropNodeInternal(node, name, nameSpace, 1)); |
6314 | 0 | } |
6315 | | |
6316 | | /** |
6317 | | * Look up the value of an element's attribute. |
6318 | | * |
6319 | | * The attribute has to match the specified namespace. A namespace of |
6320 | | * NULL means that the attribute must have no namespace. |
6321 | | * |
6322 | | * Entities are substituted. The returned value must be freed by the |
6323 | | * caller. |
6324 | | * |
6325 | | * @since 2.13.0 |
6326 | | * |
6327 | | * @param node the element |
6328 | | * @param name the attribute name |
6329 | | * @param nsUri the URI of the namespace |
6330 | | * @param out the returned string |
6331 | | * @returns 0 on success, 1 if no attribute was found, -1 if a |
6332 | | * memory allocation failed. |
6333 | | */ |
6334 | | int |
6335 | | xmlNodeGetAttrValue(const xmlNode *node, const xmlChar *name, |
6336 | 20.9M | const xmlChar *nsUri, xmlChar **out) { |
6337 | 20.9M | xmlAttrPtr prop; |
6338 | | |
6339 | 20.9M | if (out == NULL) |
6340 | 0 | return(1); |
6341 | 20.9M | *out = NULL; |
6342 | | |
6343 | 20.9M | prop = xmlGetPropNodeInternal(node, name, nsUri, 0); |
6344 | 20.9M | if (prop == NULL) |
6345 | 20.8M | return(1); |
6346 | | |
6347 | 83.2k | *out = xmlGetPropNodeValueInternal(prop); |
6348 | 83.2k | if (*out == NULL) |
6349 | 22 | return(-1); |
6350 | 83.2k | return(0); |
6351 | 83.2k | } |
6352 | | |
6353 | | /** |
6354 | | * Look up the value of an element's attribute. |
6355 | | * |
6356 | | * Entities are substituted. The returned value must be freed by the |
6357 | | * caller. |
6358 | | * |
6359 | | * This function looks in DTD attribute declarations for \#FIXED or |
6360 | | * default declaration values. |
6361 | | * |
6362 | | * NOTE: This function is ignores namespaces. Use #xmlGetNsProp or |
6363 | | * #xmlGetNoNsProp for namespace aware processing. |
6364 | | * |
6365 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
6366 | | * missing attributes. |
6367 | | * |
6368 | | * @param node the element |
6369 | | * @param name the attribute name |
6370 | | * @returns the attribute value or NULL if not found or a memory allocation |
6371 | | * failed. |
6372 | | */ |
6373 | | xmlChar * |
6374 | 169k | xmlGetProp(const xmlNode *node, const xmlChar *name) { |
6375 | 169k | xmlAttrPtr prop; |
6376 | | |
6377 | 169k | prop = xmlHasProp(node, name); |
6378 | 169k | if (prop == NULL) |
6379 | 91.8k | return(NULL); |
6380 | 77.3k | return(xmlGetPropNodeValueInternal(prop)); |
6381 | 169k | } |
6382 | | |
6383 | | /** |
6384 | | * Look up the value of an element's attribute. |
6385 | | * |
6386 | | * Entities are substituted. The returned value must be freed by the |
6387 | | * caller. |
6388 | | * |
6389 | | * This function looks in DTD attribute declarations for \#FIXED or |
6390 | | * default declaration values. |
6391 | | * |
6392 | | * This function is similar to #xmlGetProp except it will accept only |
6393 | | * an attribute in no namespace. |
6394 | | * |
6395 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
6396 | | * missing attributes. It's more robust to use #xmlNodeGetAttrValue. |
6397 | | * |
6398 | | * @param node the element |
6399 | | * @param name the attribute name |
6400 | | * @returns the attribute value or NULL if not found or a memory allocation |
6401 | | * failed. |
6402 | | */ |
6403 | | xmlChar * |
6404 | 0 | xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) { |
6405 | 0 | xmlAttrPtr prop; |
6406 | |
|
6407 | 0 | prop = xmlGetPropNodeInternal(node, name, NULL, 1); |
6408 | 0 | if (prop == NULL) |
6409 | 0 | return(NULL); |
6410 | 0 | return(xmlGetPropNodeValueInternal(prop)); |
6411 | 0 | } |
6412 | | |
6413 | | /** |
6414 | | * Look up the value of an element's attribute. |
6415 | | * |
6416 | | * The attribute has to match the specified namespace. A namespace of |
6417 | | * NULL means that the attribute must have no namespace. |
6418 | | * |
6419 | | * Entities are substituted. The returned value must be freed by the |
6420 | | * caller. |
6421 | | * |
6422 | | * This function looks in DTD attribute declaration for \#FIXED or |
6423 | | * default declaration values. |
6424 | | * |
6425 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
6426 | | * missing attributes. It's more robust to use #xmlNodeGetAttrValue. |
6427 | | * |
6428 | | * @param node the element |
6429 | | * @param name the attribute name |
6430 | | * @param nameSpace the URI of the namespace |
6431 | | * @returns the attribute value or NULL if not found or a memory allocation |
6432 | | * failed. |
6433 | | */ |
6434 | | xmlChar * |
6435 | 4.63M | xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) { |
6436 | 4.63M | xmlAttrPtr prop; |
6437 | | |
6438 | 4.63M | prop = xmlGetPropNodeInternal(node, name, nameSpace, 1); |
6439 | 4.63M | if (prop == NULL) |
6440 | 2.82M | return(NULL); |
6441 | 1.81M | return(xmlGetPropNodeValueInternal(prop)); |
6442 | 4.63M | } |
6443 | | |
6444 | | /** |
6445 | | * Remove an attribute of an element. |
6446 | | * This handles only attributes in no namespace. |
6447 | | * |
6448 | | * @param node the element |
6449 | | * @param name the attribute name |
6450 | | * @returns 0 if successful, -1 if not found |
6451 | | */ |
6452 | | int |
6453 | 0 | xmlUnsetProp(xmlNode *node, const xmlChar *name) { |
6454 | 0 | xmlAttrPtr prop; |
6455 | |
|
6456 | 0 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
6457 | 0 | if (prop == NULL) |
6458 | 0 | return(-1); |
6459 | 0 | xmlUnlinkNodeInternal((xmlNodePtr) prop); |
6460 | 0 | xmlFreeProp(prop); |
6461 | 0 | return(0); |
6462 | 0 | } |
6463 | | |
6464 | | /** |
6465 | | * Remove an attribute of an element. |
6466 | | * |
6467 | | * @param node the element |
6468 | | * @param ns the namespace definition |
6469 | | * @param name the attribute name |
6470 | | * @returns 0 if successful, -1 if not found |
6471 | | */ |
6472 | | int |
6473 | 0 | xmlUnsetNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name) { |
6474 | 0 | xmlAttrPtr prop; |
6475 | |
|
6476 | 0 | prop = xmlGetPropNodeInternal(node, name, |
6477 | 0 | (ns != NULL) ? ns->href : NULL, 0); |
6478 | 0 | if (prop == NULL) |
6479 | 0 | return(-1); |
6480 | 0 | xmlUnlinkNodeInternal((xmlNodePtr) prop); |
6481 | 0 | xmlFreeProp(prop); |
6482 | 0 | return(0); |
6483 | 0 | } |
6484 | | |
6485 | | /** |
6486 | | * Set (or reset) an element's attribute. If `name` has a prefix, |
6487 | | * the corresponding namespace will be used. It is an error if |
6488 | | * there's no such binding for the prefix in scope. |
6489 | | * |
6490 | | * @param node the node |
6491 | | * @param name the attribute name (a QName) |
6492 | | * @param value the attribute value |
6493 | | * @returns the attribute pointer. |
6494 | | */ |
6495 | | xmlAttr * |
6496 | 0 | xmlSetProp(xmlNode *node, const xmlChar *name, const xmlChar *value) { |
6497 | 0 | xmlNsPtr ns = NULL; |
6498 | 0 | const xmlChar *localname; |
6499 | 0 | xmlChar *prefix; |
6500 | 0 | int res; |
6501 | |
|
6502 | 0 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
6503 | 0 | return(NULL); |
6504 | | |
6505 | | /* |
6506 | | * handle QNames |
6507 | | */ |
6508 | 0 | localname = xmlSplitQName4(name, &prefix); |
6509 | 0 | if (localname == NULL) |
6510 | 0 | return(NULL); |
6511 | | |
6512 | 0 | if (prefix != NULL) { |
6513 | 0 | res = xmlSearchNsSafe(node, prefix, &ns); |
6514 | 0 | xmlFree(prefix); |
6515 | 0 | if (res < 0) |
6516 | 0 | return(NULL); |
6517 | 0 | if (ns != NULL) |
6518 | 0 | return(xmlSetNsProp(node, ns, localname, value)); |
6519 | 0 | } |
6520 | | |
6521 | 0 | return(xmlSetNsProp(node, NULL, name, value)); |
6522 | 0 | } |
6523 | | |
6524 | | /** |
6525 | | * Set (or reset) an element's attribute. |
6526 | | * |
6527 | | * The namespace must be in scope. |
6528 | | * |
6529 | | * @param node the node |
6530 | | * @param ns the namespace definition |
6531 | | * @param name the attribute name |
6532 | | * @param value the attribute value |
6533 | | * @returns the attribute pointer. |
6534 | | */ |
6535 | | xmlAttr * |
6536 | | xmlSetNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name, |
6537 | | const xmlChar *value) |
6538 | 57.7k | { |
6539 | 57.7k | xmlAttrPtr prop; |
6540 | | |
6541 | 57.7k | if (ns && (ns->href == NULL)) |
6542 | 0 | return(NULL); |
6543 | 57.7k | if (name == NULL) |
6544 | 2 | return(NULL); |
6545 | 57.7k | prop = xmlGetPropNodeInternal(node, name, |
6546 | 57.7k | (ns != NULL) ? ns->href : NULL, 0); |
6547 | 57.7k | if (prop != NULL) { |
6548 | 34.0k | xmlNodePtr children = NULL; |
6549 | | |
6550 | | /* |
6551 | | * Modify the attribute's value. |
6552 | | */ |
6553 | 34.0k | if (value != NULL) { |
6554 | 30.8k | children = xmlNewDocText(node->doc, value); |
6555 | 30.8k | if (children == NULL) |
6556 | 479 | return(NULL); |
6557 | 30.8k | } |
6558 | | |
6559 | 33.6k | if (prop->atype == XML_ATTRIBUTE_ID) { |
6560 | 622 | xmlRemoveID(node->doc, prop); |
6561 | 622 | prop->atype = XML_ATTRIBUTE_ID; |
6562 | 622 | } |
6563 | 33.6k | if (prop->children != NULL) |
6564 | 32.9k | xmlFreeNodeList(prop->children); |
6565 | 33.6k | prop->children = NULL; |
6566 | 33.6k | prop->last = NULL; |
6567 | 33.6k | prop->ns = ns; |
6568 | 33.6k | if (value != NULL) { |
6569 | 30.3k | xmlNodePtr tmp; |
6570 | | |
6571 | 30.3k | prop->children = children; |
6572 | 30.3k | prop->last = NULL; |
6573 | 30.3k | tmp = prop->children; |
6574 | 60.7k | while (tmp != NULL) { |
6575 | 30.3k | tmp->parent = (xmlNodePtr) prop; |
6576 | 30.3k | if (tmp->next == NULL) |
6577 | 30.3k | prop->last = tmp; |
6578 | 30.3k | tmp = tmp->next; |
6579 | 30.3k | } |
6580 | 30.3k | } |
6581 | 33.6k | if ((prop->atype == XML_ATTRIBUTE_ID) && |
6582 | 33.6k | (xmlAddIDSafe(prop, value) < 0)) { |
6583 | 6 | return(NULL); |
6584 | 6 | } |
6585 | 33.6k | return(prop); |
6586 | 33.6k | } |
6587 | | /* |
6588 | | * No equal attr found; create a new one. |
6589 | | */ |
6590 | 23.6k | return(xmlNewPropInternal(node, ns, name, value, 0)); |
6591 | 57.7k | } |
6592 | | |
6593 | | /** |
6594 | | * Check whether the node is a text node. |
6595 | | * |
6596 | | * @param node the node |
6597 | | * @returns 1 if yes, 0 if no |
6598 | | */ |
6599 | | int |
6600 | 0 | xmlNodeIsText(const xmlNode *node) { |
6601 | 0 | if (node == NULL) return(0); |
6602 | | |
6603 | 0 | if (node->type == XML_TEXT_NODE) return(1); |
6604 | 0 | return(0); |
6605 | 0 | } |
6606 | | |
6607 | | /** |
6608 | | * Checks whether this node is an empty or whitespace-only |
6609 | | * text node. |
6610 | | * |
6611 | | * @param node the node |
6612 | | * @returns 1 if yes, 0 if no |
6613 | | */ |
6614 | | int |
6615 | 25 | xmlIsBlankNode(const xmlNode *node) { |
6616 | 25 | const xmlChar *cur; |
6617 | 25 | if (node == NULL) return(0); |
6618 | | |
6619 | 25 | if ((node->type != XML_TEXT_NODE) && |
6620 | 25 | (node->type != XML_CDATA_SECTION_NODE)) |
6621 | 0 | return(0); |
6622 | 25 | if (node->content == NULL) return(1); |
6623 | 25 | cur = node->content; |
6624 | 990 | while (*cur != 0) { |
6625 | 990 | if (!IS_BLANK_CH(*cur)) return(0); |
6626 | 965 | cur++; |
6627 | 965 | } |
6628 | | |
6629 | 0 | return(1); |
6630 | 25 | } |
6631 | | |
6632 | | /** |
6633 | | * Concat the given string at the end of the existing node content. |
6634 | | * |
6635 | | * If `len` is -1, the string length will be calculated. |
6636 | | * |
6637 | | * @param node the node |
6638 | | * @param content the content |
6639 | | * @param len `content` length |
6640 | | * @returns -1 in case of error, 0 otherwise |
6641 | | */ |
6642 | | |
6643 | | int |
6644 | 0 | xmlTextConcat(xmlNode *node, const xmlChar *content, int len) { |
6645 | 0 | if (node == NULL) |
6646 | 0 | return(-1); |
6647 | | |
6648 | 0 | if ((node->type != XML_TEXT_NODE) && |
6649 | 0 | (node->type != XML_CDATA_SECTION_NODE) && |
6650 | 0 | (node->type != XML_COMMENT_NODE) && |
6651 | 0 | (node->type != XML_PI_NODE)) |
6652 | 0 | return(-1); |
6653 | | |
6654 | 0 | return(xmlTextAddContent(node, content, len)); |
6655 | 0 | } |
6656 | | |
6657 | | /** |
6658 | | * Get the compression level of a document, ZLIB based. |
6659 | | * |
6660 | | * @param doc the document |
6661 | | * @returns 0 (uncompressed) to 9 (max compression) |
6662 | | */ |
6663 | | int |
6664 | 0 | xmlGetDocCompressMode (const xmlDoc *doc) { |
6665 | 0 | if (doc == NULL) return(-1); |
6666 | 0 | return(doc->compression); |
6667 | 0 | } |
6668 | | |
6669 | | /** |
6670 | | * Set the compression level of a document, ZLIB based. |
6671 | | * |
6672 | | * Correct values: 0 (uncompressed) to 9 (max compression) |
6673 | | * |
6674 | | * @param doc the document |
6675 | | * @param mode the compression ratio |
6676 | | */ |
6677 | | void |
6678 | 0 | xmlSetDocCompressMode (xmlDoc *doc, int mode) { |
6679 | 0 | if (doc == NULL) return; |
6680 | 0 | if (mode < 0) doc->compression = 0; |
6681 | 0 | else if (mode > 9) doc->compression = 9; |
6682 | 0 | else doc->compression = mode; |
6683 | 0 | } |
6684 | | |
6685 | | /** |
6686 | | * Get the global compression level, ZLIB based. |
6687 | | * |
6688 | | * @deprecated Use #xmlGetDocCompressMode |
6689 | | * |
6690 | | * @returns 0 (uncompressed) to 9 (max compression) |
6691 | | */ |
6692 | | int |
6693 | | xmlGetCompressMode(void) |
6694 | 0 | { |
6695 | 0 | return (xmlCompressMode); |
6696 | 0 | } |
6697 | | |
6698 | | /** |
6699 | | * Set the global compression level, ZLIB based. |
6700 | | * |
6701 | | * @deprecated Use #xmlSetDocCompressMode |
6702 | | * |
6703 | | * Correct values: 0 (uncompressed) to 9 (max compression) |
6704 | | * |
6705 | | * @param mode the compression ratio |
6706 | | */ |
6707 | | void |
6708 | 0 | xmlSetCompressMode(int mode) { |
6709 | 0 | if (mode < 0) xmlCompressMode = 0; |
6710 | 0 | else if (mode > 9) xmlCompressMode = 9; |
6711 | 0 | else xmlCompressMode = mode; |
6712 | 0 | } |
6713 | | |
6714 | 0 | #define XML_TREE_NSMAP_PARENT -1 |
6715 | | #define XML_TREE_NSMAP_XML -2 |
6716 | 0 | #define XML_TREE_NSMAP_DOC -3 |
6717 | 0 | #define XML_TREE_NSMAP_CUSTOM -4 |
6718 | | |
6719 | | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
6720 | | struct xmlNsMapItem { |
6721 | | xmlNsMapItemPtr next; |
6722 | | xmlNsMapItemPtr prev; |
6723 | | xmlNsPtr oldNs; /* old ns decl reference */ |
6724 | | xmlNsPtr newNs; /* new ns decl reference */ |
6725 | | int shadowDepth; /* Shadowed at this depth */ |
6726 | | /* |
6727 | | * depth: |
6728 | | * >= 0 == @node's ns-decls |
6729 | | * -1 == @parent's ns-decls |
6730 | | * -2 == the doc->oldNs XML ns-decl |
6731 | | * -3 == the doc->oldNs storage ns-decls |
6732 | | * -4 == ns-decls provided via custom ns-handling |
6733 | | */ |
6734 | | int depth; |
6735 | | }; |
6736 | | |
6737 | | typedef struct xmlNsMap *xmlNsMapPtr; |
6738 | | struct xmlNsMap { |
6739 | | xmlNsMapItemPtr first; |
6740 | | xmlNsMapItemPtr last; |
6741 | | xmlNsMapItemPtr pool; |
6742 | | }; |
6743 | | |
6744 | 0 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
6745 | 0 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
6746 | | #define XML_NSMAP_POP(m, i) \ |
6747 | 0 | i = (m)->last; \ |
6748 | 0 | (m)->last = (i)->prev; \ |
6749 | 0 | if ((m)->last == NULL) \ |
6750 | 0 | (m)->first = NULL; \ |
6751 | 0 | else \ |
6752 | 0 | (m)->last->next = NULL; \ |
6753 | 0 | (i)->next = (m)->pool; \ |
6754 | 0 | (m)->pool = i; |
6755 | | |
6756 | | /** |
6757 | | * Frees the ns-map |
6758 | | * |
6759 | | * @param nsmap the ns-map |
6760 | | */ |
6761 | | static void |
6762 | | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
6763 | 0 | { |
6764 | 0 | xmlNsMapItemPtr cur, tmp; |
6765 | |
|
6766 | 0 | if (nsmap == NULL) |
6767 | 0 | return; |
6768 | 0 | cur = nsmap->pool; |
6769 | 0 | while (cur != NULL) { |
6770 | 0 | tmp = cur; |
6771 | 0 | cur = cur->next; |
6772 | 0 | xmlFree(tmp); |
6773 | 0 | } |
6774 | 0 | cur = nsmap->first; |
6775 | 0 | while (cur != NULL) { |
6776 | 0 | tmp = cur; |
6777 | 0 | cur = cur->next; |
6778 | 0 | xmlFree(tmp); |
6779 | 0 | } |
6780 | 0 | xmlFree(nsmap); |
6781 | 0 | } |
6782 | | |
6783 | | /** |
6784 | | * Adds an ns-mapping item. |
6785 | | * |
6786 | | * @param nsmap the ns-map |
6787 | | * @param position position |
6788 | | * @param oldNs the old ns-struct |
6789 | | * @param newNs the new ns-struct |
6790 | | * @param depth depth and ns-kind information |
6791 | | * @returns the added item. |
6792 | | */ |
6793 | | static xmlNsMapItemPtr |
6794 | | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
6795 | | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
6796 | 0 | { |
6797 | 0 | xmlNsMapItemPtr ret; |
6798 | 0 | xmlNsMapPtr map; |
6799 | |
|
6800 | 0 | if (nsmap == NULL) |
6801 | 0 | return(NULL); |
6802 | 0 | if ((position != -1) && (position != 0)) |
6803 | 0 | return(NULL); |
6804 | 0 | map = *nsmap; |
6805 | |
|
6806 | 0 | if (map == NULL) { |
6807 | | /* |
6808 | | * Create the ns-map. |
6809 | | */ |
6810 | 0 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
6811 | 0 | if (map == NULL) |
6812 | 0 | return(NULL); |
6813 | 0 | memset(map, 0, sizeof(struct xmlNsMap)); |
6814 | 0 | *nsmap = map; |
6815 | 0 | } |
6816 | | |
6817 | 0 | if (map->pool != NULL) { |
6818 | | /* |
6819 | | * Reuse an item from the pool. |
6820 | | */ |
6821 | 0 | ret = map->pool; |
6822 | 0 | map->pool = ret->next; |
6823 | 0 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
6824 | 0 | } else { |
6825 | | /* |
6826 | | * Create a new item. |
6827 | | */ |
6828 | 0 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
6829 | 0 | if (ret == NULL) |
6830 | 0 | return(NULL); |
6831 | 0 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
6832 | 0 | } |
6833 | | |
6834 | 0 | if (map->first == NULL) { |
6835 | | /* |
6836 | | * First ever. |
6837 | | */ |
6838 | 0 | map->first = ret; |
6839 | 0 | map->last = ret; |
6840 | 0 | } else if (position == -1) { |
6841 | | /* |
6842 | | * Append. |
6843 | | */ |
6844 | 0 | ret->prev = map->last; |
6845 | 0 | map->last->next = ret; |
6846 | 0 | map->last = ret; |
6847 | 0 | } else if (position == 0) { |
6848 | | /* |
6849 | | * Set on first position. |
6850 | | */ |
6851 | 0 | map->first->prev = ret; |
6852 | 0 | ret->next = map->first; |
6853 | 0 | map->first = ret; |
6854 | 0 | } |
6855 | |
|
6856 | 0 | ret->oldNs = oldNs; |
6857 | 0 | ret->newNs = newNs; |
6858 | 0 | ret->shadowDepth = -1; |
6859 | 0 | ret->depth = depth; |
6860 | 0 | return (ret); |
6861 | 0 | } |
6862 | | |
6863 | | /** |
6864 | | * Creates or reuses an xmlNs struct on doc->oldNs with |
6865 | | * the given prefix and namespace name. |
6866 | | * |
6867 | | * @param doc the doc |
6868 | | * @param nsName the namespace name |
6869 | | * @param prefix the prefix |
6870 | | * @returns the acquired ns struct or NULL in case of an API |
6871 | | * or internal error. |
6872 | | */ |
6873 | | static xmlNsPtr |
6874 | | xmlDOMWrapStoreNs(xmlDocPtr doc, |
6875 | | const xmlChar *nsName, |
6876 | | const xmlChar *prefix) |
6877 | 0 | { |
6878 | 0 | xmlNsPtr ns; |
6879 | |
|
6880 | 0 | if (doc == NULL) |
6881 | 0 | return (NULL); |
6882 | 0 | ns = xmlTreeEnsureXMLDecl(doc); |
6883 | 0 | if (ns == NULL) |
6884 | 0 | return (NULL); |
6885 | 0 | if (ns->next != NULL) { |
6886 | | /* Reuse. */ |
6887 | 0 | ns = ns->next; |
6888 | 0 | while (ns != NULL) { |
6889 | 0 | if (((ns->prefix == prefix) || |
6890 | 0 | xmlStrEqual(ns->prefix, prefix)) && |
6891 | 0 | xmlStrEqual(ns->href, nsName)) { |
6892 | 0 | return (ns); |
6893 | 0 | } |
6894 | 0 | if (ns->next == NULL) |
6895 | 0 | break; |
6896 | 0 | ns = ns->next; |
6897 | 0 | } |
6898 | 0 | } |
6899 | | /* Create. */ |
6900 | 0 | if (ns != NULL) { |
6901 | 0 | ns->next = xmlNewNs(NULL, nsName, prefix); |
6902 | 0 | return (ns->next); |
6903 | 0 | } |
6904 | 0 | return(NULL); |
6905 | 0 | } |
6906 | | |
6907 | | /** |
6908 | | * Allocates and initializes a new DOM-wrapper context. |
6909 | | * |
6910 | | * @returns the xmlDOMWrapCtxt or NULL in case of an internal error. |
6911 | | */ |
6912 | | xmlDOMWrapCtxt * |
6913 | | xmlDOMWrapNewCtxt(void) |
6914 | 0 | { |
6915 | 0 | xmlDOMWrapCtxtPtr ret; |
6916 | |
|
6917 | 0 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
6918 | 0 | if (ret == NULL) |
6919 | 0 | return (NULL); |
6920 | 0 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
6921 | 0 | return (ret); |
6922 | 0 | } |
6923 | | |
6924 | | /** |
6925 | | * Frees the DOM-wrapper context. |
6926 | | * |
6927 | | * @param ctxt the DOM-wrapper context |
6928 | | */ |
6929 | | void |
6930 | | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxt *ctxt) |
6931 | 0 | { |
6932 | 0 | if (ctxt == NULL) |
6933 | 0 | return; |
6934 | 0 | if (ctxt->namespaceMap != NULL) |
6935 | 0 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
6936 | | /* |
6937 | | * TODO: Store the namespace map in the context. |
6938 | | */ |
6939 | 0 | xmlFree(ctxt); |
6940 | 0 | } |
6941 | | |
6942 | | /** |
6943 | | * Searches for a ns-decl with the given prefix in `nsList`. |
6944 | | * |
6945 | | * @param nsList a list of ns-structs |
6946 | | * @param prefix the searched prefix |
6947 | | * @returns the ns-decl if found, NULL if not found and on |
6948 | | * API errors. |
6949 | | */ |
6950 | | static xmlNsPtr |
6951 | | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
6952 | 0 | { |
6953 | 0 | if (nsList == NULL) |
6954 | 0 | return (NULL); |
6955 | 0 | { |
6956 | 0 | xmlNsPtr ns; |
6957 | 0 | ns = nsList; |
6958 | 0 | do { |
6959 | 0 | if ((prefix == ns->prefix) || |
6960 | 0 | xmlStrEqual(prefix, ns->prefix)) { |
6961 | 0 | return (ns); |
6962 | 0 | } |
6963 | 0 | ns = ns->next; |
6964 | 0 | } while (ns != NULL); |
6965 | 0 | } |
6966 | 0 | return (NULL); |
6967 | 0 | } |
6968 | | |
6969 | | /** |
6970 | | * Puts in-scope namespaces into the ns-map. |
6971 | | * |
6972 | | * @param map the namespace map |
6973 | | * @param node the node to start with |
6974 | | * @returns 0 on success, -1 on API or internal errors. |
6975 | | */ |
6976 | | static int |
6977 | | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
6978 | | xmlNodePtr node) |
6979 | 0 | { |
6980 | 0 | xmlNodePtr cur; |
6981 | 0 | xmlNsPtr ns; |
6982 | 0 | xmlNsMapItemPtr mi; |
6983 | 0 | int shadowed; |
6984 | |
|
6985 | 0 | if ((map == NULL) || (*map != NULL)) |
6986 | 0 | return (-1); |
6987 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
6988 | 0 | return (-1); |
6989 | | /* |
6990 | | * Get in-scope ns-decls of @parent. |
6991 | | */ |
6992 | 0 | cur = node; |
6993 | 0 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
6994 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
6995 | 0 | if (cur->nsDef != NULL) { |
6996 | 0 | ns = cur->nsDef; |
6997 | 0 | do { |
6998 | 0 | shadowed = 0; |
6999 | 0 | if (XML_NSMAP_NOTEMPTY(*map)) { |
7000 | | /* |
7001 | | * Skip shadowed prefixes. |
7002 | | */ |
7003 | 0 | XML_NSMAP_FOREACH(*map, mi) { |
7004 | 0 | if ((ns->prefix == mi->newNs->prefix) || |
7005 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
7006 | 0 | shadowed = 1; |
7007 | 0 | break; |
7008 | 0 | } |
7009 | 0 | } |
7010 | 0 | } |
7011 | | /* |
7012 | | * Insert mapping. |
7013 | | */ |
7014 | 0 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
7015 | 0 | ns, XML_TREE_NSMAP_PARENT); |
7016 | 0 | if (mi == NULL) |
7017 | 0 | return (-1); |
7018 | 0 | if (shadowed) |
7019 | 0 | mi->shadowDepth = 0; |
7020 | 0 | ns = ns->next; |
7021 | 0 | } while (ns != NULL); |
7022 | 0 | } |
7023 | 0 | } |
7024 | 0 | cur = cur->parent; |
7025 | 0 | } |
7026 | 0 | return (0); |
7027 | 0 | } |
7028 | | |
7029 | | /* |
7030 | | * For internal use. Adds a ns-decl mapping. |
7031 | | * |
7032 | | * Returns 0 on success, -1 on internal errors. |
7033 | | */ |
7034 | | static int |
7035 | | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
7036 | | xmlNsPtr oldNs, xmlNsPtr newNs) |
7037 | 0 | { |
7038 | 0 | if (*number >= *size) { |
7039 | 0 | xmlNsPtr *tmp; |
7040 | 0 | int newSize; |
7041 | |
|
7042 | 0 | newSize = xmlGrowCapacity(*size, 2 * sizeof(tmp[0]), 3, XML_MAX_ITEMS); |
7043 | 0 | if (newSize < 0) |
7044 | 0 | return(-1); |
7045 | 0 | tmp = xmlRealloc(*list, newSize * 2 * sizeof(tmp[0])); |
7046 | 0 | if (tmp == NULL) |
7047 | 0 | return(-1); |
7048 | 0 | *list = tmp; |
7049 | 0 | *size = newSize; |
7050 | 0 | } |
7051 | | |
7052 | 0 | (*list)[2 * (*number)] = oldNs; |
7053 | 0 | (*list)[2 * (*number) +1] = newNs; |
7054 | 0 | (*number)++; |
7055 | 0 | return (0); |
7056 | 0 | } |
7057 | | |
7058 | | /** |
7059 | | * Unlinks the given node from its owner. |
7060 | | * |
7061 | | * This will substitute ns-references to node->nsDef for |
7062 | | * ns-references to doc->oldNs, thus ensuring the removed |
7063 | | * branch to be autark wrt ns-references. |
7064 | | * |
7065 | | * NOTE: This function was not intensively tested. |
7066 | | * |
7067 | | * @param ctxt a DOM wrapper context |
7068 | | * @param doc the doc |
7069 | | * @param node the node to be removed. |
7070 | | * @param options set of options, unused at the moment |
7071 | | * @returns 0 on success, 1 if the node is not supported, |
7072 | | * -1 on API and internal errors. |
7073 | | */ |
7074 | | int |
7075 | | xmlDOMWrapRemoveNode(xmlDOMWrapCtxt *ctxt, xmlDoc *doc, |
7076 | | xmlNode *node, int options ATTRIBUTE_UNUSED) |
7077 | 0 | { |
7078 | 0 | xmlNsPtr *list = NULL; |
7079 | 0 | int sizeList = 0, nbList = 0, ret = 0, i, j; |
7080 | 0 | xmlNsPtr ns; |
7081 | |
|
7082 | 0 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
7083 | 0 | return (-1); |
7084 | | |
7085 | | /* TODO: 0 or -1 ? */ |
7086 | 0 | if (node->parent == NULL) |
7087 | 0 | return (0); |
7088 | | |
7089 | 0 | switch (node->type) { |
7090 | 0 | case XML_TEXT_NODE: |
7091 | 0 | case XML_CDATA_SECTION_NODE: |
7092 | 0 | case XML_ENTITY_REF_NODE: |
7093 | 0 | case XML_PI_NODE: |
7094 | 0 | case XML_COMMENT_NODE: |
7095 | 0 | xmlUnlinkNodeInternal(node); |
7096 | 0 | return (0); |
7097 | 0 | case XML_ELEMENT_NODE: |
7098 | 0 | case XML_ATTRIBUTE_NODE: |
7099 | 0 | break; |
7100 | 0 | default: |
7101 | 0 | return (1); |
7102 | 0 | } |
7103 | 0 | xmlUnlinkNodeInternal(node); |
7104 | | /* |
7105 | | * Save out-of-scope ns-references in doc->oldNs. |
7106 | | */ |
7107 | 0 | do { |
7108 | 0 | switch (node->type) { |
7109 | 0 | case XML_ELEMENT_NODE: |
7110 | 0 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
7111 | 0 | ns = node->nsDef; |
7112 | 0 | do { |
7113 | 0 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
7114 | 0 | &nbList, ns, ns) == -1) |
7115 | 0 | ret = -1; |
7116 | 0 | ns = ns->next; |
7117 | 0 | } while (ns != NULL); |
7118 | 0 | } |
7119 | | /* Falls through. */ |
7120 | 0 | case XML_ATTRIBUTE_NODE: |
7121 | 0 | if (node->ns != NULL) { |
7122 | | /* |
7123 | | * Find a mapping. |
7124 | | */ |
7125 | 0 | if (list != NULL) { |
7126 | 0 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
7127 | 0 | if (node->ns == list[j]) { |
7128 | 0 | node->ns = list[++j]; |
7129 | 0 | goto next_node; |
7130 | 0 | } |
7131 | 0 | } |
7132 | 0 | } |
7133 | 0 | ns = NULL; |
7134 | 0 | if (ctxt != NULL) { |
7135 | | /* |
7136 | | * User defined. |
7137 | | */ |
7138 | 0 | } else { |
7139 | | /* |
7140 | | * Add to doc's oldNs. |
7141 | | */ |
7142 | 0 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
7143 | 0 | node->ns->prefix); |
7144 | 0 | if (ns == NULL) |
7145 | 0 | ret = -1; |
7146 | 0 | } |
7147 | 0 | if (ns != NULL) { |
7148 | | /* |
7149 | | * Add mapping. |
7150 | | */ |
7151 | 0 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
7152 | 0 | &nbList, node->ns, ns) == -1) |
7153 | 0 | ret = -1; |
7154 | 0 | } |
7155 | 0 | node->ns = ns; |
7156 | 0 | } |
7157 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
7158 | 0 | (node->properties != NULL)) { |
7159 | 0 | node = (xmlNodePtr) node->properties; |
7160 | 0 | continue; |
7161 | 0 | } |
7162 | 0 | break; |
7163 | 0 | default: |
7164 | 0 | goto next_sibling; |
7165 | 0 | } |
7166 | 0 | next_node: |
7167 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
7168 | 0 | (node->children != NULL)) { |
7169 | 0 | node = node->children; |
7170 | 0 | continue; |
7171 | 0 | } |
7172 | 0 | next_sibling: |
7173 | 0 | if (node == NULL) |
7174 | 0 | break; |
7175 | 0 | if (node->next != NULL) |
7176 | 0 | node = node->next; |
7177 | 0 | else { |
7178 | 0 | int type = node->type; |
7179 | |
|
7180 | 0 | node = node->parent; |
7181 | 0 | if ((type == XML_ATTRIBUTE_NODE) && |
7182 | 0 | (node != NULL) && |
7183 | 0 | (node->children != NULL)) { |
7184 | 0 | node = node->children; |
7185 | 0 | } else { |
7186 | 0 | goto next_sibling; |
7187 | 0 | } |
7188 | 0 | } |
7189 | 0 | } while (node != NULL); |
7190 | | |
7191 | 0 | if (list != NULL) |
7192 | 0 | xmlFree(list); |
7193 | 0 | return (ret); |
7194 | 0 | } |
7195 | | |
7196 | | /** |
7197 | | * Dynamically searches for a ns-declaration which matches |
7198 | | * the given `nsName` in the ancestor-or-self axis of `node`. |
7199 | | * |
7200 | | * @param doc the document |
7201 | | * @param node the start node |
7202 | | * @param nsName the searched namespace name |
7203 | | * @param retNs the resulting ns-decl |
7204 | | * @param prefixed if the found ns-decl must have a prefix |
7205 | | * (for attributes) |
7206 | | * @returns 1 if a ns-decl was found, 0 if not and -1 on API |
7207 | | * and internal errors. |
7208 | | */ |
7209 | | static int |
7210 | | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
7211 | | const xmlChar* nsName, |
7212 | | xmlNsPtr *retNs, int prefixed) |
7213 | 0 | { |
7214 | 0 | xmlNodePtr cur, prev = NULL, out = NULL; |
7215 | 0 | xmlNsPtr ns, prevns; |
7216 | |
|
7217 | 0 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
7218 | 0 | return (-1); |
7219 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
7220 | 0 | return(-1); |
7221 | | |
7222 | 0 | *retNs = NULL; |
7223 | 0 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
7224 | 0 | *retNs = xmlTreeEnsureXMLDecl(doc); |
7225 | 0 | if (*retNs == NULL) |
7226 | 0 | return (-1); |
7227 | 0 | return (1); |
7228 | 0 | } |
7229 | 0 | cur = node; |
7230 | 0 | do { |
7231 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
7232 | 0 | if (cur->nsDef != NULL) { |
7233 | 0 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
7234 | 0 | if (prefixed && (ns->prefix == NULL)) |
7235 | 0 | continue; |
7236 | 0 | if (prev != NULL) { |
7237 | | /* |
7238 | | * Check the last level of ns-decls for a |
7239 | | * shadowing prefix. |
7240 | | */ |
7241 | 0 | prevns = prev->nsDef; |
7242 | 0 | do { |
7243 | 0 | if ((prevns->prefix == ns->prefix) || |
7244 | 0 | ((prevns->prefix != NULL) && |
7245 | 0 | (ns->prefix != NULL) && |
7246 | 0 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
7247 | | /* |
7248 | | * Shadowed. |
7249 | | */ |
7250 | 0 | break; |
7251 | 0 | } |
7252 | 0 | prevns = prevns->next; |
7253 | 0 | } while (prevns != NULL); |
7254 | 0 | if (prevns != NULL) |
7255 | 0 | continue; |
7256 | 0 | } |
7257 | | /* |
7258 | | * Ns-name comparison. |
7259 | | */ |
7260 | 0 | if ((nsName == ns->href) || |
7261 | 0 | xmlStrEqual(nsName, ns->href)) { |
7262 | | /* |
7263 | | * At this point the prefix can only be shadowed, |
7264 | | * if we are the the (at least) 3rd level of |
7265 | | * ns-decls. |
7266 | | */ |
7267 | 0 | if (out) { |
7268 | 0 | int ret; |
7269 | |
|
7270 | 0 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
7271 | 0 | if (ret < 0) |
7272 | 0 | return (-1); |
7273 | | /* |
7274 | | * TODO: Should we try to find a matching ns-name |
7275 | | * only once? This here keeps on searching. |
7276 | | * I think we should try further since, there might |
7277 | | * be an other matching ns-decl with an unshadowed |
7278 | | * prefix. |
7279 | | */ |
7280 | 0 | if (! ret) |
7281 | 0 | continue; |
7282 | 0 | } |
7283 | 0 | *retNs = ns; |
7284 | 0 | return (1); |
7285 | 0 | } |
7286 | 0 | } |
7287 | 0 | out = prev; |
7288 | 0 | prev = cur; |
7289 | 0 | } |
7290 | 0 | } else if (cur->type == XML_ENTITY_DECL) |
7291 | 0 | return (0); |
7292 | 0 | cur = cur->parent; |
7293 | 0 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
7294 | 0 | return (0); |
7295 | 0 | } |
7296 | | |
7297 | | /** |
7298 | | * Dynamically searches for a ns-declaration which matches |
7299 | | * the given `nsName` in the ancestor-or-self axis of `node`. |
7300 | | * |
7301 | | * @param doc the document |
7302 | | * @param node the start node |
7303 | | * @param prefix the searched namespace prefix |
7304 | | * @param retNs the resulting ns-decl |
7305 | | * @returns 1 if a ns-decl was found, 0 if not and -1 on API |
7306 | | * and internal errors. |
7307 | | */ |
7308 | | static int |
7309 | | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
7310 | | const xmlChar* prefix, |
7311 | | xmlNsPtr *retNs) |
7312 | 0 | { |
7313 | 0 | xmlNodePtr cur; |
7314 | 0 | xmlNsPtr ns; |
7315 | |
|
7316 | 0 | if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
7317 | 0 | return(-1); |
7318 | | |
7319 | 0 | if (retNs) |
7320 | 0 | *retNs = NULL; |
7321 | 0 | if (IS_STR_XML(prefix)) { |
7322 | 0 | if (retNs) { |
7323 | 0 | *retNs = xmlTreeEnsureXMLDecl(doc); |
7324 | 0 | if (*retNs == NULL) |
7325 | 0 | return (-1); |
7326 | 0 | } |
7327 | 0 | return (1); |
7328 | 0 | } |
7329 | 0 | cur = node; |
7330 | 0 | do { |
7331 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
7332 | 0 | if (cur->nsDef != NULL) { |
7333 | 0 | ns = cur->nsDef; |
7334 | 0 | do { |
7335 | 0 | if ((prefix == ns->prefix) || |
7336 | 0 | xmlStrEqual(prefix, ns->prefix)) |
7337 | 0 | { |
7338 | | /* |
7339 | | * Disabled namespaces, e.g. xmlns:abc="". |
7340 | | */ |
7341 | 0 | if (ns->href == NULL) |
7342 | 0 | return(0); |
7343 | 0 | if (retNs) |
7344 | 0 | *retNs = ns; |
7345 | 0 | return (1); |
7346 | 0 | } |
7347 | 0 | ns = ns->next; |
7348 | 0 | } while (ns != NULL); |
7349 | 0 | } |
7350 | 0 | } else if (cur->type == XML_ENTITY_DECL) |
7351 | 0 | return (0); |
7352 | 0 | cur = cur->parent; |
7353 | 0 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
7354 | 0 | return (0); |
7355 | 0 | } |
7356 | | |
7357 | | /** |
7358 | | * Declares a new namespace on `elem`. It tries to use the |
7359 | | * given `prefix`. If a ns-decl with the given prefix is already existent |
7360 | | * on `elem`, it will generate an other prefix. |
7361 | | * |
7362 | | * @param doc the doc |
7363 | | * @param elem the element-node to declare on |
7364 | | * @param nsName the namespace-name of the ns-decl |
7365 | | * @param prefix the preferred prefix of the ns-decl |
7366 | | * @param checkShadow ensure that the new ns-decl doesn't shadow |
7367 | | * ancestor ns-decls |
7368 | | * @returns 1 if a ns-decl was found, 0 if not and -1 on API |
7369 | | * and internal errors. |
7370 | | */ |
7371 | | static xmlNsPtr |
7372 | | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
7373 | | xmlNodePtr elem, |
7374 | | const xmlChar *nsName, |
7375 | | const xmlChar *prefix, |
7376 | | int checkShadow) |
7377 | 0 | { |
7378 | |
|
7379 | 0 | xmlNsPtr ret; |
7380 | 0 | char buf[50]; |
7381 | 0 | const xmlChar *pref; |
7382 | 0 | int counter = 0; |
7383 | |
|
7384 | 0 | if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE)) |
7385 | 0 | return(NULL); |
7386 | | /* |
7387 | | * Create a ns-decl on @anchor. |
7388 | | */ |
7389 | 0 | pref = prefix; |
7390 | 0 | while (1) { |
7391 | | /* |
7392 | | * Lookup whether the prefix is unused in elem's ns-decls. |
7393 | | */ |
7394 | 0 | if ((elem->nsDef != NULL) && |
7395 | 0 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
7396 | 0 | goto ns_next_prefix; |
7397 | 0 | if (checkShadow && elem->parent && |
7398 | 0 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
7399 | | /* |
7400 | | * Does it shadow ancestor ns-decls? |
7401 | | */ |
7402 | 0 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
7403 | 0 | goto ns_next_prefix; |
7404 | 0 | } |
7405 | 0 | ret = xmlNewNs(NULL, nsName, pref); |
7406 | 0 | if (ret == NULL) |
7407 | 0 | return (NULL); |
7408 | 0 | if (elem->nsDef == NULL) |
7409 | 0 | elem->nsDef = ret; |
7410 | 0 | else { |
7411 | 0 | xmlNsPtr ns2 = elem->nsDef; |
7412 | 0 | while (ns2->next != NULL) |
7413 | 0 | ns2 = ns2->next; |
7414 | 0 | ns2->next = ret; |
7415 | 0 | } |
7416 | 0 | return (ret); |
7417 | 0 | ns_next_prefix: |
7418 | 0 | counter++; |
7419 | 0 | if (counter > 1000) |
7420 | 0 | return (NULL); |
7421 | 0 | if (prefix == NULL) { |
7422 | 0 | snprintf((char *) buf, sizeof(buf), |
7423 | 0 | "ns_%d", counter); |
7424 | 0 | } else |
7425 | 0 | snprintf((char *) buf, sizeof(buf), |
7426 | 0 | "%.30s_%d", (char *)prefix, counter); |
7427 | 0 | pref = BAD_CAST buf; |
7428 | 0 | } |
7429 | 0 | } |
7430 | | |
7431 | | /** |
7432 | | * Searches for a matching ns-name in the ns-decls of `nsMap`, if not |
7433 | | * found it will either declare it on `elem`, or store it in `doc->oldNs`. |
7434 | | * If a new ns-decl needs to be declared on `elem`, it tries to use the |
7435 | | * `ns->prefix` for it, if this prefix is already in use on `elem`, it will |
7436 | | * change the prefix or the new ns-decl. |
7437 | | * |
7438 | | * @param doc the doc |
7439 | | * @param elem the element-node to declare namespaces on |
7440 | | * @param ns the ns-struct to use for the search |
7441 | | * @param retNs the found/created ns-struct |
7442 | | * @param nsMap the ns-map |
7443 | | * @param depth the current tree depth |
7444 | | * @param ancestorsOnly search in ancestor ns-decls only |
7445 | | * @param prefixed if the searched ns-decl must have a prefix |
7446 | | * (for attributes) |
7447 | | * @returns 0 if succeeded, -1 otherwise and on API/internal errors. |
7448 | | */ |
7449 | | static int |
7450 | | xmlDOMWrapNSNormAcquireNormalizedNs(xmlDocPtr doc, |
7451 | | xmlNodePtr elem, |
7452 | | xmlNsPtr ns, |
7453 | | xmlNsPtr *retNs, |
7454 | | xmlNsMapPtr *nsMap, |
7455 | | |
7456 | | int depth, |
7457 | | int ancestorsOnly, |
7458 | | int prefixed) |
7459 | 0 | { |
7460 | 0 | xmlNsMapItemPtr mi; |
7461 | |
|
7462 | 0 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
7463 | 0 | (nsMap == NULL)) |
7464 | 0 | return (-1); |
7465 | | |
7466 | 0 | *retNs = NULL; |
7467 | | /* |
7468 | | * Handle XML namespace. |
7469 | | */ |
7470 | 0 | if (IS_STR_XML(ns->prefix)) { |
7471 | | /* |
7472 | | * Insert XML namespace mapping. |
7473 | | */ |
7474 | 0 | *retNs = xmlTreeEnsureXMLDecl(doc); |
7475 | 0 | if (*retNs == NULL) |
7476 | 0 | return (-1); |
7477 | 0 | return (0); |
7478 | 0 | } |
7479 | | /* |
7480 | | * If the search should be done in ancestors only and no |
7481 | | * @elem (the first ancestor) was specified, then skip the search. |
7482 | | */ |
7483 | 0 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
7484 | 0 | (! (ancestorsOnly && (elem == NULL)))) |
7485 | 0 | { |
7486 | | /* |
7487 | | * Try to find an equal ns-name in in-scope ns-decls. |
7488 | | */ |
7489 | 0 | XML_NSMAP_FOREACH(*nsMap, mi) { |
7490 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
7491 | | /* |
7492 | | * ancestorsOnly: This should be turned on to gain speed, |
7493 | | * if one knows that the branch itself was already |
7494 | | * ns-wellformed and no stale references existed. |
7495 | | * I.e. it searches in the ancestor axis only. |
7496 | | */ |
7497 | 0 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
7498 | | /* Skip shadowed prefixes. */ |
7499 | 0 | (mi->shadowDepth == -1) && |
7500 | | /* Skip xmlns="" or xmlns:foo="". */ |
7501 | 0 | ((mi->newNs->href != NULL) && |
7502 | 0 | (mi->newNs->href[0] != 0)) && |
7503 | | /* Ensure a prefix if wanted. */ |
7504 | 0 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
7505 | | /* Equal ns name */ |
7506 | 0 | ((mi->newNs->href == ns->href) || |
7507 | 0 | xmlStrEqual(mi->newNs->href, ns->href))) { |
7508 | | /* Set the mapping. */ |
7509 | 0 | mi->oldNs = ns; |
7510 | 0 | *retNs = mi->newNs; |
7511 | 0 | return (0); |
7512 | 0 | } |
7513 | 0 | } |
7514 | 0 | } |
7515 | | /* |
7516 | | * No luck, the namespace is out of scope or shadowed. |
7517 | | */ |
7518 | 0 | if (elem == NULL) { |
7519 | 0 | xmlNsPtr tmpns; |
7520 | | |
7521 | | /* |
7522 | | * Store ns-decls in "oldNs" of the document-node. |
7523 | | */ |
7524 | 0 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
7525 | 0 | if (tmpns == NULL) |
7526 | 0 | return (-1); |
7527 | | /* |
7528 | | * Insert mapping. |
7529 | | */ |
7530 | 0 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
7531 | 0 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
7532 | 0 | return (-1); |
7533 | 0 | } |
7534 | 0 | *retNs = tmpns; |
7535 | 0 | } else { |
7536 | 0 | xmlNsPtr tmpns; |
7537 | |
|
7538 | 0 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
7539 | 0 | ns->prefix, 0); |
7540 | 0 | if (tmpns == NULL) |
7541 | 0 | return (-1); |
7542 | | |
7543 | 0 | if (*nsMap != NULL) { |
7544 | | /* |
7545 | | * Does it shadow ancestor ns-decls? |
7546 | | */ |
7547 | 0 | XML_NSMAP_FOREACH(*nsMap, mi) { |
7548 | 0 | if ((mi->depth < depth) && |
7549 | 0 | (mi->shadowDepth == -1) && |
7550 | 0 | ((ns->prefix == mi->newNs->prefix) || |
7551 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
7552 | | /* |
7553 | | * Shadows. |
7554 | | */ |
7555 | 0 | mi->shadowDepth = depth; |
7556 | 0 | break; |
7557 | 0 | } |
7558 | 0 | } |
7559 | 0 | } |
7560 | 0 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
7561 | 0 | return (-1); |
7562 | 0 | } |
7563 | 0 | *retNs = tmpns; |
7564 | 0 | } |
7565 | 0 | return (0); |
7566 | 0 | } |
7567 | | |
7568 | | typedef enum { |
7569 | | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
7570 | | } xmlDOMReconcileNSOptions; |
7571 | | |
7572 | | /** |
7573 | | * Fix up namespaces. |
7574 | | * |
7575 | | * Ensures that ns-references point to ns-decls hold on element-nodes. |
7576 | | * Ensures that the tree is namespace wellformed by creating additional |
7577 | | * ns-decls where needed. Note that, since prefixes of already existent |
7578 | | * ns-decls can be shadowed by this process, it could break QNames in |
7579 | | * attribute values or element content. |
7580 | | * |
7581 | | * NOTE: This function was not intensively tested. |
7582 | | * |
7583 | | * @param ctxt DOM wrapper context, unused at the moment |
7584 | | * @param elem the element-node |
7585 | | * @param options option flags |
7586 | | * @returns 0 if succeeded, -1 otherwise and on API/internal errors. |
7587 | | */ |
7588 | | int |
7589 | | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxt *ctxt ATTRIBUTE_UNUSED, |
7590 | | xmlNode *elem, |
7591 | | int options) |
7592 | 0 | { |
7593 | 0 | int depth = -1, adoptns = 0, parnsdone = 0; |
7594 | 0 | xmlNsPtr ns, prevns; |
7595 | 0 | xmlDocPtr doc; |
7596 | 0 | xmlNodePtr cur, curElem = NULL; |
7597 | 0 | xmlNsMapPtr nsMap = NULL; |
7598 | 0 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
7599 | | /* @ancestorsOnly should be set by an option flag. */ |
7600 | 0 | int ancestorsOnly = 0; |
7601 | 0 | int optRemoveRedundantNS = |
7602 | 0 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
7603 | 0 | xmlNsPtr *listRedund = NULL; |
7604 | 0 | int sizeRedund = 0, nbRedund = 0, ret = 0, i, j; |
7605 | |
|
7606 | 0 | if ((elem == NULL) || (elem->doc == NULL) || |
7607 | 0 | (elem->type != XML_ELEMENT_NODE)) |
7608 | 0 | return (-1); |
7609 | | |
7610 | 0 | doc = elem->doc; |
7611 | 0 | cur = elem; |
7612 | 0 | do { |
7613 | 0 | switch (cur->type) { |
7614 | 0 | case XML_ELEMENT_NODE: |
7615 | 0 | adoptns = 1; |
7616 | 0 | curElem = cur; |
7617 | 0 | depth++; |
7618 | | /* |
7619 | | * Namespace declarations. |
7620 | | */ |
7621 | 0 | if (cur->nsDef != NULL) { |
7622 | 0 | prevns = NULL; |
7623 | 0 | ns = cur->nsDef; |
7624 | 0 | while (ns != NULL) { |
7625 | 0 | if (! parnsdone) { |
7626 | 0 | if ((elem->parent) && |
7627 | 0 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
7628 | | /* |
7629 | | * Gather ancestor in-scope ns-decls. |
7630 | | */ |
7631 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
7632 | 0 | elem->parent) == -1) |
7633 | 0 | ret = -1; |
7634 | 0 | } |
7635 | 0 | parnsdone = 1; |
7636 | 0 | } |
7637 | | |
7638 | | /* |
7639 | | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
7640 | | */ |
7641 | 0 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
7642 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7643 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
7644 | 0 | (mi->shadowDepth == -1) && |
7645 | 0 | ((ns->prefix == mi->newNs->prefix) || |
7646 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
7647 | 0 | ((ns->href == mi->newNs->href) || |
7648 | 0 | xmlStrEqual(ns->href, mi->newNs->href))) |
7649 | 0 | { |
7650 | | /* |
7651 | | * A redundant ns-decl was found. |
7652 | | * Add it to the list of redundant ns-decls. |
7653 | | */ |
7654 | 0 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
7655 | 0 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) { |
7656 | 0 | ret = -1; |
7657 | 0 | } else { |
7658 | | /* |
7659 | | * Remove the ns-decl from the element-node. |
7660 | | */ |
7661 | 0 | if (prevns) |
7662 | 0 | prevns->next = ns->next; |
7663 | 0 | else |
7664 | 0 | cur->nsDef = ns->next; |
7665 | 0 | goto next_ns_decl; |
7666 | 0 | } |
7667 | 0 | } |
7668 | 0 | } |
7669 | 0 | } |
7670 | | |
7671 | | /* |
7672 | | * Skip ns-references handling if the referenced |
7673 | | * ns-decl is declared on the same element. |
7674 | | */ |
7675 | 0 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
7676 | 0 | adoptns = 0; |
7677 | | /* |
7678 | | * Does it shadow any ns-decl? |
7679 | | */ |
7680 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
7681 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7682 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
7683 | 0 | (mi->shadowDepth == -1) && |
7684 | 0 | ((ns->prefix == mi->newNs->prefix) || |
7685 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
7686 | |
|
7687 | 0 | mi->shadowDepth = depth; |
7688 | 0 | } |
7689 | 0 | } |
7690 | 0 | } |
7691 | | /* |
7692 | | * Push mapping. |
7693 | | */ |
7694 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
7695 | 0 | depth) == NULL) |
7696 | 0 | ret = -1; |
7697 | |
|
7698 | 0 | prevns = ns; |
7699 | 0 | next_ns_decl: |
7700 | 0 | ns = ns->next; |
7701 | 0 | } |
7702 | 0 | } |
7703 | 0 | if (! adoptns) |
7704 | 0 | goto ns_end; |
7705 | | /* Falls through. */ |
7706 | 0 | case XML_ATTRIBUTE_NODE: |
7707 | | /* No ns, no fun. */ |
7708 | 0 | if (cur->ns == NULL) |
7709 | 0 | goto ns_end; |
7710 | | |
7711 | 0 | if (! parnsdone) { |
7712 | 0 | if ((elem->parent) && |
7713 | 0 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
7714 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
7715 | 0 | elem->parent) == -1) |
7716 | 0 | ret = -1; |
7717 | 0 | } |
7718 | 0 | parnsdone = 1; |
7719 | 0 | } |
7720 | | /* |
7721 | | * Adjust the reference if this was a redundant ns-decl. |
7722 | | */ |
7723 | 0 | if (listRedund) { |
7724 | 0 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
7725 | 0 | if (cur->ns == listRedund[j]) { |
7726 | 0 | cur->ns = listRedund[++j]; |
7727 | 0 | break; |
7728 | 0 | } |
7729 | 0 | } |
7730 | 0 | } |
7731 | | /* |
7732 | | * Adopt ns-references. |
7733 | | */ |
7734 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
7735 | | /* |
7736 | | * Search for a mapping. |
7737 | | */ |
7738 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7739 | 0 | if ((mi->shadowDepth == -1) && |
7740 | 0 | (cur->ns == mi->oldNs)) { |
7741 | |
|
7742 | 0 | cur->ns = mi->newNs; |
7743 | 0 | goto ns_end; |
7744 | 0 | } |
7745 | 0 | } |
7746 | 0 | } |
7747 | | /* |
7748 | | * Acquire a normalized ns-decl and add it to the map. |
7749 | | */ |
7750 | 0 | if (xmlDOMWrapNSNormAcquireNormalizedNs(doc, curElem, |
7751 | 0 | cur->ns, &ns, |
7752 | 0 | &nsMap, depth, |
7753 | 0 | ancestorsOnly, |
7754 | 0 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
7755 | 0 | ret = -1; |
7756 | 0 | cur->ns = ns; |
7757 | |
|
7758 | 0 | ns_end: |
7759 | 0 | if ((cur->type == XML_ELEMENT_NODE) && |
7760 | 0 | (cur->properties != NULL)) { |
7761 | | /* |
7762 | | * Process attributes. |
7763 | | */ |
7764 | 0 | cur = (xmlNodePtr) cur->properties; |
7765 | 0 | continue; |
7766 | 0 | } |
7767 | 0 | break; |
7768 | 0 | default: |
7769 | 0 | goto next_sibling; |
7770 | 0 | } |
7771 | 0 | into_content: |
7772 | 0 | if ((cur->type == XML_ELEMENT_NODE) && |
7773 | 0 | (cur->children != NULL)) { |
7774 | | /* |
7775 | | * Process content of element-nodes only. |
7776 | | */ |
7777 | 0 | cur = cur->children; |
7778 | 0 | continue; |
7779 | 0 | } |
7780 | 0 | next_sibling: |
7781 | 0 | if (cur == elem) |
7782 | 0 | break; |
7783 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
7784 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
7785 | | /* |
7786 | | * Pop mappings. |
7787 | | */ |
7788 | 0 | while ((nsMap->last != NULL) && |
7789 | 0 | (nsMap->last->depth >= depth)) |
7790 | 0 | { |
7791 | 0 | XML_NSMAP_POP(nsMap, mi) |
7792 | 0 | } |
7793 | | /* |
7794 | | * Unshadow. |
7795 | | */ |
7796 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7797 | 0 | if (mi->shadowDepth >= depth) |
7798 | 0 | mi->shadowDepth = -1; |
7799 | 0 | } |
7800 | 0 | } |
7801 | 0 | depth--; |
7802 | 0 | } |
7803 | 0 | if (cur->next != NULL) |
7804 | 0 | cur = cur->next; |
7805 | 0 | else { |
7806 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) { |
7807 | 0 | cur = cur->parent; |
7808 | 0 | goto into_content; |
7809 | 0 | } |
7810 | 0 | cur = cur->parent; |
7811 | 0 | goto next_sibling; |
7812 | 0 | } |
7813 | 0 | } while (cur != NULL); |
7814 | | |
7815 | 0 | if (listRedund) { |
7816 | 0 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
7817 | 0 | xmlFreeNs(listRedund[j]); |
7818 | 0 | } |
7819 | 0 | xmlFree(listRedund); |
7820 | 0 | } |
7821 | 0 | if (nsMap != NULL) |
7822 | 0 | xmlDOMWrapNsMapFree(nsMap); |
7823 | 0 | return (ret); |
7824 | 0 | } |
7825 | | |
7826 | | /** |
7827 | | * Ensures that ns-references point to `destDoc`: either to |
7828 | | * `elements->nsDef` entries if `destParent` is given, or to |
7829 | | * `destDoc->oldNs` otherwise. |
7830 | | * |
7831 | | * If `destParent` is given, it ensures that the tree is namespace |
7832 | | * wellformed by creating additional ns-decls where needed. |
7833 | | * Note that, since prefixes of already existent ns-decls can be |
7834 | | * shadowed by this process, it could break QNames in attribute |
7835 | | * values or element content. |
7836 | | * |
7837 | | * NOTE: This function was not intensively tested. |
7838 | | * |
7839 | | * @param ctxt the optional context for custom processing |
7840 | | * @param sourceDoc the optional sourceDoc |
7841 | | * @param node the element-node to start with |
7842 | | * @param destDoc the destination doc for adoption |
7843 | | * @param destParent the optional new parent of `node` in `destDoc` |
7844 | | * @param options option flags |
7845 | | * @returns 0 if succeeded, -1 otherwise and on API/internal errors. |
7846 | | */ |
7847 | | static int |
7848 | | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
7849 | | xmlDocPtr sourceDoc ATTRIBUTE_UNUSED, |
7850 | | xmlNodePtr node, |
7851 | | xmlDocPtr destDoc, |
7852 | | xmlNodePtr destParent, |
7853 | | int options ATTRIBUTE_UNUSED) |
7854 | 0 | { |
7855 | 0 | int ret = 0; |
7856 | 0 | xmlNodePtr cur, curElem = NULL; |
7857 | 0 | xmlNsMapPtr nsMap = NULL; |
7858 | 0 | xmlNsMapItemPtr mi; |
7859 | 0 | xmlNsPtr ns = NULL; |
7860 | 0 | int depth = -1; |
7861 | | /* gather @parent's ns-decls. */ |
7862 | 0 | int parnsdone; |
7863 | | /* @ancestorsOnly should be set per option. */ |
7864 | 0 | int ancestorsOnly = 0; |
7865 | | |
7866 | | /* |
7867 | | * Get the ns-map from the context if available. |
7868 | | */ |
7869 | 0 | if (ctxt) |
7870 | 0 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
7871 | | /* |
7872 | | * Disable search for ns-decls in the parent-axis of the |
7873 | | * destination element, if: |
7874 | | * 1) there's no destination parent |
7875 | | * 2) custom ns-reference handling is used |
7876 | | */ |
7877 | 0 | if ((destParent == NULL) || |
7878 | 0 | (ctxt && ctxt->getNsForNodeFunc)) |
7879 | 0 | { |
7880 | 0 | parnsdone = 1; |
7881 | 0 | } else |
7882 | 0 | parnsdone = 0; |
7883 | |
|
7884 | 0 | cur = node; |
7885 | |
|
7886 | 0 | while (cur != NULL) { |
7887 | 0 | if (cur->doc != destDoc) { |
7888 | 0 | if (xmlNodeSetDoc(cur, destDoc) < 0) |
7889 | 0 | ret = -1; |
7890 | 0 | } |
7891 | |
|
7892 | 0 | switch (cur->type) { |
7893 | 0 | case XML_XINCLUDE_START: |
7894 | 0 | case XML_XINCLUDE_END: |
7895 | | /* |
7896 | | * TODO |
7897 | | */ |
7898 | 0 | ret = -1; |
7899 | 0 | goto leave_node; |
7900 | 0 | case XML_ELEMENT_NODE: |
7901 | 0 | curElem = cur; |
7902 | 0 | depth++; |
7903 | | /* |
7904 | | * Namespace declarations. |
7905 | | * - ns->href and ns->prefix are never in the dict, so |
7906 | | * we need not move the values over to the destination dict. |
7907 | | * - Note that for custom handling of ns-references, |
7908 | | * the ns-decls need not be stored in the ns-map, |
7909 | | * since they won't be referenced by node->ns. |
7910 | | */ |
7911 | 0 | if ((cur->nsDef) && |
7912 | 0 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
7913 | 0 | { |
7914 | 0 | if (! parnsdone) { |
7915 | | /* |
7916 | | * Gather @parent's in-scope ns-decls. |
7917 | | */ |
7918 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
7919 | 0 | destParent) == -1) |
7920 | 0 | ret = -1; |
7921 | 0 | parnsdone = 1; |
7922 | 0 | } |
7923 | 0 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
7924 | | /* |
7925 | | * NOTE: ns->prefix and ns->href are never in the dict. |
7926 | | */ |
7927 | | /* |
7928 | | * Does it shadow any ns-decl? |
7929 | | */ |
7930 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
7931 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7932 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
7933 | 0 | (mi->shadowDepth == -1) && |
7934 | 0 | ((ns->prefix == mi->newNs->prefix) || |
7935 | 0 | xmlStrEqual(ns->prefix, |
7936 | 0 | mi->newNs->prefix))) { |
7937 | |
|
7938 | 0 | mi->shadowDepth = depth; |
7939 | 0 | } |
7940 | 0 | } |
7941 | 0 | } |
7942 | | /* |
7943 | | * Push mapping. |
7944 | | */ |
7945 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
7946 | 0 | ns, ns, depth) == NULL) |
7947 | 0 | ret = -1; |
7948 | 0 | } |
7949 | 0 | } |
7950 | | /* Falls through. */ |
7951 | 0 | case XML_ATTRIBUTE_NODE: |
7952 | | /* No namespace, no fun. */ |
7953 | 0 | if (cur->ns == NULL) |
7954 | 0 | goto ns_end; |
7955 | | |
7956 | 0 | if (! parnsdone) { |
7957 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
7958 | 0 | destParent) == -1) |
7959 | 0 | ret = -1; |
7960 | 0 | parnsdone = 1; |
7961 | 0 | } |
7962 | | /* |
7963 | | * Adopt ns-references. |
7964 | | */ |
7965 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
7966 | | /* |
7967 | | * Search for a mapping. |
7968 | | */ |
7969 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7970 | 0 | if ((mi->shadowDepth == -1) && |
7971 | 0 | (cur->ns == mi->oldNs)) { |
7972 | |
|
7973 | 0 | cur->ns = mi->newNs; |
7974 | 0 | goto ns_end; |
7975 | 0 | } |
7976 | 0 | } |
7977 | 0 | } |
7978 | | /* |
7979 | | * No matching namespace in scope. We need a new one. |
7980 | | */ |
7981 | 0 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
7982 | | /* |
7983 | | * User-defined behaviour. |
7984 | | */ |
7985 | 0 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
7986 | 0 | cur->ns->href, cur->ns->prefix); |
7987 | | /* |
7988 | | * Insert mapping if ns is available; it's the users fault |
7989 | | * if not. |
7990 | | */ |
7991 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
7992 | 0 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
7993 | 0 | ret = -1; |
7994 | 0 | cur->ns = ns; |
7995 | 0 | } else { |
7996 | | /* |
7997 | | * Acquire a normalized ns-decl and add it to the map. |
7998 | | */ |
7999 | 0 | if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc, |
8000 | | /* ns-decls on curElem or on destDoc->oldNs */ |
8001 | 0 | destParent ? curElem : NULL, |
8002 | 0 | cur->ns, &ns, |
8003 | 0 | &nsMap, depth, |
8004 | 0 | ancestorsOnly, |
8005 | | /* ns-decls must be prefixed for attributes. */ |
8006 | 0 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
8007 | 0 | ret = -1; |
8008 | 0 | cur->ns = ns; |
8009 | 0 | } |
8010 | |
|
8011 | 0 | ns_end: |
8012 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
8013 | 0 | cur->psvi = NULL; |
8014 | 0 | cur->line = 0; |
8015 | 0 | cur->extra = 0; |
8016 | | /* |
8017 | | * Walk attributes. |
8018 | | */ |
8019 | 0 | if (cur->properties != NULL) { |
8020 | | /* |
8021 | | * Process first attribute node. |
8022 | | */ |
8023 | 0 | cur = (xmlNodePtr) cur->properties; |
8024 | 0 | continue; |
8025 | 0 | } |
8026 | 0 | } |
8027 | 0 | break; |
8028 | 0 | case XML_TEXT_NODE: |
8029 | 0 | case XML_CDATA_SECTION_NODE: |
8030 | 0 | case XML_PI_NODE: |
8031 | 0 | case XML_COMMENT_NODE: |
8032 | 0 | case XML_ENTITY_REF_NODE: |
8033 | 0 | goto leave_node; |
8034 | 0 | default: |
8035 | 0 | ret = -1; |
8036 | 0 | } |
8037 | | /* |
8038 | | * Walk the tree. |
8039 | | */ |
8040 | 0 | if (cur->children != NULL) { |
8041 | 0 | cur = cur->children; |
8042 | 0 | continue; |
8043 | 0 | } |
8044 | | |
8045 | 0 | leave_node: |
8046 | 0 | if (cur == node) |
8047 | 0 | break; |
8048 | 0 | if ((cur->type == XML_ELEMENT_NODE) || |
8049 | 0 | (cur->type == XML_XINCLUDE_START) || |
8050 | 0 | (cur->type == XML_XINCLUDE_END)) |
8051 | 0 | { |
8052 | | /* |
8053 | | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
8054 | | */ |
8055 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8056 | | /* |
8057 | | * Pop mappings. |
8058 | | */ |
8059 | 0 | while ((nsMap->last != NULL) && |
8060 | 0 | (nsMap->last->depth >= depth)) |
8061 | 0 | { |
8062 | 0 | XML_NSMAP_POP(nsMap, mi) |
8063 | 0 | } |
8064 | | /* |
8065 | | * Unshadow. |
8066 | | */ |
8067 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8068 | 0 | if (mi->shadowDepth >= depth) |
8069 | 0 | mi->shadowDepth = -1; |
8070 | 0 | } |
8071 | 0 | } |
8072 | 0 | depth--; |
8073 | 0 | } |
8074 | 0 | if (cur->next != NULL) |
8075 | 0 | cur = cur->next; |
8076 | 0 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
8077 | 0 | (cur->parent->children != NULL)) |
8078 | 0 | { |
8079 | 0 | cur = cur->parent->children; |
8080 | 0 | } else { |
8081 | 0 | cur = cur->parent; |
8082 | 0 | goto leave_node; |
8083 | 0 | } |
8084 | 0 | } |
8085 | | |
8086 | | /* |
8087 | | * Cleanup. |
8088 | | */ |
8089 | 0 | if (nsMap != NULL) { |
8090 | 0 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
8091 | | /* |
8092 | | * Just cleanup the map but don't free. |
8093 | | */ |
8094 | 0 | if (nsMap->first) { |
8095 | 0 | if (nsMap->pool) |
8096 | 0 | nsMap->last->next = nsMap->pool; |
8097 | 0 | nsMap->pool = nsMap->first; |
8098 | 0 | nsMap->first = NULL; |
8099 | 0 | } |
8100 | 0 | } else |
8101 | 0 | xmlDOMWrapNsMapFree(nsMap); |
8102 | 0 | } |
8103 | 0 | return(ret); |
8104 | 0 | } |
8105 | | |
8106 | | /** |
8107 | | * Clone a node and fix namespaces. |
8108 | | * |
8109 | | * References of out-of scope ns-decls are remapped to point to `destDoc`. |
8110 | | * If `destParent` is given, then nsDef entries on element-nodes are used. |
8111 | | * If *no* `destParent` is given, then `destDoc->oldNs` entries are used. |
8112 | | * This is the case when you don't know already where the cloned branch |
8113 | | * will be added to. |
8114 | | * |
8115 | | * If `destParent` is given, it ensures that the tree is namespace |
8116 | | * wellformed by creating additional ns-decls where needed. |
8117 | | * Note that, since prefixes of already existent ns-decls can be |
8118 | | * shadowed by this process, it could break QNames in attribute |
8119 | | * values or element content. |
8120 | | * |
8121 | | * @param ctxt the optional context for custom processing |
8122 | | * @param sourceDoc the optional sourceDoc |
8123 | | * @param node the node to start with |
8124 | | * @param resNode the clone of the given `node` |
8125 | | * @param destDoc the destination doc |
8126 | | * @param destParent the optional new parent of `node` in `destDoc` |
8127 | | * @param deep descend into child if set |
8128 | | * @param options option flags |
8129 | | * @returns 0 if the operation succeeded, |
8130 | | * 1 if a node of unsupported (or not yet supported) type was given, |
8131 | | * -1 on API/internal errors. |
8132 | | */ |
8133 | | int |
8134 | | xmlDOMWrapCloneNode(xmlDOMWrapCtxt *ctxt, |
8135 | | xmlDoc *sourceDoc, |
8136 | | xmlNode *node, |
8137 | | xmlNode **resNode, |
8138 | | xmlDoc *destDoc, |
8139 | | xmlNode *destParent, |
8140 | | int deep, |
8141 | | int options ATTRIBUTE_UNUSED) |
8142 | 0 | { |
8143 | 0 | int ret = 0; |
8144 | 0 | xmlNodePtr cur, cloneElem = NULL; |
8145 | 0 | xmlNsMapPtr nsMap = NULL; |
8146 | 0 | xmlNsMapItemPtr mi; |
8147 | 0 | xmlNsPtr ns; |
8148 | 0 | int depth = -1; |
8149 | | /* int adoptStr = 1; */ |
8150 | | /* gather @parent's ns-decls. */ |
8151 | 0 | int parnsdone = 0; |
8152 | | /* |
8153 | | * @ancestorsOnly: |
8154 | | * TODO: @ancestorsOnly should be set per option. |
8155 | | * |
8156 | | */ |
8157 | 0 | int ancestorsOnly = 0; |
8158 | 0 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
8159 | 0 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
8160 | 0 | xmlDictPtr dict; /* The destination dict */ |
8161 | |
|
8162 | 0 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL) || |
8163 | 0 | ((destParent != NULL) && (destParent->doc != destDoc))) |
8164 | 0 | return(-1); |
8165 | | /* |
8166 | | * TODO: Initially we support only element-nodes. |
8167 | | */ |
8168 | 0 | if (node->type != XML_ELEMENT_NODE) |
8169 | 0 | return(1); |
8170 | | /* |
8171 | | * Check node->doc sanity. |
8172 | | */ |
8173 | 0 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
8174 | 0 | (node->doc != sourceDoc)) { |
8175 | | /* |
8176 | | * Might be an XIncluded node. |
8177 | | */ |
8178 | 0 | return (-1); |
8179 | 0 | } |
8180 | 0 | if (sourceDoc == NULL) |
8181 | 0 | sourceDoc = node->doc; |
8182 | 0 | if (sourceDoc == NULL) |
8183 | 0 | return (-1); |
8184 | | |
8185 | 0 | dict = destDoc->dict; |
8186 | | /* |
8187 | | * Reuse the namespace map of the context. |
8188 | | */ |
8189 | 0 | if (ctxt) |
8190 | 0 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
8191 | |
|
8192 | 0 | *resNode = NULL; |
8193 | |
|
8194 | 0 | cur = node; |
8195 | 0 | while (cur != NULL) { |
8196 | 0 | if (cur->doc != sourceDoc) { |
8197 | | /* |
8198 | | * We'll assume XIncluded nodes if the doc differs. |
8199 | | * TODO: Do we need to reconciliate XIncluded nodes? |
8200 | | * TODO: This here returns -1 in this case. |
8201 | | */ |
8202 | 0 | goto internal_error; |
8203 | 0 | } |
8204 | | /* |
8205 | | * Create a new node. |
8206 | | */ |
8207 | 0 | switch (cur->type) { |
8208 | 0 | case XML_XINCLUDE_START: |
8209 | 0 | case XML_XINCLUDE_END: |
8210 | | /* |
8211 | | * TODO: What to do with XInclude? |
8212 | | */ |
8213 | 0 | goto internal_error; |
8214 | 0 | break; |
8215 | 0 | case XML_ELEMENT_NODE: |
8216 | 0 | case XML_TEXT_NODE: |
8217 | 0 | case XML_CDATA_SECTION_NODE: |
8218 | 0 | case XML_COMMENT_NODE: |
8219 | 0 | case XML_PI_NODE: |
8220 | 0 | case XML_DOCUMENT_FRAG_NODE: |
8221 | 0 | case XML_ENTITY_REF_NODE: |
8222 | | /* |
8223 | | * Nodes of xmlNode structure. |
8224 | | */ |
8225 | 0 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
8226 | 0 | if (clone == NULL) |
8227 | 0 | goto internal_error; |
8228 | 0 | memset(clone, 0, sizeof(xmlNode)); |
8229 | | /* |
8230 | | * Set hierarchical links. |
8231 | | */ |
8232 | 0 | if (resultClone != NULL) { |
8233 | 0 | clone->parent = parentClone; |
8234 | 0 | if (prevClone) { |
8235 | 0 | prevClone->next = clone; |
8236 | 0 | clone->prev = prevClone; |
8237 | 0 | } else |
8238 | 0 | parentClone->children = clone; |
8239 | 0 | parentClone->last = clone; |
8240 | 0 | } else |
8241 | 0 | resultClone = clone; |
8242 | |
|
8243 | 0 | break; |
8244 | 0 | case XML_ATTRIBUTE_NODE: |
8245 | | /* |
8246 | | * Attributes (xmlAttr). |
8247 | | */ |
8248 | 0 | clone = xmlMalloc(sizeof(xmlAttr)); |
8249 | 0 | if (clone == NULL) |
8250 | 0 | goto internal_error; |
8251 | 0 | memset(clone, 0, sizeof(xmlAttr)); |
8252 | | /* |
8253 | | * Set hierarchical links. |
8254 | | * TODO: Change this to add to the end of attributes. |
8255 | | */ |
8256 | 0 | if (resultClone != NULL) { |
8257 | 0 | clone->parent = parentClone; |
8258 | 0 | if (prevClone) { |
8259 | 0 | prevClone->next = clone; |
8260 | 0 | clone->prev = prevClone; |
8261 | 0 | } else |
8262 | 0 | parentClone->properties = (xmlAttrPtr) clone; |
8263 | 0 | } else |
8264 | 0 | resultClone = clone; |
8265 | 0 | break; |
8266 | 0 | default: |
8267 | | /* |
8268 | | * TODO QUESTION: Any other nodes expected? |
8269 | | */ |
8270 | 0 | goto internal_error; |
8271 | 0 | } |
8272 | | |
8273 | 0 | clone->type = cur->type; |
8274 | 0 | clone->doc = destDoc; |
8275 | | |
8276 | | /* |
8277 | | * Clone the name of the node if any. |
8278 | | */ |
8279 | 0 | if (cur->name == xmlStringText) |
8280 | 0 | clone->name = xmlStringText; |
8281 | 0 | else if (cur->name == xmlStringTextNoenc) |
8282 | | /* |
8283 | | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
8284 | | * in tree.c, it might be set in Libxslt via |
8285 | | * "xsl:disable-output-escaping". |
8286 | | */ |
8287 | 0 | clone->name = xmlStringTextNoenc; |
8288 | 0 | else if (cur->name == xmlStringComment) |
8289 | 0 | clone->name = xmlStringComment; |
8290 | 0 | else if (cur->name != NULL) { |
8291 | 0 | if (dict != NULL) |
8292 | 0 | clone->name = xmlDictLookup(dict, cur->name, -1); |
8293 | 0 | else |
8294 | 0 | clone->name = xmlStrdup(cur->name); |
8295 | 0 | if (clone->name == NULL) |
8296 | 0 | goto internal_error; |
8297 | 0 | } |
8298 | | |
8299 | 0 | switch (cur->type) { |
8300 | 0 | case XML_XINCLUDE_START: |
8301 | 0 | case XML_XINCLUDE_END: |
8302 | | /* |
8303 | | * TODO |
8304 | | */ |
8305 | 0 | return (-1); |
8306 | 0 | case XML_ELEMENT_NODE: |
8307 | 0 | cloneElem = clone; |
8308 | 0 | depth++; |
8309 | | /* |
8310 | | * Namespace declarations. |
8311 | | */ |
8312 | 0 | if (cur->nsDef != NULL) { |
8313 | 0 | if (! parnsdone) { |
8314 | 0 | if (destParent && (ctxt == NULL)) { |
8315 | | /* |
8316 | | * Gather @parent's in-scope ns-decls. |
8317 | | */ |
8318 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
8319 | 0 | destParent) == -1) |
8320 | 0 | goto internal_error; |
8321 | 0 | } |
8322 | 0 | parnsdone = 1; |
8323 | 0 | } |
8324 | | /* |
8325 | | * Clone namespace declarations. |
8326 | | */ |
8327 | 0 | cloneNsDefSlot = &(clone->nsDef); |
8328 | 0 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
8329 | | /* |
8330 | | * Create a new xmlNs. |
8331 | | */ |
8332 | 0 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
8333 | 0 | if (cloneNs == NULL) |
8334 | 0 | goto internal_error; |
8335 | 0 | memset(cloneNs, 0, sizeof(xmlNs)); |
8336 | 0 | cloneNs->type = XML_LOCAL_NAMESPACE; |
8337 | |
|
8338 | 0 | if (ns->href != NULL) { |
8339 | 0 | cloneNs->href = xmlStrdup(ns->href); |
8340 | 0 | if (cloneNs->href == NULL) { |
8341 | 0 | xmlFreeNs(cloneNs); |
8342 | 0 | goto internal_error; |
8343 | 0 | } |
8344 | 0 | } |
8345 | 0 | if (ns->prefix != NULL) { |
8346 | 0 | cloneNs->prefix = xmlStrdup(ns->prefix); |
8347 | 0 | if (cloneNs->prefix == NULL) { |
8348 | 0 | xmlFreeNs(cloneNs); |
8349 | 0 | goto internal_error; |
8350 | 0 | } |
8351 | 0 | } |
8352 | | |
8353 | 0 | *cloneNsDefSlot = cloneNs; |
8354 | 0 | cloneNsDefSlot = &(cloneNs->next); |
8355 | | |
8356 | | /* |
8357 | | * Note that for custom handling of ns-references, |
8358 | | * the ns-decls need not be stored in the ns-map, |
8359 | | * since they won't be referenced by node->ns. |
8360 | | */ |
8361 | 0 | if ((ctxt == NULL) || |
8362 | 0 | (ctxt->getNsForNodeFunc == NULL)) |
8363 | 0 | { |
8364 | | /* |
8365 | | * Does it shadow any ns-decl? |
8366 | | */ |
8367 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8368 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8369 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
8370 | 0 | (mi->shadowDepth == -1) && |
8371 | 0 | ((ns->prefix == mi->newNs->prefix) || |
8372 | 0 | xmlStrEqual(ns->prefix, |
8373 | 0 | mi->newNs->prefix))) { |
8374 | | /* |
8375 | | * Mark as shadowed at the current |
8376 | | * depth. |
8377 | | */ |
8378 | 0 | mi->shadowDepth = depth; |
8379 | 0 | } |
8380 | 0 | } |
8381 | 0 | } |
8382 | | /* |
8383 | | * Push mapping. |
8384 | | */ |
8385 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
8386 | 0 | ns, cloneNs, depth) == NULL) |
8387 | 0 | goto internal_error; |
8388 | 0 | } |
8389 | 0 | } |
8390 | 0 | } |
8391 | | /* cur->ns will be processed further down. */ |
8392 | 0 | break; |
8393 | 0 | case XML_ATTRIBUTE_NODE: |
8394 | | /* IDs will be processed further down. */ |
8395 | | /* cur->ns will be processed further down. */ |
8396 | 0 | break; |
8397 | 0 | case XML_PI_NODE: |
8398 | 0 | case XML_COMMENT_NODE: |
8399 | 0 | case XML_TEXT_NODE: |
8400 | 0 | case XML_CDATA_SECTION_NODE: |
8401 | | /* |
8402 | | * Note that this will also cover the values of attributes. |
8403 | | */ |
8404 | 0 | if (cur->content != NULL) { |
8405 | 0 | clone->content = xmlStrdup(cur->content); |
8406 | 0 | if (clone->content == NULL) |
8407 | 0 | goto internal_error; |
8408 | 0 | } |
8409 | 0 | goto leave_node; |
8410 | 0 | case XML_ENTITY_REF_NODE: |
8411 | 0 | if (sourceDoc != destDoc) { |
8412 | 0 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
8413 | 0 | xmlEntityPtr ent; |
8414 | | /* |
8415 | | * Different doc: Assign new entity-node if available. |
8416 | | */ |
8417 | 0 | ent = xmlGetDocEntity(destDoc, cur->name); |
8418 | 0 | if (ent != NULL) { |
8419 | 0 | clone->content = ent->content; |
8420 | 0 | clone->children = (xmlNodePtr) ent; |
8421 | 0 | clone->last = (xmlNodePtr) ent; |
8422 | 0 | } |
8423 | 0 | } |
8424 | 0 | } else { |
8425 | | /* |
8426 | | * Same doc: Use the current node's entity declaration |
8427 | | * and value. |
8428 | | */ |
8429 | 0 | clone->content = cur->content; |
8430 | 0 | clone->children = cur->children; |
8431 | 0 | clone->last = cur->last; |
8432 | 0 | } |
8433 | 0 | goto leave_node; |
8434 | 0 | default: |
8435 | 0 | goto internal_error; |
8436 | 0 | } |
8437 | | |
8438 | 0 | if (cur->ns == NULL) |
8439 | 0 | goto end_ns_reference; |
8440 | | |
8441 | | /* handle_ns_reference: */ |
8442 | | /* |
8443 | | ** The following will take care of references to ns-decls ******** |
8444 | | ** and is intended only for element- and attribute-nodes. |
8445 | | ** |
8446 | | */ |
8447 | 0 | if (! parnsdone) { |
8448 | 0 | if (destParent && (ctxt == NULL)) { |
8449 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
8450 | 0 | goto internal_error; |
8451 | 0 | } |
8452 | 0 | parnsdone = 1; |
8453 | 0 | } |
8454 | | /* |
8455 | | * Adopt ns-references. |
8456 | | */ |
8457 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8458 | | /* |
8459 | | * Search for a mapping. |
8460 | | */ |
8461 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8462 | 0 | if ((mi->shadowDepth == -1) && |
8463 | 0 | (cur->ns == mi->oldNs)) { |
8464 | | /* |
8465 | | * This is the nice case: a mapping was found. |
8466 | | */ |
8467 | 0 | clone->ns = mi->newNs; |
8468 | 0 | goto end_ns_reference; |
8469 | 0 | } |
8470 | 0 | } |
8471 | 0 | } |
8472 | | /* |
8473 | | * No matching namespace in scope. We need a new one. |
8474 | | */ |
8475 | 0 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
8476 | | /* |
8477 | | * User-defined behaviour. |
8478 | | */ |
8479 | 0 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
8480 | 0 | cur->ns->href, cur->ns->prefix); |
8481 | | /* |
8482 | | * Add user's mapping. |
8483 | | */ |
8484 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
8485 | 0 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
8486 | 0 | goto internal_error; |
8487 | 0 | clone->ns = ns; |
8488 | 0 | } else { |
8489 | | /* |
8490 | | * Acquire a normalized ns-decl and add it to the map. |
8491 | | */ |
8492 | 0 | if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc, |
8493 | | /* ns-decls on cloneElem or on destDoc->oldNs */ |
8494 | 0 | destParent ? cloneElem : NULL, |
8495 | 0 | cur->ns, &ns, |
8496 | 0 | &nsMap, depth, |
8497 | | /* if we need to search only in the ancestor-axis */ |
8498 | 0 | ancestorsOnly, |
8499 | | /* ns-decls must be prefixed for attributes. */ |
8500 | 0 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
8501 | 0 | goto internal_error; |
8502 | 0 | clone->ns = ns; |
8503 | 0 | } |
8504 | | |
8505 | 0 | end_ns_reference: |
8506 | | |
8507 | | /* |
8508 | | * Some post-processing. |
8509 | | * |
8510 | | * Handle ID attributes. |
8511 | | */ |
8512 | 0 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
8513 | 0 | (clone->parent != NULL)) |
8514 | 0 | { |
8515 | 0 | int res; |
8516 | |
|
8517 | 0 | res = xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone); |
8518 | 0 | if (res < 0) |
8519 | 0 | goto internal_error; |
8520 | 0 | if (res == 1) { |
8521 | 0 | xmlChar *idVal; |
8522 | |
|
8523 | 0 | idVal = xmlNodeGetContent(cur); |
8524 | 0 | if (idVal == NULL) |
8525 | 0 | goto internal_error; |
8526 | 0 | if (xmlAddIDSafe((xmlAttrPtr) cur, idVal) < 0) { |
8527 | 0 | xmlFree(idVal); |
8528 | 0 | goto internal_error; |
8529 | 0 | } |
8530 | 0 | xmlFree(idVal); |
8531 | 0 | } |
8532 | 0 | } |
8533 | | /* |
8534 | | ** |
8535 | | ** The following will traverse the tree ************************** |
8536 | | ** |
8537 | | * |
8538 | | * Walk the element's attributes before descending into child-nodes. |
8539 | | */ |
8540 | 0 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
8541 | 0 | prevClone = NULL; |
8542 | 0 | parentClone = clone; |
8543 | 0 | cur = (xmlNodePtr) cur->properties; |
8544 | 0 | continue; |
8545 | 0 | } |
8546 | 0 | into_content: |
8547 | | /* |
8548 | | * Descend into child-nodes. |
8549 | | */ |
8550 | 0 | if (cur->children != NULL) { |
8551 | 0 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
8552 | 0 | prevClone = NULL; |
8553 | 0 | parentClone = clone; |
8554 | 0 | cur = cur->children; |
8555 | 0 | continue; |
8556 | 0 | } |
8557 | 0 | } |
8558 | | |
8559 | 0 | leave_node: |
8560 | | /* |
8561 | | * At this point we are done with the node, its content |
8562 | | * and an element-nodes's attribute-nodes. |
8563 | | */ |
8564 | 0 | if (cur == node) |
8565 | 0 | break; |
8566 | 0 | if ((cur->type == XML_ELEMENT_NODE) || |
8567 | 0 | (cur->type == XML_XINCLUDE_START) || |
8568 | 0 | (cur->type == XML_XINCLUDE_END)) { |
8569 | | /* |
8570 | | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
8571 | | */ |
8572 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8573 | | /* |
8574 | | * Pop mappings. |
8575 | | */ |
8576 | 0 | while ((nsMap->last != NULL) && |
8577 | 0 | (nsMap->last->depth >= depth)) |
8578 | 0 | { |
8579 | 0 | XML_NSMAP_POP(nsMap, mi) |
8580 | 0 | } |
8581 | | /* |
8582 | | * Unshadow. |
8583 | | */ |
8584 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8585 | 0 | if (mi->shadowDepth >= depth) |
8586 | 0 | mi->shadowDepth = -1; |
8587 | 0 | } |
8588 | 0 | } |
8589 | 0 | depth--; |
8590 | 0 | } |
8591 | 0 | if (cur->next != NULL) { |
8592 | 0 | prevClone = clone; |
8593 | 0 | cur = cur->next; |
8594 | 0 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
8595 | 0 | clone = clone->parent; |
8596 | 0 | if (clone != NULL) |
8597 | 0 | parentClone = clone->parent; |
8598 | | /* |
8599 | | * Process parent --> next; |
8600 | | */ |
8601 | 0 | cur = cur->parent; |
8602 | 0 | goto leave_node; |
8603 | 0 | } else { |
8604 | | /* This is for attributes only. */ |
8605 | 0 | clone = clone->parent; |
8606 | 0 | parentClone = clone->parent; |
8607 | | /* |
8608 | | * Process parent-element --> children. |
8609 | | */ |
8610 | 0 | cur = cur->parent; |
8611 | 0 | goto into_content; |
8612 | 0 | } |
8613 | 0 | } |
8614 | 0 | goto exit; |
8615 | | |
8616 | 0 | internal_error: |
8617 | 0 | ret = -1; |
8618 | |
|
8619 | 0 | exit: |
8620 | | /* |
8621 | | * Cleanup. |
8622 | | */ |
8623 | 0 | if (nsMap != NULL) { |
8624 | 0 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
8625 | | /* |
8626 | | * Just cleanup the map but don't free. |
8627 | | */ |
8628 | 0 | if (nsMap->first) { |
8629 | 0 | if (nsMap->pool) |
8630 | 0 | nsMap->last->next = nsMap->pool; |
8631 | 0 | nsMap->pool = nsMap->first; |
8632 | 0 | nsMap->first = NULL; |
8633 | 0 | } |
8634 | 0 | } else |
8635 | 0 | xmlDOMWrapNsMapFree(nsMap); |
8636 | 0 | } |
8637 | | /* |
8638 | | * TODO: Should we try a cleanup of the cloned node in case of a |
8639 | | * fatal error? |
8640 | | */ |
8641 | 0 | *resNode = resultClone; |
8642 | 0 | return (ret); |
8643 | 0 | } |
8644 | | |
8645 | | /** |
8646 | | * `attr` is adopted by `destDoc`. |
8647 | | * Ensures that ns-references point to `destDoc`: either to |
8648 | | * `elements->nsDef` entries if `destParent` is given, or to |
8649 | | * `destDoc->oldNs` otherwise. |
8650 | | * |
8651 | | * @param ctxt the optional context for custom processing |
8652 | | * @param sourceDoc unused |
8653 | | * @param attr the attribute-node to be adopted |
8654 | | * @param destDoc the destination doc for adoption |
8655 | | * @param destParent the optional new parent of `attr` in `destDoc` |
8656 | | * @param options option flags (unused) |
8657 | | * @returns 0 if succeeded, -1 otherwise and on API/internal errors. |
8658 | | */ |
8659 | | static int |
8660 | | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
8661 | | xmlDocPtr sourceDoc ATTRIBUTE_UNUSED, |
8662 | | xmlAttrPtr attr, |
8663 | | xmlDocPtr destDoc, |
8664 | | xmlNodePtr destParent, |
8665 | | int options ATTRIBUTE_UNUSED) |
8666 | 0 | { |
8667 | 0 | int ret = 0; |
8668 | |
|
8669 | 0 | if ((attr == NULL) || (destDoc == NULL)) |
8670 | 0 | return (-1); |
8671 | | |
8672 | 0 | if (attr->doc != destDoc) { |
8673 | 0 | if (xmlSetTreeDoc((xmlNodePtr) attr, destDoc) < 0) |
8674 | 0 | ret = -1; |
8675 | 0 | } |
8676 | |
|
8677 | 0 | if (attr->ns != NULL) { |
8678 | 0 | xmlNsPtr ns = NULL; |
8679 | |
|
8680 | 0 | if (ctxt != NULL) { |
8681 | | /* TODO: User defined. */ |
8682 | 0 | } |
8683 | | /* XML Namespace. */ |
8684 | 0 | if (IS_STR_XML(attr->ns->prefix)) { |
8685 | 0 | ns = xmlTreeEnsureXMLDecl(destDoc); |
8686 | 0 | } else if (destParent == NULL) { |
8687 | | /* |
8688 | | * Store in @destDoc->oldNs. |
8689 | | */ |
8690 | 0 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
8691 | 0 | } else { |
8692 | | /* |
8693 | | * Declare on @destParent. |
8694 | | */ |
8695 | 0 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
8696 | 0 | &ns, 1) == -1) |
8697 | 0 | ret = -1; |
8698 | 0 | if (ns == NULL) { |
8699 | 0 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
8700 | 0 | attr->ns->href, attr->ns->prefix, 1); |
8701 | 0 | } |
8702 | 0 | } |
8703 | 0 | if (ns == NULL) |
8704 | 0 | ret = -1; |
8705 | 0 | attr->ns = ns; |
8706 | 0 | } |
8707 | |
|
8708 | 0 | return (ret); |
8709 | 0 | } |
8710 | | |
8711 | | /** |
8712 | | * Fix up namespaces before moving a node. |
8713 | | * |
8714 | | * References of out-of scope ns-decls are remapped to point to `destDoc`: |
8715 | | * If `destParent` is given, then nsDef entries on element-nodes are used. |
8716 | | * If *no* `destParent` is given, then `destDoc->oldNs` entries are used |
8717 | | * This is the case when you have an unlinked node and just want to move it |
8718 | | * to the context of. |
8719 | | * |
8720 | | * If `destParent` is given, it ensures that the tree is namespace |
8721 | | * wellformed by creating additional ns-decls where needed. |
8722 | | * Note that, since prefixes of already existent ns-decls can be |
8723 | | * shadowed by this process, it could break QNames in attribute |
8724 | | * values or element content. |
8725 | | * |
8726 | | * NOTE: This function was not intensively tested. |
8727 | | * |
8728 | | * @param ctxt the optional context for custom processing |
8729 | | * @param sourceDoc the optional sourceDoc |
8730 | | * @param node the node to start with |
8731 | | * @param destDoc the destination doc |
8732 | | * @param destParent the optional new parent of `node` in `destDoc` |
8733 | | * @param options option flags |
8734 | | * @returns 0 if the operation succeeded, |
8735 | | * 1 if a node of unsupported type was given, |
8736 | | * 2 if a node of not yet supported type was given and |
8737 | | * -1 on API/internal errors. |
8738 | | */ |
8739 | | int |
8740 | | xmlDOMWrapAdoptNode(xmlDOMWrapCtxt *ctxt, |
8741 | | xmlDoc *sourceDoc, |
8742 | | xmlNode *node, |
8743 | | xmlDoc *destDoc, |
8744 | | xmlNode *destParent, |
8745 | | int options) |
8746 | 0 | { |
8747 | 0 | int ret = 0; |
8748 | |
|
8749 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || |
8750 | 0 | (destDoc == NULL) || |
8751 | 0 | ((destParent != NULL) && (destParent->doc != destDoc))) |
8752 | 0 | return(-1); |
8753 | | /* |
8754 | | * Check node->doc sanity. |
8755 | | */ |
8756 | 0 | if (sourceDoc == NULL) { |
8757 | 0 | sourceDoc = node->doc; |
8758 | 0 | } else if (node->doc != sourceDoc) { |
8759 | 0 | return (-1); |
8760 | 0 | } |
8761 | | |
8762 | | /* |
8763 | | * TODO: Shouldn't this be allowed? |
8764 | | */ |
8765 | 0 | if (sourceDoc == destDoc) |
8766 | 0 | return (-1); |
8767 | | |
8768 | 0 | switch (node->type) { |
8769 | 0 | case XML_ELEMENT_NODE: |
8770 | 0 | case XML_ATTRIBUTE_NODE: |
8771 | 0 | case XML_TEXT_NODE: |
8772 | 0 | case XML_CDATA_SECTION_NODE: |
8773 | 0 | case XML_ENTITY_REF_NODE: |
8774 | 0 | case XML_PI_NODE: |
8775 | 0 | case XML_COMMENT_NODE: |
8776 | 0 | break; |
8777 | 0 | case XML_DOCUMENT_FRAG_NODE: |
8778 | | /* TODO: Support document-fragment-nodes. */ |
8779 | 0 | return (2); |
8780 | 0 | default: |
8781 | 0 | return (1); |
8782 | 0 | } |
8783 | | /* |
8784 | | * Unlink only if @node was not already added to @destParent. |
8785 | | */ |
8786 | 0 | if ((node->parent != NULL) && (destParent != node->parent)) |
8787 | 0 | xmlUnlinkNodeInternal(node); |
8788 | |
|
8789 | 0 | if (node->type == XML_ELEMENT_NODE) { |
8790 | 0 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
8791 | 0 | destDoc, destParent, options)); |
8792 | 0 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
8793 | 0 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
8794 | 0 | (xmlAttrPtr) node, destDoc, destParent, options)); |
8795 | 0 | } else { |
8796 | 0 | if (node->doc != destDoc) { |
8797 | 0 | if (xmlNodeSetDoc(node, destDoc) < 0) |
8798 | 0 | ret = -1; |
8799 | 0 | } |
8800 | 0 | } |
8801 | 0 | return (ret); |
8802 | 0 | } |
8803 | | |
8804 | | /************************************************************************ |
8805 | | * * |
8806 | | * XHTML detection * |
8807 | | * * |
8808 | | ************************************************************************/ |
8809 | | |
8810 | 0 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
8811 | 0 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
8812 | 0 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
8813 | 0 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
8814 | 0 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
8815 | 0 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
8816 | 0 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
8817 | 0 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
8818 | 0 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
8819 | 0 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
8820 | 0 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
8821 | 0 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
8822 | | |
8823 | | /** |
8824 | | * Try to find if the document correspond to an XHTML DTD |
8825 | | * |
8826 | | * @param systemID the system identifier |
8827 | | * @param publicID the public identifier |
8828 | | * @returns 1 if true, 0 if not and -1 in case of error |
8829 | | */ |
8830 | | int |
8831 | 0 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
8832 | 0 | if ((systemID == NULL) && (publicID == NULL)) |
8833 | 0 | return(-1); |
8834 | 0 | if (publicID != NULL) { |
8835 | 0 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
8836 | 0 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
8837 | 0 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
8838 | 0 | } |
8839 | 0 | if (systemID != NULL) { |
8840 | 0 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
8841 | 0 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
8842 | 0 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
8843 | 0 | } |
8844 | 0 | return(0); |
8845 | 0 | } |
8846 | | |
8847 | | /************************************************************************ |
8848 | | * * |
8849 | | * Node callbacks * |
8850 | | * * |
8851 | | ************************************************************************/ |
8852 | | |
8853 | | /** |
8854 | | * Registers a callback for node creation |
8855 | | * |
8856 | | * @deprecated don't use |
8857 | | * |
8858 | | * @param func function pointer to the new RegisterNodeFunc |
8859 | | * @returns the old value of the registration function |
8860 | | */ |
8861 | | xmlRegisterNodeFunc |
8862 | | xmlRegisterNodeDefault(xmlRegisterNodeFunc func) |
8863 | 0 | { |
8864 | 0 | xmlRegisterNodeFunc old = xmlRegisterNodeDefaultValue; |
8865 | |
|
8866 | 0 | xmlRegisterCallbacks = 1; |
8867 | 0 | xmlRegisterNodeDefaultValue = func; |
8868 | 0 | return(old); |
8869 | 0 | } |
8870 | | |
8871 | | /** |
8872 | | * Registers a callback for node destruction |
8873 | | * |
8874 | | * @deprecated don't use |
8875 | | * |
8876 | | * @param func function pointer to the new DeregisterNodeFunc |
8877 | | * @returns the previous value of the deregistration function |
8878 | | */ |
8879 | | xmlDeregisterNodeFunc |
8880 | | xmlDeregisterNodeDefault(xmlDeregisterNodeFunc func) |
8881 | 0 | { |
8882 | 0 | xmlDeregisterNodeFunc old = xmlDeregisterNodeDefaultValue; |
8883 | |
|
8884 | 0 | xmlRegisterCallbacks = 1; |
8885 | 0 | xmlDeregisterNodeDefaultValue = func; |
8886 | 0 | return(old); |
8887 | 0 | } |
8888 | | |