Class: Acop::Enforcer
- Inherits:
-
Object
- Object
- Acop::Enforcer
- Defined in:
- lib/acop/accessibility.rb
Instance Attribute Summary collapse
-
#ah ⇒ Object
readonly
Returns the value of attribute ah.
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #accessibility_checks ⇒ Object
- #check_areas(source = @contents) ⇒ Object
- #check_doctype(source = @contents) ⇒ Object
- #check_flashing_content(source = @contents) ⇒ Object
- #check_form_control_text(source = @contents) ⇒ Object
-
#check_form_inputs(source = @contents) ⇒ Object
each input element of type (submit||reset||button) should have include a “value” attribute and should not have a corresponding label.
-
#check_form_labels(source = @contents) ⇒ Object
each form element should have a corresponding label.
- #check_frame_title(source = @contents) ⇒ Object
- #check_h1(source = @contents) ⇒ Object
- #check_heading_structure(source = @contents) ⇒ Object
- #check_heading_text(source = @contents) ⇒ Object
- #check_html_lang(source = @contents) ⇒ Object
- #check_hyperlinks(source = @contents) ⇒ Object
- #check_iframe_title(source = @contents) ⇒ Object
- #check_image_alt(source = @contents) ⇒ Object
- #check_image_input_alt(source = @contents) ⇒ Object
- #check_image_links(source = @contents) ⇒ Object
- #check_page_title(source = @contents) ⇒ Object
- #check_table_headers(source = @contents) ⇒ Object
- #check_table_summary(source = @contents) ⇒ Object
- #check_visual_formatting(source = @contents) ⇒ Object
-
#initialize(options = {}) ⇒ Enforcer
constructor
A new instance of Enforcer.
Constructor Details
#initialize(options = {}) ⇒ Enforcer
Returns a new instance of Enforcer.
10 11 12 13 14 15 16 |
# File 'lib/acop/accessibility.rb', line 10 def initialize(={}) @ah = Helpers.new url = [:url] url = "http://" + [:url] unless [:url].include?("http") @source = open(url) @contents = Nokogiri::HTML(@source) end |
Instance Attribute Details
#ah ⇒ Object (readonly)
Returns the value of attribute ah.
8 9 10 |
# File 'lib/acop/accessibility.rb', line 8 def ah @ah end |
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
8 9 10 |
# File 'lib/acop/accessibility.rb', line 8 def contents @contents end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
8 9 10 |
# File 'lib/acop/accessibility.rb', line 8 def source @source end |
Instance Method Details
#accessibility_checks ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/acop/accessibility.rb', line 18 def accessibility_checks = [] self.methods.each do |method| << (self.public_send(method)) if method[0..5] == "check_" end puts end |
#check_areas(source = @contents) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/acop/accessibility.rb', line 63 def check_areas(source=@contents) area_elements = source.css("area") = [] area_elements.each do |element| if (@ah.attribute_empty_or_nil(element, "alt")) .push("Missing alt text/attribute for area element with id/name: " + (element['name'] || element['id'] || "")) end end end |
#check_doctype(source = @contents) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/acop/accessibility.rb', line 92 def check_doctype(source=@contents) frame_elements = source.css("frame") iframe_elements = source.css("iframe") = [] if(frame_elements.length > 0 or iframe_elements.length > 0) doctype = REXML::Document.new(@source.string).doctype .push("Frames/iFrames present but doctype is missing") unless doctype end end |
#check_flashing_content(source = @contents) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/acop/accessibility.rb', line 121 def check_flashing_content(source=@contents) = [] %w{blink marquee}.each do |flashing_element| .push("Flashing elements such as 'blink' or 'marquee' should not be used") unless source.css(flashing_element).empty? end end |
#check_form_control_text(source = @contents) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/acop/accessibility.rb', line 189 def check_form_control_text(source=@contents) = [] labels = source.css("label") labels.each do |label| .push("Missing label text for label with for attribute: #{label['for']}") if (label.text==nil or label.text=="") end %w{legend button}.each do |control| fields = source.css(control) fields.each do |field| .push("Missing #{control} text for #{control}") if (field.text==nil or field.text=="") end end end |
#check_form_inputs(source = @contents) ⇒ Object
each input element of type (submit||reset||button) should have include a “value” attribute and should not have a corresponding label
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/acop/accessibility.rb', line 173 def check_form_inputs(source=@contents) forms = source.css("form") = [] forms.each do |form| input_fields = form.css("input").select {|field| field.attr('type') == 'submit' || field.attr('type') == 'reset' || field.attr('type') == 'button' } labels = form.css("label") input_fields.each do |field| value_present = field.attr('value') != nil and field.attr('value') != "" label_absent = (labels.select {|label| label['for'].to_s == field.attr('id').to_s }.size) < 1 .push("Missing value attribute/label present for input element with id/name: " + (field.attr('id').to_s || field.attr('name') || "")) unless(value_present and label_absent) end end end |
#check_form_labels(source = @contents) ⇒ Object
each form element should have a corresponding label
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/acop/accessibility.rb', line 151 def check_form_labels(source=@contents) forms = source.css("form") = [] forms.each do |form| labels = form.css("label") form_fields = [] %w{input select textarea button}.each do |element| element_fields = form.css(element) form_fields << form.css(element) unless element_fields.empty? end form_fields.flatten! form_fields.reject {|field| field.attr('submit') || field.attr('reset') || field.attr('button') } form_fields.each do |field| id = field.attr('id') .push("Missing label for form field with id/name: " + (id || field.attr('name') || "")) if (labels.select {|label| label['for'].to_s == id.to_s }.size < 1) end end end |
#check_frame_title(source = @contents) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/acop/accessibility.rb', line 129 def check_frame_title(source=@contents) return [] if source.css("frameset").length < 1 frame_elements = source.css("frame") = [] frame_elements.each do |frame| .push("Missing frame title element") unless frame['title'] .push("Empty frame title element") if frame['title'] == "" end end |
#check_h1(source = @contents) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/acop/accessibility.rb', line 205 def check_h1(source=@contents) h1_elements = source.css("h1") = [] .push("Missing heading level 1. Provide atleast one level 1 heading for document content") if h1_elements.empty? end |
#check_heading_structure(source = @contents) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/acop/accessibility.rb', line 225 def check_heading_structure(source=@contents) = [] all_nodes = [] source.at_css('body').traverse {|node| all_nodes << node.name } headings = all_nodes.select {|node| node =~ /^h\d/ } if headings.first != "h1" .push("First heading level should be h1") return end prev_heading_level = 0 headings.each do |heading| heading_level = heading[1] .push("Incorrect document heading structure") if (heading_level.to_i - prev_heading_level.to_i > 1) prev_heading_level = heading[1] end end |
#check_heading_text(source = @contents) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/acop/accessibility.rb', line 212 def check_heading_text(source=@contents) = [] headings = [] %w{h1 h2 h3 h4 h5 h6}.each do |heading| headings << source.css(heading) end headings.flatten! headings.each do |heading| .push("Missing text for #{heading.name} element") if(heading.text==nil or heading.text=="") end end |
#check_html_lang(source = @contents) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/acop/accessibility.rb', line 103 def check_html_lang(source=@contents) html = source.css("html") = [] .push("Missing lang attribute for html") if(html.attr('lang') == nil or html.attr('lang').value == "") end |
#check_hyperlinks(source = @contents) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/acop/accessibility.rb', line 110 def check_hyperlinks(source=@contents) hyperlinks = source.css("a") = [] hyperlinks.each do |link| .push("Missing link text for link with href: #{link['href']}") if(link.text==nil or link.text=="") end hyperlink_text = hyperlinks.collect {|link| link.text } .push("Links should not have duplicate text") if(hyperlink_text != hyperlink_text.uniq) end |
#check_iframe_title(source = @contents) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/acop/accessibility.rb', line 140 def check_iframe_title(source=@contents) iframe_elements = source.css("iframe") = [] iframe_elements.each do |iframe| .push("Missing iframe title element") unless iframe['title'] .push("Empty iframe title element") if iframe['title'] == "" end end |
#check_image_alt(source = @contents) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/acop/accessibility.rb', line 40 def check_image_alt(source=@contents) image_elements = source.css('img') = [] image_elements.each do |element| if (@ah.attribute_empty_or_nil(element, "alt") and element.parent.name != "a") .push("Missing alt text/attribute for image with src: " + element['src']) end end end |
#check_image_input_alt(source = @contents) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/acop/accessibility.rb', line 28 def check_image_input_alt(source=@contents) input_elements = source.css('input') image_inputs = input_elements.select {|image_input| image_input['type'] =~ /image/i} = [] image_inputs.each do |input| if (@ah.attribute_empty_or_nil(input, "alt") and input.parent.name != "a") .push("Missing alt text/attribute for image button with id/name: " + (input['name'] || input['id'] || "")) end end end |
#check_image_links(source = @contents) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/acop/accessibility.rb', line 51 def check_image_links(source=@contents) link_elements = source.css('a img') = [] image_links = [] link_elements.each do |link_element| if(link_element['alt'] != "") .push("Alt Text not empty or nil for image link with src: " + link_element['src']) end end end |
#check_page_title(source = @contents) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/acop/accessibility.rb', line 74 def check_page_title(source=@contents) title_element = source.css('title') = [] .push("Missing title element") if title_element.empty? .push("Empty title element") if(title_element.first and title_element.first.text == "") .push("More than 1 title element") if title_element.length > 1 end |
#check_table_headers(source = @contents) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/acop/accessibility.rb', line 254 def check_table_headers(source=@contents) = [] tables = source.css("table") tables.each do |table| if table.css("th").empty? .push("Missing table headers for table with summary: " + (table['summary'] || "")) end end tables.each do |table| th_elements = table.css("th") th_elements.each do |th| .push("Missing scope for table header") if (th['scope'] == nil || th['scope'] == "") end end end |
#check_table_summary(source = @contents) ⇒ Object
244 245 246 247 248 249 250 251 252 |
# File 'lib/acop/accessibility.rb', line 244 def check_table_summary(source=@contents) = [] tables = source.css("table") tables.each do |table| .push("Missing table summary") if(table['summary'] == nil or table['summary'] == "") end end |
#check_visual_formatting(source = @contents) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/acop/accessibility.rb', line 84 def check_visual_formatting(source=@contents) = [] %w{b i font center u}.each do |markup_element| .push("HTML visual formatting elements being used. Use CSS instead") unless source.css(markup_element).empty? end end |