lib/theme_check/html_visitor.rb in theme-check-1.2.0 vs lib/theme_check/html_visitor.rb in theme-check-1.3.0
- old
+ new
@@ -1,27 +1,41 @@
# frozen_string_literal: true
-require "nokogumbo"
+require "nokogiri"
require "forwardable"
module ThemeCheck
class HtmlVisitor
+ include RegexHelpers
attr_reader :checks
def initialize(checks)
@checks = checks
+ @placeholder_values = []
end
def visit_template(template)
doc = parse(template)
- visit(HtmlNode.new(doc, template))
+ visit(HtmlNode.new(doc, template, @placeholder_values))
rescue ArgumentError => e
call_checks(:on_parse_error, e, template)
end
private
def parse(template)
- Nokogiri::HTML5.fragment(template.source, max_tree_depth: 400, max_attributes: 400)
+ parseable_source = +template.source.clone
+
+ # Replace all liquid tags with {%#{i}######%} to prevent the HTML
+ # parser from freaking out. We transparently replace those placeholders in
+ # HtmlNode.
+ matches(parseable_source, LIQUID_TAG_OR_VARIABLE).each do |m|
+ value = m[0]
+ @placeholder_values.push(value)
+ key = (@placeholder_values.size - 1).to_s
+ parseable_source[m.begin(0)...m.end(0)] = "{%#{key.ljust(m.end(0) - m.begin(0) - 4, '#')}%}"
+ end
+
+ Nokogiri::HTML5.fragment(parseable_source, max_tree_depth: 400, max_attributes: 400)
end
def visit(node)
call_checks(:on_element, node) if node.element?
call_checks(:"on_#{node.name}", node)