Changeset 53

Show
Ignore:
Timestamp:
05/21/2006 22:20:45 (3 years ago)
Author:
why
Message:
  • lib/markaby/tags.rb: full sets of tags and attributes for Strict and Transitional.
  • lib/markaby/builder.rb: no more method_missing eating everything!
Location:
branches/xhtml-careful/lib/markaby
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/xhtml-careful/lib/markaby/builder.rb

    r52 r53  
    6262      @output_xml_instruction = @@default[:output_xml_instruction] 
    6363 
     64      use_tagset Markaby::XHTMLTransitionalTags 
     65 
    6466      if helpers.nil? 
    6567        @helpers = nil 
     
    166168    # * Otherwise, +sym+ and its arguments are passed to tag! 
    167169    def method_missing(sym, *args, &block) 
    168       if TAGS.include?(sym) or (FORM_TAGS.include?(sym) and args.empty?) 
     170      if @tagset and (@tagset_plain.include?(sym) or (@tagset_forms.include?(sym) and args.empty?)) 
    169171        if args.empty? and block.nil? 
    170172          return CssProxy.new do |args, block| 
    171             if FORM_TAGS.include?(sym) and args.last.respond_to?(:to_hash) and args.last[:id] 
     173            if @tagset_forms.include?(sym) and args.last.respond_to?(:to_hash) and args.last[:id] 
    172174              args.last[:name] ||= args.last[:id] 
    173175            end 
     
    179181        end 
    180182        tag!(sym, *args, &block) 
    181       elsif SELF_CLOSING_TAGS.include?(sym) 
     183      elsif @tagset and @tagset_self_closing.include?(sym) 
    182184        tag!(sym, *args) 
    183185      elsif @helpers.respond_to?(sym) 
     
    190192      elsif instance_variable_get("@#{sym}") 
    191193        instance_variable_get("@#{sym}") 
     194      elsif @tagset.nil? 
     195        tag!(sym, *args, &block) 
    192196      else 
    193         tag!(sym, *args, &block) 
     197        raise NoMethodError, "no such method `#{sym}'" 
    194198      end 
    195199    end 
     
    212216    end 
    213217 
     218    def use_tagset(tagset) 
     219      return if @tagset == tagset 
     220      @tagset = tagset 
     221      @tagset_tags = tagset.keys 
     222      @tagset_forms = @tagset_tags & FORM_TAGS 
     223      @tagset_self_closing = @tagset_tags & SELF_CLOSING_TAGS 
     224      @tagset_plain = @tagset_tags - @tagset_forms - @tagset_self_closing 
     225    end 
     226 
    214227    # Builds an html tag.  An XML 1.0 instruction and an XHTML 1.0 Transitional doctype 
    215228    # are prepended.  Also assumes <tt>:xmlns => "http://www.w3.org/1999/xhtml", 
    216229    # "xml:lang" => "en", :lang => "en"</tt>. 
    217230    def html(*doctype, &block) 
    218       doctype = XHTMLTransitional if doctype.empty? 
     231      if doctype.empty? 
     232        doctype = XHTMLTransitional 
     233        use_tagset Markaby::XHTMLTransitionalTags 
     234      end 
    219235      @builder.instruct! if @output_xml_instruction 
    220236      @builder.declare!(:DOCTYPE, :html, :PUBLIC, *doctype) 
     
    225241    # Builds an html tag with XHTML 1.0 Strict doctype instead. 
    226242    def xhtml_strict(&block) 
     243      use_tagset Markaby::XHTMLStrictTags 
    227244      html *XHTMLStrict, &block 
    228245    end 
  • branches/xhtml-careful/lib/markaby/tags.rb

    r27 r53  
    11module Markaby 
    22 
    3   TAGS = [ 
    4     :a, :abbr, :acronym, :span, :b, :caption, :del, :cite, :code, :col, 
    5     :colgroup, :dd, :dfn, :dt, :em, :fieldset, :i, :img, :ins, :kbd, :p, 
    6     :label, :legend, :li, :optgroup, :option, :select, :small, :span, :strong, 
    7     :sub, :sup,  :tbody, :td, :textarea, :thead, :title, :th, :tr, :tfoot, 
    8     :tt, :address, :blockquote, :body, :div, :dl, :form, :h1, :h2, :h3, :head, 
    9     :noscript, :object, :ol, :pre, :q, :samp, :script, :style, :table, :ul 
    10   ] 
     3  # Common sets of attributes. 
     4  AttrCustom = [:id, :class, :style] 
     5  AttrEvents = [:onclick, :ondblclick, :onmousedown, :onmouseup, :onmouseover, :onmousemove,  
     6      :onmouseout, :onkeypress, :onkeydown, :onkeyup] 
     7  AttrAnno = [:title, :lang, :dir] 
     8 
     9  # Very basic rules from the XHTML 1.0 Strict DTD. 
     10  XHTMLStrictTags = { 
     11    :pre => AttrCustom + AttrAnno + [:space],  
     12    :em => AttrCustom + AttrAnno + AttrEvents, 
     13    :code => AttrCustom + AttrAnno + AttrEvents, 
     14    :h2 => AttrCustom + AttrAnno + AttrEvents, 
     15    :h3 => AttrCustom + AttrAnno + AttrEvents, 
     16    :h1 => AttrCustom + AttrAnno + AttrEvents, 
     17    :h6 => AttrCustom + AttrAnno + AttrEvents, 
     18    :dl => AttrCustom + AttrAnno + AttrEvents, 
     19    :h4 => AttrCustom + AttrAnno + AttrEvents, 
     20    :h5 => AttrCustom + AttrAnno + AttrEvents, 
     21    :area => AttrCustom + AttrAnno + AttrEvents + [:accesskey, :tabindex, :onfocus, :onblur, :shape, :coords, :href, :nohref, :alt],  
     22    :meta => [:lang, :dir, :id, :'http-equiv', :name, :content, :scheme],  
     23    :table => [],  
     24    :dfn => AttrCustom + AttrAnno + AttrEvents, 
     25    :label => AttrCustom + AttrAnno + AttrEvents + [:for, :accesskey, :onfocus, :onblur],  
     26    :select => AttrCustom + AttrAnno + AttrEvents + [:name, :size, :multiple, :disabled, :tabindex, :onfocus, :onblur, :onchange],  
     27    :noscript => AttrCustom + AttrAnno + AttrEvents, 
     28    :style => [:lang, :dir, :id, :type, :media, :title, :space],  
     29    :strong => AttrCustom + AttrAnno + AttrEvents, 
     30    :span => AttrCustom + AttrAnno + AttrEvents, 
     31    :sub => AttrCustom + AttrAnno + AttrEvents, 
     32    :img => AttrCustom + AttrAnno + AttrEvents + [:src, :alt, :longdesc, :height, :width, :usemap, :ismap],  
     33    :title => [:lang, :dir, :id],  
     34    :bdo => AttrCustom + [:title, ] + AttrEvents + [:lang, :dir],  
     35    :tr => [],  
     36    :tbody => [],  
     37    :param => [:id, :name, :value, :valuetype, :type],  
     38    :li => AttrCustom + AttrAnno + AttrEvents, 
     39    :acronym => AttrCustom + AttrAnno + AttrEvents, 
     40    :html => [:lang, :dir, :id, :xmlns],  
     41    :caption => [],  
     42    :tfoot => [],  
     43    :th => [],  
     44    :sup => AttrCustom + AttrAnno + AttrEvents, 
     45    :var => AttrCustom + AttrAnno + AttrEvents, 
     46    :input => AttrCustom + AttrAnno + AttrEvents + [:accesskey, :tabindex, :onfocus, :onblur, :type, :name, :value, :checked, :disabled, :readonly, :size, :maxlength, :src, :alt, :usemap, :onselect, :onchange, :accept],  
     47    :td => AttrCustom + AttrAnno + AttrEvents + [:summary, :width, :border, :frame, :rules, :cellspacing, :cellpadding, :span, :align, :char, :charoff, :valign, :abbr, :axis, :headers, :scope, :rowspan, :colspan],  
     48    :samp => AttrCustom + AttrAnno + AttrEvents, 
     49    :cite => AttrCustom + AttrAnno + AttrEvents, 
     50    :thead => [],  
     51    :body => AttrCustom + AttrAnno + AttrEvents + [:onload, :onunload],  
     52    :map => AttrCustom + [:lang, :dir, ] + AttrEvents + [:title, :name],  
     53    :head => [:lang, :dir, :id, :profile],  
     54    :blockquote => AttrCustom + AttrAnno + AttrEvents + [:cite],  
     55    :fieldset => AttrCustom + AttrAnno + AttrEvents, 
     56    :option => AttrCustom + AttrAnno + AttrEvents + [:selected, :disabled, :label, :value],  
     57    :form => AttrCustom + AttrAnno + AttrEvents + [:action, :method, :enctype, :onsubmit, :onreset, :accept, 'accept-charset'],  
     58    :hr => AttrCustom + AttrAnno + AttrEvents, 
     59    :big => AttrCustom + AttrAnno + AttrEvents, 
     60    :dd => AttrCustom + AttrAnno + AttrEvents, 
     61    :object => AttrCustom + AttrAnno + AttrEvents + [:declare, :classid, :codebase, :data, :type, :codetype, :archive, :standby, :height, :width, :usemap, :name, :tabindex],  
     62    :base => [:href, :id],  
     63    :link => AttrCustom + AttrAnno + AttrEvents + [:charset, :href, :hreflang, :type, :rel, :rev, :media],  
     64    :kbd => AttrCustom + AttrAnno + AttrEvents, 
     65    :br => AttrCustom + [:title],  
     66    :address => AttrCustom + AttrAnno + AttrEvents, 
     67    :optgroup => AttrCustom + AttrAnno + AttrEvents + [:disabled, :label],  
     68    :dt => AttrCustom + AttrAnno + AttrEvents, 
     69    :ins => AttrCustom + AttrAnno + AttrEvents + [:cite, :datetime],  
     70    :b => AttrCustom + AttrAnno + AttrEvents, 
     71    :legend => AttrCustom + AttrAnno + AttrEvents + [:accesskey],  
     72    :abbr => AttrCustom + AttrAnno + AttrEvents, 
     73    :a => AttrCustom + AttrAnno + AttrEvents + [:accesskey, :tabindex, :onfocus, :onblur, :charset, :type, :name, :href, :hreflang, :rel, :rev, :shape, :coords],  
     74    :ol => AttrCustom + AttrAnno + AttrEvents, 
     75    :textarea => AttrCustom + AttrAnno + AttrEvents + [:accesskey, :tabindex, :onfocus, :onblur, :name, :rows, :cols, :disabled, :readonly, :onselect, :onchange],  
     76    :colgroup => [],  
     77    :i => AttrCustom + AttrAnno + AttrEvents, 
     78    :button => AttrCustom + AttrAnno + AttrEvents + [:accesskey, :tabindex, :onfocus, :onblur, :name, :value, :type, :disabled],  
     79    :script => [:id, :charset, :type, :src, :defer, :space],  
     80    :col => [],  
     81    :q => AttrCustom + AttrAnno + AttrEvents + [:cite],  
     82    :p => AttrCustom + AttrAnno + AttrEvents, 
     83    :del => AttrCustom + AttrAnno + AttrEvents + [:cite, :datetime],  
     84    :small => AttrCustom + AttrAnno + AttrEvents, 
     85    :div => AttrCustom + AttrAnno + AttrEvents, 
     86    :tt => AttrCustom + AttrAnno + AttrEvents, 
     87    :ul => AttrCustom + AttrAnno + AttrEvents   
     88  } 
     89 
     90  # Additional tags found in XHTML 1.0 Transitional 
     91  XHTMLTransitionalTags = XHTMLStrictTags.merge \ 
     92    :strike => AttrCustom + AttrAnno + AttrEvents, 
     93    :center => AttrCustom + AttrAnno + AttrEvents, 
     94    :dir => AttrCustom + AttrAnno + AttrEvents + [:compact],  
     95    :noframes => AttrCustom + AttrAnno + AttrEvents, 
     96    :basefont => [:id, :size, :color, :face],  
     97    :u => AttrCustom + AttrAnno + AttrEvents, 
     98    :menu => AttrCustom + AttrAnno + AttrEvents + [:compact],  
     99    :iframe => AttrCustom + [:title, :longdesc, :name, :src, :frameborder, :marginwidth, :marginheight, :scrolling, :align, :height, :width],  
     100    :font => AttrCustom + AttrAnno + [:size, :color, :face],  
     101    :s => AttrCustom + AttrAnno + AttrEvents, 
     102    :applet => AttrCustom + [:title, :codebase, :archive, :code, :object, :alt, :name, :width, :height, :align, :hspace, :vspace],  
     103    :isindex => AttrCustom + AttrAnno + [:prompt] 
     104 
     105  # Additional attributes found in XHTML 1.0 Transitional 
     106  { :script => [:language], 
     107    :a => [:target], 
     108    :td => [:bgcolor, :nowrap, :height], 
     109    :p => [:align], 
     110    :h5 => [:align], 
     111    :h3 => [:align], 
     112    :li => [:type, :value], 
     113    :div => [:align], 
     114    :pre => [:onclick, :ondblclick, :onmousedown, :onmouseup, :onmouseover, :onmousemove, :onmouseout, :onkeypress, :onkeydown, :onkeyup, :width], 
     115    :body => [:background, :bgcolor, :text, :link, :vlink, :alink], 
     116    :ol => [:type, :compact, :start], 
     117    :h4 => [:align], 
     118    :h2 => [:align], 
     119    :object => [:align, :border, :hspace, :vspace], 
     120    :img => [:name, :align, :border, :hspace, :vspace], 
     121    :link => [:target], 
     122    :legend => [:align], 
     123    :dl => [:compact], 
     124    :input => [:align], 
     125    :h6 => [:align], 
     126    :hr => [:align, :noshade, :size, :width], 
     127    :base => [:target], 
     128    :ul => [:type, :compact], 
     129    :br => [:clear], 
     130    :form => [:name, :target], 
     131    :area => [:target], 
     132    :h1 => [:align] 
     133  }.each do |k, v| 
     134      XHTMLTransitionalTags[k] += v 
     135  end 
    11136 
    12137  FORM_TAGS = [ :form, :input, :select, :textarea ]