nokogiri 1.1.1-java

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.

Files changed (142) hide show
  1. data/History.ja.txt +99 -0
  2. data/History.txt +99 -0
  3. data/Manifest.txt +141 -0
  4. data/README.ja.txt +100 -0
  5. data/README.txt +109 -0
  6. data/Rakefile +354 -0
  7. data/ext/nokogiri/extconf.rb +93 -0
  8. data/ext/nokogiri/html_document.c +86 -0
  9. data/ext/nokogiri/html_document.h +10 -0
  10. data/ext/nokogiri/html_sax_parser.c +36 -0
  11. data/ext/nokogiri/html_sax_parser.h +11 -0
  12. data/ext/nokogiri/native.c +41 -0
  13. data/ext/nokogiri/native.h +50 -0
  14. data/ext/nokogiri/xml_cdata.c +44 -0
  15. data/ext/nokogiri/xml_cdata.h +9 -0
  16. data/ext/nokogiri/xml_comment.c +42 -0
  17. data/ext/nokogiri/xml_comment.h +9 -0
  18. data/ext/nokogiri/xml_document.c +206 -0
  19. data/ext/nokogiri/xml_document.h +10 -0
  20. data/ext/nokogiri/xml_dtd.c +121 -0
  21. data/ext/nokogiri/xml_dtd.h +8 -0
  22. data/ext/nokogiri/xml_io.c +17 -0
  23. data/ext/nokogiri/xml_io.h +9 -0
  24. data/ext/nokogiri/xml_node.c +727 -0
  25. data/ext/nokogiri/xml_node.h +13 -0
  26. data/ext/nokogiri/xml_node_set.c +118 -0
  27. data/ext/nokogiri/xml_node_set.h +9 -0
  28. data/ext/nokogiri/xml_reader.c +465 -0
  29. data/ext/nokogiri/xml_reader.h +10 -0
  30. data/ext/nokogiri/xml_sax_parser.c +201 -0
  31. data/ext/nokogiri/xml_sax_parser.h +10 -0
  32. data/ext/nokogiri/xml_syntax_error.c +199 -0
  33. data/ext/nokogiri/xml_syntax_error.h +11 -0
  34. data/ext/nokogiri/xml_text.c +40 -0
  35. data/ext/nokogiri/xml_text.h +9 -0
  36. data/ext/nokogiri/xml_xpath.c +53 -0
  37. data/ext/nokogiri/xml_xpath.h +11 -0
  38. data/ext/nokogiri/xml_xpath_context.c +214 -0
  39. data/ext/nokogiri/xml_xpath_context.h +9 -0
  40. data/ext/nokogiri/xslt_stylesheet.c +123 -0
  41. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  42. data/lib/action-nokogiri.rb +30 -0
  43. data/lib/nokogiri.rb +72 -0
  44. data/lib/nokogiri/css.rb +25 -0
  45. data/lib/nokogiri/css/generated_parser.rb +721 -0
  46. data/lib/nokogiri/css/generated_tokenizer.rb +159 -0
  47. data/lib/nokogiri/css/node.rb +97 -0
  48. data/lib/nokogiri/css/parser.rb +64 -0
  49. data/lib/nokogiri/css/parser.y +216 -0
  50. data/lib/nokogiri/css/syntax_error.rb +6 -0
  51. data/lib/nokogiri/css/tokenizer.rb +9 -0
  52. data/lib/nokogiri/css/tokenizer.rex +63 -0
  53. data/lib/nokogiri/css/xpath_visitor.rb +168 -0
  54. data/lib/nokogiri/decorators.rb +2 -0
  55. data/lib/nokogiri/decorators/hpricot.rb +3 -0
  56. data/lib/nokogiri/decorators/hpricot/node.rb +56 -0
  57. data/lib/nokogiri/decorators/hpricot/node_set.rb +54 -0
  58. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +28 -0
  59. data/lib/nokogiri/decorators/slop.rb +31 -0
  60. data/lib/nokogiri/hpricot.rb +51 -0
  61. data/lib/nokogiri/html.rb +105 -0
  62. data/lib/nokogiri/html/builder.rb +9 -0
  63. data/lib/nokogiri/html/document.rb +9 -0
  64. data/lib/nokogiri/html/sax/parser.rb +21 -0
  65. data/lib/nokogiri/version.rb +3 -0
  66. data/lib/nokogiri/xml.rb +83 -0
  67. data/lib/nokogiri/xml/after_handler.rb +18 -0
  68. data/lib/nokogiri/xml/attr.rb +10 -0
  69. data/lib/nokogiri/xml/before_handler.rb +33 -0
  70. data/lib/nokogiri/xml/builder.rb +84 -0
  71. data/lib/nokogiri/xml/cdata.rb +9 -0
  72. data/lib/nokogiri/xml/comment.rb +6 -0
  73. data/lib/nokogiri/xml/document.rb +55 -0
  74. data/lib/nokogiri/xml/dtd.rb +6 -0
  75. data/lib/nokogiri/xml/element.rb +6 -0
  76. data/lib/nokogiri/xml/entity_declaration.rb +9 -0
  77. data/lib/nokogiri/xml/node.rb +333 -0
  78. data/lib/nokogiri/xml/node_set.rb +197 -0
  79. data/lib/nokogiri/xml/notation.rb +6 -0
  80. data/lib/nokogiri/xml/reader.rb +20 -0
  81. data/lib/nokogiri/xml/sax.rb +9 -0
  82. data/lib/nokogiri/xml/sax/document.rb +59 -0
  83. data/lib/nokogiri/xml/sax/parser.rb +37 -0
  84. data/lib/nokogiri/xml/syntax_error.rb +21 -0
  85. data/lib/nokogiri/xml/text.rb +6 -0
  86. data/lib/nokogiri/xml/xpath.rb +10 -0
  87. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  88. data/lib/nokogiri/xml/xpath_context.rb +14 -0
  89. data/lib/nokogiri/xslt.rb +28 -0
  90. data/lib/nokogiri/xslt/stylesheet.rb +6 -0
  91. data/test/css/test_nthiness.rb +159 -0
  92. data/test/css/test_parser.rb +237 -0
  93. data/test/css/test_tokenizer.rb +162 -0
  94. data/test/css/test_xpath_visitor.rb +64 -0
  95. data/test/files/dont_hurt_em_why.xml +422 -0
  96. data/test/files/exslt.xml +8 -0
  97. data/test/files/exslt.xslt +35 -0
  98. data/test/files/staff.xml +59 -0
  99. data/test/files/staff.xslt +32 -0
  100. data/test/files/tlm.html +850 -0
  101. data/test/helper.rb +78 -0
  102. data/test/hpricot/files/basic.xhtml +17 -0
  103. data/test/hpricot/files/boingboing.html +2266 -0
  104. data/test/hpricot/files/cy0.html +3653 -0
  105. data/test/hpricot/files/immob.html +400 -0
  106. data/test/hpricot/files/pace_application.html +1320 -0
  107. data/test/hpricot/files/tenderlove.html +16 -0
  108. data/test/hpricot/files/uswebgen.html +220 -0
  109. data/test/hpricot/files/utf8.html +1054 -0
  110. data/test/hpricot/files/week9.html +1723 -0
  111. data/test/hpricot/files/why.xml +19 -0
  112. data/test/hpricot/load_files.rb +11 -0
  113. data/test/hpricot/test_alter.rb +67 -0
  114. data/test/hpricot/test_builder.rb +27 -0
  115. data/test/hpricot/test_parser.rb +426 -0
  116. data/test/hpricot/test_paths.rb +15 -0
  117. data/test/hpricot/test_preserved.rb +77 -0
  118. data/test/hpricot/test_xml.rb +30 -0
  119. data/test/html/sax/test_parser.rb +27 -0
  120. data/test/html/test_builder.rb +89 -0
  121. data/test/html/test_document.rb +150 -0
  122. data/test/html/test_node.rb +21 -0
  123. data/test/test_convert_xpath.rb +185 -0
  124. data/test/test_css_cache.rb +57 -0
  125. data/test/test_gc.rb +15 -0
  126. data/test/test_memory_leak.rb +38 -0
  127. data/test/test_nokogiri.rb +97 -0
  128. data/test/test_reader.rb +222 -0
  129. data/test/test_xslt_transforms.rb +93 -0
  130. data/test/xml/sax/test_parser.rb +95 -0
  131. data/test/xml/test_attr.rb +15 -0
  132. data/test/xml/test_builder.rb +16 -0
  133. data/test/xml/test_cdata.rb +18 -0
  134. data/test/xml/test_comment.rb +16 -0
  135. data/test/xml/test_document.rb +195 -0
  136. data/test/xml/test_dtd.rb +43 -0
  137. data/test/xml/test_node.rb +394 -0
  138. data/test/xml/test_node_set.rb +143 -0
  139. data/test/xml/test_text.rb +13 -0
  140. data/test/xml/test_xpath.rb +105 -0
  141. data/vendor/hoe.rb +1020 -0
  142. metadata +233 -0
