Class: Hatemile::Implementation::AccessibleAssociationImplementation

Inherits:
AccessibleAssociation show all
Defined in:
lib/hatemile/implementation/accessible_association_implementation.rb

Overview

The AccessibleAssociationImplementation class is official implementation of AccessibleAssociation.

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ AccessibleAssociationImplementation

Initializes a new object that improve the accessibility of associations of parser.

Parameters:



232
233
234
235
236
237
238
239
240
241
# File 'lib/hatemile/implementation/accessible_association_implementation.rb', line 232

def initialize(parser)
  Hatemile::Helper.require_not_nil(parser)
  Hatemile::Helper.require_valid_type(
    parser,
    Hatemile::Util::Html::HTMLDOMParser
  )

  @parser = parser
  @id_generator = Hatemile::Util::IDGenerator.new('association')
end

Instance Method Details

#associate_all_data_cells_with_header_cellsObject



286
287
288
289
290
291
292
293
# File 'lib/hatemile/implementation/accessible_association_implementation.rb', line 286

def associate_all_data_cells_with_header_cells
  tables = @parser.find('table').list_results
  tables.each do |table|
    if Hatemile::Util::CommonFunctions.is_valid_element?(table)
      associate_data_cells_with_header_cells(table)
    end
  end
end

#associate_all_labels_with_fieldsObject



336
337
338
339
340
341
342
343
# File 'lib/hatemile/implementation/accessible_association_implementation.rb', line 336

def associate_all_labels_with_fields
  labels = @parser.find('label').list_results
  labels.each do |label|
    if Hatemile::Util::CommonFunctions.is_valid_element?(label)
      associate_label_with_field(label)
    end
  end
end

#associate_data_cells_with_header_cells(table) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/hatemile/implementation/accessible_association_implementation.rb', line 245

def associate_data_cells_with_header_cells(table)
  header = @parser.find(table).find_children('thead').first_result
  body = @parser.find(table).find_children('tbody').first_result
  footer = @parser.find(table).find_children('tfoot').first_result
  unless header.nil?
    prepare_header_cells(header)

    header_rows = get_model_table(header)
    if !body.nil? && valid_header?(header_rows)
      length_header = header_rows.first.size
      fake_table = get_model_table(body)
      unless footer.nil?
        fake_table = fake_table.concat(get_model_table(footer))
      end
      fake_table.each do |row|
        next unless row.size == length_header

        row.each_with_index do |cell, index|
          unless Hatemile::Util::CommonFunctions.is_valid_element?(cell)
            next
          end

          headers_ids = get_cells_headers_ids(header_rows, index)
          headers = cell.get_attribute('headers')
          headers_ids.each do |headers_id|
            headers = Hatemile::Util::CommonFunctions.increase_in_list(
              headers,
              headers_id
            )
          end
          cell.set_attribute('headers', headers)
        end
      end
    end
  end
  associate_data_cells_with_header_cells_of_row(body) unless body.nil?
  associate_data_cells_with_header_cells_of_row(footer) unless footer.nil?
end

#associate_label_with_field(label) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/hatemile/implementation/accessible_association_implementation.rb', line 297

def associate_label_with_field(label)
  return unless label.get_tag_name == 'LABEL'

  if label.has_attribute?('for')
    field = @parser.find("##{label.get_attribute('for')}").first_result
  else
    field = @parser.find(label).find_descendants(
      'input,select,textarea'
    ).first_result

    unless field.nil? ||
           !Hatemile::Util::CommonFunctions.is_valid_element?(field)
      @id_generator.generate_id(field)
      label.set_attribute('for', field.get_attribute('id'))
    end
  end

  return if field.nil?
  return unless Hatemile::Util::CommonFunctions.is_valid_element?(field)

  unless field.has_attribute?('aria-label')
    field.set_attribute(
      'aria-label',
      label.get_text_content.gsub(/[ \n\r\t]+/, ' ').strip
    )
  end

  @id_generator.generate_id(label)
  field.set_attribute(
    'aria-labelledby',
    Hatemile::Util::CommonFunctions.increase_in_list(
      field.get_attribute('aria-labelledby'),
      label.get_attribute('id')
    )
  )
end