Changeset 16
- Timestamp:
- 07/07/2006 15:09:28 (2 years ago)
- Location:
- trunk
- Files:
-
- 6 modified
-
ext/hpricot_scan/hpricot_scan.rl (modified) (1 diff)
-
lib/hpricot/elements.rb (modified) (3 diffs)
-
lib/hpricot/parse.rb (modified) (3 diffs)
-
lib/hpricot/tag.rb (modified) (2 diffs)
-
lib/hpricot/traverse.rb (modified) (3 diffs)
-
test/test_parser.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ext/hpricot_scan/hpricot_scan.rl
r15 r16 233 233 if ( mark_tag != NULL && text == 1 ) 234 234 { 235 CAT(tag, p); 235 if (done) 236 { 237 if (mark_tag < p-1) 238 { 239 CAT(tag, p-1); 240 ELE(text); 241 } 242 } 243 else 244 { 245 CAT(tag, p); 246 } 236 247 } 237 248 mark_tag = buf; -
trunk/lib/hpricot/elements.rb
r15 r16 6 6 alias_method :/, :search 7 7 8 def html 9 map { |x| x.innerHTML }.join 10 end 8 def to_html 9 map { |x| x.output("") }.join 10 end 11 alias_method :to_s, :to_html 12 13 def html(str = nil) 14 if str 15 self.html = str 16 else 17 map { |x| x.innerHTML }.join 18 end 19 end 20 alias_method :text, :html 11 21 12 22 def html=(str) … … 17 27 nodes, = Elements.filter(self, expr) 18 28 nodes 29 end 30 31 def remove 32 each { |x| x.parent.children.delete(x) } 33 end 34 35 def empty 36 each { |x| x.innerHTML = nil } 37 end 38 39 def append(str) 40 each { |x| x.innerHTML += str } 41 end 42 43 def prepend(str) 44 each { |x| x.innerHTML = str + x.innerHTML } 45 end 46 47 def before(str) 48 each { |x| x.parent.insert_before Hpricot.make(str), x } 49 end 50 51 def after(str) 52 each { |x| x.parent.insert_after Hpricot.make(str), x } 53 end 54 55 def wrap(str) 56 each do |x| 57 wrap = Hpricot.make(str) 58 nest = wrap.detect { |w| w.respond_to? :children } 59 unless nest 60 raise Exception, "No wrapping element found." 61 end 62 x.parent.replace_child(x, wrap) 63 nest = nest.children.first until nest.empty? 64 nest.children << x 65 end 19 66 end 20 67 … … 28 75 end 29 76 30 def set(k, v) 77 def set(k, v = nil) 78 case k 79 when Hash 80 each do |node| 81 k.each { |a,b| node.set_attribute(a, b) } 82 end 83 else 31 84 each do |node| 32 85 node.set_attribute(k, v) 33 86 end 87 end 34 88 end 35 89 -
trunk/lib/hpricot/parse.rb
r15 r16 1 1 require 'hpricot/htmlinfo' 2 3 def Hpricot(input) 4 Hpricot.parse(input) 5 end 2 6 3 7 module Hpricot … … 5 9 # represented by Hpricot::Doc. 6 10 def Hpricot.parse(input) 7 parse_as(input)11 Doc.new(make(input)) 8 12 end 9 13 10 14 # :stopdoc: 11 15 12 def Hpricot. parse_as(input)16 def Hpricot.make(input) 13 17 stack = [[nil, nil, []]] 14 18 Hpricot.scan(input) do |token| … … 53 57 structure_list = stack[0][2] 54 58 # structure_list = fix_structure_list(stack[0][2]) 55 nodes = structure_list.map {|s| build_node(s) } 56 Doc.new(nodes) 59 structure_list.map {|s| build_node(s) } 57 60 end 58 61 -
trunk/lib/hpricot/tag.rb
r15 r16 29 29 end 30 30 def empty?; @children.empty? end 31 [:name, :attributes, :parent , :parent=].each do |m|32 define_method(m) { |*a| @stag.send(m, *a)}31 [:name, :attributes, :parent].each do |m| 32 [m, "#{m}="].each { |m2| define_method(m2) { |*a| @stag.send(m2, *a) } } 33 33 end 34 34 def output(out) … … 49 49 @attributes = attributes 50 50 end 51 attr_ reader :name, :attributes51 attr_accessor :name, :attributes 52 52 def attributes_as_html 53 53 if @attributes -
trunk/lib/hpricot/traverse.rb
r15 r16 13 13 def bogusetag?() BogusETag::Trav === self end 14 14 15 def to_html 16 output("") 17 end 18 alias_method :to_s, :to_html 19 15 20 def get_subnode(*indexes) 16 21 n = self … … 26 31 children.grep(Container::Trav) 27 32 end 33 def replace_child(old, new) 34 children[children.index(old), 1] = [*new] 35 end 36 def insert_before(nodes, ele) 37 case nodes 38 when Array 39 nodes.each { |n| insert_before(n, ele) } 40 else 41 children[children.index(ele) || 0, 0] = nodes 42 end 43 end 44 def insert_after(nodes, ele) 45 case nodes 46 when Array 47 nodes.each { |n| insert_after(n, ele) } 48 else 49 idx = children.index(ele) 50 children[idx ? idx + 1 : children.length, 0] = nodes 51 end 52 end 28 53 def innerHTML 29 output("") 54 children.map { |x| x.output("") }.join 55 end 56 def innerHTML=(inner) 57 case inner 58 when String, IO 59 self.children = Hpricot.parse(inner).children 60 when Array 61 self.children = inner 62 when nil 63 self.children = [] 64 end 30 65 end 31 66 def search(expr, &blk) … … 449 484 module Elem::Trav 450 485 def has_attribute?(name) 451 attributes && attributes.has_key?(name)486 self.attributes && self.attributes.has_key?(name.to_s) 452 487 end 453 488 def get_attribute(name) 454 attributes && attributes[name]489 self.attributes && self.attributes[name.to_s] 455 490 end 456 491 def set_attribute(name, val) 457 attributes ||= {}458 attributes[name] = val492 self.attributes ||= {} 493 self.attributes[name.to_s] = val 459 494 end 460 495 end -
trunk/test/test_parser.rb
r15 r16 15 15 # assert_equal '', @basic.search('//p').map { |x| x.attributes } 16 16 # end 17 18 def test_scan_text 19 assert_equal 'FOO', Hpricot.make("FOO").first.content 20 end 17 21 18 22 def test_get_element_by_id … … 92 96 assert_equal 2, @boingboing.search('//table/tr:last').length 93 97 assert_equal "<p>The third paragraph</p>", 94 @basic.search('p:eq(2)'). html98 @basic.search('p:eq(2)').to_html 95 99 assert_equal '<p class="last final"><b>THE FINAL PARAGRAPH</b></p>', 96 @basic.search('p:last'). html100 @basic.search('p:last').to_html 97 101 assert_equal 'last final', @basic.search('//p:last-of-type').first.get_attribute('class').to_s 98 102 end
