Class: Hamlizer::HtmlElement
- Inherits:
-
Object
- Object
- Hamlizer::HtmlElement
- Defined in:
- lib/hamlizer/html_element.rb
Instance Method Summary collapse
- #children ⇒ Object
- #content ⇒ Object
- #div? ⇒ Boolean
- #else? ⇒ Boolean
- #empty? ⇒ Boolean
- #end? ⇒ Boolean
- #erb? ⇒ Boolean
- #erb_non_print? ⇒ Boolean
- #erb_print? ⇒ Boolean
- #has_attributes? ⇒ Boolean
- #has_class? ⇒ Boolean
- #has_content? ⇒ Boolean
- #has_id? ⇒ Boolean
- #html_class ⇒ Object
- #id ⇒ Object
- #if? ⇒ Boolean
-
#initialize(element) ⇒ HtmlElement
constructor
A new instance of HtmlElement.
- #name ⇒ Object
- #only_child? ⇒ Boolean
- #parent ⇒ Object
- #regular_attributes ⇒ Object
- #text? ⇒ Boolean
Constructor Details
#initialize(element) ⇒ HtmlElement
Returns a new instance of HtmlElement.
5 6 7 |
# File 'lib/hamlizer/html_element.rb', line 5 def initialize(element) @element = element end |
Instance Method Details
#children ⇒ Object
9 10 11 |
# File 'lib/hamlizer/html_element.rb', line 9 def children element.children.map { |child| HtmlElement.new child }.select(&:has_content?) end |
#content ⇒ Object
13 14 15 |
# File 'lib/hamlizer/html_element.rb', line 13 def content element.content.strip end |
#div? ⇒ Boolean
17 18 19 |
# File 'lib/hamlizer/html_element.rb', line 17 def div? element.name == 'div' end |
#else? ⇒ Boolean
21 22 23 |
# File 'lib/hamlizer/html_element.rb', line 21 def else? (parent.erb? && text? && content.start_with?('els')) || (erb? && children.all?(&:else?)) end |
#empty? ⇒ Boolean
25 26 27 |
# File 'lib/hamlizer/html_element.rb', line 25 def empty? text? && (!content || content.empty?) end |
#end? ⇒ Boolean
29 30 31 |
# File 'lib/hamlizer/html_element.rb', line 29 def end? parent.erb? && text? && content.start_with?('end') || (erb? && children.all?(&:end?)) end |
#erb? ⇒ Boolean
33 34 35 |
# File 'lib/hamlizer/html_element.rb', line 33 def erb? erb_print? || erb_non_print? end |
#erb_non_print? ⇒ Boolean
41 42 43 |
# File 'lib/hamlizer/html_element.rb', line 41 def erb_non_print? name == 'erb_non_print' end |
#erb_print? ⇒ Boolean
37 38 39 |
# File 'lib/hamlizer/html_element.rb', line 37 def erb_print? name == 'erb_print' end |
#has_attributes? ⇒ Boolean
45 46 47 |
# File 'lib/hamlizer/html_element.rb', line 45 def has_attributes? element.attributes.any? end |
#has_class? ⇒ Boolean
49 50 51 |
# File 'lib/hamlizer/html_element.rb', line 49 def has_class? !html_class.nil? end |
#has_content? ⇒ Boolean
53 54 55 |
# File 'lib/hamlizer/html_element.rb', line 53 def has_content? !empty? end |
#has_id? ⇒ Boolean
57 58 59 |
# File 'lib/hamlizer/html_element.rb', line 57 def has_id? !id.nil? end |
#html_class ⇒ Object
69 70 71 |
# File 'lib/hamlizer/html_element.rb', line 69 def html_class @class ||= element['class'] end |
#id ⇒ Object
73 74 75 |
# File 'lib/hamlizer/html_element.rb', line 73 def id @id ||= element['id'] end |
#if? ⇒ Boolean
77 78 79 |
# File 'lib/hamlizer/html_element.rb', line 77 def if? parent.erb? && text? && content.start_with?('if') || (erb? && children.all?(&:if?)) end |
#name ⇒ Object
81 82 83 |
# File 'lib/hamlizer/html_element.rb', line 81 def name element.name end |
#only_child? ⇒ Boolean
61 62 63 |
# File 'lib/hamlizer/html_element.rb', line 61 def only_child? parent.children.count == 1 end |
#parent ⇒ Object
65 66 67 |
# File 'lib/hamlizer/html_element.rb', line 65 def parent HtmlElement.new element.parent end |
#regular_attributes ⇒ Object
85 86 87 |
# File 'lib/hamlizer/html_element.rb', line 85 def regular_attributes Hash[element.attributes.select { |name, _| !%w(id class).include? name }] end |
#text? ⇒ Boolean
89 90 91 |
# File 'lib/hamlizer/html_element.rb', line 89 def text? element.name == 'text' end |