Skip to content

Commit

Permalink
Add aria-label for previous and next page links (#645)
Browse files Browse the repository at this point in the history
Co-authored-by: Mislav Marohnić <git@mislav.net>
  • Loading branch information
hammad-Ikhlaq-7vals and mislav committed May 29, 2023
1 parent 637b9fc commit ae522de
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/will_paginate/locale/en.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
en:
will_paginate:
previous_label: "&#8592; Previous"
previous_aria_label: "Previous page"
next_label: "Next &#8594;"
next_aria_label: "Next page"
page_gap: "&hellip;"
container_aria_label: "Pagination"
page_aria_label: "Page %{page}"
Expand Down
12 changes: 7 additions & 5 deletions lib/will_paginate/view_helpers/link_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/view_helpers/action_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def renderer.gap() '<span class="my-gap">~~</span>' end
it "should match expected markup" do
paginate
expected = <<-HTML
<div class="pagination" role="navigation" aria-label="Pagination"><span class="previous_page disabled">&#8592; Previous</span>
<div class="pagination" role="navigation" aria-label="Pagination"><span class="previous_page disabled" aria-label="Previous page">&#8592; Previous</span>
<em class="current" aria-label="Page 1" aria-current="page">1</em>
<a href="/foo/bar?page=2" aria-label="Page 2" rel="next">2</a>
<a href="/foo/bar?page=3" aria-label="Page 3">3</a>
<a href="/foo/bar?page=2" class="next_page" rel="next">Next &#8594;</a></div>
<a href="/foo/bar?page=2" class="next_page" rel="next" aria-label="Next page">Next &#8594;</a></div>
HTML
expected.strip!.gsub!(/\s{2,}/, ' ')
expected_dom = parse_html_document(expected)
Expand Down

0 comments on commit ae522de

Please sign in to comment.