Class: Fdlint::Parser::HTML::Element

Inherits:
Node
  • Object
show all
Includes:
Matchable
Defined in:
lib/fdlint/parser/html/struct.rb

Direct Known Subclasses

Tag

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Matchable

#=~

Constructor Details

#initialize(tag, props = [], children = [], close_type = :after, ending = nil) ⇒ Element

Returns a new instance of Element.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fdlint/parser/html/struct.rb', line 43

def initialize(tag, props=[], children=[], close_type=:after, ending=nil)

  @tag, @close_type, @ending = tag, close_type, ending
  @props  = to_props( props )
  @scopes = []

  if tag.respond_to?( :position ) and tag.position
    @position = @tag.position.dup
  end

  @children = (Array.[](children).flatten || []).tap do |children|
    children.each do |child|
      child.parent = self
    end
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



40
41
42
# File 'lib/fdlint/parser/html/struct.rb', line 40

def children
  @children
end

#close_typeObject

Returns the value of attribute close_type.



41
42
43
# File 'lib/fdlint/parser/html/struct.rb', line 41

def close_type
  @close_type
end

#endingObject

Returns the value of attribute ending.



41
42
43
# File 'lib/fdlint/parser/html/struct.rb', line 41

def ending
  @ending
end

#parentObject

Returns the value of attribute parent.



41
42
43
# File 'lib/fdlint/parser/html/struct.rb', line 41

def parent
  @parent
end

#positionObject

Returns the value of attribute position.



41
42
43
# File 'lib/fdlint/parser/html/struct.rb', line 41

def position
  @position
end

#propsObject (readonly)

Returns the value of attribute props.



40
41
42
# File 'lib/fdlint/parser/html/struct.rb', line 40

def props
  @props
end

#scopesObject

Returns the value of attribute scopes.



41
42
43
# File 'lib/fdlint/parser/html/struct.rb', line 41

def scopes
  @scopes
end

#tagObject (readonly)

Returns the value of attribute tag.



40
41
42
# File 'lib/fdlint/parser/html/struct.rb', line 40

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object



107
108
109
# File 'lib/fdlint/parser/html/struct.rb', line 107

def ==(other)
  other.is_a?(Element) and tag_name == tag_name.to_s && prop_text == other.prop_text && inner_html == other.inner_html
end

#[](name) ⇒ Object



132
133
134
# File 'lib/fdlint/parser/html/struct.rb', line 132

def [] name
  prop_value(name)
end

#auto_close?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/fdlint/parser/html/struct.rb', line 144

def auto_close?
  AUTO_CLOSE_TAGS.include? tag_name.downcase
end

#closed?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/fdlint/parser/html/struct.rb', line 148

def closed?
  auto_close? && !self_closed? || @close_type != :none
end

#each(&block) ⇒ Object



156
157
158
# File 'lib/fdlint/parser/html/struct.rb', line 156

def each(&block)
  children.each {|node| yield node }
end

#empty?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/fdlint/parser/html/struct.rb', line 160

def empty?
  false
end

#has_prop?(name) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/fdlint/parser/html/struct.rb', line 111

def has_prop?(name)
  @props.any? { |p| p.name_equal? name }
end

#has_scope?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/fdlint/parser/html/struct.rb', line 60

def has_scope?
  !top_level?
end

#in_scope?(scp) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/fdlint/parser/html/struct.rb', line 68

def in_scope?(scp)
  if parent
    parent.tag_name_equal? scp
  else
    scopes.include?(scp)
  end
end

#inline?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/fdlint/parser/html/struct.rb', line 140

def inline?
  INLINE_ELEMENTS.include? tag_name.downcase
end

#inner_htmlObject



84
85
86
# File 'lib/fdlint/parser/html/struct.rb', line 84

def inner_html
  @children.inject('') { |s,c| s + c.outer_html }
end

#inner_textObject Also known as: text



97
98
99
# File 'lib/fdlint/parser/html/struct.rb', line 97

def inner_text
  @children.inject('') { |s,c| s + c.inner_text }
end

#outer_htmlObject



88
89
90
91
92
93
94
95
# File 'lib/fdlint/parser/html/struct.rb', line 88

def outer_html
  if children.empty?
    "<#{@tag}#{prop_text} />"
  else
    cls = ending || "</#{@tag}>"
    "<#{@tag}#{prop_text}>#{inner_html}#{cls}"
  end
end

#prop(name) ⇒ Object



115
116
117
# File 'lib/fdlint/parser/html/struct.rb', line 115

def prop(name)
  @props.find { |p| p.name_equal? name }
end

#prop_textObject



103
104
105
# File 'lib/fdlint/parser/html/struct.rb', line 103

def prop_text
  props.inject('') { |s, p| s << " " << p.to_s }
end

#prop_value(*arg) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fdlint/parser/html/struct.rb', line 119

def prop_value(*arg)
  name, value = *arg
  if arg.size > 1
    unless @props.find { |p| p.value = value if p.name_equal? name }
      @props << Property.new(name, value) 
    end
    nil
  else
    p = prop(name)
    p.value if p
  end
end

#self_closed?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/fdlint/parser/html/struct.rb', line 152

def self_closed?
  @close_type == :self
end

#stylesheet_link?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/fdlint/parser/html/struct.rb', line 164

def stylesheet_link?
  self =~ 'link' && self['rel'] =~ /stylesheet/i
end

#tag_nameObject



80
81
82
# File 'lib/fdlint/parser/html/struct.rb', line 80

def tag_name
  @tag.is_a?(Node) ? @tag.text : @tag.to_s
end

#to_sObject



136
137
138
# File 'lib/fdlint/parser/html/struct.rb', line 136

def to_s
  "[HTML: #{outer_html}]"
end

#top_level?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/fdlint/parser/html/struct.rb', line 64

def top_level?
  parent.nil? and @scopes.empty?
end