A platform for community discussion. Free, open, simple.
FIX: Count oneboxes in document order when applying `max_oneboxes_per_post`
Régis Hanol2cf4f2b03184
diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rbindex a072c8c61d9..2c44e841a47 100644--- a/app/models/post_analyzer.rb+++ b/app/models/post_analyzer.rb@@ -30,16 +30,17 @@ class PostAnalyzercooked = PrettyText.cook(raw, opts)end- limit = SiteSetting.max_oneboxes_per_postresult =- Oneboxer.apply(cooked, extra_paths: ".inline-onebox-loading") do |url, element|+ Oneboxer.apply(+ cooked,+ extra_paths: ".#{Oneboxer::INLINE_ONEBOX_LOADING_CSS_CLASS}",+ limit: SiteSetting.max_oneboxes_per_post,+ ) do |url, element|if opts[:invalidate_oneboxes]Oneboxer.invalidate(url)InlineOneboxer.invalidate(url)endnext if element["class"] != Oneboxer::ONEBOX_CSS_CLASS- next if limit <= 0- limit -= 1@onebox_urls << urlonebox = Oneboxer.cached_onebox(url)@found_oneboxes = true if onebox.present?diff --git a/lib/cooked_processor_mixin.rb b/lib/cooked_processor_mixin.rbindex 7fec00fa18d..a1d9958b3b1 100644--- a/lib/cooked_processor_mixin.rb+++ b/lib/cooked_processor_mixin.rb@@ -6,29 +6,12 @@ module CookedProcessorMixinMIN_LIGHTBOX_HEIGHT = 100def post_process_oneboxes- limit = SiteSetting.max_oneboxes_per_post - @doc.css("aside.onebox, a.inline-onebox").size- oneboxes = {}- inlineOneboxes = {}-- Oneboxer.apply(@doc, extra_paths: [".inline-onebox-loading"]) do |url, element|- is_onebox = element["class"] == Oneboxer::ONEBOX_CSS_CLASS- map = is_onebox ? oneboxes : inlineOneboxes- skip_onebox = limit <= 0 && !map[url]-- if skip_onebox- if is_onebox- element.remove_class("onebox")- else- remove_inline_onebox_loading_class(element)- end-- next- end-- limit -= 1- map[url] = true-- if is_onebox+ Oneboxer.apply(+ @doc,+ extra_paths: [".#{Oneboxer::INLINE_ONEBOX_LOADING_CSS_CLASS}"],+ limit: SiteSetting.max_oneboxes_per_post - @doc.css("aside.onebox, a.inline-onebox").size,+ ) do |url, element|+ if element["class"] == Oneboxer::ONEBOX_CSS_CLASSonebox =Oneboxer.onebox(url,@@ -362,7 +345,7 @@ module CookedProcessorMixinenddef remove_inline_onebox_loading_class(element)- element.remove_class("inline-onebox-loading")+ element.remove_class(Oneboxer::INLINE_ONEBOX_LOADING_CSS_CLASS)enddef dirty?diff --git a/lib/oneboxer.rb b/lib/oneboxer.rbindex 3fc782b50e4..35e51638871 100644--- a/lib/oneboxer.rb+++ b/lib/oneboxer.rb@@ -6,6 +6,7 @@ Dir["#{Rails.root.join("lib/onebox/engine/*_onebox.rb")}"].sort.each { |f| requimodule OneboxerONEBOX_CSS_CLASS = "onebox"+ INLINE_ONEBOX_LOADING_CSS_CLASS = "inline-onebox-loading"AUDIO_REGEX = /\A\.(mp3|og[ga]|opus|wav|m4[abpr]|aac|flac)\z/iVIDEO_REGEX = /\A\.(mov|mp4|webm|m4v|3gp|ogv|avi|mpeg|ogv)\z/i@@ -137,7 +138,7 @@ module Oneboxer# Parse URLs out of HTML, returning the document when finished.def self.each_onebox_link(doc, extra_paths: [])- onebox_links = doc.css("a.#{ONEBOX_CSS_CLASS}", *extra_paths)+ onebox_links = doc.css(["a.#{ONEBOX_CSS_CLASS}", *extra_paths].join(", "))if onebox_links.present?onebox_links.each { |link| yield(link["href"], link) if link["href"].present? }end@@ -185,12 +186,21 @@ module Oneboxervideo]- def self.apply(string_or_doc, extra_paths: nil)+ def self.apply(string_or_doc, extra_paths: nil, limit: nil)doc = string_or_docdoc = Loofah.html5_fragment(doc) if doc.is_a?(String)changed = false+ candidates = 0++ each_onebox_link(doc, extra_paths:) do |url, element|+ if limit && candidates >= limit+ element.remove_class("#{ONEBOX_CSS_CLASS} #{INLINE_ONEBOX_LOADING_CSS_CLASS}")+ changed = true+ next+ end++ candidates += 1- each_onebox_link(doc, extra_paths: extra_paths) do |url, element|onebox, _ = yield(url, element)next if onebox.blank?diff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex 49ebb6f542e..f9825bd9f34 100644--- a/spec/lib/cooked_post_processor_spec.rb+++ b/spec/lib/cooked_post_processor_spec.rb@@ -95,7 +95,7 @@ RSpec.describe CookedPostProcessor doclass: "inline-onebox",},text: title,- count: 2,+ count: 1,)expect(cpp.html).to have_tag("aside.onebox a", text: title, count: 1)@@ -122,6 +122,92 @@ RSpec.describe CookedPostProcessor doendend+ describe "when post has more oneboxes than max_oneboxes_per_post" do+ let(:inline_url) { "https://example.com/inline" }+ let(:first_url) { "https://example.com/first" }+ let(:second_url) { "https://example.com/second" }++ before do+ SiteSetting.max_oneboxes_per_post = 1+ SiteSetting.enable_inline_onebox_on_all_domains = true+ Oneboxer.stubs(:cached_onebox).returns(nil)+ Oneboxer.stubs(:onebox).returns(nil)+ InlineOneboxer.stubs(:lookup).returns(nil)+ InlineOneboxer.stubs(:lookup).with(inline_url, anything).returns(title: "Inline title")+ end++ def onebox_html(url)+ "<aside class='onebox' data-onebox-src='#{url}'><article>#{url}</article></aside>"+ end++ def processed_html(post)+ cpp = CookedPostProcessor.new(post, disable_dominant_color: true)+ cpp.post_process+ cpp.html+ end++ it "gives the slot to the first onebox even when a later one is cached" do+ Oneboxer.stubs(:cached_onebox).with(first_url).returns(onebox_html(first_url))+ post = Fabricate(:post, raw: "See #{inline_url} here\n\n#{first_url}")++ html = processed_html(post)++ expect(html).to have_tag("a.inline-onebox", text: "Inline title")+ expect(html).not_to have_tag("aside.onebox")+ expect(html).not_to have_tag("a.onebox")+ end++ it "counts a cached embed that is not an aside" do+ Oneboxer.stubs(:cached_onebox).with(first_url).returns("<div class='video-onebox'></div>")+ Oneboxer.stubs(:onebox).with(second_url, anything).returns(onebox_html(second_url))+ post = Fabricate(:post, raw: "#{first_url}\n\n#{second_url}")++ html = processed_html(post)++ expect(html).to have_tag("div.video-onebox")+ expect(html).not_to have_tag("aside.onebox")+ end++ it "gives a repeated onebox its own slot" do+ Oneboxer.stubs(:cached_onebox).with(first_url).returns(onebox_html(first_url))+ post = Fabricate(:post, raw: "#{first_url}\n\n#{first_url}\n\n#{second_url}")++ html = processed_html(post)++ expect(html).to have_tag("aside.onebox", count: 1)+ expect(html).to have_tag("a", without: { class: "onebox" }, text: first_url)+ expect(html).to have_tag("a", without: { class: "onebox" }, text: second_url)+ end++ it "counts oneboxes already rendered in raw HTML posts" do+ Oneboxer.stubs(:onebox).with(second_url, anything).returns(onebox_html(second_url))+ post = Fabricate(:post, cook_method: Post.cook_methods[:raw_html], raw: <<~RAW)+ #{onebox_html(first_url)}+ <p><a class="onebox" href="#{second_url}">#{second_url}</a></p>+ RAW++ html = processed_html(post)++ expect(html).to have_tag("aside.onebox", count: 1)+ expect(html).to have_tag("a", without: { class: "onebox" }, text: second_url)+ end++ it "limits oneboxes written as raw HTML" do+ Oneboxer.stubs(:cached_onebox).with(first_url).returns(onebox_html(first_url))+ Oneboxer.stubs(:cached_onebox).with(second_url).returns(onebox_html(second_url))+ post = Fabricate(:post, raw: <<~RAW)+ <a class="onebox" href="#{first_url}">#{first_url}</a>++ <a class="onebox" href="#{second_url}">#{second_url}</a>+ RAW++ html = processed_html(post)++ expect(html).to have_tag("aside.onebox", count: 1)+ expect(html).to have_tag("a", without: { class: "onebox" }, text: second_url)+ end+ end+describe "when post contains inline oneboxes" dobefore { SiteSetting.enable_inline_onebox_on_all_domains = true }diff --git a/spec/lib/oneboxer_spec.rb b/spec/lib/oneboxer_spec.rbindex 2cd43c6e014..768dcc6b6ae 100644--- a/spec/lib/oneboxer_spec.rb+++ b/spec/lib/oneboxer_spec.rb@@ -1310,6 +1310,50 @@ RSpec.describe Oneboxer do<div><svg><path></path></svg></div>HTMLend++ it "gives every occurrence of a URL its own slot" do+ url = "https://example.com"+ html = "<p><a href='#{url}' class='onebox'>#{url}</a></p>" * 5++ cooked = Oneboxer.apply(html, limit: 2) { "<div>onebox</div>" }++ expect(cooked.to_html).to have_tag("div", text: "onebox", count: 2)+ expect(cooked.to_html).to have_tag("a", without: { class: "onebox" }, count: 3)+ end++ it "claims slots in document order" do+ inline_url = "https://example.com/inline"+ onebox_url = "https://example.com/onebox"+ html = <<~HTML+ <div>+ <a href="#{inline_url}" class="#{Oneboxer::INLINE_ONEBOX_LOADING_CSS_CLASS}">#{inline_url}</a>+ <a href="#{onebox_url}" class="onebox">#{onebox_url}</a>+ </div>+ HTML++ cooked =+ Oneboxer.apply(+ html,+ extra_paths: ".#{Oneboxer::INLINE_ONEBOX_LOADING_CSS_CLASS}",+ limit: 1,+ ) { nil }++ expect(cooked.to_html).to have_tag(+ "a.#{Oneboxer::INLINE_ONEBOX_LOADING_CSS_CLASS}",+ with: {+ href: inline_url,+ },+ )+ expect(cooked.to_html).to have_tag(+ "a",+ with: {+ href: onebox_url,+ },+ without: {+ class: "onebox",+ },+ )+ endenddescribe "#force_get_hosts" dodiff --git a/spec/models/post_analyzer_spec.rb b/spec/models/post_analyzer_spec.rbindex f5d89270825..208fd32cdee 100644--- a/spec/models/post_analyzer_spec.rb+++ b/spec/models/post_analyzer_spec.rb@@ -66,7 +66,7 @@ RSpec.describe PostAnalyzer doit "respects SiteSetting.max_oneboxes_per_post" doSiteSetting.max_oneboxes_per_post = 2- Oneboxer.expects(:cached_onebox).with(url).returns("something").twice+ Oneboxer.stubs(:cached_onebox).with(url).returns("something")cooked = post_analyzer.cook(<<~RAW)#{url}@@ -79,9 +79,21 @@ RSpec.describe PostAnalyzer doexpect(cooked).to match_html(<<~HTML)<p>something</p><p>something</p>- <p><a href="#{url}" class="onebox" target="_blank" rel="noopener nofollow ugc">#{url}</a></p>+ <p><a href="#{url}" rel="noopener nofollow ugc">#{url}</a></p>HTMLend++ it "only renders max_oneboxes_per_post oneboxes for repeats of a cached URL" do+ SiteSetting.max_oneboxes_per_post = 2+ Oneboxer.stubs(:cached_onebox).with(url).returns("<div>something</div>")++ raw = Array.new(3, "<a class=\"onebox\" href=\"#{url}\">#{url}</a>").join("\n\n")++ cooked = post_analyzer.cook(raw)++ expect(cooked).to have_tag("div", text: "something", count: 2)+ expect(cooked).to have_tag("a", with: { href: url }, count: 1)+ endenddescribe "links" do