Class: Clearwater::Component::Tag
- Inherits:
-
Object
- Object
- Clearwater::Component::Tag
- Defined in:
- lib/clearwater/component.rb
Instance Method Summary collapse
-
#initialize(tag_name, attributes = nil, content = nil) ⇒ Tag
constructor
A new instance of Tag.
- #sanitize_attributes(attributes) ⇒ Object
- #sanitize_content(content) ⇒ Object
- #to_html ⇒ Object (also: #to_s)
Constructor Details
#initialize(tag_name, attributes = nil, content = nil) ⇒ Tag
Returns a new instance of Tag.
43 44 45 46 47 |
# File 'lib/clearwater/component.rb', line 43 def initialize tag_name, attributes=nil, content=nil @tag_name = tag_name @attributes = sanitize_attributes(attributes) @content = content end |
Instance Method Details
#sanitize_attributes(attributes) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/clearwater/component.rb', line 68 def sanitize_attributes attributes return attributes unless attributes.is_a? Hash if attributes.key? :class_name or attributes.key? :className attributes[:class] ||= attributes.delete(:class_name) || attributes.delete(:className) end if Hash === attributes[:style] attributes[:style] = attributes[:style].map { |attr, value| attr = attr.to_s.tr('_', '-') "#{attr}:#{value}" }.join(';') end attributes.reject! do |key, handler| key[0, 2] == 'on' || DOMReference === handler end attributes end |
#sanitize_content(content) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/clearwater/component.rb', line 89 def sanitize_content content case content when Array content.map { |c| sanitize_content c }.join when String content.gsub('<', '<') else content.to_s end end |
#to_html ⇒ Object Also known as: to_s
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/clearwater/component.rb', line 49 def to_html html = "<#{@tag_name}" if @attributes @attributes.each do |attr, value| html << " #{attr}=#{value.to_s.inspect}" end end if @content html << '>' html << sanitize_content(@content) html << "</#{@tag_name}>" else html << '/>' end html end |