lib/primer/view_components/linters/helpers.rb
# This assumes that the AST provided represents valid HTML, where each tag has a corresponding closing tag.
# From the tags, we build a structured tree which represents the tag hierarchy.
# With this, we are able to know where the tags start and end.
This assumes that the AST provided represents valid HTML, where each tag has a corresponding closing tag.
From the tags, we build a structured tree which represents the tag hierarchy.
With this, we are able to know where the tags start and end.
This assumes that the AST provided represents valid HTML where each tag has a corresponding closing tag . From the tags we build a structured tree which represents the tag hierarchy . With this we are able to know where the tags start and end .
def build_tag_tree(tags)
        tag_tree = {}
        current_opened_tag = nil
        tags.each do |tag|
          if tag.closing?
            if current_opened_tag && tag.name == current_opened_tag.name
              tag_tree[current_opened_tag][:closing] = tag
              current_opened_tag = tag_tree[current_opened_tag][:parent]
            end
            next
          end
          self_closing = self_closing?(tag)
          tag_tree[tag] = {
            closing: self_closing ? tag : nil,
            parent: current_opened_tag
          }
          current_opened_tag = tag unless self_closing
        end
        tag_tree
      end
def build_tag_tree ( tags ) tag_tree = { } current_opened_tag = nil tags . each do | tag | if tag . closing? if current_opened_tag && tag . name == current_opened_tag . name tag_tree [ current_opened_tag ] [ :closing ] = tag current_opened_tag = tag_tree [ current_opened_tag ] [ :parent ] end next end self_closing = self_closing? ( tag ) tag_tree [ tag ] = { closing : self_closing ? tag : nil , parent : current_opened_tag } current_opened_tag = tag unless self_closing end tag_tree end
This assumes that the AST provided represents valid HTML, where each tag has a corresponding closing tag.
This assumes that the AST provided represents valid HTML where each tag has a corresponding closing tag .
tags