nokogiri 1.6.2.rc1-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.autotest +26 -0
- data/.editorconfig +17 -0
- data/.gemtest +0 -0
- data/.travis.yml +25 -0
- data/CHANGELOG.ja.rdoc +857 -0
- data/CHANGELOG.rdoc +880 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Gemfile +21 -0
- data/Manifest.txt +371 -0
- data/README.ja.rdoc +112 -0
- data/README.rdoc +180 -0
- data/ROADMAP.md +89 -0
- data/Rakefile +351 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +78 -0
- data/build_all +130 -0
- data/dependencies.yml +4 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +453 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +148 -0
- data/ext/nokogiri/nokogiri.h +164 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +577 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +56 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +78 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1541 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +467 -0
- data/ext/nokogiri/xml_node_set.h +14 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +681 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +312 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +63 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +307 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +270 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/nokogiri.rb +137 -0
- data/lib/nokogiri/2.0/nokogiri.so +0 -0
- data/lib/nokogiri/2.1/nokogiri.so +0 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +52 -0
- data/lib/nokogiri/css/parser.rb +715 -0
- data/lib/nokogiri/css/parser.y +249 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +219 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +37 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +333 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/html/sax/push_parser.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +106 -0
- data/lib/nokogiri/xml.rb +73 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +443 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +279 -0
- data/lib/nokogiri/xml/document_fragment.rb +112 -0
- data/lib/nokogiri/xml/dtd.rb +32 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +982 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +355 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +98 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +171 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +56 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +102 -0
- data/suppressions/README.txt +1 -0
- data/suppressions/nokogiri_ree-1.8.7.358.supp +61 -0
- data/suppressions/nokogiri_ruby-1.8.7.370.supp +0 -0
- data/suppressions/nokogiri_ruby-1.9.2.320.supp +28 -0
- data/suppressions/nokogiri_ruby-1.9.3.327.supp +28 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +222 -0
- data/test/css/test_parser.rb +358 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +96 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/atom.xml +344 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/bogus.xml +0 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/saml/saml20assertion_schema.xsd +283 -0
- data/test/files/saml/saml20protocol_schema.xsd +302 -0
- data/test/files/saml/xenc_schema.xsd +146 -0
- data/test/files/saml/xmldsig_schema.xsd +318 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/shift_jis_no_charset.html +9 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/test_document_url/bar.xml +2 -0
- data/test/files/test_document_url/document.dtd +4 -0
- data/test/files/test_document_url/document.xml +6 -0
- data/test/files/tlm.html +850 -0
- data/test/files/to_be_xincluded.xml +2 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/files/xinclude.xml +4 -0
- data/test/helper.rb +164 -0
- data/test/html/sax/test_parser.rb +141 -0
- data/test/html/sax/test_parser_context.rb +46 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +619 -0
- data/test/html/test_document_encoding.rb +148 -0
- data/test/html/test_document_fragment.rb +261 -0
- data/test/html/test_element_description.rb +105 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +196 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +14 -0
- data/test/namespaces/test_namespaces_in_builder_doc.rb +75 -0
- data/test/namespaces/test_namespaces_in_cloned_doc.rb +31 -0
- data/test/namespaces/test_namespaces_in_created_doc.rb +75 -0
- data/test/namespaces/test_namespaces_in_parsed_doc.rb +66 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +156 -0
- data/test/test_nokogiri.rb +138 -0
- data/test/test_reader.rb +558 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +279 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +382 -0
- data/test/xml/sax/test_parser_context.rb +115 -0
- data/test/xml/sax/test_push_parser.rb +157 -0
- data/test/xml/test_attr.rb +64 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +315 -0
- data/test/xml/test_c14n.rb +161 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +934 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +228 -0
- data/test/xml/test_dtd.rb +187 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +245 -0
- data/test/xml/test_namespace.rb +95 -0
- data/test/xml/test_node.rb +1155 -0
- data/test/xml/test_node_attributes.rb +113 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_inheritance.rb +32 -0
- data/test/xml/test_node_reparenting.rb +374 -0
- data/test/xml/test_node_set.rb +755 -0
- data/test/xml/test_parse_options.rb +64 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +142 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +129 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +45 -0
- data/test/xml/test_unparented_node.rb +422 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +376 -0
- data/test/xslt/test_custom_functions.rb +133 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +81 -0
- metadata +601 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
#ifndef HAVE_XMLFIRSTELEMENTCHILD
|
2
|
+
|
3
|
+
#ifndef XML_LIBXML2_HACKS
|
4
|
+
#define XML_LIBXML2_HACKS
|
5
|
+
|
6
|
+
xmlNodePtr xmlFirstElementChild(xmlNodePtr parent);
|
7
|
+
xmlNodePtr xmlNextElementSibling(xmlNodePtr node);
|
8
|
+
xmlNodePtr xmlLastElementChild(xmlNodePtr parent);
|
9
|
+
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#endif
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#include <xml_namespace.h>
|
2
|
+
|
3
|
+
VALUE cNokogiriXmlNamespace ;
|
4
|
+
|
5
|
+
/*
|
6
|
+
* call-seq:
|
7
|
+
* prefix
|
8
|
+
*
|
9
|
+
* Get the prefix for this namespace. Returns +nil+ if there is no prefix.
|
10
|
+
*/
|
11
|
+
static VALUE prefix(VALUE self)
|
12
|
+
{
|
13
|
+
xmlNsPtr ns;
|
14
|
+
|
15
|
+
Data_Get_Struct(self, xmlNs, ns);
|
16
|
+
if(!ns->prefix) return Qnil;
|
17
|
+
|
18
|
+
return NOKOGIRI_STR_NEW2(ns->prefix);
|
19
|
+
}
|
20
|
+
|
21
|
+
/*
|
22
|
+
* call-seq:
|
23
|
+
* href
|
24
|
+
*
|
25
|
+
* Get the href for this namespace
|
26
|
+
*/
|
27
|
+
static VALUE href(VALUE self)
|
28
|
+
{
|
29
|
+
xmlNsPtr ns;
|
30
|
+
|
31
|
+
Data_Get_Struct(self, xmlNs, ns);
|
32
|
+
if(!ns->href) return Qnil;
|
33
|
+
|
34
|
+
return NOKOGIRI_STR_NEW2(ns->href);
|
35
|
+
}
|
36
|
+
|
37
|
+
VALUE Nokogiri_wrap_xml_namespace(xmlDocPtr doc, xmlNsPtr node)
|
38
|
+
{
|
39
|
+
VALUE ns, document, node_cache;
|
40
|
+
|
41
|
+
assert(doc->_private);
|
42
|
+
|
43
|
+
if(node->_private)
|
44
|
+
return (VALUE)node->_private;
|
45
|
+
|
46
|
+
ns = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, node);
|
47
|
+
|
48
|
+
document = DOC_RUBY_OBJECT(doc);
|
49
|
+
|
50
|
+
node_cache = rb_iv_get(document, "@node_cache");
|
51
|
+
rb_ary_push(node_cache, ns);
|
52
|
+
|
53
|
+
rb_iv_set(ns, "@document", DOC_RUBY_OBJECT(doc));
|
54
|
+
|
55
|
+
node->_private = (void *)ns;
|
56
|
+
|
57
|
+
return ns;
|
58
|
+
}
|
59
|
+
|
60
|
+
VALUE Nokogiri_wrap_xml_namespace2(VALUE document, xmlNsPtr node)
|
61
|
+
{
|
62
|
+
xmlDocPtr doc;
|
63
|
+
Data_Get_Struct(document, xmlDoc, doc) ;
|
64
|
+
return Nokogiri_wrap_xml_namespace(doc, node);
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
void init_xml_namespace()
|
69
|
+
{
|
70
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
71
|
+
VALUE xml = rb_define_module_under(nokogiri, "XML");
|
72
|
+
VALUE klass = rb_define_class_under(xml, "Namespace", rb_cObject);
|
73
|
+
|
74
|
+
cNokogiriXmlNamespace = klass;
|
75
|
+
|
76
|
+
rb_define_method(klass, "prefix", prefix, 0);
|
77
|
+
rb_define_method(klass, "href", href, 0);
|
78
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#ifndef NOKOGIRI_XML_NAMESPACE
|
2
|
+
#define NOKOGIRI_XML_NAMESPACE
|
3
|
+
|
4
|
+
#include <nokogiri.h>
|
5
|
+
|
6
|
+
void init_xml_namespace();
|
7
|
+
|
8
|
+
extern VALUE cNokogiriXmlNamespace ;
|
9
|
+
|
10
|
+
VALUE Nokogiri_wrap_xml_namespace(xmlDocPtr doc, xmlNsPtr node) ;
|
11
|
+
VALUE Nokogiri_wrap_xml_namespace2(VALUE document, xmlNsPtr node) ;
|
12
|
+
|
13
|
+
#endif
|
@@ -0,0 +1,1541 @@
|
|
1
|
+
#include <xml_node.h>
|
2
|
+
|
3
|
+
static ID decorate, decorate_bang;
|
4
|
+
|
5
|
+
#ifdef DEBUG
|
6
|
+
static void debug_node_dealloc(xmlNodePtr x)
|
7
|
+
{
|
8
|
+
NOKOGIRI_DEBUG_START(x)
|
9
|
+
NOKOGIRI_DEBUG_END(x)
|
10
|
+
}
|
11
|
+
#else
|
12
|
+
# define debug_node_dealloc 0
|
13
|
+
#endif
|
14
|
+
|
15
|
+
static void mark(xmlNodePtr node)
|
16
|
+
{
|
17
|
+
xmlNodePtr doc = node->doc;
|
18
|
+
if(doc->type == XML_DOCUMENT_NODE || doc->type == XML_HTML_DOCUMENT_NODE) {
|
19
|
+
if(DOC_RUBY_OBJECT_TEST(doc)) {
|
20
|
+
rb_gc_mark(DOC_RUBY_OBJECT(doc));
|
21
|
+
}
|
22
|
+
} else if(node->doc->_private) {
|
23
|
+
rb_gc_mark((VALUE)doc->_private);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
/* :nodoc: */
|
28
|
+
typedef xmlNodePtr (*pivot_reparentee_func)(xmlNodePtr, xmlNodePtr);
|
29
|
+
|
30
|
+
/* :nodoc: */
|
31
|
+
static void relink_namespace(xmlNodePtr reparented)
|
32
|
+
{
|
33
|
+
xmlChar *name, *prefix;
|
34
|
+
xmlNodePtr child;
|
35
|
+
xmlNsPtr ns;
|
36
|
+
|
37
|
+
if (reparented->type != XML_ATTRIBUTE_NODE &&
|
38
|
+
reparented->type != XML_ELEMENT_NODE) return;
|
39
|
+
|
40
|
+
if (reparented->ns == NULL || reparented->ns->prefix == NULL) {
|
41
|
+
name = xmlSplitQName2(reparented->name, &prefix);
|
42
|
+
|
43
|
+
if(reparented->type == XML_ATTRIBUTE_NODE) {
|
44
|
+
if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) return;
|
45
|
+
}
|
46
|
+
|
47
|
+
ns = xmlSearchNs(reparented->doc, reparented, prefix);
|
48
|
+
|
49
|
+
if (ns == NULL && reparented->parent) {
|
50
|
+
ns = xmlSearchNs(reparented->doc, reparented->parent, prefix);
|
51
|
+
}
|
52
|
+
|
53
|
+
if (ns != NULL) {
|
54
|
+
xmlNodeSetName(reparented, name);
|
55
|
+
xmlSetNs(reparented, ns);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
/* Avoid segv when relinking against unlinked nodes. */
|
60
|
+
if (reparented->type != XML_ELEMENT_NODE || !reparented->parent) return;
|
61
|
+
|
62
|
+
/* Make sure that our reparented node has the correct namespaces */
|
63
|
+
if(!reparented->ns && reparented->doc != (xmlDocPtr)reparented->parent)
|
64
|
+
xmlSetNs(reparented, reparented->parent->ns);
|
65
|
+
|
66
|
+
/* Search our parents for an existing definition */
|
67
|
+
if(reparented->nsDef) {
|
68
|
+
xmlNsPtr curr = reparented->nsDef;
|
69
|
+
xmlNsPtr prev = NULL;
|
70
|
+
|
71
|
+
while(curr) {
|
72
|
+
xmlNsPtr ns = xmlSearchNsByHref(
|
73
|
+
reparented->doc,
|
74
|
+
reparented->parent,
|
75
|
+
curr->href
|
76
|
+
);
|
77
|
+
/* If we find the namespace is already declared, remove it from this
|
78
|
+
* definition list. */
|
79
|
+
if(ns && ns != curr) {
|
80
|
+
if (prev) {
|
81
|
+
prev->next = curr->next;
|
82
|
+
} else {
|
83
|
+
reparented->nsDef = curr->next;
|
84
|
+
}
|
85
|
+
nokogiri_root_nsdef(curr, reparented->doc);
|
86
|
+
} else {
|
87
|
+
prev = curr;
|
88
|
+
}
|
89
|
+
curr = curr->next;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
/* Only walk all children if there actually is a namespace we need to */
|
94
|
+
/* reparent. */
|
95
|
+
if(NULL == reparented->ns) return;
|
96
|
+
|
97
|
+
/* When a node gets reparented, walk it's children to make sure that */
|
98
|
+
/* their namespaces are reparented as well. */
|
99
|
+
child = reparented->children;
|
100
|
+
while(NULL != child) {
|
101
|
+
relink_namespace(child);
|
102
|
+
child = child->next;
|
103
|
+
}
|
104
|
+
|
105
|
+
if (reparented->type == XML_ELEMENT_NODE) {
|
106
|
+
child = (xmlNodePtr)((xmlElementPtr)reparented)->attributes;
|
107
|
+
while(NULL != child) {
|
108
|
+
relink_namespace(child);
|
109
|
+
child = child->next;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
/* :nodoc: */
|
115
|
+
static xmlNodePtr xmlReplaceNodeWrapper(xmlNodePtr pivot, xmlNodePtr new_node)
|
116
|
+
{
|
117
|
+
xmlNodePtr retval ;
|
118
|
+
|
119
|
+
retval = xmlReplaceNode(pivot, new_node) ;
|
120
|
+
|
121
|
+
if (retval == pivot) {
|
122
|
+
retval = new_node ; /* return semantics for reparent_node_with */
|
123
|
+
}
|
124
|
+
|
125
|
+
/* work around libxml2 issue: https://bugzilla.gnome.org/show_bug.cgi?id=615612 */
|
126
|
+
if (retval && retval->type == XML_TEXT_NODE) {
|
127
|
+
if (retval->prev && retval->prev->type == XML_TEXT_NODE) {
|
128
|
+
retval = xmlTextMerge(retval->prev, retval);
|
129
|
+
}
|
130
|
+
if (retval->next && retval->next->type == XML_TEXT_NODE) {
|
131
|
+
retval = xmlTextMerge(retval, retval->next);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
return retval ;
|
136
|
+
}
|
137
|
+
|
138
|
+
/* :nodoc: */
|
139
|
+
static VALUE reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_reparentee_func prf)
|
140
|
+
{
|
141
|
+
VALUE reparented_obj ;
|
142
|
+
xmlNodePtr reparentee, pivot, reparented, next_text, new_next_text ;
|
143
|
+
|
144
|
+
if(!rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlNode))
|
145
|
+
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
|
146
|
+
if(rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlDocument))
|
147
|
+
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
|
148
|
+
|
149
|
+
Data_Get_Struct(reparentee_obj, xmlNode, reparentee);
|
150
|
+
Data_Get_Struct(pivot_obj, xmlNode, pivot);
|
151
|
+
|
152
|
+
if(XML_DOCUMENT_NODE == reparentee->type || XML_HTML_DOCUMENT_NODE == reparentee->type)
|
153
|
+
rb_raise(rb_eArgError, "cannot reparent a document node");
|
154
|
+
|
155
|
+
xmlUnlinkNode(reparentee);
|
156
|
+
|
157
|
+
if (reparentee->doc != pivot->doc || reparentee->type == XML_TEXT_NODE) {
|
158
|
+
/*
|
159
|
+
* if the reparentee is a text node, there's a very good chance it will be
|
160
|
+
* merged with an adjacent text node after being reparented, and in that case
|
161
|
+
* libxml will free the underlying C struct.
|
162
|
+
*
|
163
|
+
* since we clearly have a ruby object which references the underlying
|
164
|
+
* memory, we can't let the C struct get freed. let's pickle the original
|
165
|
+
* reparentee by rooting it; and then we'll reparent a duplicate of the
|
166
|
+
* node that we don't care about preserving.
|
167
|
+
*
|
168
|
+
* alternatively, if the reparentee is from a different document than the
|
169
|
+
* pivot node, libxml2 is going to get confused about which document's
|
170
|
+
* "dictionary" the node's strings belong to (this is an otherwise
|
171
|
+
* uninteresting libxml2 implementation detail). as a result, we cannot
|
172
|
+
* reparent the actual reparentee, so we reparent a duplicate.
|
173
|
+
*/
|
174
|
+
nokogiri_root_node(reparentee);
|
175
|
+
if (!(reparentee = xmlDocCopyNode(reparentee, pivot->doc, 1))) {
|
176
|
+
rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
if (prf != xmlAddPrevSibling && prf != xmlAddNextSibling
|
181
|
+
&& reparentee->type == XML_TEXT_NODE && pivot->next && pivot->next->type == XML_TEXT_NODE) {
|
182
|
+
/*
|
183
|
+
* libxml merges text nodes in a right-to-left fashion, meaning that if
|
184
|
+
* there are two text nodes who would be adjacent, the right (or following,
|
185
|
+
* or next) node will be merged into the left (or preceding, or previous)
|
186
|
+
* node.
|
187
|
+
*
|
188
|
+
* and by "merged" I mean the string contents will be concatenated onto the
|
189
|
+
* left node's contents, and then the node will be freed.
|
190
|
+
*
|
191
|
+
* which means that if we have a ruby object wrapped around the right node,
|
192
|
+
* its memory would be freed out from under it.
|
193
|
+
*
|
194
|
+
* so, we detect this edge case and unlink-and-root the text node before it gets
|
195
|
+
* merged. then we dup the node and insert that duplicate back into the
|
196
|
+
* document where the real node was.
|
197
|
+
*
|
198
|
+
* yes, this is totally lame.
|
199
|
+
*/
|
200
|
+
next_text = pivot->next ;
|
201
|
+
new_next_text = xmlDocCopyNode(next_text, pivot->doc, 1) ;
|
202
|
+
|
203
|
+
xmlUnlinkNode(next_text);
|
204
|
+
nokogiri_root_node(next_text);
|
205
|
+
|
206
|
+
xmlAddNextSibling(pivot, new_next_text);
|
207
|
+
}
|
208
|
+
|
209
|
+
if(!(reparented = (*prf)(pivot, reparentee))) {
|
210
|
+
rb_raise(rb_eRuntimeError, "Could not reparent node");
|
211
|
+
}
|
212
|
+
|
213
|
+
/*
|
214
|
+
* make sure the ruby object is pointed at the just-reparented node, which
|
215
|
+
* might be a duplicate (see above) or might be the result of merging
|
216
|
+
* adjacent text nodes.
|
217
|
+
*/
|
218
|
+
DATA_PTR(reparentee_obj) = reparented ;
|
219
|
+
|
220
|
+
relink_namespace(reparented);
|
221
|
+
|
222
|
+
reparented_obj = Nokogiri_wrap_xml_node(Qnil, reparented);
|
223
|
+
|
224
|
+
rb_funcall(reparented_obj, decorate_bang, 0);
|
225
|
+
|
226
|
+
return reparented_obj ;
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
/*
|
231
|
+
* call-seq:
|
232
|
+
* document
|
233
|
+
*
|
234
|
+
* Get the document for this Node
|
235
|
+
*/
|
236
|
+
static VALUE document(VALUE self)
|
237
|
+
{
|
238
|
+
xmlNodePtr node;
|
239
|
+
Data_Get_Struct(self, xmlNode, node);
|
240
|
+
return DOC_RUBY_OBJECT(node->doc);
|
241
|
+
}
|
242
|
+
|
243
|
+
/*
|
244
|
+
* call-seq:
|
245
|
+
* pointer_id
|
246
|
+
*
|
247
|
+
* Get the internal pointer number
|
248
|
+
*/
|
249
|
+
static VALUE pointer_id(VALUE self)
|
250
|
+
{
|
251
|
+
xmlNodePtr node;
|
252
|
+
Data_Get_Struct(self, xmlNode, node);
|
253
|
+
|
254
|
+
return INT2NUM((long)(node));
|
255
|
+
}
|
256
|
+
|
257
|
+
/*
|
258
|
+
* call-seq:
|
259
|
+
* encode_special_chars(string)
|
260
|
+
*
|
261
|
+
* Encode any special characters in +string+
|
262
|
+
*/
|
263
|
+
static VALUE encode_special_chars(VALUE self, VALUE string)
|
264
|
+
{
|
265
|
+
xmlNodePtr node;
|
266
|
+
xmlChar *encoded;
|
267
|
+
VALUE encoded_str;
|
268
|
+
|
269
|
+
Data_Get_Struct(self, xmlNode, node);
|
270
|
+
encoded = xmlEncodeSpecialChars(
|
271
|
+
node->doc,
|
272
|
+
(const xmlChar *)StringValuePtr(string)
|
273
|
+
);
|
274
|
+
|
275
|
+
encoded_str = NOKOGIRI_STR_NEW2(encoded);
|
276
|
+
xmlFree(encoded);
|
277
|
+
|
278
|
+
return encoded_str;
|
279
|
+
}
|
280
|
+
|
281
|
+
/*
|
282
|
+
* call-seq:
|
283
|
+
* create_internal_subset(name, external_id, system_id)
|
284
|
+
*
|
285
|
+
* Create the internal subset of a document.
|
286
|
+
*
|
287
|
+
* doc.create_internal_subset("chapter", "-//OASIS//DTD DocBook XML//EN", "chapter.dtd")
|
288
|
+
* # => <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN" "chapter.dtd">
|
289
|
+
*
|
290
|
+
* doc.create_internal_subset("chapter", nil, "chapter.dtd")
|
291
|
+
* # => <!DOCTYPE chapter SYSTEM "chapter.dtd">
|
292
|
+
*/
|
293
|
+
static VALUE create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
|
294
|
+
{
|
295
|
+
xmlNodePtr node;
|
296
|
+
xmlDocPtr doc;
|
297
|
+
xmlDtdPtr dtd;
|
298
|
+
|
299
|
+
Data_Get_Struct(self, xmlNode, node);
|
300
|
+
|
301
|
+
doc = node->doc;
|
302
|
+
|
303
|
+
if(xmlGetIntSubset(doc))
|
304
|
+
rb_raise(rb_eRuntimeError, "Document already has an internal subset");
|
305
|
+
|
306
|
+
dtd = xmlCreateIntSubset(
|
307
|
+
doc,
|
308
|
+
NIL_P(name) ? NULL : (const xmlChar *)StringValuePtr(name),
|
309
|
+
NIL_P(external_id) ? NULL : (const xmlChar *)StringValuePtr(external_id),
|
310
|
+
NIL_P(system_id) ? NULL : (const xmlChar *)StringValuePtr(system_id)
|
311
|
+
);
|
312
|
+
|
313
|
+
if(!dtd) return Qnil;
|
314
|
+
|
315
|
+
return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
|
316
|
+
}
|
317
|
+
|
318
|
+
/*
|
319
|
+
* call-seq:
|
320
|
+
* create_external_subset(name, external_id, system_id)
|
321
|
+
*
|
322
|
+
* Create an external subset
|
323
|
+
*/
|
324
|
+
static VALUE create_external_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
|
325
|
+
{
|
326
|
+
xmlNodePtr node;
|
327
|
+
xmlDocPtr doc;
|
328
|
+
xmlDtdPtr dtd;
|
329
|
+
|
330
|
+
Data_Get_Struct(self, xmlNode, node);
|
331
|
+
|
332
|
+
doc = node->doc;
|
333
|
+
|
334
|
+
if(doc->extSubset)
|
335
|
+
rb_raise(rb_eRuntimeError, "Document already has an external subset");
|
336
|
+
|
337
|
+
dtd = xmlNewDtd(
|
338
|
+
doc,
|
339
|
+
NIL_P(name) ? NULL : (const xmlChar *)StringValuePtr(name),
|
340
|
+
NIL_P(external_id) ? NULL : (const xmlChar *)StringValuePtr(external_id),
|
341
|
+
NIL_P(system_id) ? NULL : (const xmlChar *)StringValuePtr(system_id)
|
342
|
+
);
|
343
|
+
|
344
|
+
if(!dtd) return Qnil;
|
345
|
+
|
346
|
+
return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
|
347
|
+
}
|
348
|
+
|
349
|
+
/*
|
350
|
+
* call-seq:
|
351
|
+
* external_subset
|
352
|
+
*
|
353
|
+
* Get the external subset
|
354
|
+
*/
|
355
|
+
static VALUE external_subset(VALUE self)
|
356
|
+
{
|
357
|
+
xmlNodePtr node;
|
358
|
+
xmlDocPtr doc;
|
359
|
+
xmlDtdPtr dtd;
|
360
|
+
|
361
|
+
Data_Get_Struct(self, xmlNode, node);
|
362
|
+
|
363
|
+
if(!node->doc) return Qnil;
|
364
|
+
|
365
|
+
doc = node->doc;
|
366
|
+
dtd = doc->extSubset;
|
367
|
+
|
368
|
+
if(!dtd) return Qnil;
|
369
|
+
|
370
|
+
return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
|
371
|
+
}
|
372
|
+
|
373
|
+
/*
|
374
|
+
* call-seq:
|
375
|
+
* internal_subset
|
376
|
+
*
|
377
|
+
* Get the internal subset
|
378
|
+
*/
|
379
|
+
static VALUE internal_subset(VALUE self)
|
380
|
+
{
|
381
|
+
xmlNodePtr node;
|
382
|
+
xmlDocPtr doc;
|
383
|
+
xmlDtdPtr dtd;
|
384
|
+
|
385
|
+
Data_Get_Struct(self, xmlNode, node);
|
386
|
+
|
387
|
+
if(!node->doc) return Qnil;
|
388
|
+
|
389
|
+
doc = node->doc;
|
390
|
+
dtd = xmlGetIntSubset(doc);
|
391
|
+
|
392
|
+
if(!dtd) return Qnil;
|
393
|
+
|
394
|
+
return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
|
395
|
+
}
|
396
|
+
|
397
|
+
/*
|
398
|
+
* call-seq:
|
399
|
+
* dup
|
400
|
+
*
|
401
|
+
* Copy this node. An optional depth may be passed in, but it defaults
|
402
|
+
* to a deep copy. 0 is a shallow copy, 1 is a deep copy.
|
403
|
+
*/
|
404
|
+
static VALUE duplicate_node(int argc, VALUE *argv, VALUE self)
|
405
|
+
{
|
406
|
+
VALUE level;
|
407
|
+
xmlNodePtr node, dup;
|
408
|
+
|
409
|
+
if(rb_scan_args(argc, argv, "01", &level) == 0)
|
410
|
+
level = INT2NUM((long)1);
|
411
|
+
|
412
|
+
Data_Get_Struct(self, xmlNode, node);
|
413
|
+
|
414
|
+
dup = xmlDocCopyNode(node, node->doc, (int)NUM2INT(level));
|
415
|
+
if(dup == NULL) return Qnil;
|
416
|
+
|
417
|
+
nokogiri_root_node(dup);
|
418
|
+
|
419
|
+
return Nokogiri_wrap_xml_node(rb_obj_class(self), dup);
|
420
|
+
}
|
421
|
+
|
422
|
+
/*
|
423
|
+
* call-seq:
|
424
|
+
* unlink
|
425
|
+
*
|
426
|
+
* Unlink this node from its current context.
|
427
|
+
*/
|
428
|
+
static VALUE unlink_node(VALUE self)
|
429
|
+
{
|
430
|
+
xmlNodePtr node;
|
431
|
+
Data_Get_Struct(self, xmlNode, node);
|
432
|
+
xmlUnlinkNode(node);
|
433
|
+
nokogiri_root_node(node);
|
434
|
+
return self;
|
435
|
+
}
|
436
|
+
|
437
|
+
/*
|
438
|
+
* call-seq:
|
439
|
+
* blank?
|
440
|
+
*
|
441
|
+
* Is this node blank?
|
442
|
+
*/
|
443
|
+
static VALUE blank_eh(VALUE self)
|
444
|
+
{
|
445
|
+
xmlNodePtr node;
|
446
|
+
Data_Get_Struct(self, xmlNode, node);
|
447
|
+
return (1 == xmlIsBlankNode(node)) ? Qtrue : Qfalse ;
|
448
|
+
}
|
449
|
+
|
450
|
+
/*
|
451
|
+
* call-seq:
|
452
|
+
* next_sibling
|
453
|
+
*
|
454
|
+
* Returns the next sibling node
|
455
|
+
*/
|
456
|
+
static VALUE next_sibling(VALUE self)
|
457
|
+
{
|
458
|
+
xmlNodePtr node, sibling;
|
459
|
+
Data_Get_Struct(self, xmlNode, node);
|
460
|
+
|
461
|
+
sibling = node->next;
|
462
|
+
if(!sibling) return Qnil;
|
463
|
+
|
464
|
+
return Nokogiri_wrap_xml_node(Qnil, sibling) ;
|
465
|
+
}
|
466
|
+
|
467
|
+
/*
|
468
|
+
* call-seq:
|
469
|
+
* previous_sibling
|
470
|
+
*
|
471
|
+
* Returns the previous sibling node
|
472
|
+
*/
|
473
|
+
static VALUE previous_sibling(VALUE self)
|
474
|
+
{
|
475
|
+
xmlNodePtr node, sibling;
|
476
|
+
Data_Get_Struct(self, xmlNode, node);
|
477
|
+
|
478
|
+
sibling = node->prev;
|
479
|
+
if(!sibling) return Qnil;
|
480
|
+
|
481
|
+
return Nokogiri_wrap_xml_node(Qnil, sibling);
|
482
|
+
}
|
483
|
+
|
484
|
+
/*
|
485
|
+
* call-seq:
|
486
|
+
* next_element
|
487
|
+
*
|
488
|
+
* Returns the next Nokogiri::XML::Element type sibling node.
|
489
|
+
*/
|
490
|
+
static VALUE next_element(VALUE self)
|
491
|
+
{
|
492
|
+
xmlNodePtr node, sibling;
|
493
|
+
Data_Get_Struct(self, xmlNode, node);
|
494
|
+
|
495
|
+
sibling = xmlNextElementSibling(node);
|
496
|
+
if(!sibling) return Qnil;
|
497
|
+
|
498
|
+
return Nokogiri_wrap_xml_node(Qnil, sibling);
|
499
|
+
}
|
500
|
+
|
501
|
+
/*
|
502
|
+
* call-seq:
|
503
|
+
* previous_element
|
504
|
+
*
|
505
|
+
* Returns the previous Nokogiri::XML::Element type sibling node.
|
506
|
+
*/
|
507
|
+
static VALUE previous_element(VALUE self)
|
508
|
+
{
|
509
|
+
xmlNodePtr node, sibling;
|
510
|
+
Data_Get_Struct(self, xmlNode, node);
|
511
|
+
|
512
|
+
/*
|
513
|
+
* note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
|
514
|
+
*/
|
515
|
+
sibling = node->prev;
|
516
|
+
if(!sibling) return Qnil;
|
517
|
+
|
518
|
+
while(sibling && sibling->type != XML_ELEMENT_NODE)
|
519
|
+
sibling = sibling->prev;
|
520
|
+
|
521
|
+
return sibling ? Nokogiri_wrap_xml_node(Qnil, sibling) : Qnil ;
|
522
|
+
}
|
523
|
+
|
524
|
+
/* :nodoc: */
|
525
|
+
static VALUE replace(VALUE self, VALUE new_node)
|
526
|
+
{
|
527
|
+
VALUE reparent = reparent_node_with(self, new_node, xmlReplaceNodeWrapper);
|
528
|
+
|
529
|
+
xmlNodePtr pivot;
|
530
|
+
Data_Get_Struct(self, xmlNode, pivot);
|
531
|
+
nokogiri_root_node(pivot);
|
532
|
+
|
533
|
+
return reparent;
|
534
|
+
}
|
535
|
+
|
536
|
+
/*
|
537
|
+
* call-seq:
|
538
|
+
* children
|
539
|
+
*
|
540
|
+
* Get the list of children for this node as a NodeSet
|
541
|
+
*/
|
542
|
+
static VALUE children(VALUE self)
|
543
|
+
{
|
544
|
+
xmlNodePtr node;
|
545
|
+
xmlNodePtr child;
|
546
|
+
xmlNodeSetPtr set;
|
547
|
+
VALUE document;
|
548
|
+
VALUE node_set;
|
549
|
+
|
550
|
+
Data_Get_Struct(self, xmlNode, node);
|
551
|
+
|
552
|
+
child = node->children;
|
553
|
+
set = xmlXPathNodeSetCreate(child);
|
554
|
+
|
555
|
+
document = DOC_RUBY_OBJECT(node->doc);
|
556
|
+
|
557
|
+
if(!child) return Nokogiri_wrap_xml_node_set(set, document);
|
558
|
+
|
559
|
+
child = child->next;
|
560
|
+
while(NULL != child) {
|
561
|
+
xmlXPathNodeSetAddUnique(set, child);
|
562
|
+
child = child->next;
|
563
|
+
}
|
564
|
+
|
565
|
+
node_set = Nokogiri_wrap_xml_node_set(set, document);
|
566
|
+
|
567
|
+
return node_set;
|
568
|
+
}
|
569
|
+
|
570
|
+
/*
|
571
|
+
* call-seq:
|
572
|
+
* element_children
|
573
|
+
*
|
574
|
+
* Get the list of children for this node as a NodeSet. All nodes will be
|
575
|
+
* element nodes.
|
576
|
+
*
|
577
|
+
* Example:
|
578
|
+
*
|
579
|
+
* @doc.root.element_children.all? { |x| x.element? } # => true
|
580
|
+
*/
|
581
|
+
static VALUE element_children(VALUE self)
|
582
|
+
{
|
583
|
+
xmlNodePtr node;
|
584
|
+
xmlNodePtr child;
|
585
|
+
xmlNodeSetPtr set;
|
586
|
+
VALUE document;
|
587
|
+
VALUE node_set;
|
588
|
+
|
589
|
+
Data_Get_Struct(self, xmlNode, node);
|
590
|
+
|
591
|
+
child = xmlFirstElementChild(node);
|
592
|
+
set = xmlXPathNodeSetCreate(child);
|
593
|
+
|
594
|
+
document = DOC_RUBY_OBJECT(node->doc);
|
595
|
+
|
596
|
+
if(!child) return Nokogiri_wrap_xml_node_set(set, document);
|
597
|
+
|
598
|
+
child = xmlNextElementSibling(child);
|
599
|
+
while(NULL != child) {
|
600
|
+
xmlXPathNodeSetAddUnique(set, child);
|
601
|
+
child = xmlNextElementSibling(child);
|
602
|
+
}
|
603
|
+
|
604
|
+
node_set = Nokogiri_wrap_xml_node_set(set, document);
|
605
|
+
|
606
|
+
return node_set;
|
607
|
+
}
|
608
|
+
|
609
|
+
/*
|
610
|
+
* call-seq:
|
611
|
+
* child
|
612
|
+
*
|
613
|
+
* Returns the child node
|
614
|
+
*/
|
615
|
+
static VALUE child(VALUE self)
|
616
|
+
{
|
617
|
+
xmlNodePtr node, child;
|
618
|
+
Data_Get_Struct(self, xmlNode, node);
|
619
|
+
|
620
|
+
child = node->children;
|
621
|
+
if(!child) return Qnil;
|
622
|
+
|
623
|
+
return Nokogiri_wrap_xml_node(Qnil, child);
|
624
|
+
}
|
625
|
+
|
626
|
+
/*
|
627
|
+
* call-seq:
|
628
|
+
* first_element_child
|
629
|
+
*
|
630
|
+
* Returns the first child node of this node that is an element.
|
631
|
+
*
|
632
|
+
* Example:
|
633
|
+
*
|
634
|
+
* @doc.root.first_element_child.element? # => true
|
635
|
+
*/
|
636
|
+
static VALUE first_element_child(VALUE self)
|
637
|
+
{
|
638
|
+
xmlNodePtr node, child;
|
639
|
+
Data_Get_Struct(self, xmlNode, node);
|
640
|
+
|
641
|
+
child = xmlFirstElementChild(node);
|
642
|
+
if(!child) return Qnil;
|
643
|
+
|
644
|
+
return Nokogiri_wrap_xml_node(Qnil, child);
|
645
|
+
}
|
646
|
+
|
647
|
+
/*
|
648
|
+
* call-seq:
|
649
|
+
* last_element_child
|
650
|
+
*
|
651
|
+
* Returns the last child node of this node that is an element.
|
652
|
+
*
|
653
|
+
* Example:
|
654
|
+
*
|
655
|
+
* @doc.root.last_element_child.element? # => true
|
656
|
+
*/
|
657
|
+
static VALUE last_element_child(VALUE self)
|
658
|
+
{
|
659
|
+
xmlNodePtr node, child;
|
660
|
+
Data_Get_Struct(self, xmlNode, node);
|
661
|
+
|
662
|
+
child = xmlLastElementChild(node);
|
663
|
+
if(!child) return Qnil;
|
664
|
+
|
665
|
+
return Nokogiri_wrap_xml_node(Qnil, child);
|
666
|
+
}
|
667
|
+
|
668
|
+
/*
|
669
|
+
* call-seq:
|
670
|
+
* key?(attribute)
|
671
|
+
*
|
672
|
+
* Returns true if +attribute+ is set
|
673
|
+
*/
|
674
|
+
static VALUE key_eh(VALUE self, VALUE attribute)
|
675
|
+
{
|
676
|
+
xmlNodePtr node;
|
677
|
+
Data_Get_Struct(self, xmlNode, node);
|
678
|
+
if(xmlHasProp(node, (xmlChar *)StringValuePtr(attribute)))
|
679
|
+
return Qtrue;
|
680
|
+
return Qfalse;
|
681
|
+
}
|
682
|
+
|
683
|
+
/*
|
684
|
+
* call-seq:
|
685
|
+
* namespaced_key?(attribute, namespace)
|
686
|
+
*
|
687
|
+
* Returns true if +attribute+ is set with +namespace+
|
688
|
+
*/
|
689
|
+
static VALUE namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
|
690
|
+
{
|
691
|
+
xmlNodePtr node;
|
692
|
+
Data_Get_Struct(self, xmlNode, node);
|
693
|
+
if(xmlHasNsProp(node, (xmlChar *)StringValuePtr(attribute),
|
694
|
+
NIL_P(namespace) ? NULL : (xmlChar *)StringValuePtr(namespace)))
|
695
|
+
return Qtrue;
|
696
|
+
return Qfalse;
|
697
|
+
}
|
698
|
+
|
699
|
+
/*
|
700
|
+
* call-seq:
|
701
|
+
* []=(property, value)
|
702
|
+
*
|
703
|
+
* Set the +property+ to +value+
|
704
|
+
*/
|
705
|
+
static VALUE set(VALUE self, VALUE property, VALUE value)
|
706
|
+
{
|
707
|
+
xmlNodePtr node, cur;
|
708
|
+
xmlAttrPtr prop;
|
709
|
+
Data_Get_Struct(self, xmlNode, node);
|
710
|
+
|
711
|
+
/* If a matching attribute node already exists, then xmlSetProp will destroy
|
712
|
+
* the existing node's children. However, if Nokogiri has a node object
|
713
|
+
* pointing to one of those children, we are left with a broken reference.
|
714
|
+
*
|
715
|
+
* We can avoid this by unlinking these nodes first.
|
716
|
+
*/
|
717
|
+
if (node->type != XML_ELEMENT_NODE)
|
718
|
+
return(Qnil);
|
719
|
+
prop = xmlHasProp(node, (xmlChar *)StringValuePtr(property));
|
720
|
+
if (prop && prop->children) {
|
721
|
+
for (cur = prop->children; cur; cur = cur->next) {
|
722
|
+
if (cur->_private) {
|
723
|
+
nokogiri_root_node(cur);
|
724
|
+
xmlUnlinkNode(cur);
|
725
|
+
}
|
726
|
+
}
|
727
|
+
}
|
728
|
+
|
729
|
+
xmlSetProp(node, (xmlChar *)StringValuePtr(property),
|
730
|
+
(xmlChar *)StringValuePtr(value));
|
731
|
+
|
732
|
+
return value;
|
733
|
+
}
|
734
|
+
|
735
|
+
/*
|
736
|
+
* call-seq:
|
737
|
+
* get(attribute)
|
738
|
+
*
|
739
|
+
* Get the value for +attribute+
|
740
|
+
*/
|
741
|
+
static VALUE get(VALUE self, VALUE rattribute)
|
742
|
+
{
|
743
|
+
xmlNodePtr node;
|
744
|
+
xmlChar* value = 0;
|
745
|
+
VALUE rvalue ;
|
746
|
+
char* attribute = 0;
|
747
|
+
char *colon = 0, *attr_name = 0, *prefix = 0;
|
748
|
+
xmlNsPtr ns;
|
749
|
+
|
750
|
+
if (NIL_P(rattribute)) return Qnil;
|
751
|
+
|
752
|
+
Data_Get_Struct(self, xmlNode, node);
|
753
|
+
attribute = strdup(StringValuePtr(rattribute));
|
754
|
+
|
755
|
+
colon = strchr(attribute, ':');
|
756
|
+
if (colon) {
|
757
|
+
(*colon) = 0 ; /* create two null-terminated strings of the prefix and attribute name */
|
758
|
+
prefix = attribute ;
|
759
|
+
attr_name = colon + 1 ;
|
760
|
+
ns = xmlSearchNs(node->doc, node, (const xmlChar *)(prefix));
|
761
|
+
if (ns) {
|
762
|
+
value = xmlGetNsProp(node, (xmlChar*)(attr_name), ns->href);
|
763
|
+
} else {
|
764
|
+
value = xmlGetProp(node, (xmlChar*)StringValuePtr(rattribute));
|
765
|
+
}
|
766
|
+
} else {
|
767
|
+
value = xmlGetNoNsProp(node, (xmlChar*)attribute);
|
768
|
+
}
|
769
|
+
|
770
|
+
free(attribute);
|
771
|
+
if (!value) return Qnil;
|
772
|
+
|
773
|
+
rvalue = NOKOGIRI_STR_NEW2(value);
|
774
|
+
xmlFree(value);
|
775
|
+
|
776
|
+
return rvalue ;
|
777
|
+
}
|
778
|
+
|
779
|
+
/*
|
780
|
+
* call-seq:
|
781
|
+
* set_namespace(namespace)
|
782
|
+
*
|
783
|
+
* Set the namespace to +namespace+
|
784
|
+
*/
|
785
|
+
static VALUE set_namespace(VALUE self, VALUE namespace)
|
786
|
+
{
|
787
|
+
xmlNodePtr node;
|
788
|
+
xmlNsPtr ns = NULL;
|
789
|
+
|
790
|
+
Data_Get_Struct(self, xmlNode, node);
|
791
|
+
|
792
|
+
if(!NIL_P(namespace))
|
793
|
+
Data_Get_Struct(namespace, xmlNs, ns);
|
794
|
+
|
795
|
+
xmlSetNs(node, ns);
|
796
|
+
|
797
|
+
return self;
|
798
|
+
}
|
799
|
+
|
800
|
+
/*
|
801
|
+
* call-seq:
|
802
|
+
* attribute(name)
|
803
|
+
*
|
804
|
+
* Get the attribute node with +name+
|
805
|
+
*/
|
806
|
+
static VALUE attr(VALUE self, VALUE name)
|
807
|
+
{
|
808
|
+
xmlNodePtr node;
|
809
|
+
xmlAttrPtr prop;
|
810
|
+
Data_Get_Struct(self, xmlNode, node);
|
811
|
+
prop = xmlHasProp(node, (xmlChar *)StringValuePtr(name));
|
812
|
+
|
813
|
+
if(! prop) return Qnil;
|
814
|
+
return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop);
|
815
|
+
}
|
816
|
+
|
817
|
+
/*
|
818
|
+
* call-seq:
|
819
|
+
* attribute_with_ns(name, namespace)
|
820
|
+
*
|
821
|
+
* Get the attribute node with +name+ and +namespace+
|
822
|
+
*/
|
823
|
+
static VALUE attribute_with_ns(VALUE self, VALUE name, VALUE namespace)
|
824
|
+
{
|
825
|
+
xmlNodePtr node;
|
826
|
+
xmlAttrPtr prop;
|
827
|
+
Data_Get_Struct(self, xmlNode, node);
|
828
|
+
prop = xmlHasNsProp(node, (xmlChar *)StringValuePtr(name),
|
829
|
+
NIL_P(namespace) ? NULL : (xmlChar *)StringValuePtr(namespace));
|
830
|
+
|
831
|
+
if(! prop) return Qnil;
|
832
|
+
return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop);
|
833
|
+
}
|
834
|
+
|
835
|
+
/*
|
836
|
+
* call-seq:
|
837
|
+
* attribute_nodes()
|
838
|
+
*
|
839
|
+
* returns a list containing the Node attributes.
|
840
|
+
*/
|
841
|
+
static VALUE attribute_nodes(VALUE self)
|
842
|
+
{
|
843
|
+
/* this code in the mode of xmlHasProp() */
|
844
|
+
xmlNodePtr node;
|
845
|
+
VALUE attr;
|
846
|
+
|
847
|
+
Data_Get_Struct(self, xmlNode, node);
|
848
|
+
|
849
|
+
attr = rb_ary_new();
|
850
|
+
Nokogiri_xml_node_properties(node, attr);
|
851
|
+
|
852
|
+
return attr ;
|
853
|
+
}
|
854
|
+
|
855
|
+
|
856
|
+
/*
|
857
|
+
* call-seq:
|
858
|
+
* namespace()
|
859
|
+
*
|
860
|
+
* returns the namespace of the element or attribute node as a Namespace
|
861
|
+
* object, or nil if there is no namespace for the element or attribute.
|
862
|
+
*/
|
863
|
+
static VALUE namespace(VALUE self)
|
864
|
+
{
|
865
|
+
xmlNodePtr node ;
|
866
|
+
Data_Get_Struct(self, xmlNode, node);
|
867
|
+
|
868
|
+
if (node->ns)
|
869
|
+
return Nokogiri_wrap_xml_namespace(node->doc, node->ns);
|
870
|
+
|
871
|
+
return Qnil ;
|
872
|
+
}
|
873
|
+
|
874
|
+
/*
|
875
|
+
* call-seq:
|
876
|
+
* namespace_definitions()
|
877
|
+
*
|
878
|
+
* returns namespaces defined on self element directly, as an array of Namespace objects. Includes both a default namespace (as in"xmlns="), and prefixed namespaces (as in "xmlns:prefix=").
|
879
|
+
*/
|
880
|
+
static VALUE namespace_definitions(VALUE self)
|
881
|
+
{
|
882
|
+
/* this code in the mode of xmlHasProp() */
|
883
|
+
xmlNodePtr node ;
|
884
|
+
VALUE list;
|
885
|
+
xmlNsPtr ns;
|
886
|
+
|
887
|
+
Data_Get_Struct(self, xmlNode, node);
|
888
|
+
|
889
|
+
list = rb_ary_new();
|
890
|
+
|
891
|
+
ns = node->nsDef;
|
892
|
+
|
893
|
+
if(!ns) return list;
|
894
|
+
|
895
|
+
while(NULL != ns) {
|
896
|
+
rb_ary_push(list, Nokogiri_wrap_xml_namespace(node->doc, ns));
|
897
|
+
ns = ns->next;
|
898
|
+
}
|
899
|
+
|
900
|
+
return list;
|
901
|
+
}
|
902
|
+
|
903
|
+
/*
|
904
|
+
* call-seq:
|
905
|
+
* namespace_scopes()
|
906
|
+
*
|
907
|
+
* returns namespaces in scope for self -- those defined on self element
|
908
|
+
* directly or any ancestor node -- as an array of Namespace objects. Default
|
909
|
+
* namespaces ("xmlns=" style) for self are included in this array; Default
|
910
|
+
* namespaces for ancestors, however, are not. See also #namespaces
|
911
|
+
*/
|
912
|
+
static VALUE namespace_scopes(VALUE self)
|
913
|
+
{
|
914
|
+
xmlNodePtr node ;
|
915
|
+
VALUE list;
|
916
|
+
xmlNsPtr *ns_list;
|
917
|
+
int j;
|
918
|
+
|
919
|
+
Data_Get_Struct(self, xmlNode, node);
|
920
|
+
|
921
|
+
list = rb_ary_new();
|
922
|
+
ns_list = xmlGetNsList(node->doc, node);
|
923
|
+
|
924
|
+
if(!ns_list) return list;
|
925
|
+
|
926
|
+
for (j = 0 ; ns_list[j] != NULL ; ++j) {
|
927
|
+
rb_ary_push(list, Nokogiri_wrap_xml_namespace(node->doc, ns_list[j]));
|
928
|
+
}
|
929
|
+
|
930
|
+
xmlFree(ns_list);
|
931
|
+
return list;
|
932
|
+
}
|
933
|
+
|
934
|
+
/*
|
935
|
+
* call-seq:
|
936
|
+
* node_type
|
937
|
+
*
|
938
|
+
* Get the type for this Node
|
939
|
+
*/
|
940
|
+
static VALUE node_type(VALUE self)
|
941
|
+
{
|
942
|
+
xmlNodePtr node;
|
943
|
+
Data_Get_Struct(self, xmlNode, node);
|
944
|
+
return INT2NUM((long)node->type);
|
945
|
+
}
|
946
|
+
|
947
|
+
/*
|
948
|
+
* call-seq:
|
949
|
+
* content=
|
950
|
+
*
|
951
|
+
* Set the content for this Node
|
952
|
+
*/
|
953
|
+
static VALUE native_content(VALUE self, VALUE content)
|
954
|
+
{
|
955
|
+
xmlNodePtr node, child, next ;
|
956
|
+
Data_Get_Struct(self, xmlNode, node);
|
957
|
+
|
958
|
+
child = node->children;
|
959
|
+
while (NULL != child) {
|
960
|
+
next = child->next ;
|
961
|
+
xmlUnlinkNode(child) ;
|
962
|
+
nokogiri_root_node(child);
|
963
|
+
child = next ;
|
964
|
+
}
|
965
|
+
|
966
|
+
xmlNodeSetContent(node, (xmlChar *)StringValuePtr(content));
|
967
|
+
return content;
|
968
|
+
}
|
969
|
+
|
970
|
+
/*
|
971
|
+
* call-seq:
|
972
|
+
* content
|
973
|
+
*
|
974
|
+
* Returns the content for this Node
|
975
|
+
*/
|
976
|
+
static VALUE get_content(VALUE self)
|
977
|
+
{
|
978
|
+
xmlNodePtr node;
|
979
|
+
xmlChar * content;
|
980
|
+
|
981
|
+
Data_Get_Struct(self, xmlNode, node);
|
982
|
+
|
983
|
+
content = xmlNodeGetContent(node);
|
984
|
+
if(content) {
|
985
|
+
VALUE rval = NOKOGIRI_STR_NEW2(content);
|
986
|
+
xmlFree(content);
|
987
|
+
return rval;
|
988
|
+
}
|
989
|
+
return Qnil;
|
990
|
+
}
|
991
|
+
|
992
|
+
/* :nodoc: */
|
993
|
+
static VALUE add_child(VALUE self, VALUE new_child)
|
994
|
+
{
|
995
|
+
return reparent_node_with(self, new_child, xmlAddChild);
|
996
|
+
}
|
997
|
+
|
998
|
+
/*
|
999
|
+
* call-seq:
|
1000
|
+
* parent
|
1001
|
+
*
|
1002
|
+
* Get the parent Node for this Node
|
1003
|
+
*/
|
1004
|
+
static VALUE get_parent(VALUE self)
|
1005
|
+
{
|
1006
|
+
xmlNodePtr node, parent;
|
1007
|
+
Data_Get_Struct(self, xmlNode, node);
|
1008
|
+
|
1009
|
+
parent = node->parent;
|
1010
|
+
if(!parent) return Qnil;
|
1011
|
+
|
1012
|
+
return Nokogiri_wrap_xml_node(Qnil, parent) ;
|
1013
|
+
}
|
1014
|
+
|
1015
|
+
/*
|
1016
|
+
* call-seq:
|
1017
|
+
* name=(new_name)
|
1018
|
+
*
|
1019
|
+
* Set the name for this Node
|
1020
|
+
*/
|
1021
|
+
static VALUE set_name(VALUE self, VALUE new_name)
|
1022
|
+
{
|
1023
|
+
xmlNodePtr node;
|
1024
|
+
Data_Get_Struct(self, xmlNode, node);
|
1025
|
+
xmlNodeSetName(node, (xmlChar*)StringValuePtr(new_name));
|
1026
|
+
return new_name;
|
1027
|
+
}
|
1028
|
+
|
1029
|
+
/*
|
1030
|
+
* call-seq:
|
1031
|
+
* name
|
1032
|
+
*
|
1033
|
+
* Returns the name for this Node
|
1034
|
+
*/
|
1035
|
+
static VALUE get_name(VALUE self)
|
1036
|
+
{
|
1037
|
+
xmlNodePtr node;
|
1038
|
+
Data_Get_Struct(self, xmlNode, node);
|
1039
|
+
if(node->name)
|
1040
|
+
return NOKOGIRI_STR_NEW2(node->name);
|
1041
|
+
return Qnil;
|
1042
|
+
}
|
1043
|
+
|
1044
|
+
/*
|
1045
|
+
* call-seq:
|
1046
|
+
* path
|
1047
|
+
*
|
1048
|
+
* Returns the path associated with this Node
|
1049
|
+
*/
|
1050
|
+
static VALUE path(VALUE self)
|
1051
|
+
{
|
1052
|
+
xmlNodePtr node;
|
1053
|
+
xmlChar *path ;
|
1054
|
+
VALUE rval;
|
1055
|
+
|
1056
|
+
Data_Get_Struct(self, xmlNode, node);
|
1057
|
+
|
1058
|
+
path = xmlGetNodePath(node);
|
1059
|
+
rval = NOKOGIRI_STR_NEW2(path);
|
1060
|
+
xmlFree(path);
|
1061
|
+
return rval ;
|
1062
|
+
}
|
1063
|
+
|
1064
|
+
/* :nodoc: */
|
1065
|
+
static VALUE add_next_sibling(VALUE self, VALUE new_sibling)
|
1066
|
+
{
|
1067
|
+
return reparent_node_with(self, new_sibling, xmlAddNextSibling) ;
|
1068
|
+
}
|
1069
|
+
|
1070
|
+
/* :nodoc: */
|
1071
|
+
static VALUE add_previous_sibling(VALUE self, VALUE new_sibling)
|
1072
|
+
{
|
1073
|
+
return reparent_node_with(self, new_sibling, xmlAddPrevSibling) ;
|
1074
|
+
}
|
1075
|
+
|
1076
|
+
/*
|
1077
|
+
* call-seq:
|
1078
|
+
* native_write_to(io, encoding, options)
|
1079
|
+
*
|
1080
|
+
* Write this Node to +io+ with +encoding+ and +options+
|
1081
|
+
*/
|
1082
|
+
static VALUE native_write_to(
|
1083
|
+
VALUE self,
|
1084
|
+
VALUE io,
|
1085
|
+
VALUE encoding,
|
1086
|
+
VALUE indent_string,
|
1087
|
+
VALUE options
|
1088
|
+
) {
|
1089
|
+
xmlNodePtr node;
|
1090
|
+
const char * before_indent;
|
1091
|
+
xmlSaveCtxtPtr savectx;
|
1092
|
+
|
1093
|
+
Data_Get_Struct(self, xmlNode, node);
|
1094
|
+
|
1095
|
+
xmlIndentTreeOutput = 1;
|
1096
|
+
|
1097
|
+
before_indent = xmlTreeIndentString;
|
1098
|
+
|
1099
|
+
xmlTreeIndentString = StringValuePtr(indent_string);
|
1100
|
+
|
1101
|
+
savectx = xmlSaveToIO(
|
1102
|
+
(xmlOutputWriteCallback)io_write_callback,
|
1103
|
+
(xmlOutputCloseCallback)io_close_callback,
|
1104
|
+
(void *)io,
|
1105
|
+
RTEST(encoding) ? StringValuePtr(encoding) : NULL,
|
1106
|
+
(int)NUM2INT(options)
|
1107
|
+
);
|
1108
|
+
|
1109
|
+
xmlSaveTree(savectx, node);
|
1110
|
+
xmlSaveClose(savectx);
|
1111
|
+
|
1112
|
+
xmlTreeIndentString = before_indent;
|
1113
|
+
return io;
|
1114
|
+
}
|
1115
|
+
|
1116
|
+
/*
|
1117
|
+
* call-seq:
|
1118
|
+
* line
|
1119
|
+
*
|
1120
|
+
* Returns the line for this Node
|
1121
|
+
*/
|
1122
|
+
static VALUE line(VALUE self)
|
1123
|
+
{
|
1124
|
+
xmlNodePtr node;
|
1125
|
+
Data_Get_Struct(self, xmlNode, node);
|
1126
|
+
|
1127
|
+
return INT2NUM(xmlGetLineNo(node));
|
1128
|
+
}
|
1129
|
+
|
1130
|
+
/*
|
1131
|
+
* call-seq:
|
1132
|
+
* add_namespace_definition(prefix, href)
|
1133
|
+
*
|
1134
|
+
* Adds a namespace definition with +prefix+ using +href+ value. The result is
|
1135
|
+
* as if parsed XML for this node had included an attribute
|
1136
|
+
* 'xmlns:prefix=value'. A default namespace for this node ("xmlns=") can be
|
1137
|
+
* added by passing 'nil' for prefix. Namespaces added this way will not
|
1138
|
+
* show up in #attributes, but they will be included as an xmlns attribute
|
1139
|
+
* when the node is serialized to XML.
|
1140
|
+
*/
|
1141
|
+
static VALUE add_namespace_definition(VALUE self, VALUE prefix, VALUE href)
|
1142
|
+
{
|
1143
|
+
xmlNodePtr node, namespacee;
|
1144
|
+
xmlNsPtr ns;
|
1145
|
+
|
1146
|
+
Data_Get_Struct(self, xmlNode, node);
|
1147
|
+
namespacee = node ;
|
1148
|
+
|
1149
|
+
ns = xmlSearchNs(
|
1150
|
+
node->doc,
|
1151
|
+
node,
|
1152
|
+
(const xmlChar *)(NIL_P(prefix) ? NULL : StringValuePtr(prefix))
|
1153
|
+
);
|
1154
|
+
|
1155
|
+
if(!ns) {
|
1156
|
+
if (node->type != XML_ELEMENT_NODE) {
|
1157
|
+
namespacee = node->parent;
|
1158
|
+
}
|
1159
|
+
ns = xmlNewNs(
|
1160
|
+
namespacee,
|
1161
|
+
(const xmlChar *)StringValuePtr(href),
|
1162
|
+
(const xmlChar *)(NIL_P(prefix) ? NULL : StringValuePtr(prefix))
|
1163
|
+
);
|
1164
|
+
}
|
1165
|
+
|
1166
|
+
if (!ns) return Qnil ;
|
1167
|
+
|
1168
|
+
if(NIL_P(prefix) || node != namespacee) xmlSetNs(node, ns);
|
1169
|
+
|
1170
|
+
return Nokogiri_wrap_xml_namespace(node->doc, ns);
|
1171
|
+
}
|
1172
|
+
|
1173
|
+
/*
|
1174
|
+
* call-seq:
|
1175
|
+
* new(name, document)
|
1176
|
+
*
|
1177
|
+
* Create a new node with +name+ sharing GC lifecycle with +document+
|
1178
|
+
*/
|
1179
|
+
static VALUE new(int argc, VALUE *argv, VALUE klass)
|
1180
|
+
{
|
1181
|
+
xmlDocPtr doc;
|
1182
|
+
xmlNodePtr node;
|
1183
|
+
VALUE name;
|
1184
|
+
VALUE document;
|
1185
|
+
VALUE rest;
|
1186
|
+
VALUE rb_node;
|
1187
|
+
|
1188
|
+
rb_scan_args(argc, argv, "2*", &name, &document, &rest);
|
1189
|
+
|
1190
|
+
Data_Get_Struct(document, xmlDoc, doc);
|
1191
|
+
|
1192
|
+
node = xmlNewNode(NULL, (xmlChar *)StringValuePtr(name));
|
1193
|
+
node->doc = doc->doc;
|
1194
|
+
nokogiri_root_node(node);
|
1195
|
+
|
1196
|
+
rb_node = Nokogiri_wrap_xml_node(
|
1197
|
+
klass == cNokogiriXmlNode ? (VALUE)NULL : klass,
|
1198
|
+
node
|
1199
|
+
);
|
1200
|
+
rb_obj_call_init(rb_node, argc, argv);
|
1201
|
+
|
1202
|
+
if(rb_block_given_p()) rb_yield(rb_node);
|
1203
|
+
|
1204
|
+
return rb_node;
|
1205
|
+
}
|
1206
|
+
|
1207
|
+
/*
|
1208
|
+
* call-seq:
|
1209
|
+
* dump_html
|
1210
|
+
*
|
1211
|
+
* Returns the Node as html.
|
1212
|
+
*/
|
1213
|
+
static VALUE dump_html(VALUE self)
|
1214
|
+
{
|
1215
|
+
xmlBufferPtr buf ;
|
1216
|
+
xmlNodePtr node ;
|
1217
|
+
VALUE html;
|
1218
|
+
|
1219
|
+
Data_Get_Struct(self, xmlNode, node);
|
1220
|
+
|
1221
|
+
buf = xmlBufferCreate() ;
|
1222
|
+
htmlNodeDump(buf, node->doc, node);
|
1223
|
+
html = NOKOGIRI_STR_NEW2(buf->content);
|
1224
|
+
xmlBufferFree(buf);
|
1225
|
+
return html ;
|
1226
|
+
}
|
1227
|
+
|
1228
|
+
/*
|
1229
|
+
* call-seq:
|
1230
|
+
* compare(other)
|
1231
|
+
*
|
1232
|
+
* Compare this Node to +other+ with respect to their Document
|
1233
|
+
*/
|
1234
|
+
static VALUE compare(VALUE self, VALUE _other)
|
1235
|
+
{
|
1236
|
+
xmlNodePtr node, other;
|
1237
|
+
Data_Get_Struct(self, xmlNode, node);
|
1238
|
+
Data_Get_Struct(_other, xmlNode, other);
|
1239
|
+
|
1240
|
+
return INT2NUM((long)xmlXPathCmpNodes(other, node));
|
1241
|
+
}
|
1242
|
+
|
1243
|
+
|
1244
|
+
/*
|
1245
|
+
* call-seq:
|
1246
|
+
* process_xincludes(options)
|
1247
|
+
*
|
1248
|
+
* Loads and substitutes all xinclude elements below the node. The
|
1249
|
+
* parser context will be initialized with +options+.
|
1250
|
+
*/
|
1251
|
+
static VALUE process_xincludes(VALUE self, VALUE options)
|
1252
|
+
{
|
1253
|
+
int rcode ;
|
1254
|
+
xmlNodePtr node;
|
1255
|
+
VALUE error_list = rb_ary_new();
|
1256
|
+
|
1257
|
+
Data_Get_Struct(self, xmlNode, node);
|
1258
|
+
|
1259
|
+
xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
|
1260
|
+
rcode = xmlXIncludeProcessTreeFlags(node, (int)NUM2INT(options));
|
1261
|
+
xmlSetStructuredErrorFunc(NULL, NULL);
|
1262
|
+
|
1263
|
+
if (rcode < 0) {
|
1264
|
+
xmlErrorPtr error;
|
1265
|
+
|
1266
|
+
error = xmlGetLastError();
|
1267
|
+
if(error)
|
1268
|
+
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
|
1269
|
+
else
|
1270
|
+
rb_raise(rb_eRuntimeError, "Could not perform xinclude substitution");
|
1271
|
+
}
|
1272
|
+
|
1273
|
+
return self;
|
1274
|
+
}
|
1275
|
+
|
1276
|
+
|
1277
|
+
/* TODO: DOCUMENT ME */
|
1278
|
+
static VALUE in_context(VALUE self, VALUE _str, VALUE _options)
|
1279
|
+
{
|
1280
|
+
xmlNodePtr node, list = 0, tmp, child_iter, node_children, doc_children;
|
1281
|
+
xmlNodeSetPtr set;
|
1282
|
+
xmlParserErrors error;
|
1283
|
+
VALUE doc, err;
|
1284
|
+
int doc_is_empty;
|
1285
|
+
|
1286
|
+
Data_Get_Struct(self, xmlNode, node);
|
1287
|
+
|
1288
|
+
doc = DOC_RUBY_OBJECT(node->doc);
|
1289
|
+
err = rb_iv_get(doc, "@errors");
|
1290
|
+
doc_is_empty = (node->doc->children == NULL) ? 1 : 0;
|
1291
|
+
node_children = node->children;
|
1292
|
+
doc_children = node->doc->children;
|
1293
|
+
|
1294
|
+
xmlSetStructuredErrorFunc((void *)err, Nokogiri_error_array_pusher);
|
1295
|
+
|
1296
|
+
/* Twiddle global variable because of a bug in libxml2.
|
1297
|
+
* http://git.gnome.org/browse/libxml2/commit/?id=e20fb5a72c83cbfc8e4a8aa3943c6be8febadab7
|
1298
|
+
*/
|
1299
|
+
#ifndef HTML_PARSE_NOIMPLIED
|
1300
|
+
htmlHandleOmittedElem(0);
|
1301
|
+
#endif
|
1302
|
+
|
1303
|
+
/* This function adds a fake node to the child of +node+. If the parser
|
1304
|
+
* does not exit cleanly with XML_ERR_OK, the list is freed. This can
|
1305
|
+
* leave the child pointers in a bad state if they were originally empty.
|
1306
|
+
*
|
1307
|
+
* http://git.gnome.org/browse/libxml2/tree/parser.c#n13177
|
1308
|
+
* */
|
1309
|
+
error = xmlParseInNodeContext(node, StringValuePtr(_str),
|
1310
|
+
(int)RSTRING_LEN(_str),
|
1311
|
+
(int)NUM2INT(_options), &list);
|
1312
|
+
|
1313
|
+
/* xmlParseInNodeContext should not mutate the original document or node,
|
1314
|
+
* so reassigning these pointers should be OK. The reason we're reassigning
|
1315
|
+
* is because if there were errors, it's possible for the child pointers
|
1316
|
+
* to be manipulated. */
|
1317
|
+
if (error != XML_ERR_OK) {
|
1318
|
+
node->doc->children = doc_children;
|
1319
|
+
node->children = node_children;
|
1320
|
+
}
|
1321
|
+
|
1322
|
+
/* make sure parent/child pointers are coherent so an unlink will work
|
1323
|
+
* properly (#331)
|
1324
|
+
*/
|
1325
|
+
child_iter = node->doc->children ;
|
1326
|
+
while (child_iter) {
|
1327
|
+
if (child_iter->parent != (xmlNodePtr)node->doc)
|
1328
|
+
child_iter->parent = (xmlNodePtr)node->doc;
|
1329
|
+
child_iter = child_iter->next;
|
1330
|
+
}
|
1331
|
+
|
1332
|
+
#ifndef HTML_PARSE_NOIMPLIED
|
1333
|
+
htmlHandleOmittedElem(1);
|
1334
|
+
#endif
|
1335
|
+
|
1336
|
+
xmlSetStructuredErrorFunc(NULL, NULL);
|
1337
|
+
|
1338
|
+
/* Workaround for a libxml2 bug where a parsing error may leave a broken
|
1339
|
+
* node reference in node->doc->children.
|
1340
|
+
* This workaround is limited to when a parse error occurs, the document
|
1341
|
+
* went from having no children to having children, and the context node is
|
1342
|
+
* part of a document fragment.
|
1343
|
+
* https://bugzilla.gnome.org/show_bug.cgi?id=668155
|
1344
|
+
*/
|
1345
|
+
if (error != XML_ERR_OK && doc_is_empty && node->doc->children != NULL) {
|
1346
|
+
child_iter = node;
|
1347
|
+
while (child_iter->parent)
|
1348
|
+
child_iter = child_iter->parent;
|
1349
|
+
|
1350
|
+
if (child_iter->type == XML_DOCUMENT_FRAG_NODE)
|
1351
|
+
node->doc->children = NULL;
|
1352
|
+
}
|
1353
|
+
|
1354
|
+
/* FIXME: This probably needs to handle more constants... */
|
1355
|
+
switch (error) {
|
1356
|
+
case XML_ERR_INTERNAL_ERROR:
|
1357
|
+
case XML_ERR_NO_MEMORY:
|
1358
|
+
rb_raise(rb_eRuntimeError, "error parsing fragment (%d)", error);
|
1359
|
+
break;
|
1360
|
+
default:
|
1361
|
+
break;
|
1362
|
+
}
|
1363
|
+
|
1364
|
+
set = xmlXPathNodeSetCreate(NULL);
|
1365
|
+
|
1366
|
+
while (list) {
|
1367
|
+
tmp = list->next;
|
1368
|
+
list->next = NULL;
|
1369
|
+
xmlXPathNodeSetAddUnique(set, list);
|
1370
|
+
nokogiri_root_node(list);
|
1371
|
+
list = tmp;
|
1372
|
+
}
|
1373
|
+
|
1374
|
+
return Nokogiri_wrap_xml_node_set(set, doc);
|
1375
|
+
}
|
1376
|
+
|
1377
|
+
|
1378
|
+
VALUE Nokogiri_wrap_xml_node(VALUE klass, xmlNodePtr node)
|
1379
|
+
{
|
1380
|
+
VALUE document = Qnil ;
|
1381
|
+
VALUE node_cache = Qnil ;
|
1382
|
+
VALUE rb_node = Qnil ;
|
1383
|
+
nokogiriTuplePtr node_has_a_document;
|
1384
|
+
xmlDocPtr doc;
|
1385
|
+
void (*mark_method)(xmlNodePtr) = NULL ;
|
1386
|
+
|
1387
|
+
assert(node);
|
1388
|
+
|
1389
|
+
if(node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE)
|
1390
|
+
return DOC_RUBY_OBJECT(node->doc);
|
1391
|
+
|
1392
|
+
/* It's OK if the node doesn't have a fully-realized document (as in XML::Reader). */
|
1393
|
+
/* see https://github.com/sparklemotion/nokogiri/issues/95 */
|
1394
|
+
/* and https://github.com/sparklemotion/nokogiri/issues/439 */
|
1395
|
+
doc = node->doc;
|
1396
|
+
if (doc->type == XML_DOCUMENT_FRAG_NODE) doc = doc->doc;
|
1397
|
+
node_has_a_document = DOC_RUBY_OBJECT_TEST(doc);
|
1398
|
+
|
1399
|
+
if(node->_private && node_has_a_document)
|
1400
|
+
return (VALUE)node->_private;
|
1401
|
+
|
1402
|
+
if(!RTEST(klass)) {
|
1403
|
+
switch(node->type)
|
1404
|
+
{
|
1405
|
+
case XML_ELEMENT_NODE:
|
1406
|
+
klass = cNokogiriXmlElement;
|
1407
|
+
break;
|
1408
|
+
case XML_TEXT_NODE:
|
1409
|
+
klass = cNokogiriXmlText;
|
1410
|
+
break;
|
1411
|
+
case XML_ATTRIBUTE_NODE:
|
1412
|
+
klass = cNokogiriXmlAttr;
|
1413
|
+
break;
|
1414
|
+
case XML_ENTITY_REF_NODE:
|
1415
|
+
klass = cNokogiriXmlEntityReference;
|
1416
|
+
break;
|
1417
|
+
case XML_COMMENT_NODE:
|
1418
|
+
klass = cNokogiriXmlComment;
|
1419
|
+
break;
|
1420
|
+
case XML_DOCUMENT_FRAG_NODE:
|
1421
|
+
klass = cNokogiriXmlDocumentFragment;
|
1422
|
+
break;
|
1423
|
+
case XML_PI_NODE:
|
1424
|
+
klass = cNokogiriXmlProcessingInstruction;
|
1425
|
+
break;
|
1426
|
+
case XML_ENTITY_DECL:
|
1427
|
+
klass = cNokogiriXmlEntityDecl;
|
1428
|
+
break;
|
1429
|
+
case XML_CDATA_SECTION_NODE:
|
1430
|
+
klass = cNokogiriXmlCData;
|
1431
|
+
break;
|
1432
|
+
case XML_DTD_NODE:
|
1433
|
+
klass = cNokogiriXmlDtd;
|
1434
|
+
break;
|
1435
|
+
case XML_ATTRIBUTE_DECL:
|
1436
|
+
klass = cNokogiriXmlAttributeDecl;
|
1437
|
+
break;
|
1438
|
+
case XML_ELEMENT_DECL:
|
1439
|
+
klass = cNokogiriXmlElementDecl;
|
1440
|
+
break;
|
1441
|
+
default:
|
1442
|
+
klass = cNokogiriXmlNode;
|
1443
|
+
}
|
1444
|
+
}
|
1445
|
+
|
1446
|
+
mark_method = node_has_a_document ? mark : NULL ;
|
1447
|
+
|
1448
|
+
rb_node = Data_Wrap_Struct(klass, mark_method, debug_node_dealloc, node) ;
|
1449
|
+
node->_private = (void *)rb_node;
|
1450
|
+
|
1451
|
+
if (node_has_a_document) {
|
1452
|
+
document = DOC_RUBY_OBJECT(doc);
|
1453
|
+
node_cache = DOC_NODE_CACHE(doc);
|
1454
|
+
rb_ary_push(node_cache, rb_node);
|
1455
|
+
rb_funcall(document, decorate, 1, rb_node);
|
1456
|
+
}
|
1457
|
+
|
1458
|
+
return rb_node ;
|
1459
|
+
}
|
1460
|
+
|
1461
|
+
|
1462
|
+
void Nokogiri_xml_node_properties(xmlNodePtr node, VALUE attr_list)
|
1463
|
+
{
|
1464
|
+
xmlAttrPtr prop;
|
1465
|
+
prop = node->properties ;
|
1466
|
+
while (prop != NULL) {
|
1467
|
+
rb_ary_push(attr_list, Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop));
|
1468
|
+
prop = prop->next ;
|
1469
|
+
}
|
1470
|
+
}
|
1471
|
+
|
1472
|
+
VALUE cNokogiriXmlNode ;
|
1473
|
+
VALUE cNokogiriXmlElement ;
|
1474
|
+
|
1475
|
+
void init_xml_node()
|
1476
|
+
{
|
1477
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
1478
|
+
VALUE xml = rb_define_module_under(nokogiri, "XML");
|
1479
|
+
VALUE klass = rb_define_class_under(xml, "Node", rb_cObject);
|
1480
|
+
|
1481
|
+
cNokogiriXmlNode = klass;
|
1482
|
+
|
1483
|
+
cNokogiriXmlElement = rb_define_class_under(xml, "Element", klass);
|
1484
|
+
|
1485
|
+
rb_define_singleton_method(klass, "new", new, -1);
|
1486
|
+
|
1487
|
+
rb_define_method(klass, "add_namespace_definition", add_namespace_definition, 2);
|
1488
|
+
rb_define_method(klass, "node_name", get_name, 0);
|
1489
|
+
rb_define_method(klass, "document", document, 0);
|
1490
|
+
rb_define_method(klass, "node_name=", set_name, 1);
|
1491
|
+
rb_define_method(klass, "parent", get_parent, 0);
|
1492
|
+
rb_define_method(klass, "child", child, 0);
|
1493
|
+
rb_define_method(klass, "first_element_child", first_element_child, 0);
|
1494
|
+
rb_define_method(klass, "last_element_child", last_element_child, 0);
|
1495
|
+
rb_define_method(klass, "children", children, 0);
|
1496
|
+
rb_define_method(klass, "element_children", element_children, 0);
|
1497
|
+
rb_define_method(klass, "next_sibling", next_sibling, 0);
|
1498
|
+
rb_define_method(klass, "previous_sibling", previous_sibling, 0);
|
1499
|
+
rb_define_method(klass, "next_element", next_element, 0);
|
1500
|
+
rb_define_method(klass, "previous_element", previous_element, 0);
|
1501
|
+
rb_define_method(klass, "node_type", node_type, 0);
|
1502
|
+
rb_define_method(klass, "content", get_content, 0);
|
1503
|
+
rb_define_method(klass, "path", path, 0);
|
1504
|
+
rb_define_method(klass, "key?", key_eh, 1);
|
1505
|
+
rb_define_method(klass, "namespaced_key?", namespaced_key_eh, 2);
|
1506
|
+
rb_define_method(klass, "blank?", blank_eh, 0);
|
1507
|
+
rb_define_method(klass, "attribute_nodes", attribute_nodes, 0);
|
1508
|
+
rb_define_method(klass, "attribute", attr, 1);
|
1509
|
+
rb_define_method(klass, "attribute_with_ns", attribute_with_ns, 2);
|
1510
|
+
rb_define_method(klass, "namespace", namespace, 0);
|
1511
|
+
rb_define_method(klass, "namespace_definitions", namespace_definitions, 0);
|
1512
|
+
rb_define_method(klass, "namespace_scopes", namespace_scopes, 0);
|
1513
|
+
rb_define_method(klass, "encode_special_chars", encode_special_chars, 1);
|
1514
|
+
rb_define_method(klass, "dup", duplicate_node, -1);
|
1515
|
+
rb_define_method(klass, "unlink", unlink_node, 0);
|
1516
|
+
rb_define_method(klass, "internal_subset", internal_subset, 0);
|
1517
|
+
rb_define_method(klass, "external_subset", external_subset, 0);
|
1518
|
+
rb_define_method(klass, "create_internal_subset", create_internal_subset, 3);
|
1519
|
+
rb_define_method(klass, "create_external_subset", create_external_subset, 3);
|
1520
|
+
rb_define_method(klass, "pointer_id", pointer_id, 0);
|
1521
|
+
rb_define_method(klass, "line", line, 0);
|
1522
|
+
rb_define_method(klass, "native_content=", native_content, 1);
|
1523
|
+
|
1524
|
+
rb_define_private_method(klass, "process_xincludes", process_xincludes, 1);
|
1525
|
+
rb_define_private_method(klass, "in_context", in_context, 2);
|
1526
|
+
rb_define_private_method(klass, "add_child_node", add_child, 1);
|
1527
|
+
rb_define_private_method(klass, "add_previous_sibling_node", add_previous_sibling, 1);
|
1528
|
+
rb_define_private_method(klass, "add_next_sibling_node", add_next_sibling, 1);
|
1529
|
+
rb_define_private_method(klass, "replace_node", replace, 1);
|
1530
|
+
rb_define_private_method(klass, "dump_html", dump_html, 0);
|
1531
|
+
rb_define_private_method(klass, "native_write_to", native_write_to, 4);
|
1532
|
+
rb_define_private_method(klass, "get", get, 1);
|
1533
|
+
rb_define_private_method(klass, "set", set, 2);
|
1534
|
+
rb_define_private_method(klass, "set_namespace", set_namespace, 1);
|
1535
|
+
rb_define_private_method(klass, "compare", compare, 1);
|
1536
|
+
|
1537
|
+
decorate = rb_intern("decorate");
|
1538
|
+
decorate_bang = rb_intern("decorate!");
|
1539
|
+
}
|
1540
|
+
|
1541
|
+
/* vim: set noet sw=4 sws=4 */
|