Skip to content

Commit 00a853c

Browse files
committed
Do not add tbody when it's not present
The HTML spec says tbodys are optional
1 parent 0b24d39 commit 00a853c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/govspeak/html_sanitizer.rb

+18
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,34 @@ 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+
#raise Govspeak::Compare::TBodyError
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+
51+
end
52+
3953
def initialize(dirty_html, options = {})
4054
@dirty_html = dirty_html
4155
@allowed_image_hosts = options[:allowed_image_hosts]
56+
@strip_tbody = options[:strip_tbody]
4257
end
4358

4459
def sanitize
4560
transformers = [TableCellTextAlignWhitelister.new]
4661
if @allowed_image_hosts && @allowed_image_hosts.any?
4762
transformers << ImageSourceWhitelister.new(@allowed_image_hosts)
4863
end
64+
if @strip_tbody
65+
transformers << TableBodyWhitelister.new
66+
end
4967
Sanitize.clean(@dirty_html, Sanitize::Config.merge(sanitize_config, transformers: transformers))
5068
end
5169

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.merge!({ 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)