Class: Erector::XMLWidget

Inherits:
AbstractWidget show all
Includes:
Needs
Defined in:
lib/erector/xml_widget.rb

Overview

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

Direct Known Subclasses

HTMLWidget

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Needs

included, #initialize

Methods inherited from AbstractWidget

#call_block, #capture_content, #content, #emit, hyphenize_underscores, hyphenize_underscores=, #initialize, inline, prettyprint_default, #prettyprint_default, prettyprint_default=, #to_a, #to_s, #widget

Methods included from Convenience

#css, #dom_id, #javascript, #join, #to_pretty, #to_text, #url

Methods included from AfterInitialize

included, #initialize

Methods included from Text

#character, #h, #nbsp, #raw, #text, #text!

Methods included from Attributes

#format_attributes, #format_sorted, #sort_attributes

Methods included from Element

#_element, #_empty_element, #element, #empty_element

Class Method Details

.full_tagsObject

Tags which can contain other stuff



64
65
66
# 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_tagsObject

Tags which are always self-closing



59
60
61
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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(<<-SRC, __FILE__, __LINE__ + 1)
      def #{tag.method_name}(*args, &block)
        _empty_element('#{tag.name}', *args, &block)
      end
    SRC
  else
    self.class_eval(<<-SRC, __FILE__, __LINE__ + 1)
    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
    SRC
  end
end

.tag_named(tag_name, checked = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#comment(text = '') ⇒ Object

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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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"}) ⇒ Object

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



78
79
80
# 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) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# 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