class Erector::XmlWidget

Abstract base class for XML Widgets and HTMLWidget. Declares “tags” which define methods that emit tags.

Public Class Methods

full_tags() click to toggle source

Tags which can contain other stuff

# File lib/erector/xml_widget.rb, line 64
def self.full_tags
  @tags.values.select{|tag| !tag.self_closing?}.map{|tag| tag.name}
end
self_closing_tags() click to toggle source

Tags which are always self-closing

# File lib/erector/xml_widget.rb, line 59
def self.self_closing_tags
  @tags.values.select{|tag| tag.self_closing?}.map{|tag| tag.name}
end
tag(*args) click to toggle source
# File lib/erector/xml_widget.rb, line 29
    def self.tag *args
      tag = Tag.new(*args)
      @tags ||= {}
      @tags[tag.name] = tag

      if instance_methods.include?(tag.method_name.to_sym)
        warn "method '#{tag.method_name}' is already defined; skipping #{caller[1]}"
        return
      end

      if tag.self_closing?
        self.class_eval("          def #{tag.method_name}(*args, &block)
            _empty_element('#{tag.name}', *args, &block)
          end
", __FILE__, __LINE__ + 1)
      else
        self.class_eval("        def #{tag.method_name}(*args, &block)
              _element('#{tag.name}', *args, &block)
          end

          def #{tag.method_name}!(*args, &block)
            _element('#{tag.name}', *(args.map{|a|raw(a)}), &block)
          end
", __FILE__, __LINE__ + 1)
      end
    end
tag_named(tag_name, checked = []) click to toggle source
# File lib/erector/xml_widget.rb, line 12
def self.tag_named tag_name, checked = []
  @tags ||= {}
  @tags[tag_name] || begin
    tag = nil
    checked << self
    taggy_ancestors = (ancestors - checked).select{|k| k.respond_to? :tag_named}
    taggy_ancestors.each do |k|
      tag = k.tag_named(tag_name, checked)
      if tag
        @tags[tag_name] = tag
        break
      end
    end
    tag
  end
end

Public Instance Methods

comment(text = '') { || ... } click to toggle source

Emits an XML/HTML comment (&lt;!– … –&gt;) surrounding text and/or the output of block. see www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

If text is an Internet Explorer conditional comment condition such as “[if IE]”, the output includes the opening condition and closing “[endif]”. See www.quirksmode.org/css/condcom.html

Since “Authors should avoid putting two or more adjacent hyphens inside comments,” we emit a warning if you do that.

# File lib/erector/xml_widget.rb, line 92
def comment(text = '')
  puts "Warning: Authors should avoid putting two or more adjacent hyphens inside comments." if text =~ /--/

  conditional = text =~ /\[if .*\]/

  rawtext "<!--"
  rawtext text
  rawtext ">" if conditional

  if block_given?
    rawtext "\n"
    yield
    rawtext "\n"
  end

  rawtext "<![endif]" if conditional
  rawtext "-->\n"
end
instruct(attributes={:version => "1.0", :encoding => "UTF-8"}) click to toggle source

Emits an XML instruction, which looks like this: <?xml version="1.0" encoding="UTF-8" ?>

# File lib/erector/xml_widget.rb, line 78
def instruct(attributes={:version => "1.0", :encoding => "UTF-8"})
  output << raw("<?xml#{format_sorted(sort_for_xml_declaration(attributes))}?>")
end
newliney?(tag_name) click to toggle source
# File lib/erector/xml_widget.rb, line 68
def newliney?(tag_name)
  tag = self.class.tag_named tag_name
  if tag
    tag.newliney?
  else
    true
  end
end

Protected Instance Methods

sort_for_xml_declaration(attributes) click to toggle source
# File lib/erector/xml_widget.rb, line 115
def sort_for_xml_declaration(attributes)
  # correct order is "version, encoding, standalone" (XML 1.0 section 2.8).
  # But we only try to put version before encoding for now.
  stringized = []
  attributes.each do |key, value|
    stringized << [key.to_s, value]
  end
  stringized.sort{|a, b| b <=> a}
end