Class: Hatemile::Implementation::AccessibleNavigationImplementation

Inherits:
AccessibleNavigation show all
Defined in:
lib/hatemile/implementation/accessible_navigation_implementation.rb

Overview

The AccessibleNavigationImplementation class is official implementation of AccessibleNavigation interface.

Constant Summary collapse

ID_CONTAINER_SKIPPERS =

The id of list element that contains the skippers.

'container-skippers'.freeze
ID_CONTAINER_HEADING_BEFORE =

The id of list element that contains the links for the headings, before the whole content of page.

'container-heading-before'.freeze
ID_CONTAINER_HEADING_AFTER =

The id of list element that contains the links for the headings, after the whole content of page.

'container-heading-after'.freeze
CLASS_TEXT_HEADING =

The HTML class of text of description of container of heading links.

'text-heading'.freeze
CLASS_SKIPPER_ANCHOR =

The HTML class of anchor of skipper.

'skipper-anchor'.freeze
CLASS_HEADING_ANCHOR =

The HTML class of anchor of heading link.

'heading-anchor'.freeze
'force-link-before'.freeze
'force-link-after'.freeze
DATA_ANCHOR_FOR =

The name of attribute that links the anchor of skipper with the element.

'data-anchorfor'.freeze
DATA_HEADING_ANCHOR_FOR =

The name of attribute that links the anchor of heading link with heading.

'data-headinganchorfor'.freeze
DATA_HEADING_LEVEL =

The name of attribute that indicates the level of heading of link.

'data-headinglevel'.freeze
DATA_ATTRIBUTE_LONG_DESCRIPTION_OF =

The name of attribute that link the anchor of long description with the image.

'data-attributelongdescriptionof'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(parser, configure, skipper_file_name = nil) ⇒ AccessibleNavigationImplementation

Initializes a new object that manipulate the accessibility of the navigation of parser.

Parameters:



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 330

def initialize(parser, configure, skipper_file_name = nil)
  Hatemile::Helper.require_not_nil(parser, configure)
  Hatemile::Helper.require_valid_type(
    parser,
    Hatemile::Util::Html::HTMLDOMParser
  )
  Hatemile::Helper.require_valid_type(
    configure,
    Hatemile::Util::Configure
  )
  Hatemile::Helper.require_valid_type(skipper_file_name, String)

  @parser = parser
  @id_generator = Hatemile::Util::IDGenerator.new('navigation')
  @elements_heading_before = configure.get_parameter(
    'elements-heading-before'
  )
  @elements_heading_after = configure.get_parameter(
    'elements-heading-after'
  )
  @attribute_long_description_prefix_before = configure.get_parameter(
    'attribute-longdescription-prefix-before'
  )
  @attribute_long_description_suffix_before = configure.get_parameter(
    'attribute-longdescription-suffix-before'
  )
  @attribute_long_description_prefix_after = configure.get_parameter(
    'attribute-longdescription-prefix-after'
  )
  @attribute_long_description_suffix_after = configure.get_parameter(
    'attribute-longdescription-suffix-after'
  )
  @skippers = get_skippers(configure, skipper_file_name)
  @list_skippers_added = false
  @list_heading_added = false
  @validate_heading = false
  @valid_heading = false
  @list_skippers = nil
  @list_heading_before = nil
  @list_heading_after = nil
end

Instance Method Details

#provide_navigation_by_all_headingsObject



495
496
497
498
499
500
501
502
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 495

def provide_navigation_by_all_headings
  headings = @parser.find('h1,h2,h3,h4,h5,h6').list_results
  headings.each do |heading|
    if Hatemile::Util::CommonFunctions.is_valid_element?(heading)
      provide_navigation_by_heading(heading)
    end
  end
end

#provide_navigation_by_all_skippersObject



414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 414

def provide_navigation_by_all_skippers
  @skippers.each do |skipper|
    elements = @parser.find(skipper[:selector]).list_results
    elements.each do |element|
      next unless Hatemile::Util::CommonFunctions.is_valid_element?(
        element
      )

      provide_navigation_by_skipper(element)
    end
  end
end

#provide_navigation_by_heading(heading) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 429

