Class: HTMLProofer::Element
- Inherits:
-
Object
- Object
- HTMLProofer::Element
- Includes:
- Utils
- Defined in:
- lib/html-proofer/element.rb
Overview
Represents the element currently being processed
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#alt ⇒ Object
readonly
Returns the value of attribute alt.
-
#data_proofer_ignore ⇒ Object
readonly
Returns the value of attribute data_proofer_ignore.
-
#href ⇒ Object
readonly
Returns the value of attribute href.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#src ⇒ Object
readonly
Returns the value of attribute src.
Instance Method Summary collapse
- #absolute_path ⇒ Object
- #allow_hash_href? ⇒ Boolean
-
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd.
-
#external? ⇒ Boolean
path is external to the file.
- #file_path ⇒ Object
- #follow_location? ⇒ Boolean
- #hash ⇒ Object
- #ignore? ⇒ Boolean
- #ignore_empty_alt? ⇒ Boolean
- #ignores_pattern_check(links) ⇒ Object
-
#initialize(obj, check) ⇒ Element
constructor
A new instance of Element.
-
#internal? ⇒ Boolean
path is an anchor or a query.
- #non_http_remote? ⇒ Boolean
- #parts ⇒ Object
- #path ⇒ Object
-
#remote? ⇒ Boolean
path is to an external server.
- #scheme ⇒ Object
- #unslashed_directory?(file) ⇒ Boolean
- #url ⇒ Object
- #valid? ⇒ Boolean
Methods included from Utils
clean_content, create_nokogiri, #pluralize, swap
Constructor Details
#initialize(obj, check) ⇒ Element
Returns a new instance of Element.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/html-proofer/element.rb', line 11 def initialize(obj, check) # Contruct readable ivars for every element obj.attributes.each_pair do |attribute, value| name = "#{attribute.tr('-:.', '_')}".to_sym (class << self; self; end).send(:attr_reader, name) instance_variable_set("@#{name}", value.value) end @text = obj.content @check = check @checked_paths = {} @type = check.class.name @line = obj.line # fix up missing protocols @href.insert 0, 'http:' if @href =~ %r{^//} @src.insert 0, 'http:' if @src =~ %r{^//} end |
Instance Attribute Details
#alt ⇒ Object (readonly)
Returns the value of attribute alt.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def alt @alt end |
#data_proofer_ignore ⇒ Object (readonly)
Returns the value of attribute data_proofer_ignore.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def data_proofer_ignore @data_proofer_ignore end |
#href ⇒ Object (readonly)
Returns the value of attribute href.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def href @href end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def id @id end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def line @line end |
#link ⇒ Object (readonly)
Returns the value of attribute link.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def link @link end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def name @name end |
#src ⇒ Object (readonly)
Returns the value of attribute src.
9 10 11 |
# File 'lib/html-proofer/element.rb', line 9 def src @src end |
Instance Method Details
#absolute_path ⇒ Object
140 141 142 143 |
# File 'lib/html-proofer/element.rb', line 140 def absolute_path path = file_path || @check.path File. path, Dir.pwd end |
#allow_hash_href? ⇒ Boolean
89 90 91 |
# File 'lib/html-proofer/element.rb', line 89 def allow_hash_href? @check.[:allow_hash_href] end |
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd
135 136 137 138 |
# File 'lib/html-proofer/element.rb', line 135 def exists? return @checked_paths[absolute_path] if @checked_paths.key? absolute_path @checked_paths[absolute_path] = File.exist? absolute_path end |
#external? ⇒ Boolean
path is external to the file
94 95 96 |
# File 'lib/html-proofer/element.rb', line 94 def external? !internal? end |
#file_path ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/html-proofer/element.rb', line 103 def file_path return if path.nil? path_dot_ext = '' if @check.[:assume_extension] path_dot_ext = path + @check.[:extension] end if path =~ %r{^/} # path relative to root base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src) elsif File.exist?(File.(path, @check.src)) || File.exist?(File.(path_dot_ext, @check.src)) # relative links, path is a file base = File.dirname @check.path elsif File.exist?(File.join(File.dirname(@check.path), path)) || File.exist?(File.join(File.dirname(@check.path), path_dot_ext)) # relative links in nested dir, path is a file base = File.dirname @check.path else # relative link, path is a directory base = @check.path end file = File.join base, path # implicit index support if File.directory?(file) && !unslashed_directory?(file) file = File.join file, @check.[:directory_index_file] elsif @check.[:assume_extension] && File.file?("#{file}#{@check.[:extension]}") file = "#{file}#{@check.[:extension]}" end file end |
#follow_location? ⇒ Boolean
161 162 163 |
# File 'lib/html-proofer/element.rb', line 161 def follow_location? @check.[:typhoeus] && @check.[:typhoeus][:followlocation] end |
#hash ⇒ Object
50 51 52 |
# File 'lib/html-proofer/element.rb', line 50 def hash parts.fragment unless parts.nil? end |
#ignore? ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/html-proofer/element.rb', line 67 def ignore? return true if @data_proofer_ignore return true if url.match(/^javascript:/) # ignore base64 encoded images if %w(ImageCheck FaviconCheck).include? @type return true if url.match(/^data:image/) end # ignore user defined URLs return true if ignores_pattern_check(@check.[:url_ignore]) # ignore user defined alts return false unless 'ImageCheck' == @type return true if ignores_pattern_check(@check.[:alt_ignore]) end |
#ignore_empty_alt? ⇒ Boolean
85 86 87 |
# File 'lib/html-proofer/element.rb', line 85 def ignore_empty_alt? @check.[:empty_alt_ignore] end |
#ignores_pattern_check(links) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/html-proofer/element.rb', line 145 def ignores_pattern_check(links) links.each do |ignore| if ignore.is_a? String return true if ignore == url elsif ignore.is_a? Regexp return true if ignore =~ url end end false end |
#internal? ⇒ Boolean
path is an anchor or a query
99 100 101 |
# File 'lib/html-proofer/element.rb', line 99 def internal? url.start_with? '#', '?' end |
#non_http_remote? ⇒ Boolean
63 64 65 |
# File 'lib/html-proofer/element.rb', line 63 def non_http_remote? !scheme.nil? && !remote? end |
#parts ⇒ Object
40 41 42 43 44 |
# File 'lib/html-proofer/element.rb', line 40 def parts @parts ||= Addressable::URI.parse url rescue URI::Error, Addressable::URI::InvalidURIError @parts = nil end |
#path ⇒ Object
46 47 48 |
# File 'lib/html-proofer/element.rb', line 46 def path Addressable::URI.unencode parts.path unless parts.nil? end |
#remote? ⇒ Boolean
path is to an external server
59 60 61 |
# File 'lib/html-proofer/element.rb', line 59 def remote? %w( http https ).include? scheme end |
#scheme ⇒ Object
54 55 56 |
# File 'lib/html-proofer/element.rb', line 54 def scheme parts.scheme unless parts.nil? end |
#unslashed_directory?(file) ⇒ Boolean
157 158 159 |
# File 'lib/html-proofer/element.rb', line 157 def unslashed_directory?(file) File.directory?(file) && !file.end_with?(File::SEPARATOR) && !follow_location? end |
#url ⇒ Object
30 31 32 33 34 |
# File 'lib/html-proofer/element.rb', line 30 def url url = @src || @srcset || @href || '' return url if @check.[:url_swap].empty? swap(url, @check.[:url_swap]) end |
#valid? ⇒ Boolean
36 37 38 |
# File 'lib/html-proofer/element.rb', line 36 def valid? !parts.nil? end |