Skip to content

Commit

Permalink
Fix visual bug with contents list manually numbered headings
Browse files Browse the repository at this point in the history
If the headings had trailing spaces, the regex would not fire
  • Loading branch information
AshGDS committed Oct 30, 2023
1 parent 30925ce commit f704f0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Fix visual bug with contents list manually numbered headings ([PR #3694](https://github.com/alphagov/govuk_publishing_components/pull/3694))

## 35.21.0

* Add metadata inverse no padding option ([PR #3689](https://github.com/alphagov/govuk_publishing_components/pull/3689))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def list_item_classes(list_item, nested)

def wrap_numbers_with_spans(content_item_link)
content_item_text = strip_tags(content_item_link) # just the text of the link

content_item_text_stripped = content_item_text.strip # strip trailing spaces for the regex. Keep original content_item_text for the string replacement.
# Must start with a number
# Number must be between 1 and 999 (ie not 2014)
# Must be followed by a space
# May contain a period `1.`
# May be a decimal `1.2`
number = /^\d{1,3}(\.?|\.\d{1,2})(?=\s)/.match(content_item_text)
number = /^\d{1,3}(\.?|\.\d{1,2})(?=\s)/.match(content_item_text_stripped)

if number
words = content_item_text.sub(number.to_s, "").strip # remove the number from the text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
assert_split_number_and_text("100. Thing", "100.", "Thing")
end

it "accounts for trailing spaces in the heading" do
assert_split_number_and_text(" 1. Thing", "1.", "Thing")
assert_split_number_and_text(" 10. Thing ", "10.", "Thing")
assert_split_number_and_text("100. Thing ", "100.", "Thing")
end

it "keeps a space between number and text for screen reader pronunciation" do
cl = GovukPublishingComponents::Presenters::ContentsListHelper.new({})
# 1.Thing can be pronounced "1 dot Thing"
Expand Down

0 comments on commit f704f0e

Please sign in to comment.