def provide_navigation_by_heading(heading)
  @valid_heading = valid_heading? unless @validate_heading

  return unless @valid_heading

  anchor = generate_anchor_for(
    heading,
    DATA_HEADING_ANCHOR_FOR,
    CLASS_HEADING_ANCHOR
  )

  return if anchor.nil?

  generate_list_heading unless @list_heading_added
  list_before = nil
  list_after = nil
  level = get_heading_level(heading)
  if level == 1
    list_before = @list_heading_before
    list_after = @list_heading_after
  else
    selector = "[#{DATA_HEADING_LEVEL}=\"#{level - 1}\"]"
    unless @list_heading_before.nil?
      super_item_before = @parser.find(
        @list_heading_before
      ).find_descendants(selector).last_result
    end
    unless super_item_before.nil?
      list_before = @parser.find(
        super_item_before
      ).find_children('ol').first_result
      if list_before.nil?
        list_before = @parser.create_element('ol')
        super_item_before.append_element(list_before)
      end
    end
    unless @list_heading_after.nil?
      super_item_after = @parser.find(
        @list_heading_after
      ).find_descendants(selector).last_result
    end
    unless super_item_after.nil?
      list_after = @parser.find(
        super_item_after
      ).find_children('ol').first_result
      if list_after.nil?
        list_after = @parser.create_element('ol')
        super_item_after.append_element(list_after)
      end
    end
  end

  item = @parser.create_element('li')
  item.set_attribute(DATA_HEADING_LEVEL, level.to_s)

  link = @parser.create_element('a')
  link.set_attribute('href', "##{anchor.get_attribute('name')}")
  link.append_text(heading.get_text_content)
  item.append_element(link)

  list_before.append_element(item.clone_element) unless list_before.nil?
  list_after.append_element(item.clone_element) unless list_after.nil?
end

#provide_navigation_by_skipper(element) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 374

def provide_navigation_by_skipper(element)
  skipper = nil
  @skippers.each do |auxiliar_skipper|
    elements = @parser.find(auxiliar_skipper[:selector]).list_results
    if elements.include?(element)
      skipper = auxiliar_skipper
      break
    end
  end

  return if skipper.nil?

  @list_skippers = generate_list_skippers unless @list_skippers_added

  return if @list_skippers.nil?

  anchor = generate_anchor_for(
    element,
    DATA_ANCHOR_FOR,
    CLASS_SKIPPER_ANCHOR
  )

  return if anchor.nil?

  item_link = @parser.create_element('li')
  link = @parser.create_element('a')
  link.set_attribute('href', "##{anchor.get_attribute('name')}")
  link.append_text(skipper[:description])

  free_shortcut(skipper[:shortcut])
  link.set_attribute('accesskey', skipper[:shortcut])

  @id_generator.generate_id(link)

  item_link.append_element(link)
  @list_skippers.append_element(item_link)
end

#provide_navigation_to_all_long_descriptionsObject



551
552
553
554
555
556
557
558
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 551

def provide_navigation_to_all_long_descriptions
  images = @parser.find('[alt][longdesc]').list_results
  images.each do |image|
    if Hatemile::Util::CommonFunctions.is_valid_element?(image)
      provide_navigation_to_long_description(image)
    end
  end
end

#provide_navigation_to_long_description(image) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/hatemile/implementation/accessible_navigation_implementation.rb', line 506

def provide_navigation_to_long_description(image)
  unless image.has_attribute?('longdesc') && image.has_attribute?('alt')
    return
  end

  @id_generator.generate_id(image)
  id = image.get_attribute('id')

  selector = "[#{DATA_ATTRIBUTE_LONG_DESCRIPTION_OF}=\"#{id}\"]"
  return unless @parser.find(selector).first_result.nil?

  alternative_text = image.get_attribute('alt').gsub(
    /[ \n\t\r]+/,
    ' '
  ).strip
  unless @attribute_long_description_prefix_before.empty? &&
         @attribute_long_description_suffix_before.empty?
    before_text = "#{@attribute_long_description_prefix_before}" \
                  "#{alternative_text}" \
                  "#{@attribute_long_description_suffix_before}"
    before_anchor = @parser.create_element('a')
    before_anchor.set_attribute('href', image.get_attribute('longdesc'))
    before_anchor.set_attribute('target', '_blank')
    before_anchor.set_attribute(DATA_ATTRIBUTE_LONG_DESCRIPTION_OF, id)
    before_anchor.set_attribute('class', CLASS_FORCE_LINK_BEFORE)
    before_anchor.append_text(before_text)
    image.insert_after(before_anchor)
  end
  unless @attribute_long_description_prefix_after.empty? &&
         @attribute_long_description_suffix_after.empty?
    after_text = "#{@attribute_long_description_prefix_after}" \
                  "#{alternative_text}" \
                  "#{@attribute_long_description_suffix_after}"
    after_anchor = @parser.create_element('a')
    after_anchor.set_attribute('href', image.get_attribute('longdesc'))
    after_anchor.set_attribute('target', '_blank')
    after_anchor.set_attribute(DATA_ATTRIBUTE_LONG_DESCRIPTION_OF, id)
    after_anchor.set_attribute('class', CLASS_FORCE_LINK_AFTER)
    after_anchor.append_text(after_text)
    image.insert_after(after_anchor)
  end
end