Don't sanitize anchor elements with no href (#16667)

* If href is nil, allow node

* Add unit test showing no change

My first pass had the `<a>` within an h1 content, but the scrubber
added a newline after the closing a tag?
This commit is contained in:
Daniel Uber 2022-02-22 11:27:38 -06:00 committed by GitHub
parent 40639ea602
commit a052b16015
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View file

@ -8,6 +8,6 @@ class FeedMarkdownScrubber < Rails::Html::PermitScrubber
def allowed_node?(node)
return true if tags.include?(node.name) && node.name != "a"
node.name == "a" && !node["href"].start_with?("#")
node.name == "a" && !node["href"]&.start_with?("#")
end
end

View file

@ -57,4 +57,10 @@ RSpec.describe FeedMarkdownScrubber, type: :permit_scrubber do
clean = sanitize(bad_html, scrubber: described_class.new)
expect(clean).to eq(good_html)
end
it "does not scrub anchors with no link" do
good_html = "I put an anchor <a>here</a>"
clean = sanitize(good_html, scrubber: described_class.new)
expect(clean).to eq(good_html)
end
end