Skip to content

A platform for community discussion. Free, open, simple.

FIX: Don't escape HTML entities twice in oneboxes (#37141)

Loïc Guitautcaaa8f9c9e0d
diff --git a/lib/onebox/normalizer.rb b/lib/onebox/normalizer.rb
index 2a5e3c2e401..2f374039865 100644
--- a/lib/onebox/normalizer.rb
+++ b/lib/onebox/normalizer.rb
@@ -11,6 +11,8 @@ module Onebox
sanitize_value(value, *args)
end
+ private
+
def method_missing(attr, *args, &block)
value = get(attr, *args)
@@ -26,7 +28,17 @@ module Onebox
end
end
- private
+ def respond_to_missing?(attr, include_private = false)
+ data.key?(attr) || super
+ end
+
+ def sanitize_value(value, length = nil, sanitize = true)
+ value = html_entities.decode(value)
+ value = html_entities.decode(Sanitize.fragment(value)) if sanitize
+ value.strip!
+ value = Onebox::Helpers.truncate(value, length) if length
+ value
+ end
def integer_suffixes
%w[width height]
@@ -39,13 +51,5 @@ module Onebox
def html_entities
@html_entities ||= HTMLEntities.new
end
-
- def sanitize_value(value, length = nil, sanitize = true)
- value = html_entities.decode(value)
- value = Sanitize.fragment(value) if sanitize
- value.strip!
- value = Onebox::Helpers.truncate(value, length) if length
- value
- end
end
end
diff --git a/spec/lib/onebox/open_graph_spec.rb b/spec/lib/onebox/open_graph_spec.rb
index 71de9468e8f..7b915828a27 100644
--- a/spec/lib/onebox/open_graph_spec.rb
+++ b/spec/lib/onebox/open_graph_spec.rb
@@ -3,26 +3,29 @@
require "onebox/open_graph"
RSpec.describe Onebox::OpenGraph do
- it "excludes html tags in title" do
- doc =
+ describe "Normalization" do
+ subject(:graph) { described_class.new(doc) }
+
+ let(:doc) do
Nokogiri.HTML(
- '<html><title>Did&#8217; you &lt;b&gt;miss me&lt;/b&gt;? - Album on Imgur</title><meta name="og:description" content="Post with 7 votes and 151 views. Shared by vinothkannans. Did you &lt;b&gt;miss me&lt;/b&gt;?" /><meta property="og:image" content="https://i.imgur.com/j1CNCZY.gif?noredirect" /></html>',
+ '<html><title>Did&#8217; you &lt;b&gt;miss me&lt;/b&gt;? - Album on Imgur</title><meta name="og:description" content="Post with 7 votes and 151 views. Shared by vinothkannans. Did you &lt;b&gt;miss me&lt;/b&gt;?" /><meta property="og:image" content="http://test.com/test\'ing.mp3" /><meta name="og:author" content="Batman &amp; Robin" /></html>',
)
- og = described_class.new(doc)
- expect(og.title).to eq("Did’ you miss me? - Album on Imgur")
- expect(og.description).to eq(
- "Post with 7 votes and 151 views. Shared by vinothkannans. Did you miss me?",
- )
- expect(og.image).to eq("https://i.imgur.com/j1CNCZY.gif?noredirect")
- end
+ end
- it "correctly normalizes the url properties" do
- doc =
- Nokogiri.HTML(
- "<html><meta property=\"og:image\" content=\"http://test.com/test'ing.mp3\" /></html>",
+ it "excludes html tags" do
+ expect(graph).to have_attributes(
+ title: "Did’ you miss me? - Album on Imgur",
+ description: "Post with 7 votes and 151 views. Shared by vinothkannans. Did you miss me?",
)
- og = described_class.new(doc)
- expect(og.image).to eq("http://test.com/test&apos;ing.mp3")
+ end
+
+ it "correctly normalizes the url properties" do
+ expect(graph.image).to eq("http://test.com/test&apos;ing.mp3")
+ end
+
+ it "normalizes ampersands properly" do
+ expect(graph.author).to eq("Batman & Robin")
+ end
end
describe "Collections" do