-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.c
2647 lines (2266 loc) · 70.9 KB
/
node.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of libdom.
* Licensed under the MIT License,
* http://www.opensource.org/licenses/mit-license.php
* Copyright 2007 John-Mark Bell <[email protected]>
* Copyright 2009 Bo Yang <[email protected]>
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dom/core/attr.h>
#include <dom/core/text.h>
#include <dom/core/document.h>
#include <dom/core/namednodemap.h>
#include <dom/core/nodelist.h>
#include <dom/core/implementation.h>
#include <dom/core/document_type.h>
#include <dom/events/events.h>
#include "core/string.h"
#include "core/namednodemap.h"
#include "core/attr.h"
#include "core/cdatasection.h"
#include "core/comment.h"
#include "core/document.h"
#include "core/document_type.h"
#include "core/doc_fragment.h"
#include "core/element.h"
#include "core/entity_ref.h"
#include "core/node.h"
#include "core/pi.h"
#include "core/text.h"
#include "utils/utils.h"
#include "utils/validate.h"
#include "events/mutation_event.h"
static bool _dom_node_permitted_child(const dom_node_internal *parent,
const dom_node_internal *child);
static inline dom_exception _dom_node_attach(dom_node_internal *node,
dom_node_internal *parent,
dom_node_internal *previous,
dom_node_internal *next);
static inline void _dom_node_detach(dom_node_internal *node);
static inline dom_exception _dom_node_attach_range(dom_node_internal *first,
dom_node_internal *last,
dom_node_internal *parent,
dom_node_internal *previous,
dom_node_internal *next);
static inline dom_exception _dom_node_detach_range(dom_node_internal *first,
dom_node_internal *last);
static inline void _dom_node_replace(dom_node_internal *old,
dom_node_internal *replacement);
static const struct dom_node_vtable node_vtable = {
{
DOM_NODE_EVENT_TARGET_VTABLE
},
DOM_NODE_VTABLE
};
static const struct dom_node_protect_vtable node_protect_vtable = {
DOM_NODE_PROTECT_VTABLE
};
/*----------------------------------------------------------------------*/
/* The constructor and destructor of this object */
/* Create a DOM node and compose the vtable */
dom_node_internal * _dom_node_create(void)
{
dom_node_internal *node = malloc(sizeof(struct dom_node_internal));
if (node == NULL)
return NULL;
node->base.vtable = &node_vtable;
node->vtable = &node_protect_vtable;
return node;
}
/**
* Destroy a DOM node
*
* \param node The node to destroy
*
* ::node's parent link must be NULL and its reference count must be 0.
*
* ::node will be freed.
*
* This function should only be called from dom_node_unref or type-specific
* destructors (for destroying child nodes). Anything else should not
* be attempting to destroy nodes -- they should simply be unreferencing
* them (so destruction will occur at the appropriate time).
*/
void _dom_node_destroy(struct dom_node_internal *node)
{
struct dom_document *owner = node->owner;
bool null_owner_permitted = (node->type == DOM_DOCUMENT_NODE ||
node->type == DOM_DOCUMENT_TYPE_NODE);
assert(null_owner_permitted || owner != NULL);
if (!null_owner_permitted) {
/* Claim a reference upon the owning document during
* destruction to ensure that the document doesn't get
* destroyed before its contents. */
dom_node_ref(owner);
}
/* Finalise this node, this should also destroy all the child nodes. */
_dom_node_finalise(node);
if (!null_owner_permitted) {
/* Release the reference we claimed on the document. If this
* is the last reference held on the document and the list
* of nodes pending deletion is empty, then the document will
* be destroyed. */
dom_node_unref(owner);
}
/* Release our memory */
free(node);
}
/**
* Initialise a DOM node
*
* \param node The node to initialise
* \param doc The document which owns the node
* \param type The node type required
* \param name The node (local) name, or NULL
* \param value The node value, or NULL
* \param namespace Namespace URI to use for node, or NULL
* \param prefix Namespace prefix to use for node, or NULL
* \return DOM_NO_ERR on success.
*
* ::name, ::value, ::namespace, and ::prefix will have their reference
* counts increased.
*/
dom_exception _dom_node_initialise(dom_node_internal *node,
struct dom_document *doc, dom_node_type type,
dom_string *name, dom_string *value,
dom_string *namespace, dom_string *prefix)
{
node->owner = doc;
if (name != NULL)
node->name = dom_string_ref(name);
else
node->name = NULL;
if (value != NULL)
node->value = dom_string_ref(value);
else
node->value = NULL;
node->type = type;
node->parent = NULL;
node->first_child = NULL;
node->last_child = NULL;
node->previous = NULL;
node->next = NULL;
/* Note: nodes do not reference the document to which they belong,
* as this would result in the document never being destroyed once
* the client has finished with it. The document will be aware of
* any nodes that it owns through 2 mechanisms:
*
* either a) Membership of the document tree
* or b) Membership of the list of nodes pending deletion
*
* It is not possible for any given node to be a member of both
* data structures at the same time.
*
* The document will not be destroyed until both of these
* structures are empty. It will forcibly attempt to empty
* the document tree on document destruction. Any still-referenced
* nodes at that time will be added to the list of nodes pending
* deletion. This list will not be forcibly emptied, as it contains
* those nodes (and their sub-trees) in use by client code.
*/
if (namespace != NULL)
node->namespace = dom_string_ref(namespace);
else
node->namespace = NULL;
if (prefix != NULL)
node->prefix = dom_string_ref(prefix);
else
node->prefix = NULL;
node->user_data = NULL;
node->base.refcnt = 1;
list_init(&node->pending_list);
if (node->type != DOM_DOCUMENT_NODE) {
/* A Node should be in the pending list when it is created */
dom_node_mark_pending(node);
}
return _dom_event_target_internal_initialise(&node->eti);
}
/**
* Finalise a DOM node
*
* \param node The node to finalise
*
* The contents of ::node will be cleaned up. ::node will not be freed.
* All children of ::node should have been removed prior to finalisation.
*/
void _dom_node_finalise(dom_node_internal *node)
{
struct dom_user_data *u, *v;
struct dom_node_internal *p;
struct dom_node_internal *n = NULL;
/* Destroy user data */
for (u = node->user_data; u != NULL; u = v) {
v = u->next;
if (u->handler != NULL)
u->handler(DOM_NODE_DELETED, u->key, u->data,
NULL, NULL);
dom_string_unref(u->key);
free(u);
}
node->user_data = NULL;
if (node->prefix != NULL) {
dom_string_unref(node->prefix);
node->prefix = NULL;
}
if (node->namespace != NULL) {
dom_string_unref(node->namespace);
node->namespace = NULL;
}
/* Destroy all the child nodes of this node */
p = node->first_child;
while (p != NULL) {
n = p->next;
p->parent = NULL;
dom_node_try_destroy(p);
p = n;
}
/* Paranoia */
node->next = NULL;
node->previous = NULL;
node->last_child = NULL;
node->first_child = NULL;
node->parent = NULL;
if (node->value != NULL) {
dom_string_unref(node->value);
node->value = NULL;
}
if (node->name != NULL) {
dom_string_unref(node->name);
node->name = NULL;
}
/* If the node has no owner document, we need not to finalise its
* dom_event_target_internal structure.
*/
if (node->owner != NULL)
_dom_event_target_internal_finalise(&node->eti);
/* Detach from the pending list, if we are in it,
* this part of code should always be the end of this function. */
if (node->pending_list.prev != &node->pending_list) {
assert (node->pending_list.next != &node->pending_list);
list_del(&node->pending_list);
if (node->owner != NULL && node->type != DOM_DOCUMENT_NODE) {
/* Deleting this node from the pending list may cause
* the list to be null and we should try to destroy
* the document. */
_dom_document_try_destroy(node->owner);
}
}
}
/* ---------------------------------------------------------------------*/
/* The public non-virtual function of this interface Node */
dom_exception _dom_node_contains(struct dom_node_internal *node,
struct dom_node_internal *other,
bool *contains)
{
assert(node != NULL);
assert(other != NULL);
assert(contains != NULL);
if (node->owner != other->owner) {
*contains = false;
return DOM_NO_ERR;
}
while (other != NULL) {
if (other == node) {
*contains = true;
return DOM_NO_ERR;
}
other = other->parent;
}
*contains = false;
return DOM_NO_ERR;
}
/* ---------------------------------------------------------------------*/
/* The public virtual function of this interface Node */
/**
* Retrieve the name of a DOM node
*
* \param node The node to retrieve the name of
* \param result Pointer to location to receive node name
* \return DOM_NO_ERR.
*
* The returned string will have its reference count increased. It is
* the responsibility of the caller to unref the string once it has
* finished with it.
*/
dom_exception _dom_node_get_node_name(dom_node_internal *node,
dom_string **result)
{
dom_string *node_name, *temp;
dom_exception err;
/* Document Node and DocumentType Node can have no owner */
assert(node->type == DOM_DOCUMENT_TYPE_NODE ||
node->type == DOM_DOCUMENT_NODE ||
node->owner != NULL);
assert(node->name != NULL);
/* If this node was created using a namespace-aware method and
* has a defined prefix, then nodeName is a QName comprised
* of prefix:name. */
if (node->prefix != NULL) {
dom_string *colon;
err = dom_string_create((const uint8_t *) ":", SLEN(":"),
&colon);
if (err != DOM_NO_ERR) {
return err;
}
/* Prefix + : */
err = dom_string_concat(node->prefix, colon, &temp);
if (err != DOM_NO_ERR) {
dom_string_unref(colon);
return err;
}
/* Finished with colon */
dom_string_unref(colon);
/* Prefix + : + Localname */
err = dom_string_concat(temp, node->name, &node_name);
if (err != DOM_NO_ERR) {
dom_string_unref(temp);
return err;
}
/* Finished with temp */
dom_string_unref(temp);
} else {
node_name = dom_string_ref(node->name);
}
*result = node_name;
return DOM_NO_ERR;
}
/**
* Retrieve the value of a DOM node
*
* \param node The node to retrieve the value of
* \param result Pointer to location to receive node value
* \return DOM_NO_ERR.
*
* The returned string will have its reference count increased. It is
* the responsibility of the caller to unref the string once it has
* finished with it.
*
* DOM3Core states that this can raise DOMSTRING_SIZE_ERR. It will not in
* this implementation; dom_strings are unbounded.
*/
dom_exception _dom_node_get_node_value(dom_node_internal *node,
dom_string **result)
{
if (node->value != NULL)
dom_string_ref(node->value);
*result = node->value;
return DOM_NO_ERR;
}
/**
* Set the value of a DOM node
*
* \param node Node to set the value of
* \param value New value for node
* \return DOM_NO_ERR on success,
* DOM_NO_MODIFICATION_ALLOWED_ERR if the node is readonly and the
* value is not defined to be null.
*
* The new value will have its reference count increased, so the caller
* should unref it after the call (as the caller should have already claimed
* a reference on the string). The node's existing value will be unrefed.
*/
dom_exception _dom_node_set_node_value(dom_node_internal *node,
dom_string *value)
{
/* TODO
* Whether we should change this to a virtual function?
*/
/* This is a NOP if the value is defined to be null. */
if (node->type == DOM_DOCUMENT_NODE ||
node->type == DOM_DOCUMENT_FRAGMENT_NODE ||
node->type == DOM_DOCUMENT_TYPE_NODE ||
node->type == DOM_ELEMENT_NODE ||
node->type == DOM_ENTITY_NODE ||
node->type == DOM_ENTITY_REFERENCE_NODE ||
node->type == DOM_NOTATION_NODE) {
return DOM_NO_ERR;
}
/* Ensure node is writable */
if (_dom_node_readonly(node))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
/* If it's an attribute node, then delegate setting to
* the type-specific function */
if (node->type == DOM_ATTRIBUTE_NODE)
return dom_attr_set_value((struct dom_attr *) node, value);
if (node->value != NULL)
dom_string_unref(node->value);
if (value != NULL)
dom_string_ref(value);
node->value = value;
return DOM_NO_ERR;
}
/**
* Retrieve the type of a DOM node
*
* \param node The node to retrieve the type of
* \param result Pointer to location to receive node type
* \return DOM_NO_ERR.
*/
dom_exception _dom_node_get_node_type(dom_node_internal *node,
dom_node_type *result)
{
*result = node->type;
return DOM_NO_ERR;
}
/**
* Retrieve the parent of a DOM node
*
* \param node The node to retrieve the parent of
* \param result Pointer to location to receive node parent
* \return DOM_NO_ERR.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_get_parent_node(dom_node_internal *node,
dom_node_internal **result)
{
/* Attr nodes have no parent */
if (node->type == DOM_ATTRIBUTE_NODE) {
*result = NULL;
return DOM_NO_ERR;
}
/* If there is a parent node, then increase its reference count */
if (node->parent != NULL)
dom_node_ref(node->parent);
*result = node->parent;
return DOM_NO_ERR;
}
/**
* Retrieve a list of children of a DOM node
*
* \param node The node to retrieve the children of
* \param result Pointer to location to receive child list
* \return DOM_NO_ERR.
*
* The returned NodeList will be referenced. It is the responsibility
* of the caller to unref the list once it has finished with it.
*/
dom_exception _dom_node_get_child_nodes(dom_node_internal *node,
struct dom_nodelist **result)
{
/* Can't do anything without an owning document.
* This is only a problem for DocumentType nodes
* which are not yet attached to a document.
* DocumentType nodes have no children, anyway. */
if (node->owner == NULL)
return DOM_NOT_SUPPORTED_ERR;
return _dom_document_get_nodelist(node->owner, DOM_NODELIST_CHILDREN,
node, NULL, NULL, NULL, result);
}
/**
* Retrieve the first child of a DOM node
*
* \param node The node to retrieve the first child of
* \param result Pointer to location to receive node's first child
* \return DOM_NO_ERR.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_get_first_child(dom_node_internal *node,
dom_node_internal **result)
{
/* If there is a first child, increase its reference count */
if (node->first_child != NULL)
dom_node_ref(node->first_child);
*result = node->first_child;
return DOM_NO_ERR;
}
/**
* Retrieve the last child of a DOM node
*
* \param node The node to retrieve the last child of
* \param result Pointer to location to receive node's last child
* \return DOM_NO_ERR.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_get_last_child(dom_node_internal *node,
dom_node_internal **result)
{
/* If there is a last child, increase its reference count */
if (node->last_child != NULL)
dom_node_ref(node->last_child);
*result = node->last_child;
return DOM_NO_ERR;
}
/**
* Retrieve the previous sibling of a DOM node
*
* \param node The node to retrieve the previous sibling of
* \param result Pointer to location to receive node's previous sibling
* \return DOM_NO_ERR.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_get_previous_sibling(dom_node_internal *node,
dom_node_internal **result)
{
/* Attr nodes have no previous siblings */
if (node->type == DOM_ATTRIBUTE_NODE) {
*result = NULL;
return DOM_NO_ERR;
}
/* If there is a previous sibling, increase its reference count */
if (node->previous != NULL)
dom_node_ref(node->previous);
*result = node->previous;
return DOM_NO_ERR;
}
/**
* Retrieve the subsequent sibling of a DOM node
*
* \param node The node to retrieve the subsequent sibling of
* \param result Pointer to location to receive node's subsequent sibling
* \return DOM_NO_ERR.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_get_next_sibling(dom_node_internal *node,
dom_node_internal **result)
{
/* Attr nodes have no next siblings */
if (node->type == DOM_ATTRIBUTE_NODE) {
*result = NULL;
return DOM_NO_ERR;
}
/* If there is a subsequent sibling, increase its reference count */
if (node->next != NULL)
dom_node_ref(node->next);
*result = node->next;
return DOM_NO_ERR;
}
/**
* Retrieve a map of attributes associated with a DOM node
*
* \param node The node to retrieve the attributes of
* \param result Pointer to location to receive attribute map
* \return DOM_NO_ERR.
*
* The returned NamedNodeMap will be referenced. It is the responsibility
* of the caller to unref the map once it has finished with it.
*
* If ::node is not an Element, then NULL will be returned.
*/
dom_exception _dom_node_get_attributes(dom_node_internal *node,
struct dom_namednodemap **result)
{
UNUSED(node);
*result = NULL;
return DOM_NO_ERR;
}
/**
* Retrieve the owning document of a DOM node
*
* \param node The node to retrieve the owner of
* \param result Pointer to location to receive node's owner
* \return DOM_NO_ERR.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_get_owner_document(dom_node_internal *node,
struct dom_document **result)
{
/* Document nodes have no owner, as far as clients are concerned
* In reality, they own themselves as this simplifies code elsewhere */
if (node->type == DOM_DOCUMENT_NODE) {
*result = NULL;
return DOM_NO_ERR;
}
/* If there is an owner, increase its reference count */
if (node->owner != NULL)
dom_node_ref(node->owner);
*result = node->owner;
return DOM_NO_ERR;
}
/**
* Insert a child into a node
*
* \param node Node to insert into
* \param new_child Node to insert
* \param ref_child Node to insert before, or NULL to insert as last child
* \param result Pointer to location to receive node being inserted
* \return DOM_NO_ERR on success,
* DOM_HIERARCHY_REQUEST_ERR if ::new_child's type is not
* permitted as a child of ::node,
* or ::new_child is an ancestor of
* ::node (or is ::node itself), or
* ::node is of type Document and a
* second DocumentType or Element is
* being inserted,
* DOM_WRONG_DOCUMENT_ERR if ::new_child was created from a
* different document than ::node,
* DOM_NO_MODIFICATION_ALLOWED_ERR if ::node is readonly, or
* ::new_child's parent is readonly,
* DOM_NOT_FOUND_ERR if ::ref_child is not a child of
* ::node.
*
* If ::new_child is a DocumentFragment, all of its children are inserted.
* If ::new_child is already in the tree, it is first removed.
*
* Attempting to insert a node before itself is a NOP.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_insert_before(dom_node_internal *node,
dom_node_internal *new_child, dom_node_internal *ref_child,
dom_node_internal **result)
{
dom_exception err;
dom_node_internal *n;
assert(node != NULL);
/* Ensure that new_child and node are owned by the same document */
if ((new_child->type == DOM_DOCUMENT_TYPE_NODE &&
new_child->owner != NULL &&
new_child->owner != node->owner) ||
(new_child->type != DOM_DOCUMENT_TYPE_NODE &&
new_child->owner != node->owner))
return DOM_WRONG_DOCUMENT_ERR;
/* Ensure node isn't read only */
if (_dom_node_readonly(node))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
/* Ensure that ref_child (if any) is a child of node */
if (ref_child != NULL && ref_child->parent != node)
return DOM_NOT_FOUND_ERR;
/* Ensure that new_child is not an ancestor of node, nor node itself */
for (n = node; n != NULL; n = n->parent) {
if (n == new_child)
return DOM_HIERARCHY_REQUEST_ERR;
}
/* Ensure that new_child is permitted as a child of node */
if (new_child->type != DOM_DOCUMENT_FRAGMENT_NODE &&
!_dom_node_permitted_child(node, new_child))
return DOM_HIERARCHY_REQUEST_ERR;
/* Attempting to insert a node before itself is a NOP */
if (new_child == ref_child) {
dom_node_ref(new_child);
*result = new_child;
return DOM_NO_ERR;
}
/* If new_child is already in the tree and
* its parent isn't read only, remove it */
if (new_child->parent != NULL) {
if (_dom_node_readonly(new_child->parent))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
_dom_node_detach(new_child);
}
/* When a Node is attached, it should be removed from the pending
* list */
dom_node_remove_pending(new_child);
/* If new_child is a DocumentFragment, insert its children.
* Otherwise, insert new_child */
if (new_child->type == DOM_DOCUMENT_FRAGMENT_NODE) {
/* Test the children of the docment fragment can be appended */
dom_node_internal *c = new_child->first_child;
for (; c != NULL; c = c->next)
if (!_dom_node_permitted_child(node, c))
return DOM_HIERARCHY_REQUEST_ERR;
if (new_child->first_child != NULL) {
err = _dom_node_attach_range(new_child->first_child,
new_child->last_child,
node,
ref_child == NULL ? node->last_child
: ref_child->previous,
ref_child == NULL ? NULL
: ref_child);
if (err != DOM_NO_ERR)
return err;
new_child->first_child = NULL;
new_child->last_child = NULL;
}
} else {
err = _dom_node_attach(new_child,
node,
ref_child == NULL ? node->last_child
: ref_child->previous,
ref_child == NULL ? NULL
: ref_child);
if (err != DOM_NO_ERR)
return err;
}
/* DocumentType nodes are created outside the Document so,
* if we're trying to attach a DocumentType node, then we
* also need to set its owner. */
if (node->type == DOM_DOCUMENT_NODE &&
new_child->type == DOM_DOCUMENT_TYPE_NODE) {
/* See long comment in _dom_node_initialise as to why
* we don't ref the document here */
new_child->owner = (struct dom_document *) node;
}
/** \todo Is it correct to return DocumentFragments? */
dom_node_ref(new_child);
*result = new_child;
return DOM_NO_ERR;
}
/**
* Replace a node's child with a new one
*
* \param node Node whose child to replace
* \param new_child Replacement node
* \param old_child Child to replace
* \param result Pointer to location to receive replaced node
* \return DOM_NO_ERR on success,
* DOM_HIERARCHY_REQUEST_ERR if ::new_child's type is not
* permitted as a child of ::node,
* or ::new_child is an ancestor of
* ::node (or is ::node itself), or
* ::node is of type Document and a
* second DocumentType or Element is
* being inserted,
* DOM_WRONG_DOCUMENT_ERR if ::new_child was created from a
* different document than ::node,
* DOM_NO_MODIFICATION_ALLOWED_ERR if ::node is readonly, or
* ::new_child's parent is readonly,
* DOM_NOT_FOUND_ERR if ::old_child is not a child of
* ::node,
* DOM_NOT_SUPPORTED_ERR if ::node is of type Document and
* ::new_child is of type
* DocumentType or Element.
*
* If ::new_child is a DocumentFragment, ::old_child is replaced by all of
* ::new_child's children.
* If ::new_child is already in the tree, it is first removed.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_replace_child(dom_node_internal *node,
dom_node_internal *new_child, dom_node_internal *old_child,
dom_node_internal **result)
{
dom_node_internal *n;
/* We don't support replacement of DocumentType or root Elements */
if (node->type == DOM_DOCUMENT_NODE &&
(new_child->type == DOM_DOCUMENT_TYPE_NODE ||
new_child->type == DOM_ELEMENT_NODE))
return DOM_NOT_SUPPORTED_ERR;
/* Ensure that new_child and node are owned by the same document */
if (new_child->owner != node->owner)
return DOM_WRONG_DOCUMENT_ERR;
/* Ensure node isn't read only */
if (_dom_node_readonly(node))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
/* Ensure that old_child is a child of node */
if (old_child->parent != node)
return DOM_NOT_FOUND_ERR;
/* Ensure that new_child is not an ancestor of node, nor node itself */
for (n = node; n != NULL; n = n->parent) {
if (n == new_child)
return DOM_HIERARCHY_REQUEST_ERR;
}
/* Ensure that new_child is permitted as a child of node */
if (new_child->type == DOM_DOCUMENT_FRAGMENT_NODE) {
/* If this node is a doc fragment, we should test all its
* children nodes */
dom_node_internal *c;
c = new_child->first_child;
while (c != NULL) {
if (!_dom_node_permitted_child(node, c))
return DOM_HIERARCHY_REQUEST_ERR;
c = c->next;
}
} else {
if (!_dom_node_permitted_child(node, new_child))
return DOM_HIERARCHY_REQUEST_ERR;
}
/* Attempting to replace a node with itself is a NOP */
if (new_child == old_child) {
dom_node_ref(old_child);
*result = old_child;
return DOM_NO_ERR;
}
/* If new_child is already in the tree and
* its parent isn't read only, remove it */
if (new_child->parent != NULL) {
if (_dom_node_readonly(new_child->parent))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
_dom_node_detach(new_child);
}
/* When a Node is attached, it should be removed from the pending
* list */
dom_node_remove_pending(new_child);
/* Perform the replacement */
_dom_node_replace(old_child, new_child);
/* Sort out the return value */
dom_node_ref(old_child);
/* The replaced node should be marded pending */
dom_node_mark_pending(old_child);
*result = old_child;
return DOM_NO_ERR;
}
/**
* Remove a child from a node
*
* \param node Node whose child to replace
* \param old_child Child to remove
* \param result Pointer to location to receive removed node
* \return DOM_NO_ERR on success,
* DOM_NO_MODIFICATION_ALLOWED_ERR if ::node is readonly
* DOM_NOT_FOUND_ERR if ::old_child is not a child of
* ::node,
* DOM_NOT_SUPPORTED_ERR if ::node is of type Document and
* ::new_child is of type
* DocumentType or Element.
*
* The returned node will have its reference count increased. It is
* the responsibility of the caller to unref the node once it has
* finished with it.
*/
dom_exception _dom_node_remove_child(dom_node_internal *node,
dom_node_internal *old_child,
dom_node_internal **result)
{
dom_exception err;
bool success = true;
/* We don't support removal of DocumentType or root Element nodes */
if (node->type == DOM_DOCUMENT_NODE &&
(old_child->type == DOM_DOCUMENT_TYPE_NODE ||
old_child->type == DOM_ELEMENT_NODE))
return DOM_NOT_SUPPORTED_ERR;
/* Ensure old_child is a child of node */
if (old_child->parent != node)
return DOM_NOT_FOUND_ERR;
/* Ensure node is writable */
if (_dom_node_readonly(node))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
/* Dispatch a DOMNodeRemoval event */
err = dom_node_dispatch_node_change_event(node->owner, old_child, node,
DOM_MUTATION_REMOVAL, &success);
if (err != DOM_NO_ERR)
return err;
/* Detach the node */
_dom_node_detach(old_child);
/* When a Node is removed, it should be destroy. When its refcnt is not
* zero, it will be added to the document's deletion pending list.
* When a Node is removed, its parent should be NULL, but its owner
* should remain to be the document. */
dom_node_ref(old_child);