forked from andreww/fox
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathm_dom_node.m4
1722 lines (1470 loc) · 51.7 KB
/
m_dom_node.m4
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
TOHW_m_dom_publics(`
public :: getNodeName
public :: getNodeValue
public :: setNodeValue
public :: getNodeType
public :: getParentNode
public :: getChildNodes
public :: getFirstChild
public :: getLastChild
public :: getNextSibling
public :: getPreviousSibling
public :: getAttributes
public :: getOwnerDocument
public :: insertBefore
public :: replaceChild
public :: removeChild
public :: appendChild
public :: hasChildNodes
public :: cloneNode
public :: normalize
public :: isSupported
public :: getNamespaceURI
public :: getPrefix
public :: setPrefix
public :: getLocalName
public :: hasAttributes
public :: isEqualNode
public :: isSameNode
public :: isDefaultNamespace
public :: lookupNamespaceURI
public :: lookupPrefix
public :: getTextContent
public :: setTextContent
public :: getNodePath
public :: setStringValue
public :: getStringValue
public :: setReadonlyNode
public :: getReadOnly
public :: getBaseURI
')`'dnl
TOHW_m_dom_contents(`
TOHW_m_dom_get(DOMString, nodeName, np%nodeName)
pure function getNodeValue_len(np, p) result(n)
type(Node), intent(in) :: np
logical, intent(in) :: p
integer :: n
n = 0
if (.not.p) return
select case(np%nodeType)
case (ATTRIBUTE_NODE)
n = getTextContent_len(np, .true.)
case (CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE)
n = size(np%nodeValue)
end select
end function getNodeValue_len
TOHW_function(getNodeValue, (np), c)
type(Node), pointer :: np
#ifdef RESTRICTED_ASSOCIATED_BUG
character(len=getNodeValue_len(np, .true.)) :: c
#else
character(len=getNodeValue_len(np, associated(np))) :: c
#endif
if (.not.associated(np)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
select case(np%nodeType)
case (ATTRIBUTE_NODE)
c = getTextContent(np)
case (CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE)
c = str_vs(np%nodeValue)
case default
c = ""
end select
end function getNodeValue
TOHW_subroutine(setNodeValue, (arg, nodeValue))
type(Node), pointer :: arg
character(len=*) :: nodeValue
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (associated(getOwnerDocument(arg))) then
if (.not.checkChars(nodeValue, getXmlVersionEnum(getOwnerDocument(arg)))) then
TOHW_m_dom_throw_error(FoX_INVALID_CHARACTER)
endif
endif ! Otherwise its a document node, and nothing will happen anyway
select case(arg%nodeType)
case (ATTRIBUTE_NODE)
call setValue(arg, nodeValue, ex)
case (CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE)
call setData(arg, nodeValue, ex)
end select
end subroutine setNodeValue
TOHW_m_dom_get(integer, nodeType, np%nodeType)
TOHW_m_dom_get(Node, parentNode, np%parentNode)
TOHW_m_dom_get(NodeList, childNodes, np%childNodes)
TOHW_m_dom_get(Node, firstChild, np%firstChild)
TOHW_m_dom_get(Node, lastChild, np%lastChild)
TOHW_m_dom_get(Node, previousSibling, np%previousSibling)
TOHW_m_dom_get(Node, nextSibling, np%nextSibling)
TOHW_function(getAttributes, (arg), nnm)
type(Node), pointer :: arg
type(NamedNodeMap), pointer :: nnm
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (getNodeType(arg)==ELEMENT_NODE) then
nnm => arg%elExtras%attributes
else
nnm => null()
endif
end function getAttributes
TOHW_function(getOwnerDocument, (arg), np)
type(Node), pointer :: arg
type(Node), pointer :: np
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (arg%nodeType==DOCUMENT_NODE) then
np => null()
else
np => arg%ownerDocument
endif
end function getOwnerDocument
TOHW_m_dom_set(Node, ownerDocument, np%ownerDocument, (DOCUMENT_NODE))
TOHW_function(insertBefore, (arg, newChild, refChild), np)
type(Node), pointer :: arg
type(Node), pointer :: newChild
type(Node), pointer :: refChild
type(Node), pointer :: np
type(Node), pointer :: testChild, testParent, treeroot, this
type(ListNode), pointer :: temp_nl(:)
integer :: i, i2, i_t, i_tree
logical :: doneChildren, doneAttributes
if (.not.associated(arg).or..not.associated(newChild)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (.not.associated(refChild)) then
np => appendChild(arg, newChild, ex)
return
endif
if (arg%readonly) then
TOHW_m_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR)
endif
testParent => arg
! Check if you are allowed to put a newChild nodetype under a arg nodetype
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i = 1, newChild%childNodes%length
testChild => newChild%childNodes%nodes(i)%this
TOHW_m_dom_hierarchy_test
enddo
else
testChild => newChild
TOHW_m_dom_hierarchy_test
! And then check that newChild is not arg or one of args ancestors
! (this would never be true if newChild is a documentFragment)
testParent => arg
do while (associated(testParent))
if (associated(testParent, newChild)) then
TOHW_m_dom_throw_error(HIERARCHY_REQUEST_ERR)
endif
testParent => testParent%parentNode
enddo
endif
if (getNodeType(newChild)/=DOCUMENT_TYPE_NODE.and. &
.not.(associated(arg%ownerDocument, newChild%ownerDocument) &
.or.associated(arg, newChild%ownerDocument))) then
TOHW_m_dom_throw_error(WRONG_DOCUMENT_ERR)
endif
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE &
.and. newChild%childNodes%length==0) then
np => newChild
return
! Nothing to do
endif
if (associated(getParentNode(newChild))) then
np => removeChild(getParentNode(newChild), newChild, ex)
newChild => np
endif
if (arg%childNodes%length==0) then
TOHW_m_dom_throw_error(NOT_FOUND_ERR)
elseif (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
allocate(temp_nl(arg%childNodes%length+newChild%childNodes%length))
else
allocate(temp_nl(arg%childNodes%length+1))
endif
i_t = 0
np => null()
do i = 1, arg%childNodes%length
if (associated(arg%childNodes%nodes(i)%this, refChild)) then
np => refChild
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i2 = 1, newChild%childNodes%length
i_t = i_t + 1
temp_nl(i_t)%this => newChild%childNodes%nodes(i2)%this
temp_nl(i_t)%this%parentNode => arg
! call namespaceFixup(temp_nl(i_t)%this)
enddo
else
i_t = i_t + 1
temp_nl(i_t)%this => newChild
temp_nl(i_t)%this%parentNode => arg
! call namespaceFixup(temp_nl(i_t)%this)
endif
if (i==1) then
arg%firstChild => temp_nl(1)%this
!temp_nl(1)%this%previousSibling => null() ! This is a no-op
else
temp_nl(i-1)%this%nextSibling => temp_nl(i)%this
temp_nl(i)%this%previousSibling => temp_nl(i-1)%this
endif
arg%childNodes%nodes(i)%this%previousSibling => temp_nl(i_t)%this
temp_nl(i_t)%this%nextSibling => arg%childNodes%nodes(i)%this
endif
i_t = i_t + 1
temp_nl(i_t)%this => arg%childNodes%nodes(i)%this
enddo
if (.not.associated(np)) then
TOHW_m_dom_throw_error(NOT_FOUND_ERR, (temp_nl))
endif
np => newChild
if (getGCstate(arg%ownerDocument)) then
if (arg%inDocument) then
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i = 1, newChild%childNodes%length
call putNodesInDocument(arg%ownerDocument, newChild%childNodes%nodes(i)%this)
enddo
else
call putNodesInDocument(arg%ownerDocument, newChild)
endif
! If newChild was originally in document, it was removed above so must be re-added
! Ideally we would avoid the cost of removal & readding to hanging nodelist
endif
! If arg was not in the document, then newChildren were either
! a) removed above in call to removeChild or
! b) in a document fragment and therefore not part of doc either
endif
if (getNodeType(newChild)==DOCUMENT_FRAGMENT_NODE) then
deallocate(newChild%childNodes%nodes)
allocate(newChild%childNodes%nodes(0))
newChild%childNodes%length = 0
endif
deallocate(arg%childNodes%nodes)
arg%childNodes%nodes => temp_nl
arg%childNodes%length = size(arg%childNodes%nodes)
call updateNodeLists(arg%ownerDocument)
call updateTextContentLength(arg, newChild%textContentLength)
end function insertBefore
TOHW_function(replaceChild, (arg, newChild, oldChild), np)
type(Node), pointer :: arg
type(Node), pointer :: newChild
type(Node), pointer :: oldChild
type(Node), pointer :: np
type(Node), pointer :: testChild, testParent, treeroot, this
type(ListNode), pointer :: temp_nl(:)
integer :: i, i2, i_t, i_tree
logical :: doneChildren, doneAttributes
if (.not.associated(arg).or..not.associated(newChild).or..not.associated(oldChild)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (arg%readonly) then
TOHW_m_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR)
endif
testParent => arg
! Check if you are allowed to put a newChild nodetype under a arg nodetype
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i = 1, newChild%childNodes%length
testChild => newChild%childNodes%nodes(i)%this
TOHW_m_dom_hierarchy_test
enddo
else
testChild => newChild
TOHW_m_dom_hierarchy_test
! And then check that newChild is not arg or one of args ancestors
! (this would never be true if newChild is a documentFragment)
testParent => arg
do while (associated(testParent))
if (associated(testParent, newChild)) then
TOHW_m_dom_throw_error(HIERARCHY_REQUEST_ERR)
endif
testParent => testParent%parentNode
enddo
endif
if (getNodeType(newChild)/=DOCUMENT_TYPE_NODE.and. &
.not.(associated(arg%ownerDocument, newChild%ownerDocument) &
.or.associated(arg, newChild%ownerDocument))) then
TOHW_m_dom_throw_error(WRONG_DOCUMENT_ERR)
endif
if (associated(getParentNode(newChild))) &
newChild => removeChild(getParentNode(newChild), newChild, ex)
if (arg%childNodes%length==0) then
TOHW_m_dom_throw_error(NOT_FOUND_ERR)
elseif (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
allocate(temp_nl(arg%childNodes%length+newChild%childNodes%length-1))
else
temp_nl => arg%childNodes%nodes
endif
i_t = 0
np => null()
do i = 1, arg%childNodes%length
if (associated(arg%childNodes%nodes(i)%this, oldChild)) then
np => oldChild
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i2 = 1, newChild%childNodes%length
i_t = i_t + 1
temp_nl(i_t)%this => newChild%childNodes%nodes(i2)%this
temp_nl(i_t)%this%parentNode => arg
! call namespaceFixup(temp_nl(i_t)%this)
enddo
else
i_t = i_t + 1
temp_nl(i_t)%this => newChild
temp_nl(i_t)%this%parentNode => arg
! call namespaceFixup(temp_nl(i_t)%this)
endif
if (i==1) then
arg%firstChild => temp_nl(1)%this
!temp_nl(1)%this%previousSibling => null() ! This is a no-op
else
temp_nl(i-1)%this%nextSibling => temp_nl(i)%this
temp_nl(i)%this%previousSibling => temp_nl(i-1)%this
endif
if (i==arg%childNodes%length) then
arg%lastChild => temp_nl(i_t)%this
!temp_nl(i_t)%this%nextSibling => null() ! This is a no-op
else
arg%childNodes%nodes(i+1)%this%previousSibling => temp_nl(i_t)%this
temp_nl(i_t)%this%nextSibling => arg%childNodes%nodes(i+1)%this
endif
else
i_t = i_t + 1
temp_nl(i_t)%this => arg%childNodes%nodes(i)%this
endif
enddo
if (.not.associated(np)) then
TOHW_m_dom_throw_error(NOT_FOUND_ERR)
endif
np%parentNode => null()
np%previousSibling => null()
np%nextSibling => null()
! call namespaceFixup(np)
if (getGCstate(arg%ownerDocument)) then
if (arg%inDocument) then
call removeNodesFromDocument(arg%ownerDocument, oldChild)
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i = 1, newChild%childNodes%length
call putNodesInDocument(arg%ownerDocument, newChild%childNodes%nodes(i)%this)
enddo
else
call putNodesInDocument(arg%ownerDocument, newChild)
endif
! If newChild was originally in document, it was removed above so must be re-added
! Ideally we would avoid the cost of removing & re-adding to hangingnodelist
endif
! If arg was not in the document, then newChildren were either
! a) removed above in call to removeChild or
! b) in a document fragment and therefore not part of doc either
endif
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
deallocate(newChild%childNodes%nodes)
allocate(newChild%childNodes%nodes(0))
newChild%childNodes%length = 0
deallocate(arg%childNodes%nodes)
arg%childNodes%nodes => temp_nl
arg%childNodes%length = size(arg%childNodes%nodes)
endif
call updateNodeLists(arg%ownerDocument)
call updateTextContentLength(arg, newChild%textContentLength-oldChild%textContentLength)
end function replaceChild
TOHW_function(removeChild, (arg, oldChild), np)
type(Node), pointer :: arg
type(Node), pointer :: oldChild
type(Node), pointer :: np
type(ListNode), pointer :: temp_nl(:)
integer :: i, i_t
if (.not.associated(arg).or..not.associated(oldChild)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (arg%readonly) then
TOHW_m_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR)
endif
allocate(temp_nl(size(arg%childNodes%nodes)-1))
i_t = 1
do i = 1, size(arg%childNodes%nodes)
if (associated(arg%childNodes%nodes(i)%this, oldChild)) then
if (associated(arg%firstChild, arg%lastChild)) then
! There is only one child, we are removing it.
arg%firstChild => null()
arg%lastChild => null()
elseif (i==1) then
! We are removing the first child, but there is a second
arg%firstChild => arg%childNodes%nodes(2)%this
arg%childNodes%nodes(2)%this%previousSibling => null()
elseif (i==size(arg%childNodes%nodes)) then
! We are removing the last child, but there is a second-to-last
arg%lastChild => arg%childNodes%nodes(i-1)%this
arg%childNodes%nodes(i-1)%this%nextSibling => null()
else
! We are removing a child in the middle
arg%childNodes%nodes(i-1)%this%nextSibling => arg%childNodes%nodes(i+1)%this
arg%childNodes%nodes(i+1)%this%previousSibling => arg%childNodes%nodes(i-1)%this
endif
else
if (i_t==size(arg%childNodes%nodes)) exit ! We have failed to find the child
temp_nl(i_t)%this => arg%childNodes%nodes(i)%this
i_t = i_t + 1
endif
enddo
deallocate(arg%childNodes%nodes)
arg%childNodes%nodes => temp_nl
arg%childNodes%length = size(temp_nl)
if (i==i_t) then
TOHW_m_dom_throw_error(NOT_FOUND_ERR)
endif
oldChild%parentNode => null()
oldChild%previousSibling => null()
oldChild%nextSibling => null()
! call namespaceFixup(oldChild)
if (getGCstate(arg%ownerDocument)) then
if (arg%inDocument) then
call removeNodesFromDocument(arg%ownerDocument, oldChild)
endif
endif
np => oldChild
call updateNodeLists(arg%ownerDocument)
call updateTextContentLength(arg, -oldChild%textContentLength)
end function removeChild
TOHW_function(appendChild, (arg, newChild), np)
type(Node), pointer :: arg
type(Node), pointer :: newChild
type(Node), pointer :: np
type(Node), pointer :: testChild, testParent, treeroot, this
type(ListNode), pointer :: temp_nl(:)
integer :: i, i_t, i_tree
logical :: doneChildren, doneAttributes
if (.not.associated(arg).or..not.associated(newChild)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (arg%readonly) then
TOHW_m_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR)
endif
testParent => arg
! Check if you are allowed to put a newChild nodetype under a arg nodetype
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
do i = 1, newChild%childNodes%length
testChild => newChild%childNodes%nodes(i)%this
TOHW_m_dom_hierarchy_test
enddo
else
testChild => newChild
TOHW_m_dom_hierarchy_test
! And then check that newChild is not arg or one of args ancestors
! (this would never be true if newChild is a documentFragment)
testParent => arg
do while (associated(testParent))
if (associated(testParent, newChild)) then
TOHW_m_dom_throw_error(HIERARCHY_REQUEST_ERR)
endif
testParent => testParent%parentNode
enddo
endif
if (getNodeType(newChild)/=DOCUMENT_TYPE_NODE.and. &
.not.(associated(arg%ownerDocument, newChild%ownerDocument) &
.or.associated(arg, newChild%ownerDocument))) then
TOHW_m_dom_throw_error(WRONG_DOCUMENT_ERR)
endif
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE &
.and. newChild%childNodes%length==0) then
np => newChild
return
! Nothing to do
endif
if (associated(getParentNode(newChild))) &
newChild => removeChild(getParentNode(newChild), newChild, ex)
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
allocate(temp_nl(arg%childNodes%length+newChild%childNodes%length))
else
allocate(temp_nl(arg%childNodes%length+1))
endif
do i = 1, arg%childNodes%length
temp_nl(i)%this => arg%childNodes%nodes(i)%this
enddo
if (newChild%nodeType==DOCUMENT_FRAGMENT_NODE) then
i_t = arg%childNodes%length
do i = 1, newChild%childNodes%length
i_t = i_t + 1
temp_nl(i_t)%this => newChild%childNodes%nodes(i)%this
if (arg%inDocument) &
call putNodesInDocument(arg%ownerDocument, temp_nl(i_t)%this)
temp_nl(i_t)%this%parentNode => arg
! call namespaceFixup(temp_nl(i_t)%this)
enddo
if (arg%childNodes%length==0) then
arg%firstChild => newChild%firstChild
else
newChild%firstChild%previousSibling => arg%lastChild
arg%lastChild%nextSibling => newChild%firstChild
endif
arg%lastChild => newChild%lastChild
newChild%firstChild => null()
newChild%lastChild => null()
deallocate(newChild%childNodes%nodes)
allocate(newChild%childNodes%nodes(0))
newChild%childNodes%length = 0
else
temp_nl(i)%this => newChild
if (i==1) then
arg%firstChild => newChild
newChild%previousSibling => null()
else
temp_nl(i-1)%this%nextSibling => newChild
newChild%previousSibling => temp_nl(i-1)%this
endif
if (getGCstate(arg%ownerDocument)) then
if (arg%inDocument.and..not.newChild%inDocument) then
call putNodesInDocument(arg%ownerDocument, newChild)
endif
endif
newChild%nextSibling => null()
arg%lastChild => newChild
newChild%parentNode => arg
! call namespaceFixup(newChild)
endif
deallocate(arg%childNodes%nodes)
arg%childNodes%nodes => temp_nl
arg%childNodes%length = size(temp_nl)
np => newChild
call updateNodeLists(arg%ownerDocument)
call updateTextContentLength(arg, newChild%textContentLength)
end function appendChild
TOHW_function(hasChildNodes, (arg))
type(Node), pointer :: arg
logical :: hasChildNodes
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
hasChildNodes = associated(arg%firstChild)
end function hasChildNodes
recursive TOHW_function(cloneNode, (arg, deep), np)
! Needs to be recursive in case of entity-references within each other.
type(Node), pointer :: arg
logical, intent(in) :: deep
type(Node), pointer :: np
type(Node), pointer :: doc, treeroot, thatParent, this, new, ERchild
logical :: doneAttributes, doneChildren, readonly, brokenNS
integer :: i_tree
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
thatParent => null()
ERchild => null()
doc => getOwnerDocument(arg)
if (.not.associated(doc)) return
np => null()
brokenNS = doc%docExtras%brokenNS
doc%docExtras%brokenNS = .true. ! May need to do stupid NS things
readonly = .false.
treeroot => arg
TOHW_m_dom_treewalk(`
new => null()
select case(getNodeType(this))
case (ELEMENT_NODE)
if (getParameter(getDomConfig(doc), "namespaces")) then
new => createEmptyElementNS(doc, getNamespaceURI(this), getTagName(this))
else
new => createEmptyElement(doc, getTagName(this))
endif
case (ATTRIBUTE_NODE)
if (getParameter(getDomConfig(doc), "namespaces")) then
new => createAttributeNS(doc, getNamespaceURI(this), getName(this))
else
new => createAttribute(doc, getName(this))
endif
if (associated(this, arg)) then
call setSpecified(new, .true.)
else
call setSpecified(new, getSpecified(this))
endif
case (TEXT_NODE)
new => createTextNode(doc, getData(this))
case (CDATA_SECTION_NODE)
new => createCDataSection(doc, getData(this))
case (ENTITY_REFERENCE_NODE)
ERchild => this
readonly = .true.
new => createEntityReference(doc, getNodeName(this))
doneChildren = .true.
case (ENTITY_NODE)
return
case (PROCESSING_INSTRUCTION_NODE)
new => createProcessingInstruction(doc, getTarget(this), getData(this))
case (COMMENT_NODE)
new => createComment(doc, getData(this))
case (DOCUMENT_NODE)
return
case (DOCUMENT_TYPE_NODE)
return
case (DOCUMENT_FRAGMENT_NODE)
new => createDocumentFragment(doc)
case (NOTATION_NODE)
return
end select
if (.not.associated(thatParent)) then
thatParent => new
elseif (associated(new)) then
if (this%nodeType==ATTRIBUTE_NODE) then
new => setAttributeNode(thatParent, new)
else
new => appendChild(thatParent, new)
endif
endif
if (.not.deep) then
if (getNodeType(arg)==ATTRIBUTE_NODE.or.getNodeType(arg)==ELEMENT_NODE) then
continue
else
exit
endif
endif
', `'`
if (getNodeType(this)==ENTITY_REFERENCE_NODE &
.and.associated(ERchild, this)) then
ERchild => null()
readonly = .false.
endif
this%readonly = readonly
', `parentNode')
np => thatParent
doc%docExtras%brokenNS = brokenNS
end function cloneNode
TOHW_function(hasAttributes, (arg))
type(Node), pointer :: arg
logical :: hasAttributes
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (arg%nodeType == ELEMENT_NODE) then
hasAttributes = (getLength(getAttributes(arg)) > 0)
else
hasAttributes = .false.
endif
end function hasAttributes
! function getBaseURI FIXME
! function compareDocumentPosition FIXME
TOHW_subroutine(normalize, (arg))
type(Node), pointer :: arg
type(Node), pointer :: this, tempNode, oldNode, treeroot
integer :: i_tree, i_t
logical :: doneChildren, doneAttributes
character, pointer :: temp(:)
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
! DOM standard requires we ignore readonly status
treeroot => arg
TOHW_m_dom_treewalk(`
if (getNodeType(this)==TEXT_NODE) then
if (associated(this, arg)) exit ! If we are called on a text node itself, then do nothing.
i_t = getLength(this)
tempNode => getNextSibling(this)
do while (associated(tempNode))
if (getNodeType(tempNode)/=TEXT_NODE) exit
i_t = i_t + getLength(tempNode)
tempNode => getNextSibling(tempNode)
enddo
if (.not.associated(tempNode, getNextSibling(this))) then
allocate(temp(i_t))
temp(:getLength(this)) = vs_str(getData(this))
i_t = getLength(this)
tempNode => getNextSibling(this)
do while (associated(tempNode))
if (getNodeType(tempNode)/=TEXT_NODE) exit
temp(i_t+1:i_t+getLength(tempNode)) = vs_str(getData(tempNode))
i_t = i_t + getLength(tempNode)
oldNode => tempNode
tempNode => getNextSibling(tempNode)
oldNode => removeChild(getParentNode(oldNode), oldNode)
call remove_node_nl(arg%ownerDocument%docExtras%hangingNodes, oldNode)
call destroy(oldNode)
enddo
deallocate(this%nodeValue)
this%nodeValue => temp
endif
end if
',`')
end subroutine normalize
TOHW_function(isSupported, (arg, feature, version), p)
type(Node), pointer :: arg
character(len=*), intent(in) :: feature
character(len=*), intent(in) :: version
logical :: p
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
p = hasFeature(getImplementation(arg%ownerDocument), feature, version)
end function isSupported
pure function getNamespaceURI_len(arg, p) result(n)
type(Node), intent(in) :: arg
logical, intent(in) :: p
integer :: n
n = 0
if (p) then
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
n = size(arg%elExtras%namespaceURI)
endif
endif
end function getNamespaceURI_len
TOHW_function(getNamespaceURI, (arg), c)
type(Node), pointer :: arg
#ifdef RESTRICTED_ASSOCIATED_BUG
character(len=getNamespaceURI_len(arg, .true.)) :: c
#else
character(len=getNamespaceURI_len(arg, associated(arg))) :: c
#endif
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
c = ""
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
c = str_vs(arg%elExtras%namespaceURI)
endif
end function getNamespaceURI
TOHW_m_dom_set(DOMString, namespaceURI, np%elExtras%namespaceURI, (XPATH_NAMESPACE_NODE))
pure function getPrefix_len(arg, p) result(n)
type(Node), intent(in) :: arg
logical, intent(in) :: p
integer :: n
n = 0
if (p) then
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
n = size(arg%elExtras%prefix)
endif
endif
end function getPrefix_len
TOHW_function(getPrefix, (arg), c)
type(Node), pointer :: arg
#ifdef RESTRICTED_ASSOCIATED_BUG
character(len=getPrefix_len(arg, .true.)) :: c
#else
character(len=getPrefix_len(arg, associated(arg))) :: c
#endif
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
c = ""
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
c = str_vs(arg%elExtras%prefix)
endif
end function getPrefix
TOHW_subroutine(setPrefix, (arg, prefix))
type(Node), pointer :: arg
character(len=*) :: prefix
character, pointer :: tmp(:)
integer :: i
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
if (arg%readonly) then
TOHW_m_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR)
elseif (.not.checkName(prefix, getXmlVersionEnum(getOwnerDocument(arg)))) then
TOHW_m_dom_throw_error(INVALID_CHARACTER_ERR)
elseif (.not.checkNCName(prefix, getXmlVersionEnum(getOwnerDocument(arg)))) then
TOHW_m_dom_throw_error(NAMESPACE_ERR)
elseif (size(arg%elExtras%namespaceURI)==0) then
TOHW_m_dom_throw_error(NAMESPACE_ERR)
elseif (prefix=="xml" .and. &
str_vs(arg%elExtras%namespaceURI)/="http://www.w3.org/XML/1998/namespace") then
TOHW_m_dom_throw_error(NAMESPACE_ERR)
elseif (prefix=="xmlns" .and. (getNodeType(arg)/=ATTRIBUTE_NODE &
.or. str_vs(arg%elExtras%namespaceURI)/="http://www.w3.org/2000/xmlns/")) then
TOHW_m_dom_throw_error(NAMESPACE_ERR)
elseif (getNodeType(arg)==ATTRIBUTE_NODE.and.getName(arg)=="xmlns") then
TOHW_m_dom_throw_error(NAMESPACE_ERR)
endif
! FIXME check if prefix is declared and already points to same namespace
! but only if we ever get full error-checking up and running.
deallocate(arg%elExtras%prefix)
arg%elExtras%prefix => vs_str_alloc(prefix)
tmp => arg%nodeName
i = index(str_vs(arg%nodeName), ":")
if (i==0) then
arg%nodeName => vs_str_alloc(prefix//":"//str_vs(tmp))
else
arg%nodeName => vs_str_alloc(prefix//str_vs(tmp(i:)))
endif
deallocate(tmp)
endif
call updateNodeLists(arg%ownerDocument)
end subroutine setPrefix
pure function getLocalName_len(arg, p) result(n)
type(Node), intent(in) :: arg
logical, intent(in) :: p
integer :: n
n = 0
if (p) then
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
n = size(arg%elExtras%localName)
endif
endif
end function getLocalName_len
TOHW_function(getLocalName, (arg), c)
type(Node), pointer :: arg
#ifdef RESTRICTED_ASSOCIATED_BUG
character(len=getLocalName_len(arg, .true.)) :: c
#else
character(len=getLocalName_len(arg, associated(arg))) :: c
#endif
if (.not.associated(arg)) then
TOHW_m_dom_throw_error(FoX_NODE_IS_NULL)
endif
c = ""
if (arg%nodeType==ELEMENT_NODE &
.or. arg%nodeType==ATTRIBUTE_NODE &
.or. arg%nodeType==XPATH_NAMESPACE_NODE) then
c = str_vs(arg%elExtras%localName)
endif
end function getLocalName
recursive TOHW_function(isEqualNode, (arg, other), p)
! We only have one level of recursion, in case of element attributes
type(Node), pointer :: arg
type(Node), pointer :: other
logical :: p
type(Node), pointer :: this, that, treeroot, treeroot2, att1, att2
type(NodeList), pointer :: children1, children2
type(NamedNodeMap), pointer :: atts1, atts2
integer :: i_tree, i
logical :: doneChildren, doneAttributes, equal