Class: PlatformosCheck::HtmlNode

Inherits:
Node
  • Object
show all
Extended by:
Forwardable, RegexHelpers
Includes:
PositionHelper, RegexHelpers
Defined in:
lib/platformos_check/html_node.rb

Constant Summary

Constants included from RegexHelpers

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

Methods included from RegexHelpers

matches

Methods included from PositionHelper

#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_fileObject (readonly)

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

#parentObject (readonly)

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

  # Replace all non-empty liquid tags with ≬{i}######≬ to prevent the HTML
  # parser from freaking out. We transparently replace those placeholders in
  # HtmlNode.
  #
  # We're using base36 to prevent index bleeding on 36^3 tags.
  # `{{x}}` -> `≬#{i}≬` would properly be transformed for 46656 tags in a single file.
  # Should be enough.
  #
  # The base10 alternative would have overflowed at 1000 (`{{x}}` -> `≬1000≬`) which seemed more likely.
  #
  # Didn't go with base64 because of the `=` character that would have messed with HTML parsing.
  #
  # (Note, we're also maintaining newline characters in there so
  # that line numbers match the source...)
  matches(parseable_source, LIQUID_TAG_OR_VARIABLE).each do |m|
    value = m[0]
    next unless value.size > 4 # skip empty tags/variables {%%} and {{}}

    placeholder_values.push(value)
    key = (placeholder_values.size - 1).to_s(36)

    # Doing shenanigans so that line numbers match... Ugh.
    keyed_placeholder = parseable_source[m.begin(0)...m.end(0)]

    # First and last chars are ≬
    keyed_placeholder[0] = ""
    keyed_placeholder[-1] = ""

    # Non newline characters are #
    keyed_placeholder.gsub!(/[^\n≬]/, '#')

    # First few # are replaced by the base10 ID of the tag
    i = -1
    keyed_placeholder.gsub!('#') do
      i += 1
      if i > key.size - 1
        '#'
      else
        key[i]
      end
    end

    # Replace source by placeholder
    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

#attributesObject



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

#childrenObject



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

#contentObject



180
181
182
# File 'lib/platformos_check/html_node.rb', line 180

def content
  @content ||= replace_placeholders(@value.content)
end

#element?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/platformos_check/html_node.rb', line 134

def element?
  @value.element?
end

#end_columnObject



126
127
128
# File 'lib/platformos_check/html_node.rb', line 126

def end_column
  position.end_column
end

#end_indexObject



110
111
112
# File 'lib/platformos_check/html_node.rb', line 110

def end_index
  position.end_index
end

#end_rowObject



122
123
124
# File 'lib/platformos_check/html_node.rb', line 122

def end_row
  position.end_row
end

#line_numberObject



102
103
104
# File 'lib/platformos_check/html_node.rb', line 102

def line_number
  @value.line
end

#literal?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/platformos_check/html_node.rb', line 130

def literal?
  @value.name == "text"
end

#markupObject



98
99
100
# File 'lib/platformos_check/html_node.rb', line 98

def markup
  @markup ||= replace_placeholders(parseable_markup)
end

#nameObject



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_markupObject



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.comment?
  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
  # Don't know what's up with the following issue. Don't think
  # null check is correct approach. This should give us more info.
  # https://github.com/Platform-OS/platformos-lsp/issues/528
  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_columnObject



118
119
120
# File 'lib/platformos_check/html_node.rb', line 118

def start_column
  position.start_column
end

#start_indexObject



106
107
108
# File 'lib/platformos_check/html_node.rb', line 106

def start_index
  position.start_index
end

#start_rowObject



114
115
116
# File 'lib/platformos_check/html_node.rb', line 114

def start_row
  position.start_row
end

#valueObject

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