class HTMLProofer::Element
Represents the element currently being processed
Constants
- IMAGE_CANDIDATE_REGEX
-
From github.com/sindresorhus/srcset/blob/f7c48acd7facf18e94dec47e6b96e84e0f0e69dc/index.js#LL1-L16C71 This regex represents a loose rule of an “image candidate string”; see html.spec.whatwg.org/multipage/images.html#srcset-attribute An “image candidate string” roughly consists of the following:
-
Zero or more whitespace characters.
-
A non-empty URL that does not start or end with ‘,`.
-
Zero or more whitespace characters.
-
An optional “descriptor” that starts with a whitespace character.
-
Zero or more whitespace characters.
-
Each image candidate string is separated by a ‘,`.
We intentionally implement a loose rule here so that we can perform more aggressive error handling and reporting in the below code.
-
Attributes
Public Class Methods
Source
# File lib/html_proofer/element.rb, line 12 def initialize(runner, node, base_url: nil) @runner = runner @node = node swap_attributes! @base_url = base_url @url = Attribute::Url.new(runner, link_attribute, base_url: base_url, source: @runner.current_source, filename: @runner.current_filename) @line = node.line @content = node.content end
Public Instance Methods
Source
# File lib/html_proofer/element.rb, line 63 def href return if !a_tag? && !link_tag? @node["href"] end
Source
# File lib/html_proofer/element.rb, line 122 def ignore? return true if @node.attributes["data-proofer-ignore"] return true if ancestors_ignorable? return true if url&.ignore? false end
Source
# File lib/html_proofer/element.rb, line 25 def link_attribute meta_content || src || srcset || href end
Source
# File lib/html_proofer/element.rb, line 73 def link_tag? @node.name == "link" end
Source
# File lib/html_proofer/element.rb, line 29 def meta_content return unless meta_tag? @node["content"] end
Source
# File lib/html_proofer/element.rb, line 35 def meta_tag? @node.name == "meta" end
Source
# File lib/html_proofer/element.rb, line 106 def multiple_sizes? return false if blank?(srcsets) srcsets.any? do |srcset| !blank?(srcset) && srcset.split(" ").size > 1 end end
Source
# File lib/html_proofer/element.rb, line 81 def multiple_srcsets? !blank?(srcset) && srcset.split(",").size > 1 end
Source
# File lib/html_proofer/element.rb, line 49 def script_tag? @node.name == "script" end
Source
# File lib/html_proofer/element.rb, line 59 def source_tag? @node.name == "source" end
Source
# File lib/html_proofer/element.rb, line 39 def src return if !img_tag? && !script_tag? && !source_tag? @node["src"] end
Source
# File lib/html_proofer/element.rb, line 53 def srcset return if !img_tag? && !source_tag? @node["srcset"] end
Source
# File lib/html_proofer/element.rb, line 98 def srcsets return if blank?(srcset) srcset.split(IMAGE_CANDIDATE_REGEX).select.with_index do |_part, idx| idx.odd? end.map(&:strip) end
Source
# File lib/html_proofer/element.rb, line 114 def srcsets_wo_sizes return if blank?(srcsets) srcsets.map do |srcset| srcset.split(" ").first end end
Private Instance Methods
Source
# File lib/html_proofer/element.rb, line 152 def ancestors_ignorable? ancestors_attributes = @node.ancestors.map { |a| a.respond_to?(:attributes) && a.attributes } ancestors_attributes.pop # remove document at the end ancestors_attributes.any? { |a| !a["data-proofer-ignore"].nil? } end
Source
# File lib/html_proofer/element.rb, line 131 def attribute_swapped? return false if blank?(@runner.options[:swap_attributes]) attrs = @runner.options[:swap_attributes][@node.name] true unless blank?(attrs) end
Source
# File lib/html_proofer/element.rb, line 139 def swap_attributes! return unless attribute_swapped? attr_swaps = @runner.options[:swap_attributes][@node.name] attr_swaps.flatten.each_slice(2) do |(old_attr, new_attr)| next if blank?(node[old_attr]) node[new_attr] = node[old_attr] node.delete(old_attr) end end