Skip to content

Commit

Permalink
Merge pull request #264 from alphagov/superscript_fix
Browse files Browse the repository at this point in the history
Fix superscript rendering in table headings.
  • Loading branch information
kashifatcha authored Dec 16, 2022
2 parents b752bf5 + 177854f commit a723f0d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 7.0.1

* Govspeak was stripping superscript from markdown when it shouldn't have. [#264](https://github.com/alphagov/govspeak/pull/264)

## 7.0.0

* BREAKING CHANGE Remove `PriorityList`, the `PrimaryList` JS module to render the priority list on the frontend is to be removed [#249](https://github.com/alphagov/govspeak/pull/249)
Expand Down
18 changes: 9 additions & 9 deletions lib/govspeak/post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,28 @@ def self.extension(title, &block)

extension("Add table headers and row / column scopes") do |document|
document.css("thead th").map do |el|
el.content = el.content.gsub(/^# /, "")
el.content = el.content.gsub(/[[:space:]]/, "") if el.content.blank? # Removes a strange whitespace in the cell if the cell is already blank.
el.name = "td" if el.content.blank? # This prevents a `th` with nothing inside it; a `td` is preferable.
el[:scope] = "col" if el.content.present? # `scope` shouldn't be used if there's nothing in the table heading.
el.inner_html = el.inner_html.gsub(/^# /, "")
el.inner_html = el.inner_html.gsub(/[[:space:]]/, "") if el.inner_html.blank? # Removes a strange whitespace in the cell if the cell is already blank.
el.name = "td" if el.inner_html.blank? # This prevents a `th` with nothing inside it; a `td` is preferable.
el[:scope] = "col" if el.inner_html.present? # `scope` shouldn't be used if there's nothing in the table heading.
end

document.css(":not(thead) tr td:first-child").map do |el|
next unless el.content.match?(/^#($|\s.*$)/)
next unless el.inner_html.match?(/^#($|\s.*$)/)

# Replace '# ' and '#', but not '#Word'.
# This runs on the first child of the element to preserve any links
el.children.first.content = el.children.first.content.gsub(/^#($|\s)/, "")
el.name = "th" if el.content.present? # This also prevents a `th` with nothing inside it; a `td` is preferable.
el[:scope] = "row" if el.content.present? # `scope` shouldn't be used if there's nothing in the table heading.
el.name = "th" if el.inner_html.present? # This also prevents a `th` with nothing inside it; a `td` is preferable.
el[:scope] = "row" if el.inner_html.present? # `scope` shouldn't be used if there's nothing in the table heading.
end
end

extension("use gem component for buttons") do |document|
document.css(".govuk-button").map do |el|
button_html = GovukPublishingComponents.render(
"govuk_publishing_components/components/button",
text: el.content,
text: el.inner_html,
href: el["href"],
start: el["data-start"],
data_attributes: {
Expand All @@ -123,7 +123,7 @@ def self.extension(title, &block)
extension("use custom footnotes") do |document|
document.css("a.footnote").map do |el|
footnote_number = el[:href].gsub(/\D/, "")
el.content = "[footnote #{footnote_number}]"
el.inner_html = "[footnote #{footnote_number}]"
end
document.css("[role='doc-backlink']").map do |el|
backlink_number = " #{el.css('sup')[0].content}" if el.css("sup")[0].present?
Expand Down
2 changes: 1 addition & 1 deletion lib/govspeak/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Govspeak
VERSION = "7.0.0".freeze
VERSION = "7.0.1".freeze
end
32 changes: 32 additions & 0 deletions test/govspeak_table_with_headers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ def expected_outcome_for_table_with_blank_table_headers
)
end

def expected_outcome_for_table_with_table_headers_containing_superscript
%(
<table>
<thead>
<tr>
<th scope="col">Foo<sup>bar</sup>
</th>
<th scope="col">Third Column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
</table>
)
end

def document_body_with_table_headers_containing_superscript
@document_body_with_table_headers_containing_superscript ||= Govspeak::Document.new(%(
| Foo<sup>bar</sup> | Third Column |
| --------------- | ------------------- |
| Cell | Cell |
))
end

def expected_outcome_for_table_headers_containing_links
%(
<table>
Expand Down Expand Up @@ -242,4 +270,8 @@ def document_body_with_table_headers_containing_links
test "Table headers are not blank" do
assert_equal document_body_with_blank_table_headers.to_html, expected_outcome_for_table_with_blank_table_headers
end

test "Table header superscript should parse" do
assert_equal document_body_with_table_headers_containing_superscript.to_html, expected_outcome_for_table_with_table_headers_containing_superscript
end
end

0 comments on commit a723f0d

Please sign in to comment.