Sha256: 71782c2bc0c8a4c6aad67dd11745d26b9787bab2cc7fc55e60fab134ea8e5fef

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true
require "forwardable"

module ThemeCheck
  class HtmlNode < Node
    extend Forwardable
    include RegexHelpers
    attr_reader :theme_file, :parent

    def initialize(value, theme_file, placeholder_values = [], parent = nil)
      @value = value
      @theme_file = theme_file
      @placeholder_values = placeholder_values
      @parent = parent
    end

    # @value is not forwarded because we _need_ to replace the
    # placeholders for the HtmlNode to make sense.
    def value
      if literal?
        content
      else
        markup
      end
    end

    def children
      @children ||= @value
        .children
        .map { |child| HtmlNode.new(child, theme_file, @placeholder_values, self) }
    end

    def markup
      @markup ||= replace_placeholders(@value.to_html)
    end

    def line_number
      @value.line
    end

    def start_index
      raise NotImplementedError
    end

    def end_index
      raise NotImplementedError
    end

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

    def element?
      @value.element?
    end

    def attributes
      @attributes ||= @value.attributes
        .map { |k, v| [replace_placeholders(k), replace_placeholders(v.value)] }
        .to_h
    end

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

    def name
      if @value.name == "#document-fragment"
        "document"
      else
        @value.name
      end
    end

    private

    def replace_placeholders(string)
      # Replace all ≬{i}####≬ with the actual content.
      string.gsub(HTML_LIQUID_PLACEHOLDER) do |match|
        key = /[0-9a-z]+/.match(match)[0]
        @placeholder_values[key.to_i(36)]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
theme-check-1.8.0 lib/theme_check/html_node.rb
theme-check-1.7.2 lib/theme_check/html_node.rb
theme-check-1.7.1 lib/theme_check/html_node.rb
theme-check-1.7.0 lib/theme_check/html_node.rb
theme-check-1.6.2 lib/theme_check/html_node.rb