Class: Fdlint::Parser::HTML::Element
- Includes:
- Matchable
- Defined in:
- lib/fdlint/parser/html/struct.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#close_type ⇒ Object
Returns the value of attribute close_type.
-
#ending ⇒ Object
Returns the value of attribute ending.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#position ⇒ Object
Returns the value of attribute position.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
-
#scopes ⇒ Object
Returns the value of attribute scopes.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object
- #auto_close? ⇒ Boolean
- #closed? ⇒ Boolean
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #has_prop?(name) ⇒ Boolean
- #has_scope? ⇒ Boolean
- #in_scope?(scp) ⇒ Boolean
-
#initialize(tag, props = [], children = [], close_type = :after, ending = nil) ⇒ Element
constructor
A new instance of Element.
- #inline? ⇒ Boolean
- #inner_html ⇒ Object
- #inner_text ⇒ Object (also: #text)
- #outer_html ⇒ Object
- #prop(name) ⇒ Object
- #prop_text ⇒ Object
- #prop_value(*arg) ⇒ Object
- #self_closed? ⇒ Boolean
- #stylesheet_link? ⇒ Boolean
- #tag_name ⇒ Object
- #to_s ⇒ Object
- #top_level? ⇒ Boolean
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
40 41 42 |
# File 'lib/fdlint/parser/html/struct.rb', line 40 def children @children end |
#close_type ⇒ Object
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 |
#ending ⇒ Object
Returns the value of attribute ending.
41 42 43 |
# File 'lib/fdlint/parser/html/struct.rb', line 41 def ending @ending end |
#parent ⇒ Object
Returns the value of attribute parent.
41 42 43 |
# File 'lib/fdlint/parser/html/struct.rb', line 41 def parent @parent end |
#position ⇒ Object
Returns the value of attribute position.
41 42 43 |
# File 'lib/fdlint/parser/html/struct.rb', line 41 def position @position end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
40 41 42 |
# File 'lib/fdlint/parser/html/struct.rb', line 40 def props @props end |
#scopes ⇒ Object
Returns the value of attribute scopes.
41 42 43 |
# File 'lib/fdlint/parser/html/struct.rb', line 41 def scopes @scopes end |
#tag ⇒ Object (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
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
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
160 161 162 |
# File 'lib/fdlint/parser/html/struct.rb', line 160 def empty? false end |
#has_prop?(name) ⇒ 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
60 61 62 |
# File 'lib/fdlint/parser/html/struct.rb', line 60 def has_scope? !top_level? end |
#in_scope?(scp) ⇒ 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
140 141 142 |
# File 'lib/fdlint/parser/html/struct.rb', line 140 def inline? INLINE_ELEMENTS.include? tag_name.downcase end |
#inner_html ⇒ Object
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_text ⇒ Object 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_html ⇒ Object
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_text ⇒ Object
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
152 153 154 |
# File 'lib/fdlint/parser/html/struct.rb', line 152 def self_closed? @close_type == :self end |
#stylesheet_link? ⇒ Boolean
164 165 166 |
# File 'lib/fdlint/parser/html/struct.rb', line 164 def stylesheet_link? self =~ 'link' && self['rel'] =~ /stylesheet/i end |
#tag_name ⇒ Object
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_s ⇒ Object
136 137 138 |
# File 'lib/fdlint/parser/html/struct.rb', line 136 def to_s "[HTML: #{outer_html}]" end |
#top_level? ⇒ Boolean
64 65 66 |
# File 'lib/fdlint/parser/html/struct.rb', line 64 def top_level? parent.nil? and @scopes.empty? end |