Changeset 155
- Timestamp:
- 08/31/2007 17:30:41 (15 months ago)
- Location:
- trunk
- Files:
-
- 5 modified
-
lib/hpricot/elements.rb (modified) (5 diffs)
-
lib/hpricot/parse.rb (modified) (1 diff)
-
lib/hpricot/tag.rb (modified) (2 diffs)
-
lib/hpricot/traverse.rb (modified) (4 diffs)
-
test/test_alter.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/hpricot/elements.rb
r150 r155 131 131 # Pass in an HTML +str+, which is turned into Hpricot elements. 132 132 def append(str = nil, &blk) 133 each { |x| x.html(x.children + Hpricot.make(str, &blk)) }133 each { |x| x.html(x.children + x.make(str, &blk)) } 134 134 end 135 135 … … 137 137 # Pass in an HTML +str+, which is turned into Hpricot elements. 138 138 def prepend(str = nil, &blk) 139 each { |x| x.html( Hpricot.make(str, &blk) + x.children) }139 each { |x| x.html(x.make(str, &blk) + x.children) } 140 140 end 141 141 … … 143 143 # Pass in an HTML +str+, which is turned into Hpricot elements. 144 144 def before(str = nil, &blk) 145 each { |x| x.parent.insert_before Hpricot.make(str, &blk), x }145 each { |x| x.parent.insert_before x.make(str, &blk), x } 146 146 end 147 147 … … 149 149 # Pass in an HTML +str+, which is turned into Hpricot elements. 150 150 def after(str = nil, &blk) 151 each { |x| x.parent.insert_after Hpricot.make(str, &blk), x }151 each { |x| x.parent.insert_after x.make(str, &blk), x } 152 152 end 153 153 … … 162 162 def wrap(str = nil, &blk) 163 163 each do |x| 164 wrap = Hpricot.make(str, &blk)164 wrap = x.make(str, &blk) 165 165 nest = wrap.detect { |w| w.respond_to? :children } 166 166 unless nest -
trunk/lib/hpricot/parse.rb
r151 r155 13 13 # represented by Hpricot::Doc. 14 14 def Hpricot.parse(input = nil, opts = {}, &blk) 15 Doc.new(make(input, opts, &blk) )15 Doc.new(make(input, opts, &blk), opts) 16 16 end 17 17 18 18 # Hpricot::XML parses <i>input</i>, disregarding all the HTML rules 19 19 # and returning a document tree. 20 def Hpricot.XML(input, opts = {}) 21 Doc.new(make(input, opts.merge(:xml => true))) 20 def Hpricot.XML(input = nil, opts = {}, &blk) 21 opts.merge! :xml => true 22 Doc.new(make(input, opts, &blk), opts) 22 23 end 23 24 -
trunk/lib/hpricot/tag.rb
r154 r155 4 4 class Doc 5 5 attr_accessor :children 6 def initialize(children = [] )6 def initialize(children = [], options = {}) 7 7 @children = children ? children.each { |c| c.parent = self } : [] 8 @options = options 8 9 end 9 10 def output(out, opts = {}) … … 12 13 end 13 14 out 15 end 16 def make(input = nil, &blk) 17 Hpricot.make(input, @options, &blk) 14 18 end 15 19 def altered!; end -
trunk/lib/hpricot/traverse.rb
r149 r155 21 21 def bogusetag?() BogusETag::Trav === self end 22 22 23 # Parses an HTML string, making an HTML fragment based on 24 # the options used to create the container document. 25 def make(input = nil, &blk) 26 if parent and parent.respond_to? :make 27 parent.make(input, &blk) 28 else 29 Hpricot.make(input, &blk) 30 end 31 end 32 23 33 # Builds an HTML string from this node and its contents. 24 34 # If you need to write to a stream, try calling <tt>output(io)</tt> … … 110 120 # Adds elements immediately after this element, contained in the +html+ string. 111 121 def after(html = nil, &blk) 112 parent.insert_after( Hpricot.make(html, &blk), self)122 parent.insert_after(make(html, &blk), self) 113 123 end 114 124 115 125 # Adds elements immediately before this element, contained in the +html+ string. 116 126 def before(html = nil, &blk) 117 parent.insert_before( Hpricot.make(html, &blk), self)127 parent.insert_before(make(html, &blk), self) 118 128 end 119 129 … … 123 133 def swap(html = nil, &blk) 124 134 parent.altered! 125 parent.replace_child(self, Hpricot.make(html, &blk))135 parent.replace_child(self, make(html, &blk)) 126 136 end 127 137 … … 159 169 self.children = inner 160 170 else 161 self.children = Hpricot.make(inner, &blk)171 self.children = make(inner, &blk) 162 172 end 163 173 reparent self.children -
trunk/test/test_alter.rb
r150 r155 59 59 end 60 60 61 def test_xml_casing 62 doc = Hpricot.XML("<root><wildCat>text</wildCat></root>") 63 (doc/:root/:wildCat).after("<beanPole>gravity</beanPole>") 64 assert_equal doc.to_s, "<root><wildCat>text</wildCat><beanPole>gravity</beanPole></root>" 65 66 frag = Hpricot.XML do 67 b { i "A bit of HTML" } 68 end 69 (frag/:b).after("<beanPole>gravity</beanPole>") 70 assert_equal frag.to_s, "<b><i>A bit of HTML</i></b><beanPole>gravity</beanPole>" 71 end 72 61 73 def assert_changed original, selector, set, &block 62 74 assert set.all?(&block)
