diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b623cdc..236386f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +* Replace inline style attributes in td/th elements with classes [#268](https://github.com/alphagov/govspeak/pull/268) + ## 7.0.2 * Fix for abbreviations nested in button. [#267](https://github.com/alphagov/govspeak/pull/267) diff --git a/lib/govspeak/html_sanitizer.rb b/lib/govspeak/html_sanitizer.rb index 9458f45e..de554a94 100644 --- a/lib/govspeak/html_sanitizer.rb +++ b/lib/govspeak/html_sanitizer.rb @@ -69,6 +69,8 @@ def sanitize_config(allowed_elements: []) "svg" => Sanitize::Config::RELAXED[:attributes][:all] + %w[xmlns width height viewbox focusable], "path" => Sanitize::Config::RELAXED[:attributes][:all] + %w[fill d], "div" => [:data], + # @TODO These style attributes can be removed once we've checked there + # isn't hardcoded HTML in documents that uses them "th" => Sanitize::Config::RELAXED[:attributes]["th"] + %w[style], "td" => Sanitize::Config::RELAXED[:attributes]["td"] + %w[style], "govspeak-embed-attachment" => %w[content-id], diff --git a/lib/govspeak/post_processor.rb b/lib/govspeak/post_processor.rb index 26f5cfbd..d6178711 100644 --- a/lib/govspeak/post_processor.rb +++ b/lib/govspeak/post_processor.rb @@ -102,6 +102,23 @@ def self.extension(title, &block) end end + extension("convert table cell inline styles to classes") do |document| + document.css("th[style], td[style]").each do |el| + style = el.remove_attribute("style") + matches = style.value.scan(/text-align:\s*(left|center|right)/).flatten + + next unless matches.any? + + class_name = "cell-text-#{matches.last}" + + if el[:class] + el[:class] += " #{class_name}" + else + el[:class] = class_name + end + end + end + extension("use gem component for buttons") do |document| document.css(".govuk-button").map do |el| button_html = GovukPublishingComponents.render( diff --git a/test/govspeak_table_with_headers_test.rb b/test/govspeak_table_with_headers_test.rb index 61bdb990..539b046b 100644 --- a/test/govspeak_table_with_headers_test.rb +++ b/test/govspeak_table_with_headers_test.rb @@ -58,21 +58,21 @@ def expected_outcome_for_table_with_alignments - - - + + + - - - + + + - - - + + +
Second ColumnThird ColumnSecond ColumnThird Column
First rowCellCellFirst rowCellCell
Second rowCellCellSecond rowCellCell
@@ -255,7 +255,7 @@ def document_body_with_table_headers_containing_links assert_equal document_body_with_hashes_for_row_headers.to_html, expected_outcome end - test "Cells are aligned correctly" do + test "Cells are given classes to indicate alignment" do assert_equal document_body_with_alignments.to_html, expected_outcome_for_table_with_alignments end