Sha256: 92c558d7bd996af98d7298d262814075d48d8609e1498c12cda37e434ff5e581

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true
require "forwardable"

module ThemeCheck
  class HtmlNode
    extend Forwardable
    include RegexHelpers
    attr_reader :template, :parent

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

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

    def element?
      @value.element?
    end

    def children
      @children ||= @value
        .children
        .map { |child| HtmlNode.new(child, template, @placeholder_values, self) }
    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

    # @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 name
      if @value.name == "#document-fragment"
        "document"
      else
        @value.name
      end
    end

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

    def line_number
      @value.line
    end

    private

    def replace_placeholders(string)
      # Replace all {%#{i}####%} with the actual content.
      string.gsub(LIQUID_TAG) do |match|
        key = /\d+/.match(match)[0]
        @placeholder_values[key.to_i]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
theme-check-1.5.2 lib/theme_check/html_node.rb
theme-check-1.5.1 lib/theme_check/html_node.rb
theme-check-1.5.0 lib/theme_check/html_node.rb
theme-check-1.4.0 lib/theme_check/html_node.rb
theme-check-1.3.0 lib/theme_check/html_node.rb