Class: Hamlizer::HtmlElement

Inherits:
Object
  • Object
show all
Defined in:
lib/hamlizer/html_element.rb

Instance Method Summary collapse

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

#childrenObject



9
10
11
# File 'lib/hamlizer/html_element.rb', line 9

def children
  element.children.map { |child| HtmlElement.new child }.select(&:has_content?)
end

#contentObject



13
14
15
# File 'lib/hamlizer/html_element.rb', line 13

def content
  element.content.strip
end

#div?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/hamlizer/html_element.rb', line 17

def div?
  element.name == 'div'
end

#else?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


25
26
27
# File 'lib/hamlizer/html_element.rb', line 25

def empty?
  text? && (!content || content.empty?)
end

#end?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/hamlizer/html_element.rb', line 33

def erb?
  erb_print? || erb_non_print?
end

#erb_non_print?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/hamlizer/html_element.rb', line 41

def erb_non_print?
  name == 'erb_non_print'
end

#erb_print?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/hamlizer/html_element.rb', line 37

def erb_print?
  name == 'erb_print'
end

#has_attributes?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/hamlizer/html_element.rb', line 45

def has_attributes?
  element.attributes.any?
end

#has_class?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/hamlizer/html_element.rb', line 49

def has_class?
  !html_class.nil?
end

#has_content?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/hamlizer/html_element.rb', line 53

def has_content?
  !empty?
end

#has_id?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/hamlizer/html_element.rb', line 57

def has_id?
  !id.nil?
end

#html_classObject



69
70
71
# File 'lib/hamlizer/html_element.rb', line 69

def html_class
  @class ||= element['class']
end

#idObject



73
74
75
# File 'lib/hamlizer/html_element.rb', line 73

def id
  @id ||= element['id']
end

#if?Boolean

Returns:

  • (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

#nameObject



81
82
83
# File 'lib/hamlizer/html_element.rb', line 81

def name
  element.name
end

#only_child?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/hamlizer/html_element.rb', line 61

def only_child?
  parent.children.count == 1
end

#parentObject



65
66
67
# File 'lib/hamlizer/html_element.rb', line 65

def parent
  HtmlElement.new element.parent
end

#regular_attributesObject



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

Returns:

  • (Boolean)


89
90
91
# File 'lib/hamlizer/html_element.rb', line 89

def text?
  element.name == 'text'
end