module Hpricot::Builder
def tag!(tag, *args, &block)
Create a tag named +tag+. Other than the first argument which is the tag name,
def tag!(tag, *args, &block) ele_id = nil if @auto_validation and @tagset if !@tagset.tagset.has_key?(tag) raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}" elsif args.last.respond_to?(:to_hash) attrs = args.last.to_hash if @tagset.forms.include?(tag) and attrs[:id] attrs[:name] ||= attrs[:id] end attrs.each do |k, v| atname = k.to_s.downcase.intern unless k =~ /:/ or @tagset.tagset[tag].include? atname raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements" end if atname == :id ele_id = v.to_s if @elements.has_key? ele_id raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)." end end end end end # turn arguments into children or attributes childs = [] attrs = args.grep(Hash) childs.concat((args - attrs).flatten.map do |x| if x.respond_to? :to_html Hpricot.make(x.to_html) elsif x Text.new(x.fast_xs) end end.flatten) attrs = attrs.inject({}) do |hsh, ath| ath.each do |k, v| hsh[k] = v.to_s.fast_xs if v end hsh end # create the element itself tag = tag.to_s f = Elem.new(tag, attrs, childs, ETag.new(tag)) # build children from the block if block build(f, &block) end add_child f f end