Changeset 155

Show
Ignore:
Timestamp:
08/31/2007 17:30:41 (15 months ago)
Author:
why
Message:

* lib/hpricot/traverse.rb: Element.make, for building fragments using the same options used to parse the doc. (thanks go to Rick Wargo for talking about this openly.)
* lib/hpricot/: switched almost every use of Hpricot.make with ele.make.

Location:
trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/hpricot/elements.rb

    r150 r155  
    131131    # Pass in an HTML +str+, which is turned into Hpricot elements. 
    132132    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)) } 
    134134    end 
    135135 
     
    137137    # Pass in an HTML +str+, which is turned into Hpricot elements. 
    138138    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) } 
    140140    end 
    141141  
     
    143143    # Pass in an HTML +str+, which is turned into Hpricot elements. 
    144144    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 } 
    146146    end 
    147147 
     
    149149    # Pass in an HTML +str+, which is turned into Hpricot elements. 
    150150    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 } 
    152152    end 
    153153 
     
    162162    def wrap(str = nil, &blk) 
    163163      each do |x| 
    164         wrap = Hpricot.make(str, &blk) 
     164        wrap = x.make(str, &blk) 
    165165        nest = wrap.detect { |w| w.respond_to? :children } 
    166166        unless nest 
  • trunk/lib/hpricot/parse.rb

    r151 r155  
    1313  # represented by Hpricot::Doc. 
    1414  def Hpricot.parse(input = nil, opts = {}, &blk) 
    15     Doc.new(make(input, opts, &blk)) 
     15    Doc.new(make(input, opts, &blk), opts) 
    1616  end 
    1717 
    1818  # Hpricot::XML parses <i>input</i>, disregarding all the HTML rules 
    1919  # 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) 
    2223  end 
    2324 
  • trunk/lib/hpricot/tag.rb

    r154 r155  
    44  class Doc 
    55    attr_accessor :children 
    6     def initialize(children = []) 
     6    def initialize(children = [], options = {}) 
    77      @children = children ? children.each { |c| c.parent = self }  : [] 
     8      @options = options 
    89    end 
    910    def output(out, opts = {}) 
     
    1213      end 
    1314      out 
     15    end 
     16    def make(input = nil, &blk) 
     17      Hpricot.make(input, @options, &blk) 
    1418    end 
    1519    def altered!; end 
  • trunk/lib/hpricot/traverse.rb

    r149 r155  
    2121    def bogusetag?() BogusETag::Trav === self end 
    2222 
     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 
    2333    # Builds an HTML string from this node and its contents. 
    2434    # If you need to write to a stream, try calling <tt>output(io)</tt> 
     
    110120    # Adds elements immediately after this element, contained in the +html+ string. 
    111121    def after(html = nil, &blk) 
    112       parent.insert_after(Hpricot.make(html, &blk), self) 
     122      parent.insert_after(make(html, &blk), self) 
    113123    end 
    114124 
    115125    # Adds elements immediately before this element, contained in the +html+ string. 
    116126    def before(html = nil, &blk) 
    117       parent.insert_before(Hpricot.make(html, &blk), self) 
     127      parent.insert_before(make(html, &blk), self) 
    118128    end 
    119129 
     
    123133    def swap(html = nil, &blk) 
    124134      parent.altered! 
    125       parent.replace_child(self, Hpricot.make(html, &blk)) 
     135      parent.replace_child(self, make(html, &blk)) 
    126136    end 
    127137 
     
    159169          self.children = inner 
    160170        else 
    161           self.children = Hpricot.make(inner, &blk) 
     171          self.children = make(inner, &blk) 
    162172        end 
    163173        reparent self.children 
  • trunk/test/test_alter.rb

    r150 r155  
    5959  end 
    6060 
     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 
    6173  def assert_changed original, selector, set, &block 
    6274    assert set.all?(&block)