From ae522de8c4bf610a76475224fb401514e99be572 Mon Sep 17 00:00:00 2001 From: hammadikhlaq7vals <70008214+hammad-Ikhlaq-7vals@users.noreply.github.com> Date: Mon, 29 May 2023 23:59:42 +0500 Subject: [PATCH] Add aria-label for previous and next page links (#645) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- lib/will_paginate/locale/en.yml | 2 ++ lib/will_paginate/view_helpers/link_renderer.rb | 12 +++++++----- spec/view_helpers/action_view_spec.rb | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/will_paginate/locale/en.yml b/lib/will_paginate/locale/en.yml index bf24f00d3..fd0312b73 100644 --- a/lib/will_paginate/locale/en.yml +++ b/lib/will_paginate/locale/en.yml @@ -1,7 +1,9 @@ en: will_paginate: previous_label: "← Previous" + previous_aria_label: "Previous page" next_label: "Next →" + next_aria_label: "Next page" page_gap: "…" container_aria_label: "Pagination" page_aria_label: "Page %{page}" diff --git a/lib/will_paginate/view_helpers/link_renderer.rb b/lib/will_paginate/view_helpers/link_renderer.rb index ecfd00b5d..f430492a7 100644 --- a/lib/will_paginate/view_helpers/link_renderer.rb +++ b/lib/will_paginate/view_helpers/link_renderer.rb @@ -59,19 +59,21 @@ def gap def previous_page num = @collection.current_page > 1 && @collection.current_page - 1 - previous_or_next_page(num, @options[:previous_label], 'previous_page') + aria_label = @template.will_paginate_translate(:previous_aria_label) { "Previous page" } + previous_or_next_page(num, @options[:previous_label], 'previous_page', aria_label) end def next_page num = @collection.current_page < total_pages && @collection.current_page + 1 - previous_or_next_page(num, @options[:next_label], 'next_page') + aria_label = @template.will_paginate_translate(:next_aria_label) { "Next page" } + previous_or_next_page(num, @options[:next_label], 'next_page', aria_label) end - def previous_or_next_page(page, text, classname) + def previous_or_next_page(page, text, classname, aria_label = nil) if page - link(text, page, :class => classname) + link(text, page, :class => classname, :'aria-label' => aria_label) else - tag(:span, text, :class => classname + ' disabled') + tag(:span, text, :class => classname + ' disabled', :'aria-label' => aria_label) end end diff --git a/spec/view_helpers/action_view_spec.rb b/spec/view_helpers/action_view_spec.rb index 6c44c8485..ccebc2728 100644 --- a/spec/view_helpers/action_view_spec.rb +++ b/spec/view_helpers/action_view_spec.rb @@ -127,11 +127,11 @@ def renderer.gap() '~~' end it "should match expected markup" do paginate expected = <<-HTML - HTML expected.strip!.gsub!(/\s{2,}/, ' ') expected_dom = parse_html_document(expected)