Class: XRay::HTML::Element
- Inherits:
-
Node
- Object
- XRay::HTML::Element
show all
- Defined in:
- lib/html/query.rb,
lib/html/struct.rb
Constant Summary
collapse
- WORD =
/[A-Za-z][-_\w]*/
- CLASS =
%r(\.#{WORD})
- ID =
%r(##{WORD})
- PROP_PAIR =
%r(\s*,?\s*#{WORD}(==[^\s,])?)
- PROP =
%r(\[#{WORD}.*?\])
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tag, props = [], children = [], close_type = :after, ending = nil) ⇒ Element
Returns a new instance of Element.
20
21
22
23
24
25
|
# File 'lib/html/struct.rb', line 20
def initialize(tag, props=[], children=[], close_type=:after, ending=nil)
@tag, @props, @children, @close_type, @ending = tag, to_props(props), Array.[](children).flatten || [], close_type, ending
@position = @tag.position.dup if tag.is_a?(Node) and tag.position
@children.each { |el| el.parent = self }
@scopes = []
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
17
18
19
|
# File 'lib/html/struct.rb', line 17
def children
@children
end
|
#close_type ⇒ Object
Returns the value of attribute close_type.
18
19
20
|
# File 'lib/html/struct.rb', line 18
def close_type
@close_type
end
|
#ending ⇒ Object
Returns the value of attribute ending.
18
19
20
|
# File 'lib/html/struct.rb', line 18
def ending
@ending
end
|
#parent ⇒ Object
Returns the value of attribute parent.
18
19
20
|
# File 'lib/html/struct.rb', line 18
def parent
@parent
end
|
#props ⇒ Object
Returns the value of attribute props.
17
18
19
|
# File 'lib/html/struct.rb', line 17
def props
@props
end
|
#scopes ⇒ Object
Returns the value of attribute scopes.
18
19
20
|
# File 'lib/html/struct.rb', line 18
def scopes
@scopes
end
|
#tag ⇒ Object
Returns the value of attribute tag.
17
18
19
|
# File 'lib/html/struct.rb', line 17
def tag
@tag
end
|
Instance Method Details
#==(other) ⇒ Object
76
77
78
|
# File 'lib/html/struct.rb', line 76
def ==(other)
other.is_a?(Element) and tag_name == tag_name.to_s && prop_text == other.prop_text && inner_html == other.inner_html
end
|
#auto_close? ⇒ Boolean
109
110
111
|
# File 'lib/html/struct.rb', line 109
def auto_close?
AUTO_CLOSE_TAGS.include? tag_name.downcase
end
|
#closed? ⇒ Boolean
113
114
115
|
# File 'lib/html/struct.rb', line 113
def closed?
@close_type != :none
end
|
#each(&block) ⇒ Object
121
122
123
|
# File 'lib/html/struct.rb', line 121
def each(&block)
children.each {|node| yield node }
end
|
#empty? ⇒ Boolean
125
126
127
|
# File 'lib/html/struct.rb', line 125
def empty?
false
end
|
#has_prop?(name) ⇒ Boolean
80
81
82
|
# File 'lib/html/struct.rb', line 80
def has_prop?(name)
@props.any? { |p| p.name_equal? name }
end
|
#has_scope? ⇒ Boolean
27
28
29
|
# File 'lib/html/struct.rb', line 27
def has_scope?
!(parent.nil? and @scopes.empty?)
end
|
#in_scope?(scp) ⇒ Boolean
31
32
33
34
35
36
37
|
# File 'lib/html/struct.rb', line 31
def in_scope?(scp)
if parent
parent.tag_name_equal? scp
else
scopes.include?(scp)
end
end
|
#inline? ⇒ Boolean
105
106
107
|
# File 'lib/html/struct.rb', line 105
def inline?
INLINE_ELEMENTS.include? tag_name.downcase
end
|
#inner_html ⇒ Object
53
54
55
|
# File 'lib/html/struct.rb', line 53
def inner_html
@children.inject('') { |s,c| s + c.outer_html }
end
|
#inner_text ⇒ Object
Also known as:
text
66
67
68
|
# File 'lib/html/struct.rb', line 66
def inner_text
@children.inject('') { |s,c| s + c.inner_text }
end
|
#match?(str) ⇒ Boolean
Also known as:
===
This method implemented CSS selector for HTML (like Sizzle) very simply. It is not fully supported CSS selector.
TODO: support full CSS3 selector
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/html/query.rb', line 21
def match?(str)
return false if is_a?(TextElement)
obj = query_obj(str)
tag = obj[:tag]
cls = obj[:classes]
props = obj[:properties]
unless tag.nil? or tag_name_equal? tag
return false
end
classes = prop_value(:class)
cls.each { |c| return false unless classes.include? c }
props.each do |n, v|
if v.nil?
return false unless has_prop? n
else
return false unless prop_value(n) == v
end
end
true
end
|
#outer_html ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/html/struct.rb', line 57
def outer_html
if children.empty?
"<#{@tag}#{prop_text} />"
else
cls = ending || "</#{@tag}>"
"<#{@tag}#{prop_text}>#{inner_html}#{cls}"
end
end
|
#prop(name) ⇒ Object
84
85
86
|
# File 'lib/html/struct.rb', line 84
def prop(name)
@props.find { |p| p.name_equal? name }
end
|
#prop_text ⇒ Object
72
73
74
|
# File 'lib/html/struct.rb', line 72
def prop_text
props.inject('') { |s, p| s << " " << p.to_s }
end
|
#prop_value(*arg) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/html/struct.rb', line 88
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
|
#query(selector, &block) ⇒ Object
Also known as:
*
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/html/query.rb', line 77
def query( selector, &block )
ret = []
if match?(selector)
ret << self
yield self if block_given?
end
children && children.each do |node|
ret += node.query(selector, &block)
end
ret
end
|
#self_closed? ⇒ Boolean
117
118
119
|
# File 'lib/html/struct.rb', line 117
def self_closed?
@close_type == :self
end
|
#tag_name ⇒ Object
45
46
47
|
# File 'lib/html/struct.rb', line 45
def tag_name
@tag.is_a?(Node) ? @tag.text : @tag.to_s
end
|
#tag_name_equal?(name) ⇒ Boolean
49
50
51
|
# File 'lib/html/struct.rb', line 49
def tag_name_equal?(name)
tag_name.downcase == name.downcase
end
|
#to_s ⇒ Object
101
102
103
|
# File 'lib/html/struct.rb', line 101
def to_s
"[HTML: #{outer_html}]"
end
|