Method: HTMLProofer::Element#initialize

Defined in:
lib/html-proofer/element.rb

#initialize(obj, check, logger) ⇒ Element

Returns a new instance of Element.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/html-proofer/element.rb', line 13

def initialize(obj, check, logger)
  @logger = logger
  # Construct readable ivars for every element
  begin
    obj.attributes.each_pair do |attribute, value|
      name = attribute.tr('-:.;@', '_').to_s.to_sym
      (class << self; self; end).send(:attr_reader, name)
      instance_variable_set("@#{name}", value.value)
    end
  rescue NameError => e
    @logger.log :error, "Attribute set `#{obj}` contains an error!"
    raise e
  end

  @aria_hidden = defined?(@aria_hidden) && @aria_hidden == 'true'

  @data_proofer_ignore = defined?(@data_proofer_ignore)

  @text = obj.content
  @check = check
  @checked_paths = {}
  @type = check.class.name
  @line = obj.line

  @html = check.html

  parent_attributes = obj.ancestors.map { |a| a.respond_to?(:attributes) && a.attributes }
  parent_attributes.pop # remove document at the end
  @parent_ignorable = parent_attributes.any? { |a| !a['data-proofer-ignore'].nil? }

  # fix up missing protocols
  if defined?(@href)
    @href.insert(0, 'http:') if %r{^//}.match?(@href)
  else
    @href = nil
  end

  if defined?(@src)
    @src.insert(0, 'http:') if %r{^//}.match?(@src)
  else
    @src = nil
  end

  if defined?(@srcset)
    @srcset.insert(0, 'http:') if %r{^//}.match?(@srcset)
  else
    @srcset = nil
  end
end