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, #widget

Methods included from Convenience

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

Methods included from AfterInitialize

included

Methods included from Text

#character, #h, #nbsp, #raw, #rawtext, #text

Methods included from Attributes

#format_attributes

Methods included from Element

#_element, #_empty_element, #element, #empty_element

Class Method Details

.add_tags(tags) ⇒ Object



21
22
23
24
# File 'lib/erector/xml_widget.rb', line 21

def self.add_tags(tags)
  @tags ||= {}
  @tags = @tags.merge(tags)
end

.full_tagsObject

Tags which can contain other stuff



61
62
63
# File 'lib/erector/xml_widget.rb', line 61

def self.full_tags
  @tags.values.select{|tag| !tag.self_closing?}.map{|tag| tag.name}
end

.inherited(subclass) ⇒ Object



12
13
14
15
# File 'lib/erector/xml_widget.rb', line 12

def self.inherited(subclass)
  super
  subclass.add_tags(@tags) if @tags
end

.newliney?(tag_name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/erector/xml_widget.rb', line 65

def self.newliney?(tag_name)
  if (tag = self.tag_named(tag_name))
    tag.newliney?
  else
    true
  end
end

.self_closing_tagsObject

Tags which are always self-closing



56
57
58
# File 'lib/erector/xml_widget.rb', line 56

def self.self_closing_tags
  @tags.values.select{|tag| tag.self_closing?}.map{|tag| tag.name}
end

.tag(*args) ⇒ Object



26
27
28
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
# File 'lib/erector/xml_widget.rb', line 26

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) ⇒ Object



17
18
19
# File 'lib/erector/xml_widget.rb', line 17

def self.tag_named(tag_name)
  @tags && @tags[tag_name]
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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/erector/xml_widget.rb', line 88

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" ?>



74
75
76
# File 'lib/erector/xml_widget.rb', line 74

def instruct(attributes={:version => "1.0", :encoding => "UTF-8"})
  output << raw("<?xml#{format_attributes(sort_for_xml_declaration(attributes))}?>")
end