Module: ERBLint::Linters::StartTagHelper

Defined in:
lib/erb_lint/linters/deprecated_classes.rb

Overview

Provides methods and classes for finding HTML start tags and their attributes.

Defined Under Namespace

Classes: Attribute, StartTag

Constant Summary collapse

ATTRIBUTE_NAME_PATTERN =

attribute names must be non empty and can’t contain a certain set of special characters

%r{[^\s"'>\/=]+}
ATTRIBUTE_VALUE_PATTERN =
%r{
  "([^"]*)" |           # double-quoted value
  '([^']*)' |           # single-quoted value
  ([^\s"'=<>`]+)        # unquoted non-empty value without special characters
}x
ATTRIBUTE_PATTERN =

attributes can be empty or have an attribute value

%r{
  #{ATTRIBUTE_NAME_PATTERN}        # attribute name
  (
    \s*=\s*                        # any whitespace around equals sign
    (#{ATTRIBUTE_VALUE_PATTERN})   # attribute value
  )?                               # attributes can be empty or have an assignemnt.
}x
TAG_NAME_PATTERN =
/[A-Za-z0-9]+/
START_TAG_PATTERN =
%r{
  <(#{TAG_NAME_PATTERN})         # start of tag with tag name
  (
    (
      \s+                        # required whitespace between tag name and first attribute and between attributes
      #{ATTRIBUTE_PATTERN}       # attributes
    )*
  )?                             # having an attribute block is optional
  \/?>                           # void or foreign elements can have a slash before tag close
}x

Class Method Summary collapse

Class Method Details

.start_tags(line) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/erb_lint/linters/deprecated_classes.rb', line 131

def start_tags(line)
  # TODO: Implement String Scanner to track quotes before the start tag begins to ensure that it is
  #       not enclosed inside of a string. Alternatively this problem would be solved by using
  #       a 3rd party parser like Nokogiri::XML

  start_tag_matching_groups = line.scan(/(#{START_TAG_PATTERN})/)
  start_tag_matching_groups.map do |start_tag_matching_group|
    tag_name = start_tag_matching_group[1]

    # attributes_string can be nil if there is no space after the tag name (and therefore no attributes).
    attributes_string = start_tag_matching_group[2] || ''

    attribute_list = attributes(attributes_string)

    StartTag.new(tag_name, attribute_list)
  end
end