Skip to content

Commit 0e8aabd

Browse files
committed
Do not add tbody when it's not present
The HTML spec says tbodys are optional
1 parent 688654c commit 0e8aabd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/govspeak/html_sanitizer.rb

+17
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,33 @@ def invalid_style_attribute?(style)
3636
end
3737
end
3838

39+
class TableBodyWhitelister
40+
def call(sanitize_context)
41+
return unless %w[tbody].include?(sanitize_context[:node_name])
42+
43+
tbody = sanitize_context[:node]
44+
table = tbody.parent
45+
tbody.children.each do |node|
46+
table.add_child(node)
47+
end
48+
tbody.unlink
49+
end
50+
end
51+
3952
def initialize(dirty_html, options = {})
4053
@dirty_html = dirty_html
4154
@allowed_image_hosts = options[:allowed_image_hosts]
55+
@strip_tbody = options[:strip_tbody]
4256
end
4357

4458
def sanitize
4559
transformers = [TableCellTextAlignWhitelister.new]
4660
if @allowed_image_hosts && @allowed_image_hosts.any?
4761
transformers << ImageSourceWhitelister.new(@allowed_image_hosts)
4862
end
63+
if @strip_tbody
64+
transformers << TableBodyWhitelister.new
65+
end
4966
Sanitize.clean(@dirty_html, Sanitize::Config.merge(sanitize_config, transformers: transformers))
5067
end
5168

lib/govspeak/html_validator.rb

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def invalid?
1313
def valid?
1414
dirty_html = govspeak_to_html
1515
dirty_html.gsub!(Sanitize::REGEX_UNSUITABLE_CHARS, '')
16+
@sanitization_options[:strip_tbody] = true if !dirty_html.include?('tbody')
1617
clean_html = Govspeak::HtmlSanitizer.new(dirty_html, @sanitization_options).sanitize
1718
normalise_html(dirty_html) == normalise_html(clean_html)
1819
end

0 commit comments

Comments
 (0)