Changeset 55

Show
Ignore:
Timestamp:
05/22/2006 02:06:16 (3 years ago)
Author:
why
Message:
  • lib/markaby/builder.rb: validate correct attributes on xhtml elements.
  • README: explain the implicit stringification and validation additions on the branch.
Location:
branches/xhtml-careful
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • branches/xhtml-careful/README

    r20 r55  
    133133IDs may be added by the use of bang methods: 
    134134 
    135   div.page! 
    136     div.content! 
     135  div.page! { 
     136    div.content! { 
    137137      h1 "A Short Short Saintly Dog" 
    138     end 
    139   end 
     138    } 
     139  } 
    140140 
    141141Which results in: 
     
    156156  end 
    157157 
    158 == The <tt>capture</tt> Method 
    159  
    160 Want to catch a block of HTML as a string and play with it a bit? 
    161 Use the <tt>capture</tt> method. 
    162  
    163 Commonly used to join HTML blocks together: 
     158== Markaby prevents invalid HTML 
     159 
     160Since Markaby knows which doctype you're using, it checks a big 
     161list of valid tags and attributes before printing anything. 
     162 
     163  >> div :styl => "padding: 10px" do 
     164  >>   img :src => "samorost.jpg" 
     165  >> end 
     166  InvalidHtmlError: no such attribute `styl' 
     167 
     168== Auto-stringification 
     169 
     170If you end up using any of your Markaby "tags" as a string, the 
     171tag won't be output.  It'll be up to you to add the new string 
     172back into the HTML output. 
     173 
     174This means if you call <tt>to_s</tt>, you'll get a string back. 
     175 
     176  div.title { "Rock Bottom" + span(" by Robert Wyatt").to_s } 
     177 
     178But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically. 
     179 
     180  div.title { "Rock Bottom" + span(" by Robert Wyatt") } 
     181 
     182Interpolation works fine. 
     183 
     184  div.title { "Rock Bottom #{span(" by Robert Wyatt")}" } 
     185 
     186And any other operation you might perform on a string. 
    164187 
    165188  div.menu! \ 
    166189    ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category| 
    167       capture { link_to category } 
     190      link_to category 
    168191    end. 
    169192    join( " | " ) 
  • branches/xhtml-careful/lib/markaby.rb

    r30 r55  
    2121module Markaby 
    2222  VERSION = '0.4' 
     23 
     24  class InvalidXhtmlError < Exception; end 
    2325end 
    2426 
  • branches/xhtml-careful/lib/markaby/builder.rb

    r54 r55  
    145145    # the arguments are the same as the tags implemented via method_missing. 
    146146    def tag!(tag, *args, &block) 
     147      if @tagset 
     148          if !@tagset.has_key?(tag) 
     149              raise InvalidXhtmlError, "no element `#{tag}' for #{doctype}" 
     150          elsif args.last.respond_to?(:to_hash) 
     151              attrs = args.last.to_hash 
     152              attrs.each do |k, v| 
     153                  unless @tagset[tag].include? k 
     154                      raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements" 
     155                  end 
     156              end 
     157          end 
     158      end 
    147159      if block 
    148160        str = capture &block 
  • branches/xhtml-careful/test/test_markaby.rb

    r52 r55  
    1414class MarkabyTest < Test::Unit::TestCase 
    1515   
     16  def assert_exception(str, exclass, exmsg) 
     17    begin 
     18      mab(str) 
     19    rescue Exception => e 
     20      assert_equal e.class, exclass 
     21      assert_equal e.message[/^.+?: (.+)$/, 1], exmsg 
     22    else 
     23      assert nil, exclass 
     24    end 
     25  end 
     26 
    1627  def mab(string, assigns = {}, helpers = nil) 
    1728    Markaby::Template.new(string.to_s).render(assigns, helpers) 
     
    6475        mab(%q{div { @a = small 'Miniature'; @b = strong 'Large'; h1 "Monkeys"; h2 { "Giraffes #{@a} and #{@b}" }; h3 "Donkeys"; h4 { "Parakeet #{@b} as well..." }}}) 
    6576  end 
     77 
     78  def test_invalid_xhtml 
     79    assert_exception %{dav {}}, NoMethodError, "no such method `dav'" 
     80    assert_exception %{div(:styl => 'ok') {}}, Markaby::InvalidXhtmlError, "no attribute `styl' on div elements" 
     81    assert_exception %{tbody.okay {}}, Markaby::InvalidXhtmlError, "no attribute `class' on tbody elements" 
     82  end 
    6683end