A platform for community discussion. Free, open, simple.
FIX: Prevent post text from breaking lazy-videos' VideoObject JSON-LD (#45095)
Arpit Jalan873ad0ef268c
diff --git a/plugins/discourse-lazy-videos/lib/discourse_lazy_videos/crawler_post_end.rb b/plugins/discourse-lazy-videos/lib/discourse_lazy_videos/crawler_post_end.rbindex 607ff00b041..4c3d1bcdd3f 100644--- a/plugins/discourse-lazy-videos/lib/discourse_lazy_videos/crawler_post_end.rb+++ b/plugins/discourse-lazy-videos/lib/discourse_lazy_videos/crawler_post_end.rb@@ -5,7 +5,6 @@ module DiscourseLazyVideosattr_reader :controller, :postPROVIDER_NAMES = { "youtube" => "YouTube", "vimeo" => "Vimeo", "tiktok" => "TikTok" }.freeze- SCRIPT_ESCAPE_REGEX = %r{</script}iLAZY_VIDEO_CONTAINER = "lazy-video-container"def initialize(controller, post)@@ -82,8 +81,7 @@ module DiscourseLazyVideosenddef build_json_script(schema)- json = MultiJson.dump(schema).gsub(SCRIPT_ESCAPE_REGEX, "<\\/script")- "<script type=\"application/ld+json\">#{json}</script>"+ "<script type=\"application/ld+json\">#{ERB::Util.json_escape(MultiJson.dump(schema))}</script>"enddef get_embed_url(provider, video_id)diff --git a/plugins/discourse-lazy-videos/spec/lib/discourse_lazy_videos/crawler_post_end_spec.rb b/plugins/discourse-lazy-videos/spec/lib/discourse_lazy_videos/crawler_post_end_spec.rbindex 8e4f34a7a5b..f6f362f1bf1 100644--- a/plugins/discourse-lazy-videos/spec/lib/discourse_lazy_videos/crawler_post_end_spec.rb+++ b/plugins/discourse-lazy-videos/spec/lib/discourse_lazy_videos/crawler_post_end_spec.rb@@ -172,6 +172,28 @@ describe DiscourseLazyVideos::CrawlerPostEnd doendend+ context "when the post text contains an HTML comment opener and a script tag" do+ before { post1.update!(cooked: <<~HTML) }+ <p>Why does <!--<script> break my template?</p>+ <div class="youtube-onebox lazy-video-container"+ data-video-id="test123"+ data-video-title="Test Video"+ data-provider-name="youtube">+ <a href="https://www.youtube.com/watch?v=test123">+ <img src="https://img.youtube.com/vi/test123/maxresdefault.jpg">+ </a>+ </div>+ HTML++ it "keeps the script element closed and the description intact" do+ fragment = Nokogiri::HTML5.fragment("#{post_crawler_schema.html}<p></p>")+ schema = JSON.parse(fragment.at_css('script[type="application/ld+json"]').content)++ expect(fragment.children.map(&:name)).to eq(%w[script p])+ expect(schema["description"]).to start_with("Why does <!--<script> break my template?")+ end+ end+context "with missing video data" dobefore { post1.update!(cooked: <<~HTML) }<div class="lazy-video-container"