/
xml_node.c
1840 lines (1562 loc) · 45 KB
/
xml_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
#include <nokogiri.h>
VALUE cNokogiriXmlNode ;
static ID id_decorate, id_decorate_bang;
#ifdef DEBUG
static void
debug_node_dealloc(xmlNodePtr x)
{
NOKOGIRI_DEBUG_START(x)
NOKOGIRI_DEBUG_END(x)
}
#else
# define debug_node_dealloc 0
#endif
static void
mark(xmlNodePtr node)
{
xmlDocPtr doc = node->doc;
if (doc->type == XML_DOCUMENT_NODE || doc->type == XML_HTML_DOCUMENT_NODE) {
if (DOC_RUBY_OBJECT_TEST(doc)) {
rb_gc_mark(DOC_RUBY_OBJECT(doc));
}
} else if (node->doc->_private) {
rb_gc_mark((VALUE)doc->_private);
}
}
/* :nodoc: */
typedef xmlNodePtr(*pivot_reparentee_func)(xmlNodePtr, xmlNodePtr);
/* :nodoc: */
static void
relink_namespace(xmlNodePtr reparented)
{
xmlNodePtr child;
xmlAttrPtr attr;
if (reparented->type != XML_ATTRIBUTE_NODE &&
reparented->type != XML_ELEMENT_NODE) { return; }
if (reparented->ns == NULL || reparented->ns->prefix == NULL) {
xmlNsPtr ns = NULL;
xmlChar *name = NULL, *prefix = NULL;
name = xmlSplitQName2(reparented->name, &prefix);
if (reparented->type == XML_ATTRIBUTE_NODE) {
if (prefix == NULL || strcmp((char *)prefix, XMLNS_PREFIX) == 0) {
xmlFree(name);
xmlFree(prefix);
return;
}
}
ns = xmlSearchNs(reparented->doc, reparented, prefix);
if (ns != NULL) {
xmlNodeSetName(reparented, name);
xmlSetNs(reparented, ns);
}
xmlFree(name);
xmlFree(prefix);
}
/* Avoid segv when relinking against unlinked nodes. */
if (reparented->type != XML_ELEMENT_NODE || !reparented->parent) { return; }
/* Search our parents for an existing definition */
if (reparented->nsDef) {
xmlNsPtr curr = reparented->nsDef;
xmlNsPtr prev = NULL;
while (curr) {
xmlNsPtr ns = xmlSearchNsByHref(
reparented->doc,
reparented->parent,
curr->href
);
/* If we find the namespace is already declared, remove it from this
* definition list. */
if (ns && ns != curr && xmlStrEqual(ns->prefix, curr->prefix)) {
if (prev) {
prev->next = curr->next;
} else {
reparented->nsDef = curr->next;
}
noko_xml_document_pin_namespace(curr, reparented->doc);
} else {
prev = curr;
}
curr = curr->next;
}
}
/*
* Search our parents for an existing definition of current namespace,
* because the definition it's pointing to may have just been removed nsDef.
*
* And although that would technically probably be OK, I'd feel better if we
* referred to a namespace that's still present in a node's nsDef somewhere
* in the doc.
*/
if (reparented->ns) {
xmlNsPtr ns = xmlSearchNs(reparented->doc, reparented, reparented->ns->prefix);
if (ns
&& ns != reparented->ns
&& xmlStrEqual(ns->prefix, reparented->ns->prefix)
&& xmlStrEqual(ns->href, reparented->ns->href)
) {
xmlSetNs(reparented, ns);
}
}
/* Only walk all children if there actually is a namespace we need to */
/* reparent. */
if (NULL == reparented->ns) { return; }
/* When a node gets reparented, walk it's children to make sure that */
/* their namespaces are reparented as well. */
child = reparented->children;
while (NULL != child) {
relink_namespace(child);
child = child->next;
}
if (reparented->type == XML_ELEMENT_NODE) {
attr = reparented->properties;
while (NULL != attr) {
relink_namespace((xmlNodePtr)attr);
attr = attr->next;
}
}
}
/* :nodoc: */
static xmlNodePtr
xmlReplaceNodeWrapper(xmlNodePtr pivot, xmlNodePtr new_node)
{
xmlNodePtr retval ;
retval = xmlReplaceNode(pivot, new_node) ;
if (retval == pivot) {
retval = new_node ; /* return semantics for reparent_node_with */
}
/* work around libxml2 issue: https://bugzilla.gnome.org/show_bug.cgi?id=615612 */
if (retval && retval->type == XML_TEXT_NODE) {
if (retval->prev && retval->prev->type == XML_TEXT_NODE) {
retval = xmlTextMerge(retval->prev, retval);
}
if (retval->next && retval->next->type == XML_TEXT_NODE) {
retval = xmlTextMerge(retval, retval->next);
}
}
return retval ;
}
/* :nodoc: */
static VALUE
reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_reparentee_func prf)
{
VALUE reparented_obj ;
xmlNodePtr reparentee, original_reparentee, pivot, reparented, next_text, new_next_text, parent ;
int original_ns_prefix_is_default = 0 ;
if (!rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlNode)) {
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
}
if (rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlDocument)) {
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
}
Data_Get_Struct(reparentee_obj, xmlNode, reparentee);
Data_Get_Struct(pivot_obj, xmlNode, pivot);
/*
* Check if nodes given are appropriate to have a parent-child
* relationship, based on the DOM specification.
*
* cf. http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1590626202
*/
if (prf == xmlAddChild) {
parent = pivot;
} else {
parent = pivot->parent;
}
if (parent) {
switch (parent->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
switch (reparentee->type) {
case XML_ELEMENT_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
case XML_DOCUMENT_TYPE_NODE:
/*
* The DOM specification says no to adding text-like nodes
* directly to a document, but we allow it for compatibility.
*/
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_ENTITY_REF_NODE:
goto ok;
default:
break;
}
break;
case XML_DOCUMENT_FRAG_NODE:
case XML_ENTITY_REF_NODE:
case XML_ELEMENT_NODE:
switch (reparentee->type) {
case XML_ELEMENT_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_ENTITY_REF_NODE:
goto ok;
default:
break;
}
break;
case XML_ATTRIBUTE_NODE:
switch (reparentee->type) {
case XML_TEXT_NODE:
case XML_ENTITY_REF_NODE:
goto ok;
default:
break;
}
break;
case XML_TEXT_NODE:
/*
* xmlAddChild() breaks the DOM specification in that it allows
* adding a text node to another, in which case text nodes are
* coalesced, but since our JRuby version does not support such
* operation, we should inhibit it.
*/
break;
default:
break;
}
rb_raise(rb_eArgError, "cannot reparent %s there", rb_obj_classname(reparentee_obj));
}
ok:
original_reparentee = reparentee;
if (reparentee->doc != pivot->doc || reparentee->type == XML_TEXT_NODE) {
/*
* if the reparentee is a text node, there's a very good chance it will be
* merged with an adjacent text node after being reparented, and in that case
* libxml will free the underlying C struct.
*
* since we clearly have a ruby object which references the underlying
* memory, we can't let the C struct get freed. let's pickle the original
* reparentee by rooting it; and then we'll reparent a duplicate of the
* node that we don't care about preserving.
*
* alternatively, if the reparentee is from a different document than the
* pivot node, libxml2 is going to get confused about which document's
* "dictionary" the node's strings belong to (this is an otherwise
* uninteresting libxml2 implementation detail). as a result, we cannot
* reparent the actual reparentee, so we reparent a duplicate.
*/
if (reparentee->type == XML_TEXT_NODE && reparentee->_private) {
/*
* additionally, since we know this C struct isn't going to be related to
* a Ruby object anymore, let's break the relationship on this end as
* well.
*
* this is not absolutely necessary unless libxml-ruby is also in effect,
* in which case its global callback `rxml_node_deregisterNode` will try
* to do things to our data.
*
* for more details on this particular (and particularly nasty) edge
* case, see:
*
* https://github.com/sparklemotion/nokogiri/issues/1426
*/
reparentee->_private = NULL ;
}
if (reparentee->ns != NULL && reparentee->ns->prefix == NULL) {
original_ns_prefix_is_default = 1;
}
noko_xml_document_pin_node(reparentee);
if (!(reparentee = xmlDocCopyNode(reparentee, pivot->doc, 1))) {
rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
}
if (original_ns_prefix_is_default && reparentee->ns != NULL && reparentee->ns->prefix != NULL) {
/*
* issue #391, where new node's prefix may become the string "default"
* see libxml2 tree.c xmlNewReconciliedNs which implements this behavior.
*/
xmlFree(DISCARD_CONST_QUAL_XMLCHAR(reparentee->ns->prefix));
reparentee->ns->prefix = NULL;
}
}
xmlUnlinkNode(original_reparentee);
if (prf != xmlAddPrevSibling && prf != xmlAddNextSibling
&& reparentee->type == XML_TEXT_NODE && pivot->next && pivot->next->type == XML_TEXT_NODE) {
/*
* libxml merges text nodes in a right-to-left fashion, meaning that if
* there are two text nodes who would be adjacent, the right (or following,
* or next) node will be merged into the left (or preceding, or previous)
* node.
*
* and by "merged" I mean the string contents will be concatenated onto the
* left node's contents, and then the node will be freed.
*
* which means that if we have a ruby object wrapped around the right node,
* its memory would be freed out from under it.
*
* so, we detect this edge case and unlink-and-root the text node before it gets
* merged. then we dup the node and insert that duplicate back into the
* document where the real node was.
*
* yes, this is totally lame.
*/
next_text = pivot->next ;
new_next_text = xmlDocCopyNode(next_text, pivot->doc, 1) ;
xmlUnlinkNode(next_text);
noko_xml_document_pin_node(next_text);
xmlAddNextSibling(pivot, new_next_text);
}
if (!(reparented = (*prf)(pivot, reparentee))) {
rb_raise(rb_eRuntimeError, "Could not reparent node");
}
/*
* make sure the ruby object is pointed at the just-reparented node, which
* might be a duplicate (see above) or might be the result of merging
* adjacent text nodes.
*/
DATA_PTR(reparentee_obj) = reparented ;
relink_namespace(reparented);
reparented_obj = noko_xml_node_wrap(Qnil, reparented);
rb_funcall(reparented_obj, id_decorate_bang, 0);
return reparented_obj ;
}
/*
* call-seq:
* document
*
* Get the document for this Node
*/
static VALUE
document(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return DOC_RUBY_OBJECT(node->doc);
}
/*
* call-seq:
* pointer_id
*
* Get the internal pointer number
*/
static VALUE
pointer_id(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return INT2NUM((long)(node));
}
/*
* call-seq:
* encode_special_chars(string)
*
* Encode any special characters in +string+
*/
static VALUE
encode_special_chars(VALUE self, VALUE string)
{
xmlNodePtr node;
xmlChar *encoded;
VALUE encoded_str;
Data_Get_Struct(self, xmlNode, node);
encoded = xmlEncodeSpecialChars(
node->doc,
(const xmlChar *)StringValueCStr(string)
);
encoded_str = NOKOGIRI_STR_NEW2(encoded);
xmlFree(encoded);
return encoded_str;
}
/*
* call-seq:
* create_internal_subset(name, external_id, system_id)
*
* Create the internal subset of a document.
*
* doc.create_internal_subset("chapter", "-//OASIS//DTD DocBook XML//EN", "chapter.dtd")
* # => <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN" "chapter.dtd">
*
* doc.create_internal_subset("chapter", nil, "chapter.dtd")
* # => <!DOCTYPE chapter SYSTEM "chapter.dtd">
*/
static VALUE
create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
doc = node->doc;
if (xmlGetIntSubset(doc)) {
rb_raise(rb_eRuntimeError, "Document already has an internal subset");
}
dtd = xmlCreateIntSubset(
doc,
NIL_P(name) ? NULL : (const xmlChar *)StringValueCStr(name),
NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id),
NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id)
);
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
/*
* call-seq:
* create_external_subset(name, external_id, system_id)
*
* Create an external subset
*/
static VALUE
create_external_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
doc = node->doc;
if (doc->extSubset) {
rb_raise(rb_eRuntimeError, "Document already has an external subset");
}
dtd = xmlNewDtd(
doc,
NIL_P(name) ? NULL : (const xmlChar *)StringValueCStr(name),
NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id),
NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id)
);
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
/*
* call-seq:
* external_subset
*
* Get the external subset
*/
static VALUE
external_subset(VALUE self)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
if (!node->doc) { return Qnil; }
doc = node->doc;
dtd = doc->extSubset;
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
/*
* call-seq:
* internal_subset
*
* Get the internal subset
*/
static VALUE
internal_subset(VALUE self)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
if (!node->doc) { return Qnil; }
doc = node->doc;
dtd = xmlGetIntSubset(doc);
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
/*
* call-seq:
* dup
* dup(depth)
* dup(depth, new_parent_doc)
*
* Copy this node.
* An optional depth may be passed in. 0 is a shallow copy, 1 (the default) is a deep copy.
* An optional new_parent_doc may also be passed in, which will be the new
* node's parent document. Defaults to the current node's document.
* current document.
*/
static VALUE
duplicate_node(int argc, VALUE *argv, VALUE self)
{
VALUE r_level, r_new_parent_doc;
int level;
int n_args;
xmlDocPtr new_parent_doc;
xmlNodePtr node, dup;
Data_Get_Struct(self, xmlNode, node);
n_args = rb_scan_args(argc, argv, "02", &r_level, &r_new_parent_doc);
if (n_args < 1) {
r_level = INT2NUM((long)1);
}
level = (int)NUM2INT(r_level);
if (n_args < 2) {
new_parent_doc = node->doc;
} else {
Data_Get_Struct(r_new_parent_doc, xmlDoc, new_parent_doc);
}
dup = xmlDocCopyNode(node, new_parent_doc, level);
if (dup == NULL) { return Qnil; }
noko_xml_document_pin_node(dup);
return noko_xml_node_wrap(rb_obj_class(self), dup);
}
/*
* call-seq:
* unlink
*
* Unlink this node from its current context.
*/
static VALUE
unlink_node(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlUnlinkNode(node);
noko_xml_document_pin_node(node);
return self;
}
/*
* call-seq:
* blank?
*
* Is this node blank?
*/
static VALUE
blank_eh(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return (1 == xmlIsBlankNode(node)) ? Qtrue : Qfalse ;
}
/*
* call-seq:
* next_sibling
*
* Returns the next sibling node
*/
static VALUE
next_sibling(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = node->next;
if (!sibling) { return Qnil; }
return noko_xml_node_wrap(Qnil, sibling) ;
}
/*
* call-seq:
* previous_sibling
*
* Returns the previous sibling node
*/
static VALUE
previous_sibling(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = node->prev;
if (!sibling) { return Qnil; }
return noko_xml_node_wrap(Qnil, sibling);
}
/*
* call-seq:
* next_element
*
* Returns the next Nokogiri::XML::Element type sibling node.
*/
static VALUE
next_element(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = xmlNextElementSibling(node);
if (!sibling) { return Qnil; }
return noko_xml_node_wrap(Qnil, sibling);
}
/*
* call-seq:
* previous_element
*
* Returns the previous Nokogiri::XML::Element type sibling node.
*/
static VALUE
previous_element(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
/*
* note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
*/
sibling = node->prev;
if (!sibling) { return Qnil; }
while (sibling && sibling->type != XML_ELEMENT_NODE) {
sibling = sibling->prev;
}
return sibling ? noko_xml_node_wrap(Qnil, sibling) : Qnil ;
}
/* :nodoc: */
static VALUE
replace(VALUE self, VALUE new_node)
{
VALUE reparent = reparent_node_with(self, new_node, xmlReplaceNodeWrapper);
xmlNodePtr pivot;
Data_Get_Struct(self, xmlNode, pivot);
noko_xml_document_pin_node(pivot);
return reparent;
}
/*
* call-seq:
* children
*
* Get the list of children for this node as a NodeSet
*/
static VALUE
children(VALUE self)
{
xmlNodePtr node;
xmlNodePtr child;
xmlNodeSetPtr set;
VALUE document;
VALUE node_set;
Data_Get_Struct(self, xmlNode, node);
child = node->children;
set = xmlXPathNodeSetCreate(child);
document = DOC_RUBY_OBJECT(node->doc);
if (!child) { return noko_xml_node_set_wrap(set, document); }
child = child->next;
while (NULL != child) {
xmlXPathNodeSetAddUnique(set, child);
child = child->next;
}
node_set = noko_xml_node_set_wrap(set, document);
return node_set;
}
/*
* call-seq:
* element_children
*
* Get the list of children for this node as a NodeSet. All nodes will be
* element nodes.
*
* Example:
*
* @doc.root.element_children.all? { |x| x.element? } # => true
*/
static VALUE
element_children(VALUE self)
{
xmlNodePtr node;
xmlNodePtr child;
xmlNodeSetPtr set;
VALUE document;
VALUE node_set;
Data_Get_Struct(self, xmlNode, node);
child = xmlFirstElementChild(node);
set = xmlXPathNodeSetCreate(child);
document = DOC_RUBY_OBJECT(node->doc);
if (!child) { return noko_xml_node_set_wrap(set, document); }
child = xmlNextElementSibling(child);
while (NULL != child) {
xmlXPathNodeSetAddUnique(set, child);
child = xmlNextElementSibling(child);
}
node_set = noko_xml_node_set_wrap(set, document);
return node_set;
}
/*
* call-seq:
* child
*
* Returns the child node
*/
static VALUE
child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = node->children;
if (!child) { return Qnil; }
return noko_xml_node_wrap(Qnil, child);
}
/*
* call-seq:
* first_element_child
*
* Returns the first child node of this node that is an element.
*
* Example:
*
* @doc.root.first_element_child.element? # => true
*/
static VALUE
first_element_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = xmlFirstElementChild(node);
if (!child) { return Qnil; }
return noko_xml_node_wrap(Qnil, child);
}
/*
* call-seq:
* last_element_child
*
* Returns the last child node of this node that is an element.
*
* Example:
*
* @doc.root.last_element_child.element? # => true
*/
static VALUE
last_element_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = xmlLastElementChild(node);
if (!child) { return Qnil; }
return noko_xml_node_wrap(Qnil, child);
}
/*
* call-seq:
* key?(attribute)
*
* Returns true if +attribute+ is set
*/
static VALUE
key_eh(VALUE self, VALUE attribute)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
if (xmlHasProp(node, (xmlChar *)StringValueCStr(attribute))) {
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* namespaced_key?(attribute, namespace)
*
* Returns true if +attribute+ is set with +namespace+
*/
static VALUE
namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
if (xmlHasNsProp(node, (xmlChar *)StringValueCStr(attribute),
NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace))) {
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* []=(property, value)
*
* Set the +property+ to +value+
*/
static VALUE
set(VALUE self, VALUE property, VALUE value)
{
xmlNodePtr node, cur;
xmlAttrPtr prop;
Data_Get_Struct(self, xmlNode, node);
/* If a matching attribute node already exists, then xmlSetProp will destroy
* the existing node's children. However, if Nokogiri has a node object
* pointing to one of those children, we are left with a broken reference.
*
* We can avoid this by unlinking these nodes first.
*/
if (node->type != XML_ELEMENT_NODE) {
return (Qnil);
}
prop = xmlHasProp(node, (xmlChar *)StringValueCStr(property));
if (prop && prop->children) {
for (cur = prop->children; cur; cur = cur->next) {
if (cur->_private) {
noko_xml_document_pin_node(cur);
xmlUnlinkNode(cur);
}
}
}
xmlSetProp(node, (xmlChar *)StringValueCStr(property),
(xmlChar *)StringValueCStr(value));
return value;
}
/*
* call-seq:
* get(attribute)
*
* Get the value for +attribute+
*/
static VALUE
get(VALUE self, VALUE rattribute)
{
xmlNodePtr node;
xmlChar *value = 0;
VALUE rvalue;
xmlChar *colon;
xmlChar *attribute, *attr_name, *prefix;
xmlNsPtr ns;
if (NIL_P(rattribute)) { return Qnil; }
Data_Get_Struct(self, xmlNode, node);
attribute = xmlCharStrdup(StringValueCStr(rattribute));
colon = DISCARD_CONST_QUAL_XMLCHAR(xmlStrchr(attribute, (const xmlChar)':'));
if (colon) {
/* split the attribute string into separate prefix and name by
* null-terminating the prefix at the colon */
prefix = attribute;
attr_name = colon + 1;
(*colon) = 0;
ns = xmlSearchNs(node->doc, node, prefix);
if (ns) {
value = xmlGetNsProp(node, attr_name, ns->href);
} else {
value = xmlGetProp(node, (xmlChar *)StringValueCStr(rattribute));
}
} else {
value = xmlGetNoNsProp(node, attribute);
}
xmlFree((void *)attribute);
if (!value) { return Qnil; }
rvalue = NOKOGIRI_STR_NEW2(value);
xmlFree((void *)value);
return rvalue ;
}
/*
* call-seq:
* set_namespace(namespace)
*
* Set the namespace to +namespace+
*/
static VALUE
set_namespace(VALUE self, VALUE namespace)
{
xmlNodePtr node;
xmlNsPtr ns = NULL;
Data_Get_Struct(self, xmlNode, node);
if (!NIL_P(namespace)) {
Data_Get_Struct(namespace, xmlNs, ns);
}
xmlSetNs(node, ns);
return self;
}
/*
* call-seq:
* attribute(name)
*
* Get the attribute node with +name+
*/
static VALUE
attr(VALUE self, VALUE name)
{
xmlNodePtr node;
xmlAttrPtr prop;
Data_Get_Struct(self, xmlNode, node);
prop = xmlHasProp(node, (xmlChar *)StringValueCStr(name));