Changeset 102

Show
Ignore:
Timestamp:
02/04/2007 11:00:55 (22 months ago)
Author:
tec
Message:
  • lib/markaby/builder.rb: moved Auto-Form-Tag-Naming outside of CssProxy?
  • lib/markaby/cssproxy.rb: allow tags with no attributes or child content (see ticket:54)
  • test/test_markaby.rb: tests for the above changes
Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/markaby/builder.rb

    r101 r102  
    117117          elsif args.last.respond_to?(:to_hash) 
    118118              attrs = args.last.to_hash 
     119               
     120              if @tagset.forms.include?(tag) and attrs[:id] 
     121                attrs[:name] ||= attrs[:id] 
     122              end 
     123               
    119124              attrs.each do |k, v| 
    120125                  atname = k.to_s.downcase.intern 
     
    190195      if @auto_validation and @tagset.self_closing.include?(sym) and block 
    191196        raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block" 
    192       end 
    193       if args.empty? and block.nil? 
    194         return CssProxy.new do |args, block| 
    195           if @tagset.forms.include?(sym) and args.last.respond_to?(:to_hash) and args.last[:id] 
    196             args.last[:name] ||= args.last[:id] 
    197           end 
    198           tag!(sym, *args, &block) 
    199         end 
    200       end 
    201       tag!(sym, *args, &block) 
     197      elsif args.empty? and block.nil? 
     198        CssProxy.new(self, @streams.last, sym) 
     199      else 
     200        tag!(sym, *args, &block) 
     201      end 
    202202    end 
    203203 
  • trunk/lib/markaby/cssproxy.rb

    r74 r102  
    66  class CssProxy 
    77 
    8     # Creates a CssProxy object.  The +opts+ and +block+ passed in are 
    9     # stored until the element is created by Builder.tag! 
    10     def initialize(opts = {}, &blk) 
    11       @opts = opts 
    12       @blk = blk 
     8    # Creates a CssProxy object. 
     9    def initialize(builder, stream, sym) 
     10      @builder, @stream, @sym, @attrs = builder, stream, sym, {} 
     11       
     12      @original_stream_length = @stream.length 
     13       
     14      @builder.tag! @sym 
    1315    end 
    14    
    15     # Adds attributes to an element, for internal use only.  For example, if you 
    16     # want to write a wrapper which sets a bunch of default attributes for a certain 
    17     # tag.  Like the default `img' method included with Markaby automatically sets an 
    18     # empty alt attribute. 
    19     def merge!(opts) 
    20       @opts.merge! opts 
    21       self 
     16     
     17    # Adds attributes to an element.  Bang methods set the :id attribute. 
     18    # Other methods add to the :class attribute. 
     19    def method_missing(id_or_class, *args, &block) 
     20      if (idc = id_or_class.to_s) =~ /!$/ 
     21        @attrs[:id] = $` 
     22      else 
     23        @attrs[:class] = @attrs[:class].nil? ? idc : "#{@attrs[:class]} #{idc}".strip 
     24      end 
     25 
     26      unless args.empty? 
     27        if args.last.respond_to? :to_hash 
     28          @attrs.merge! args.pop.to_hash 
     29        end 
     30      end 
     31       
     32      args.push(@attrs) 
     33       
     34      while @stream.length > @original_stream_length 
     35        @stream.pop 
     36      end 
     37       
     38      if block 
     39        @builder.tag! @sym, *args, &block 
     40      else 
     41        @builder.tag! @sym, *args 
     42      end 
     43       
     44      return self 
    2245    end 
    23  
    24     # Adds attributes to an element.  Bang methods set the :id attribute. 
    25     # Other methods add to the :class attribute.  If a block is supplied, 
    26     # it is executed with a merged hash (@opts + args). 
    27     def method_missing(id_or_class, *args, &blk) 
    28       idc = id_or_class.to_s 
    29       case idc 
    30       when "pass" 
    31       when /!$/ 
    32         @opts[:id] = $` 
    33       else  
    34         @opts[:class] = "#{@opts[:class]} #{idc}".strip 
    35       end 
    36       if args.empty? and blk.nil? 
    37         self 
    38       else 
    39         if args.last.respond_to? :to_hash 
    40           @opts.merge!(args.pop.to_hash) 
    41         end 
    42         args.push @opts 
    43         @blk.call(args, blk) 
    44       end 
    45     end 
    46  
    47     def to_str 
    48       @blk.call([[@opts]]).to_s 
    49     end 
    50     alias_method :to_s, :to_str 
    5146 
    5247  end 
  • trunk/test/test_markaby.rb

    r100 r102  
    2929  def test_simple 
    3030    assert_equal "<hr/>", mab { hr } 
     31    assert_equal "<hr/><br/>", mab { hr; br } 
    3132    assert_equal "<p>foo</p>", mab { p 'foo' } 
    3233    assert_equal "<p>foo</p>", mab { p { 'foo' } } 
     
    3839    assert_equal %{<div id="three"></div>}, mab { div.three! '' } 
    3940    assert_equal %{<hr class="hidden"/>}, mab { hr.hidden } 
     41    assert_equal %{<input class="foo" name="bar" id="bar"/>}, mab { input.foo :id => 'bar' } 
    4042  end 
    4143