Changeset 16

Show
Ignore:
Timestamp:
07/07/2006 15:09:28 (2 years ago)
Author:
why
Message:
  • lib/hpricot/elements.rb: from JQuery: remove, empty, append, prepend, before, after, wrap, set, html(...), to_html, to_s.
  • lib/hpricot/traverse.rb: to_html, replace_child, insert_before, insert_after, innerHTML=.
  • lib/hpricot/parse.rb: Hpricot.make returns nodes without document, Hpricot(...) is an alias for parse.
  • lib/hpricot/tag.rb: open up all properties to setters, let people do as they may.
  • test/test_parser.rb: simple textnode parse, use to_html for the full html.
  • ext/hpricot_scan/hpricot_scan.rl: return the final text node, if it has content.
Location:
trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/ext/hpricot_scan/hpricot_scan.rl

    r15 r16  
    233233      if ( mark_tag != NULL && text == 1 ) 
    234234      { 
    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        } 
    236247      } 
    237248      mark_tag = buf; 
  • trunk/lib/hpricot/elements.rb

    r15 r16  
    66    alias_method :/, :search 
    77 
    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 
    1121 
    1222    def html=(str) 
     
    1727        nodes, = Elements.filter(self, expr) 
    1828        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 
    1966    end 
    2067 
     
    2875    end 
    2976 
    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 
    3184        each do |node| 
    3285          node.set_attribute(k, v) 
    3386        end 
     87      end 
    3488    end 
    3589 
  • trunk/lib/hpricot/parse.rb

    r15 r16  
    11require 'hpricot/htmlinfo' 
     2 
     3def Hpricot(input) 
     4  Hpricot.parse(input) 
     5end 
    26 
    37module Hpricot 
     
    59  # represented by Hpricot::Doc. 
    610  def Hpricot.parse(input) 
    7     parse_as(input) 
     11    Doc.new(make(input)) 
    812  end 
    913 
    1014  # :stopdoc: 
    1115 
    12   def Hpricot.parse_as(input) 
     16  def Hpricot.make(input) 
    1317    stack = [[nil, nil, []]] 
    1418    Hpricot.scan(input) do |token| 
     
    5357    structure_list = stack[0][2] 
    5458    # 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) } 
    5760  end 
    5861 
  • trunk/lib/hpricot/tag.rb

    r15 r16  
    2929    end 
    3030    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) } } 
    3333    end 
    3434    def output(out) 
     
    4949      @attributes = attributes 
    5050    end 
    51     attr_reader :name, :attributes 
     51    attr_accessor :name, :attributes 
    5252    def attributes_as_html 
    5353      if @attributes 
  • trunk/lib/hpricot/traverse.rb

    r15 r16  
    1313    def bogusetag?() BogusETag::Trav === self end 
    1414 
     15    def to_html 
     16      output("") 
     17    end 
     18    alias_method :to_s, :to_html 
     19 
    1520    def get_subnode(*indexes) 
    1621      n = self 
     
    2631      children.grep(Container::Trav) 
    2732    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 
    2853    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 
    3065    end 
    3166    def search(expr, &blk) 
     
    449484  module Elem::Trav 
    450485    def has_attribute?(name) 
    451       attributes && attributes.has_key?(name) 
     486      self.attributes && self.attributes.has_key?(name.to_s) 
    452487    end 
    453488    def get_attribute(name) 
    454       attributes && attributes[name] 
     489      self.attributes && self.attributes[name.to_s] 
    455490    end 
    456491    def set_attribute(name, val) 
    457       attributes ||= {} 
    458       attributes[name] = val 
     492      self.attributes ||= {} 
     493      self.attributes[name.to_s] = val 
    459494    end 
    460495  end 
  • trunk/test/test_parser.rb

    r15 r16  
    1515  #   assert_equal '', @basic.search('//p').map { |x| x.attributes } 
    1616  # end 
     17 
     18  def test_scan_text 
     19    assert_equal 'FOO', Hpricot.make("FOO").first.content 
     20  end 
    1721 
    1822  def test_get_element_by_id 
     
    9296    assert_equal 2, @boingboing.search('//table/tr:last').length 
    9397    assert_equal "<p>The third paragraph</p>", 
    94         @basic.search('p:eq(2)').html 
     98        @basic.search('p:eq(2)').to_html 
    9599    assert_equal '<p class="last final"><b>THE FINAL PARAGRAPH</b></p>', 
    96         @basic.search('p:last').html 
     100        @basic.search('p:last').to_html 
    97101    assert_equal 'last final', @basic.search('//p:last-of-type').first.get_attribute('class').to_s 
    98102  end