class BetterHtml::TestHelper::SafeErb::Base

Attributes

errors[R]

Public Class Methods

new(parser, config: BetterHtml.config) click to toggle source
# File lib/better_html/test_helper/safe_erb/base.rb, line 12
def initialize(parser, config: BetterHtml.config)
  @parser = parser
  @config = config
  @errors = BetterHtml::Errors.new
end

Public Instance Methods

add_error(message, location:) click to toggle source
# File lib/better_html/test_helper/safe_erb/base.rb, line 18
def add_error(message, location:)
  @errors.add(SafetyError.new(message, location: location))
end

Protected Instance Methods

ast() click to toggle source
# File lib/better_html/test_helper/safe_erb/base.rb, line 50
def ast
  @parser.ast
end
erb_nodes(root_node) click to toggle source
# File lib/better_html/test_helper/safe_erb/base.rb, line 24
def erb_nodes(root_node)
  Enumerator.new do |yielder|
    next if root_node.nil?
    root_node.descendants(:erb).each do |erb_node|
      indicator_node, _, code_node, _ = *erb_node
      yielder.yield(erb_node, indicator_node, code_node)
    end
  end
end
script_tags() click to toggle source
# File lib/better_html/test_helper/safe_erb/base.rb, line 34
def script_tags
  Enumerator.new do |yielder|
    @parser.nodes_with_type(:tag).each do |tag_node|
      tag = Tree::Tag.from_node(tag_node)
      next if tag.closing?

      if tag.name == 'script'
        index = ast.to_a.find_index(tag_node)
        next_node = ast.to_a[index + 1]

        yielder.yield(tag, next_node&.type == :text ? next_node : nil)
      end
    end
  end
end