Skip to content

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

FIX: broken XPath escaping and undefined CSS constant in anchor lookup (#37546)

Same60890dbaed4
diff --git a/lib/onebox/engine/standard_embed.rb b/lib/onebox/engine/standard_embed.rb
index 378e2803c5d..ea6516cebb8 100644
--- a/lib/onebox/engine/standard_embed.rb
+++ b/lib/onebox/engine/standard_embed.rb
@@ -233,9 +233,8 @@ module Onebox
end
def find_anchor_target(fragment)
- html_doc.at_xpath("//*[@id='#{fragment.gsub("'", "\\'")}']") ||
- html_doc.at_xpath("//a[@name='#{fragment.gsub("'", "\\'")}']") ||
- html_doc.at_css("##{CSS.escape(fragment)}")
+ escaped = fragment.gsub('"', """)
+ html_doc.at_xpath(%{//*[@id="#{escaped}"]}) || html_doc.at_xpath(%{//a[@name="#{escaped}"]})
end
def extract_description_from_target(target)
diff --git a/spec/lib/onebox/engine/standard_embed_spec.rb b/spec/lib/onebox/engine/standard_embed_spec.rb
index 51ac202f29c..6fc783828a4 100644
--- a/spec/lib/onebox/engine/standard_embed_spec.rb
+++ b/spec/lib/onebox/engine/standard_embed_spec.rb
@@ -203,6 +203,13 @@ RSpec.describe Onebox::Engine::StandardEmbed do
embed.send(:enhance_title_with_anchor)
expect(embed.instance_variable_get(:@raw)[:title]).to eq("Page")
end
+
+ it "handles fragments containing single quotes" do
+ html = "<h2 id=\"it's-complicated\">It's Complicated</h2>"
+ embed = embed_with_html("https://x.com#it's-complicated", html, { title: "Docs" })
+ embed.send(:enhance_title_with_anchor)
+ expect(embed.instance_variable_get(:@raw)[:title]).to eq("It's Complicated - Docs")
+ end
end
describe "#enhance_description_with_anchor" do