@@ -0,0 +1,105 @@
1
+ require 'nokogiri/html/document'
2
+ require 'nokogiri/html/sax/parser'
3
+
4
+ module Nokogiri
5
+ class << self
6
+ ###
7
+ # Parse HTML. +thing+ may be a String, or any object that
8
+ # responds to _read_ and _close_ such as an IO, or StringIO.
9
+ # +url+ is resource where this document is located. +encoding+ is the
10
+ # encoding that should be used when processing the document. +options+
11
+ # is a number that sets options in the parser, such as
12
+ # Nokogiri::XML::PARSE_RECOVER. See the constants in
13
+ # Nokogiri::XML.
14
+ def HTML thing, url = nil, encoding = nil, options = 2145
15
+ Nokogiri::HTML.parse(thing, url, encoding, options)
16
+ end
17
+ end
18
+
19
+ module HTML
20
+ # Parser options
21
+ PARSE_NOERROR = 1 << 5 # No error reports
22
+ PARSE_NOWARNING = 1 << 6 # No warnings
23
+ PARSE_PEDANTIC = 1 << 7 # Pedantic errors
24
+ PARSE_NOBLANKS = 1 << 8 # Remove blanks nodes
25
+ PARSE_NONET = 1 << 11 # No network access
26
+
27
+ class << self
28
+ ###
29
+ # Parse HTML. See Nokogiri.HTML.
30
+ def parse string_or_io, url = nil, encoding = nil, options = 2145
31
+ if string_or_io.respond_to?(:read)
32
+ url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
33
+ string_or_io = string_or_io.read
34
+ end
35
+
36
+ Document.read_memory(string_or_io, url, encoding, options)
37
+ end
38
+
39
+ ####
40
+ # Parse a fragment from +string+ in to a NodeSet.
41
+ def fragment string
42
+ doc = parse(string)
43
+ finder = lambda { |children, f|
44
+ children.each do |child|
45
+ return children if string =~ /<#{child.name}/
46
+ finder.call(child.children, f)
47
+ end
48
+ }
49
+ finder.call(doc.children, finder)
50
+ end
51
+ end
52
+
53
+ NamedCharacters =
54
+ {"AElig"=>198, "Aacute"=>193, "Acirc"=>194, "Agrave"=>192, "Alpha"=>913,
55
+ "Aring"=>197, "Atilde"=>195, "Auml"=>196, "Beta"=>914, "Ccedil"=>199,
56
+ "Chi"=>935, "Dagger"=>8225, "Delta"=>916, "ETH"=>208, "Eacute"=>201,
57
+ "Ecirc"=>202, "Egrave"=>200, "Epsilon"=>917, "Eta"=>919, "Euml"=>203,
58
+ "Gamma"=>915, "Iacute"=>205, "Icirc"=>206, "Igrave"=>204, "Iota"=>921,
59
+ "Iuml"=>207, "Kappa"=>922, "Lambda"=>923, "Mu"=>924, "Ntilde"=>209, "Nu"=>925,
60
+ "OElig"=>338, "Oacute"=>211, "Ocirc"=>212, "Ograve"=>210, "Omega"=>937,
61
+ "Omicron"=>927, "Oslash"=>216, "Otilde"=>213, "Ouml"=>214, "Phi"=>934,
62
+ "Pi"=>928, "Prime"=>8243, "Psi"=>936, "Rho"=>929, "Scaron"=>352, "Sigma"=>931,
63
+ "THORN"=>222, "Tau"=>932, "Theta"=>920, "Uacute"=>218, "Ucirc"=>219,
64
+ "Ugrave"=>217, "Upsilon"=>933, "Uuml"=>220, "Xi"=>926, "Yacute"=>221,
65
+ "Yuml"=>376, "Zeta"=>918, "aacute"=>225, "acirc"=>226, "acute"=>180,
66
+ "aelig"=>230, "agrave"=>224, "alefsym"=>8501, "alpha"=>945, "amp"=>38,
67
+ "and"=>8743, "ang"=>8736, "apos"=>39, "aring"=>229, "asymp"=>8776,
68
+ "atilde"=>227, "auml"=>228, "bdquo"=>8222, "beta"=>946, "brvbar"=>166,
69
+ "bull"=>8226, "cap"=>8745, "ccedil"=>231, "cedil"=>184, "cent"=>162,
70
+ "chi"=>967, "circ"=>710, "clubs"=>9827, "cong"=>8773, "copy"=>169,
71
+ "crarr"=>8629, "cup"=>8746, "curren"=>164, "dArr"=>8659, "dagger"=>8224,
72
+ "darr"=>8595, "deg"=>176, "delta"=>948, "diams"=>9830, "divide"=>247,
73
+ "eacute"=>233, "ecirc"=>234, "egrave"=>232, "empty"=>8709, "emsp"=>8195,
74
+ "ensp"=>8194, "epsilon"=>949, "equiv"=>8801, "eta"=>951, "eth"=>240,
75
+ "euml"=>235, "euro"=>8364, "exist"=>8707, "fnof"=>402, "forall"=>8704,
76
+ "frac12"=>189, "frac14"=>188, "frac34"=>190, "frasl"=>8260, "gamma"=>947,
77
+ "ge"=>8805, "gt"=>62, "hArr"=>8660, "harr"=>8596, "hearts"=>9829,
78
+ "hellip"=>8230, "iacute"=>237, "icirc"=>238, "iexcl"=>161, "igrave"=>236,
79
+ "image"=>8465, "infin"=>8734, "int"=>8747, "iota"=>953, "iquest"=>191,
80
+ "isin"=>8712, "iuml"=>239, "kappa"=>954, "lArr"=>8656, "lambda"=>955,
81
+ "lang"=>9001, "laquo"=>171, "larr"=>8592, "lceil"=>8968, "ldquo"=>8220,
82
+ "le"=>8804, "lfloor"=>8970, "lowast"=>8727, "loz"=>9674, "lrm"=>8206,
83
+ "lsaquo"=>8249, "lsquo"=>8216, "lt"=>60, "macr"=>175, "mdash"=>8212,
84
+ "micro"=>181, "middot"=>183, "minus"=>8722, "mu"=>956, "nabla"=>8711,
85
+ "nbsp"=>160, "ndash"=>8211, "ne"=>8800, "ni"=>8715, "not"=>172, "notin"=>8713,
86
+ "nsub"=>8836, "ntilde"=>241, "nu"=>957, "oacute"=>243, "ocirc"=>244,
87
+ "oelig"=>339, "ograve"=>242, "oline"=>8254, "omega"=>969, "omicron"=>959,
88
+ "oplus"=>8853, "or"=>8744, "ordf"=>170, "ordm"=>186, "oslash"=>248,
89
+ "otilde"=>245, "otimes"=>8855, "ouml"=>246, "para"=>182, "part"=>8706,
90
+ "permil"=>8240, "perp"=>8869, "phi"=>966, "pi"=>960, "piv"=>982,
91
+ "plusmn"=>177, "pound"=>163, "prime"=>8242, "prod"=>8719, "prop"=>8733,
92
+ "psi"=>968, "quot"=>34, "rArr"=>8658, "radic"=>8730, "rang"=>9002,
93
+ "raquo"=>187, "rarr"=>8594, "rceil"=>8969, "rdquo"=>8221, "real"=>8476,
94
+ "reg"=>174, "rfloor"=>8971, "rho"=>961, "rlm"=>8207, "rsaquo"=>8250,
95
+ "rsquo"=>8217, "sbquo"=>8218, "scaron"=>353, "sdot"=>8901, "sect"=>167,
96
+ "shy"=>173, "sigma"=>963, "sigmaf"=>962, "sim"=>8764, "spades"=>9824,
97
+ "sub"=>8834, "sube"=>8838, "sum"=>8721, "sup"=>8835, "sup1"=>185, "sup2"=>178,
98
+ "sup3"=>179, "supe"=>8839, "szlig"=>223, "tau"=>964, "there4"=>8756,
99
+ "theta"=>952, "thetasym"=>977, "thinsp"=>8201, "thorn"=>254, "tilde"=>732,
100
+ "times"=>215, "trade"=>8482, "uArr"=>8657, "uacute"=>250, "uarr"=>8593,
101
+ "ucirc"=>251, "ugrave"=>249, "uml"=>168, "upsih"=>978, "upsilon"=>965,
102
+ "uuml"=>252, "weierp"=>8472, "xi"=>958, "yacute"=>253, "yen"=>165,
103
+ "yuml"=>255, "zeta"=>950, "zwj"=>8205, "zwnj"=>8204}
104
+ end
105
+ end
@@ -0,0 +1,9 @@
1
+ module Nokogiri
2
+ module HTML
3
+ class Builder < XML::Builder
4
+ def to_html
5
+ @doc.to_html
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Nokogiri
2
+ module HTML
3
+ class Document < XML::Document
4
+ def to_html
5
+ serialize
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module Nokogiri
2
+ module HTML
3
+ module SAX
4
+ class Parser < XML::SAX::Parser
5
+ ###
6
+ # Parse html stored in +data+ using +encoding+
7
+ def parse_memory data, encoding = 'UTF-8'
8
+ native_parse_memory(data, encoding)
9
+ end
10
+
11
+ ###
12
+ # Parse a file with +filename+
13
+ def parse_file filename, encoding = 'UTF-8'
14
+ raise Errno::ENOENT unless File.exists?(filename)
15
+ raise Errno::EISDIR if File.directory?(filename)
16
+ native_parse_file filename, encoding
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Nokogiri
2
+ VERSION = '1.1.1'
3
+ end
@@ -0,0 +1,83 @@
1
+ require 'nokogiri/xml/sax'
2
+ require 'nokogiri/xml/before_handler'
3
+ require 'nokogiri/xml/after_handler'
4
+ require 'nokogiri/xml/node'
5
+ require 'nokogiri/xml/attr'
6
+ require 'nokogiri/xml/dtd'
7
+ require 'nokogiri/xml/text'
8
+ require 'nokogiri/xml/cdata'
9
+ require 'nokogiri/xml/comment'
10
+ require 'nokogiri/xml/document'
11
+ require 'nokogiri/xml/node_set'
12
+ require 'nokogiri/xml/xpath'
13
+ require 'nokogiri/xml/xpath_context'
14
+ require 'nokogiri/xml/builder'
15
+ require 'nokogiri/xml/reader'
16
+ require 'nokogiri/xml/syntax_error'
17
+ require 'nokogiri/xml/notation'
18
+ require 'nokogiri/xml/element'
19
+ require 'nokogiri/xml/entity_declaration'
20
+
21
+ module Nokogiri
22
+ class << self
23
+ ###
24
+ # Parse an XML file. +thing+ may be a String, or any object that
25
+ # responds to _read_ and _close_ such as an IO, or StringIO.
26
+ # +url+ is resource where this document is located. +encoding+ is the
27
+ # encoding that should be used when processing the document. +options+
28
+ # is a number that sets options in the parser, such as
29
+ # Nokogiri::XML::PARSE_RECOVER. See the constants in
30
+ # Nokogiri::XML.
31
+ def XML thing, url = nil, encoding = nil, options = 1
32
+ Nokogiri::XML.parse(thing, url, encoding, options)
33
+ end
34
+ end
35
+
36
+ module XML
37
+ # Parser options
38
+ PARSE_RECOVER = 1 << 0 # Recover from errors
39
+ PARSE_NOENT = 1 << 1 # Substitute entities
40
+ PARSE_DTDLOAD = 1 << 2 # Load external subsets
41
+ PARSE_DTDATTR = 1 << 3 # Default DTD attributes
42
+ PARSE_DTDVALID = 1 << 4 # validate with the DTD
43
+ PARSE_NOERROR = 1 << 5 # suppress error reports
44
+ PARSE_NOWARNING = 1 << 6 # suppress warning reports
45
+ PARSE_PEDANTIC = 1 << 7 # pedantic error reporting
46
+ PARSE_NOBLANKS = 1 << 8 # remove blank nodes
47
+ PARSE_SAX1 = 1 << 9 # use the SAX1 interface internally
48
+ PARSE_XINCLUDE = 1 << 10 # Implement XInclude substitition
49
+ PARSE_NONET = 1 << 11 # Forbid network access
50
+ PARSE_NODICT = 1 << 12 # Do not reuse the context dictionnary
51
+ PARSE_NSCLEAN = 1 << 13 # remove redundant namespaces declarations
52
+ PARSE_NOCDATA = 1 << 14 # merge CDATA as text nodes
53
+ PARSE_NOXINCNODE = 1 << 15 # do not generate XINCLUDE START/END nodes
54
+
55
+ class << self
56
+ ###
57
+ # Parse an XML document. See Nokogiri.XML.
58
+ def parse string_or_io, url = nil, encoding = nil, options = 2159
59
+ if string_or_io.respond_to?(:read)
60
+ url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
61
+ return Document.read_io(string_or_io, url, encoding, options)
62
+ end
63
+
64
+ # read_memory pukes on empty docs
65
+ return Document.new if string_or_io.nil? or string_or_io.empty?
66
+
67
+ Document.read_memory(string_or_io, url, encoding, options)
68
+ end
69
+
70
+ ###
71
+ # Sets whether or not entities should be substituted.
72
+ def substitute_entities=(value = true)
73
+ Document.substitute_entities = value
74
+ end
75
+
76
+ ###
77
+ # Sets whether or not external subsets should be loaded
78
+ def load_external_subsets=(value = true)
79
+ Document.load_external_subsets = value
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,18 @@
1
+ module Nokogiri
2
+ module XML
3
+ class AfterHandler < BeforeHandler
4
+ attr_accessor :after_nodes
5
+
6
+ def initialize node, original_html
7
+ super
8
+ @after_nodes = []
9
+ end
10
+
11
+ def end_element name
12
+ return unless @original_html =~ /<#{name}/i
13
+ @after_nodes << @stack.last if @stack.length == 1
14
+ @stack.pop
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Attr < Node
4
+ def value
5
+ children.first.to_s
6
+ end
7
+ alias :to_s :value
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module Nokogiri
2
+ module XML
3
+ class BeforeHandler < Nokogiri::XML::SAX::Document # :nodoc:
4
+ def initialize node, original_html
5
+ @original_html = original_html
6
+ @node = node
7
+ @document = node.document
8
+ @stack = []
9
+ end
10
+
11
+ def start_element name, attrs = []
12
+ return unless @original_html =~ /<#{name}/i
13
+ node = Node.new(name, @document)
14
+ Hash[*attrs].each do |k,v|
15
+ node[k] = v
16
+ end
17
+ node.parent = @stack.last if @stack.length != 0
18
+ @stack << node
19
+ end
20
+
21
+ def characters string
22
+ node = @stack.last
23
+ node.content += string
24
+ end
25
+
26
+ def end_element name
27
+ return unless @original_html =~ /<#{name}/i
28
+ @node.add_previous_sibling @stack.last if @stack.length == 1
29
+ @stack.pop
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,84 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Builder
4
+ attr_accessor :doc, :parent
5
+ def initialize(&block)
6
+ namespace = self.class.name.split('::')
7
+ namespace[-1] = 'Document'
8
+ @doc = eval(namespace.join('::')).new
9
+ @parent = @doc
10
+ @context = eval('self', block.binding)
11
+ instance_eval(&block)
12
+ @parent = @doc
13
+ end
14
+
15
+ def text(string)
16
+ node = Nokogiri::XML::Text.new(string, @doc)
17
+ insert(node)
18
+ end
19
+
20
+ def cdata(string)
21
+ node = Nokogiri::XML::CDATA.new(@doc, string)
22
+ insert(node)
23
+ end
24
+
25
+ def to_xml
26
+ @doc.to_xml
27
+ end
28
+
29
+ def method_missing(method, *args, &block)
30
+ if @context.respond_to?(method)
31
+ @context.send(method, *args, &block)
32
+ else
33
+ node = Nokogiri::XML::Node.new(method.to_s, @doc) { |n|
34
+ if content = args.first
35
+ if content.is_a?(Hash)
36
+ content.each { |k,v| n[k.to_s] = v.to_s }
37
+ else
38
+ n.content = content
39
+ end
40
+ end
41
+ }
42
+ insert(node, &block)
43
+ end
44
+ end
45
+
46
+ private
47
+ def insert(node, &block)
48
+ node.parent = @parent
49
+ if block_given?
50
+ @parent = node
51
+ instance_eval(&block)
52
+ @parent = node.parent
53
+ end
54
+ NodeBuilder.new(node, self)
55
+ end
56
+
57
+ class NodeBuilder # :nodoc:
58
+ def initialize(node, doc_builder)
59
+ @node = node
60
+ @doc_builder = doc_builder
61
+ end
62
+
63
+ def method_missing(method, *args, &block)
64
+ case method.to_s
65
+ when /^(.*)!$/
66
+ @node['id'] = $1
67
+ @node.content = args.first if args.first
68
+ when /^(.*)=/
69
+ @node[$1] = args.first
70
+ else
71
+ @node['class'] =
72
+ ((@node['class'] || '').split(/\s/) + [method.to_s]).join(' ')
73
+ @node.content = args.first if args.first
74
+ end
75
+ if block_given?
76
+ @doc_builder.parent = @node
77
+ return @doc_builder.instance_eval(&block)
78
+ end
79
+ self
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,9 @@
1
+ module Nokogiri
2
+ module XML
3
+ class CDATA < Text
4
+ def name
5
+ 'cdata-section'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Comment < Node
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,55 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Document < Node
4
+ def name
5
+ 'document'
6
+ end
7
+
8
+ def document
9
+ self
10
+ end
11
+
12
+ def decorators(key)
13
+ @decorators ||= Hash.new
14
+ @decorators[key] ||= []
15
+ end
16
+
17
+ ###
18
+ # Explore a document with shortcut methods.
19
+ def slop!
20
+ unless decorators(XML::Node).include? Nokogiri::Decorators::Slop
21
+ decorators(XML::Node) << Nokogiri::Decorators::Slop
22
+ decorate!
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ ###
29
+ # Apply any decorators to +node+
30
+ def decorate(node)
31
+ return unless @decorators
32
+ @decorators.each { |klass,list|
33
+ next unless node.is_a?(klass)
34
+ list.each { |moodule| node.extend(moodule) }
35
+ }
36
+ end
37
+
38
+ def node_cache
39
+ @node_cache ||= {}
40
+ end
41
+
42
+ def to_xml
43
+ serialize
44
+ end
45
+
46
+ def inner_html
47
+ serialize
48
+ end
49
+
50
+ def namespaces
51
+ root ? root.collect_namespaces : {}
52
+ end
53
+ end
54
+ end
55
+ end