/
xml_node.c
824 lines (696 loc) · 17.9 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
#include <xml_node.h>
#ifdef DEBUG
static void debug_node_dealloc(xmlNodePtr x)
{
NOKOGIRI_DEBUG_START(x)
NOKOGIRI_DEBUG_END(x)
}
#else
# define debug_node_dealloc 0
#endif
/* :nodoc: */
typedef xmlNodePtr (*node_other_func)(xmlNodePtr, xmlNodePtr);
/* :nodoc: */
static VALUE reparent_node_with(VALUE node_obj, VALUE other_obj, node_other_func func)
{
VALUE reparented_obj ;
xmlNodePtr node, other, reparented ;
Data_Get_Struct(node_obj, xmlNode, node);
Data_Get_Struct(other_obj, xmlNode, other);
if (node->doc == other->doc) {
xmlUnlinkNode(node) ;
if(!(reparented = (*func)(other, node))) {
rb_raise(rb_eRuntimeError, "Could not reparent node (1)");
}
} else {
xmlNodePtr duped_node ;
// recursively copy to the new document
if (!(duped_node = xmlDocCopyNode(node, other->doc, 1))) {
rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
}
if(!(reparented = (*func)(other, duped_node))) {
rb_raise(rb_eRuntimeError, "Could not reparent node (2)");
}
xmlUnlinkNode(node);
NOKOGIRI_ROOT_NODE(node);
}
// the child was a text node that was coalesced. we need to have the object
// point at SOMETHING, or we'll totally bomb out.
if (reparented != node) {
DATA_PTR(node_obj) = reparented ;
}
// Make sure that our reparented node has the correct namespaces
if(reparented->doc != reparented->parent)
reparented->ns = reparented->parent->ns;
reparented_obj = Nokogiri_wrap_xml_node(reparented);
rb_funcall(reparented_obj, rb_intern("decorate!"), 0);
return reparented_obj ;
}
/*
* 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((int)(node));
}
/*
* call-seq:
* encode_special_chars(string)
*
* Encode any special characters in +string+
*/
static VALUE encode_special_chars(VALUE self, VALUE string)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlChar * encoded = xmlEncodeSpecialChars(
node->doc,
(const xmlChar *)StringValuePtr(string)
);
VALUE encoded_str = NOKOGIRI_STR_NEW2(encoded, node->doc->encoding);
xmlFree(encoded);
return encoded_str;
}
/*
* call-seq:
* internal_subset
*
* Get the internal subset
*/
static VALUE internal_subset(VALUE self)
{
xmlNodePtr node;
xmlDocPtr doc;
Data_Get_Struct(self, xmlNode, node);
if(!node->doc) return Qnil;
doc = node->doc;
xmlDtdPtr dtd = xmlGetIntSubset(doc);
if(!dtd) return Qnil;
return Nokogiri_wrap_xml_node((xmlNodePtr)dtd);
}
/*
* call-seq:
* dup
*
* Copy this node. An optional depth may be passed in, but it defaults
* to a deep copy. 0 is a shallow copy, 1 is a deep copy.
*/
static VALUE duplicate_node(int argc, VALUE *argv, VALUE self)
{
VALUE level;
if(rb_scan_args(argc, argv, "01", &level) == 0)
level = INT2NUM(1);
xmlNodePtr node, dup;
Data_Get_Struct(self, xmlNode, node);
dup = xmlDocCopyNode(node, node->doc, NUM2INT(level));
if(dup == NULL) return Qnil;
return Nokogiri_wrap_xml_node(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);
NOKOGIRI_ROOT_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);
if(1 == xmlIsBlankNode(node))
return Qtrue;
return 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 Nokogiri_wrap_xml_node(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 Nokogiri_wrap_xml_node(sibling);
}
/* :nodoc: */
static VALUE replace(VALUE self, VALUE _new_node)
{
xmlNodePtr node, new_node;
Data_Get_Struct(self, xmlNode, node);
Data_Get_Struct(_new_node, xmlNode, new_node);
xmlReplaceNode(node, new_node);
return self ;
}
/*
* call-seq:
* children
*
* Get the list of children for this node as a NodeSet
*/
static VALUE children(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlNodePtr child = node->children;
xmlNodeSetPtr set = xmlXPathNodeSetCreate(child);
if(!child) return Nokogiri_wrap_xml_node_set(set);
child = child->next;
while(NULL != child) {
xmlXPathNodeSetAdd(set, child);
child = child->next;
}
return Nokogiri_wrap_xml_node_set(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 Nokogiri_wrap_xml_node(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 *)StringValuePtr(attribute)))
return Qtrue;
return Qfalse;
}
/*
* call-seq:
* []=(property, value)
*
* Set the +property+ to +value+
*/
static VALUE set(VALUE self, VALUE property, VALUE value)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlSetProp(node, (xmlChar *)StringValuePtr(property),
(xmlChar *)StringValuePtr(value));
return value;
}
/*
* call-seq:
* get(attribute)
*
* Get the value for +attribute+
*/
static VALUE get(VALUE self, VALUE attribute)
{
xmlNodePtr node;
xmlChar* propstr ;
VALUE rval ;
Data_Get_Struct(self, xmlNode, node);
if(attribute == Qnil) return Qnil;
propstr = xmlGetProp(node, (xmlChar *)StringValuePtr(attribute));
if(NULL == propstr) return Qnil;
rval = NOKOGIRI_STR_NEW2(propstr, node->doc->encoding);
xmlFree(propstr);
return rval ;
}
/*
* 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 *)StringValuePtr(name));
if(! prop) return Qnil;
return Nokogiri_wrap_xml_node((xmlNodePtr)prop);
}
/*
* call-seq:
* attribute_nodes()
*
* returns a list containing the Node attributes.
*/
static VALUE attribute_nodes(VALUE self)
{
/* this code in the mode of xmlHasProp() */
xmlNodePtr node ;
VALUE attr ;
attr = rb_ary_new() ;
Data_Get_Struct(self, xmlNode, node);
Nokogiri_xml_node_properties(node, attr);
return attr ;
}
/*
* call-seq:
* namespace()
*
* returns the namespace prefix for the node, if one exists.
*/
static VALUE namespace(VALUE self)
{
xmlNodePtr node ;
Data_Get_Struct(self, xmlNode, node);
if (node->ns && node->ns->prefix) {
return NOKOGIRI_STR_NEW2(node->ns->prefix, node->doc->encoding);
}
return Qnil ;
}
/*
* call-seq:
* namespaces()
*
* returns a hash containing the node's namespaces.
*/
static VALUE namespaces(VALUE self)
{
/* this code in the mode of xmlHasProp() */
xmlNodePtr node ;
VALUE attr ;
attr = rb_hash_new() ;
Data_Get_Struct(self, xmlNode, node);
Nokogiri_xml_node_namespaces(node, attr);
return attr ;
}
/*
* call-seq:
* node_type
*
* Get the type for this Node
*/
static VALUE node_type(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return INT2NUM((int)node->type);
}
/*
* call-seq:
* content=
*
* Set the content for this Node
*/
static VALUE set_content(VALUE self, VALUE content)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlNodeSetContent(node, (xmlChar *)StringValuePtr(content));
return content;
}
/*
* call-seq:
* content
*
* Returns the content for this Node
*/
static VALUE get_content(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlChar * content = xmlNodeGetContent(node);
if(content) {
VALUE rval = NOKOGIRI_STR_NEW2(content, node->doc->encoding);
xmlFree(content);
return rval;
}
return Qnil;
}
/*
* call-seq:
* add_child(node)
*
* Add +node+ as a child of this node. Returns the new child node.
*/
static VALUE add_child(VALUE self, VALUE child)
{
return reparent_node_with(child, self, xmlAddChild);
}
/*
* call-seq:
* parent
*
* Get the parent Node for this Node
*/
static VALUE get_parent(VALUE self)
{
xmlNodePtr node, parent;
Data_Get_Struct(self, xmlNode, node);
parent = node->parent;
if(!parent) return Qnil;
return Nokogiri_wrap_xml_node(parent) ;
}
/*
* call-seq:
* name=(new_name)
*
* Set the name for this Node
*/
static VALUE set_name(VALUE self, VALUE new_name)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlNodeSetName(node, (xmlChar*)StringValuePtr(new_name));
return new_name;
}
/*
* call-seq:
* name
*
* Returns the name for this Node
*/
static VALUE get_name(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
if(node->name)
return NOKOGIRI_STR_NEW2(node->name, node->doc->encoding);
return Qnil;
}
/*
* call-seq:
* path
*
* Returns the path associated with this Node
*/
static VALUE path(VALUE self)
{
xmlNodePtr node;
xmlChar *path ;
Data_Get_Struct(self, xmlNode, node);
path = xmlGetNodePath(node);
VALUE rval = NOKOGIRI_STR_NEW2(path, node->doc->encoding);
xmlFree(path);
return rval ;
}
/*
* call-seq:
* add_next_sibling(node)
*
* Insert +node+ after this node (as a sibling).
*/
static VALUE add_next_sibling(VALUE self, VALUE rb_node)
{
return reparent_node_with(rb_node, self, xmlAddNextSibling) ;
}
/*
* call-seq:
* add_previous_sibling(node)
*
* Insert +node+ before this node (as a sibling).
*/
static VALUE add_previous_sibling(VALUE self, VALUE rb_node)
{
return reparent_node_with(rb_node, self, xmlAddPrevSibling) ;
}
/*
* call-seq:
* native_write_to(io, encoding, options)
*
* Write this Node to +io+ with +encoding+ and +options+
*/
static VALUE native_write_to(VALUE self, VALUE io, VALUE encoding, VALUE options)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlSaveCtxtPtr savectx = xmlSaveToIO(
(xmlOutputWriteCallback)io_write_callback,
(xmlOutputCloseCallback)io_close_callback,
(void *)io,
RTEST(encoding) ? StringValuePtr(encoding) : NULL,
NUM2INT(options)
);
xmlSaveTree(savectx, node);
xmlSaveClose(savectx);
return io;
}
/*
* call-seq:
* line
*
* Returns the line for this Node
*/
static VALUE line(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return INT2NUM(node->line);
}
/*
* call-seq:
* add_namespace(prefix, href)
*
* Add a namespace with +prefix+ using +href+
*/
static VALUE add_namespace(VALUE self, VALUE prefix, VALUE href)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlNsPtr ns = xmlNewNs(
node,
(const xmlChar *)StringValuePtr(href),
(const xmlChar *)StringValuePtr(prefix)
);
if(NULL == ns) return self;
/*
xmlNewNsProp(
node,
ns,
(const xmlChar *)StringValuePtr(href),
(const xmlChar *)StringValuePtr(prefix)
);
*/
return self;
}
/*
* call-seq:
* new(name)
*
* Create a new node with +name+
*/
static VALUE new(VALUE klass, VALUE name, VALUE document)
{
xmlDocPtr doc;
Data_Get_Struct(document, xmlDoc, doc);
xmlNodePtr node = xmlNewNode(NULL, (xmlChar *)StringValuePtr(name));
node->doc = doc->doc;
VALUE rb_node = Nokogiri_wrap_xml_node(node);
if(rb_block_given_p()) rb_yield(rb_node);
return rb_node;
}
/*
* call-seq:
* dump_html
*
* Returns the Node as html.
*/
static VALUE dump_html(VALUE self)
{
xmlBufferPtr buf ;
xmlNodePtr node ;
Data_Get_Struct(self, xmlNode, node);
if(node->doc->type == XML_DOCUMENT_NODE)
return rb_funcall(self, rb_intern("to_xml"), 0);
buf = xmlBufferCreate() ;
htmlNodeDump(buf, node->doc, node);
VALUE html = NOKOGIRI_STR_NEW2(buf->content, node->doc->encoding);
xmlBufferFree(buf);
return html ;
}
VALUE Nokogiri_wrap_xml_node(xmlNodePtr node)
{
assert(node);
VALUE index = INT2NUM((int)node);
VALUE document = Qnil ;
VALUE node_cache = Qnil ;
VALUE rb_node = Qnil ;
if(node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE)
return DOC_RUBY_OBJECT(node->doc);
if(NULL != node->_private) return (VALUE)node->_private;
switch(node->type)
{
VALUE klass;
case XML_ELEMENT_NODE:
klass = cNokogiriXmlElement;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_TEXT_NODE:
klass = cNokogiriXmlText;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_ENTITY_REF_NODE:
klass = cNokogiriXmlEntityReference;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_COMMENT_NODE:
klass = cNokogiriXmlComment;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_DOCUMENT_FRAG_NODE:
klass = cNokogiriXmlDocumentFragment;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_PI_NODE:
klass = cNokogiriXmlProcessingInstruction;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_ATTRIBUTE_NODE:
klass = cNokogiriXmlAttr;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_ENTITY_DECL:
klass = rb_const_get(mNokogiriXml, rb_intern("EntityDeclaration"));
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_CDATA_SECTION_NODE:
klass = cNokogiriXmlCData;
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
case XML_DTD_NODE:
klass = rb_const_get(mNokogiriXml, rb_intern("DTD"));
rb_node = Data_Wrap_Struct(klass, 0, debug_node_dealloc, node) ;
break;
default:
rb_node = Data_Wrap_Struct(cNokogiriXmlNode, 0, debug_node_dealloc, node) ;
}
node->_private = (void *)rb_node;
if (DOC_RUBY_OBJECT_TEST(node->doc) && DOC_RUBY_OBJECT(node->doc)) {
document = DOC_RUBY_OBJECT(node->doc);
node_cache = rb_funcall(document, rb_intern("node_cache"), 0);
}
if (node_cache != Qnil) rb_hash_aset(node_cache, index, rb_node);
rb_iv_set(rb_node, "@document", document);
rb_funcall(rb_node, rb_intern("decorate!"), 0);
return rb_node ;
}
void Nokogiri_xml_node_properties(xmlNodePtr node, VALUE attr_list)
{
xmlAttrPtr prop;
prop = node->properties ;
while (prop != NULL) {
rb_ary_push(attr_list, Nokogiri_wrap_xml_node((xmlNodePtr)prop));
prop = prop->next ;
}
}
#define XMLNS_PREFIX "xmlns"
#define XMLNS_PREFIX_LEN 6 /* including either colon or \0 */
#define XMLNS_BUFFER_LEN 128
void Nokogiri_xml_node_namespaces(xmlNodePtr node, VALUE attr_hash)
{
xmlNsPtr ns;
static char buffer[XMLNS_BUFFER_LEN] ;
char *key ;
size_t keylen ;
if (node->type != XML_ELEMENT_NODE) return ;
ns = node->nsDef;
while (ns != NULL) {
keylen = XMLNS_PREFIX_LEN + (ns->prefix ? (strlen((const char*)ns->prefix) + 1) : 0) ;
if (keylen > XMLNS_BUFFER_LEN) {
key = (char*)malloc(keylen) ;
} else {
key = buffer ;
}
if (ns->prefix) {
sprintf(key, "%s:%s", XMLNS_PREFIX, ns->prefix);
} else {
sprintf(key, "%s", XMLNS_PREFIX);
}
rb_hash_aset(attr_hash,
NOKOGIRI_STR_NEW2(key, node->doc->encoding),
NOKOGIRI_STR_NEW2(ns->href, node->doc->encoding)
);
if (key != buffer) {
free(key);
}
ns = ns->next ;
}
}
VALUE cNokogiriXmlNode ;
VALUE cNokogiriXmlElement ;
void init_xml_node()
{
VALUE nokogiri = rb_define_module("Nokogiri");
VALUE xml = rb_define_module_under(nokogiri, "XML");
VALUE klass = rb_define_class_under(xml, "Node", rb_cObject);
cNokogiriXmlNode = klass;
cNokogiriXmlElement = rb_define_class_under(xml, "Element", klass);
rb_define_singleton_method(klass, "new", new, 2);
rb_define_method(klass, "add_namespace", add_namespace, 2);
rb_define_method(klass, "node_name", get_name, 0);
rb_define_method(klass, "node_name=", set_name, 1);
rb_define_method(klass, "add_child", add_child, 1);
rb_define_method(klass, "parent", get_parent, 0);
rb_define_method(klass, "child", child, 0);
rb_define_method(klass, "children", children, 0);
rb_define_method(klass, "next_sibling", next_sibling, 0);
rb_define_method(klass, "previous_sibling", previous_sibling, 0);
rb_define_method(klass, "node_type", node_type, 0);
rb_define_method(klass, "content", get_content, 0);
rb_define_method(klass, "path", path, 0);
rb_define_method(klass, "key?", key_eh, 1);
rb_define_method(klass, "blank?", blank_eh, 0);
rb_define_method(klass, "[]=", set, 2);
rb_define_method(klass, "attribute_nodes", attribute_nodes, 0);
rb_define_method(klass, "attribute", attr, 1);
rb_define_method(klass, "namespace", namespace, 0);
rb_define_method(klass, "namespaces", namespaces, 0);
rb_define_method(klass, "add_previous_sibling", add_previous_sibling, 1);
rb_define_method(klass, "add_next_sibling", add_next_sibling, 1);
rb_define_method(klass, "encode_special_chars", encode_special_chars, 1);
rb_define_method(klass, "dup", duplicate_node, -1);
rb_define_method(klass, "unlink", unlink_node, 0);
rb_define_method(klass, "internal_subset", internal_subset, 0);
rb_define_method(klass, "pointer_id", pointer_id, 0);
rb_define_method(klass, "line", line, 0);
rb_define_private_method(klass, "dump_html", dump_html, 0);
rb_define_private_method(klass, "native_write_to", native_write_to, 3);
rb_define_private_method(klass, "replace_with_node", replace, 1);
rb_define_private_method(klass, "native_content=", set_content, 1);
rb_define_private_method(klass, "get", get, 1);
}