Class: PlatformosCheck::HtmlNode
Constant Summary
RegexHelpers::HTML_LIQUID_PLACEHOLDER, RegexHelpers::LIQUID_TAG, RegexHelpers::LIQUID_TAG_OR_VARIABLE, RegexHelpers::LIQUID_VARIABLE, RegexHelpers::START_OR_END_QUOTE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
matches
#bounded, #from_index_to_row_column, #from_row_column_to_index
Constructor Details
#initialize(value, app_file, placeholder_values, parseable_source, parent = nil) ⇒ HtmlNode
Returns a new instance of HtmlNode.
74
75
76
77
78
79
80
|
# File 'lib/platformos_check/html_node.rb', line 74
def initialize(value, app_file, placeholder_values, parseable_source, parent = nil)
@value = value
@app_file = app_file
@placeholder_values = placeholder_values
@parseable_source = parseable_source
@parent = parent
end
|
Instance Attribute Details
#app_file ⇒ Object
Returns the value of attribute app_file.
10
11
12
|
# File 'lib/platformos_check/html_node.rb', line 10
def app_file
@app_file
end
|
#parent ⇒ Object
Returns the value of attribute parent.
10
11
12
|
# File 'lib/platformos_check/html_node.rb', line 10
def parent
@parent
end
|
Class Method Details
.parse(liquid_file) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/platformos_check/html_node.rb', line 15
def parse(liquid_file)
placeholder_values = []
parseable_source = +liquid_file.source.clone
matches(parseable_source, LIQUID_TAG_OR_VARIABLE).each do |m|
value = m[0]
next unless value.size > 4
placeholder_values.push(value)
key = (placeholder_values.size - 1).to_s(36)
keyed_placeholder = parseable_source[m.begin(0)...m.end(0)]
keyed_placeholder[0] = "≬"
keyed_placeholder[-1] = "≬"
keyed_placeholder.gsub!(/[^\n≬]/, '#')
i = -1
keyed_placeholder.gsub!('#') do
i += 1
if i > key.size - 1
'#'
else
key[i]
end
end
parseable_source[m.begin(0)...m.end(0)] = keyed_placeholder
end
new(
Nokogiri::HTML5.fragment(parseable_source, max_tree_depth: 400, max_attributes: 400),
liquid_file,
placeholder_values,
parseable_source
)
end
|
Instance Method Details
#attributes ⇒ Object
138
139
140
141
142
|
# File 'lib/platformos_check/html_node.rb', line 138
def attributes
@attributes ||= @value.attributes
.map { |k, v| [replace_placeholders(k), replace_placeholders(v.value)] }
.to_h
end
|
#children ⇒ Object
92
93
94
95
96
|
# File 'lib/platformos_check/html_node.rb', line 92
def children
@children ||= @value
.children
.map { |child| HtmlNode.new(child, app_file, @placeholder_values, @parseable_source, self) }
end
|
#content ⇒ Object
180
181
182
|
# File 'lib/platformos_check/html_node.rb', line 180
def content
@content ||= replace_placeholders(@value.content)
end
|
#element? ⇒ Boolean
134
135
136
|
# File 'lib/platformos_check/html_node.rb', line 134
def element?
@value.element?
end
|
#end_column ⇒ Object
126
127
128
|
# File 'lib/platformos_check/html_node.rb', line 126
def end_column
position.end_column
end
|
#end_index ⇒ Object
110
111
112
|
# File 'lib/platformos_check/html_node.rb', line 110
def end_index
position.end_index
end
|
#end_row ⇒ Object
122
123
124
|
# File 'lib/platformos_check/html_node.rb', line 122
def end_row
position.end_row
end
|
#line_number ⇒ Object
102
103
104
|
# File 'lib/platformos_check/html_node.rb', line 102
def line_number
@value.line
end
|
#literal? ⇒ Boolean
130
131
132
|
# File 'lib/platformos_check/html_node.rb', line 130
def literal?
@value.name == "text"
end
|
#markup ⇒ Object
98
99
100
|
# File 'lib/platformos_check/html_node.rb', line 98
def markup
@markup ||= replace_placeholders(parseable_markup)
end
|
#name ⇒ Object
184
185
186
187
188
189
190
|
# File 'lib/platformos_check/html_node.rb', line 184
def name
if @value.name == "#document-fragment"
"document"
else
@value.name
end
end
|
#parseable_markup ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/platformos_check/html_node.rb', line 144
def parseable_markup
return @parseable_source if @value.name == "#document-fragment"
return @value.to_str if @value.
return @value.content if literal?
start_index = from_row_column_to_index(@parseable_source, line_number - 1, 0)
@parseable_source
.match(/<\s*#{name}[^>]*>/im, start_index)[0]
rescue NoMethodError
PlatformosCheck.bug(<<~MSG)
Can't find a parseable tag of name #{name} inside the parseable HTML.
Tag name:
#{@value.name.inspect}
File:
#{@app_file.relative_path}
Line number:
#{line_number}
Excerpt:
```
#{@app_file.source.lines[line_number - 1...line_number + 5].join("")}
```
Parseable Excerpt:
```
#{@parseable_source.lines[line_number - 1...line_number + 5].join("")}
```
MSG
end
|
#start_column ⇒ Object
118
119
120
|
# File 'lib/platformos_check/html_node.rb', line 118
def start_column
position.start_column
end
|
#start_index ⇒ Object
106
107
108
|
# File 'lib/platformos_check/html_node.rb', line 106
def start_index
position.start_index
end
|
#start_row ⇒ Object
114
115
116
|
# File 'lib/platformos_check/html_node.rb', line 114
def start_row
position.start_row
end
|
#value ⇒ Object
placeholders for the HtmlNode to make sense.
84
85
86
87
88
89
90
|
# File 'lib/platformos_check/html_node.rb', line 84
def value
if literal?
content
else
markup
end
end
|