diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js index 7d89caa0701..7af84831e70 100644 --- a/app/assets/javascripts/discourse/app/components/composer-editor.js +++ b/app/assets/javascripts/discourse/app/components/composer-editor.js @@ -43,10 +43,6 @@ import { cacheShortUploadUrl, resolveAllShortUrls } from "pretty-text/upload-short-url"; -import { - INLINE_ONEBOX_LOADING_CSS_CLASS, - INLINE_ONEBOX_CSS_CLASS -} from "pretty-text/context/inline-onebox-css-classes"; import ENV from "discourse-common/config/environment"; const REBUILD_SCROLL_MAP_EVENTS = ["composer:resized", "composer:typed-reply"]; @@ -968,32 +964,29 @@ export default Component.extend({ // Inline Oneboxes = `a.inline-onebox-loading` -> `a.inline-onebox` let loadedOneboxes = $preview.find( - `aside.onebox, a.${LOADING_ONEBOX_CSS_CLASS}, a.${INLINE_ONEBOX_CSS_CLASS}` + `aside.onebox, a.${LOADING_ONEBOX_CSS_CLASS}, a.inline-onebox-loading` ).length; - $preview - .find(`a.onebox, a.${INLINE_ONEBOX_LOADING_CSS_CLASS}`) - .each((_, link) => { - const $link = $(link); - const text = $link.text(); - const isInline = - $link.attr("class") === INLINE_ONEBOX_LOADING_CSS_CLASS; - const m = isInline ? inlineOneboxes : oneboxes; + $preview.find(`a.onebox, a.inline-onebox-loading`).each((_, link) => { + const $link = $(link); + const text = $link.text(); + const isInline = $link.attr("class") === "inline-onebox-loading"; + const m = isInline ? inlineOneboxes : oneboxes; - if (loadedOneboxes < this.siteSettings.max_oneboxes_per_post) { - if (m[text] === undefined) { - m[text] = []; - loadedOneboxes++; - } - m[text].push(link); - } else { - if (m[text] !== undefined) { - m[text].push(link); - } else if (isInline) { - $link.removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS); - } + if (loadedOneboxes < this.siteSettings.max_oneboxes_per_post) { + if (m[text] === undefined) { + m[text] = []; + loadedOneboxes++; } - }); + m[text].push(link); + } else { + if (m[text] !== undefined) { + m[text].push(link); + } else if (isInline) { + $link.removeClass("inline-onebox-loading"); + } + } + }); if (Object.keys(oneboxes).length > 0) { this._loadOneboxes(oneboxes); diff --git a/app/assets/javascripts/pretty-text-bundle.js b/app/assets/javascripts/pretty-text-bundle.js index a2adade3823..62dd6c1439c 100644 --- a/app/assets/javascripts/pretty-text-bundle.js +++ b/app/assets/javascripts/pretty-text-bundle.js @@ -11,6 +11,5 @@ //= require ./pretty-text/addon/sanitizer //= require ./pretty-text/addon/oneboxer //= require ./pretty-text/addon/oneboxer-cache -//= require ./pretty-text/addon/context/inline-onebox-css-classes //= require ./pretty-text/addon/inline-oneboxer //= require ./pretty-text/addon/upload-short-url diff --git a/app/assets/javascripts/pretty-text/addon/context/inline-onebox-css-classes.js.erb b/app/assets/javascripts/pretty-text/addon/context/inline-onebox-css-classes.js.erb deleted file mode 100644 index a252fb28803..00000000000 --- a/app/assets/javascripts/pretty-text/addon/context/inline-onebox-css-classes.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -export const INLINE_ONEBOX_LOADING_CSS_CLASS = - "<%= CookedPostProcessor::INLINE_ONEBOX_LOADING_CSS_CLASS %>"; - -export const INLINE_ONEBOX_CSS_CLASS = - "<%= CookedPostProcessor::INLINE_ONEBOX_CSS_CLASS %>"; diff --git a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js index 7358bdaa062..94bd5dd60f3 100644 --- a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js +++ b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js @@ -1,9 +1,5 @@ import { lookupCache } from "pretty-text/oneboxer-cache"; import { cachedInlineOnebox } from "pretty-text/inline-oneboxer"; -import { - INLINE_ONEBOX_LOADING_CSS_CLASS, - INLINE_ONEBOX_CSS_CLASS -} from "pretty-text/context/inline-onebox-css-classes"; const ONEBOX = 1; const INLINE = 2; @@ -103,9 +99,9 @@ function applyOnebox(state, silent) { if (onebox && onebox.title) { text.content = onebox.title; - attrs.push(["class", INLINE_ONEBOX_CSS_CLASS]); + attrs.push(["class", "inline-onebox"]); } else if (!onebox) { - attrs.push(["class", INLINE_ONEBOX_LOADING_CSS_CLASS]); + attrs.push(["class", "inline-onebox-loading"]); } } } diff --git a/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js b/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js index 6645d1dac7e..a4587eda745 100644 --- a/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js +++ b/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js @@ -1,8 +1,3 @@ -import { - INLINE_ONEBOX_LOADING_CSS_CLASS, - INLINE_ONEBOX_CSS_CLASS -} from "pretty-text/context/inline-onebox-css-classes"; - const _cache = {}; export function applyInlineOneboxes(inline, ajax, opts) { @@ -27,8 +22,8 @@ export function applyInlineOneboxes(inline, ajax, opts) { links.forEach(link => { $(link) .text(onebox.title) - .addClass(INLINE_ONEBOX_CSS_CLASS) - .removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS); + .addClass("inline-onebox") + .removeClass("inline-onebox-loading"); }); } }); diff --git a/app/assets/javascripts/pretty-text/addon/white-lister.js b/app/assets/javascripts/pretty-text/addon/white-lister.js index 8a3a1140f1e..88eb29085b4 100644 --- a/app/assets/javascripts/pretty-text/addon/white-lister.js +++ b/app/assets/javascripts/pretty-text/addon/white-lister.js @@ -1,8 +1,3 @@ -import { - INLINE_ONEBOX_LOADING_CSS_CLASS, - INLINE_ONEBOX_CSS_CLASS -} from "pretty-text/context/inline-onebox-css-classes"; - // to match: // abcd // abcd[test] @@ -121,8 +116,8 @@ export const DEFAULT_LIST = [ "a.mention", "a.mention-group", "a.onebox", - `a.${INLINE_ONEBOX_CSS_CLASS}`, - `a.${INLINE_ONEBOX_LOADING_CSS_CLASS}`, + `a.inline-onebox`, + `a.inline-onebox-loading`, "a[data-bbcode]", "a[name]", "a[rel=nofollow]", diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index d2e8f4d2d50..d6fb05a3084 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -4,8 +4,6 @@ # For example, inserting the onebox content, or image sizes/thumbnails. class CookedPostProcessor - INLINE_ONEBOX_LOADING_CSS_CLASS = "inline-onebox-loading" - INLINE_ONEBOX_CSS_CLASS = "inline-onebox" LIGHTBOX_WRAPPER_CSS_CLASS = "lightbox-wrapper" LOADING_SIZE = 10 LOADING_COLORS = 32 @@ -518,7 +516,7 @@ class CookedPostProcessor oneboxes = {} inlineOneboxes = {} - Oneboxer.apply(@doc, extra_paths: [".#{INLINE_ONEBOX_LOADING_CSS_CLASS}"]) do |url, element| + 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] @@ -724,14 +722,14 @@ class CookedPostProcessor if title = inline_onebox&.dig(:title) element.children = CGI.escapeHTML(title) - element.add_class(INLINE_ONEBOX_CSS_CLASS) + element.add_class("inline-onebox") end remove_inline_onebox_loading_class(element) end def remove_inline_onebox_loading_class(element) - element.remove_class(INLINE_ONEBOX_LOADING_CSS_CLASS) + element.remove_class("inline-onebox-loading") end def is_svg?(img) diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index a429d64bee7..b62350d2be5 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -97,10 +97,7 @@ describe CookedPostProcessor do cpp.post_process expect(cpp.html).to have_tag('a', - with: { - href: url, - class: described_class::INLINE_ONEBOX_CSS_CLASS - }, + with: { href: url, class: "inline-onebox" }, text: title, count: 2 ) @@ -113,9 +110,7 @@ describe CookedPostProcessor do ) expect(cpp.html).to have_tag('a', - without: { - class: described_class::INLINE_ONEBOX_LOADING_CSS_CLASS - }, + without: { class: "inline-onebox-loading" }, text: not_oneboxed_url, count: 1 ) @@ -131,10 +126,6 @@ describe CookedPostProcessor do end describe 'when post contains inline oneboxes' do - let(:loading_css_class) do - described_class::INLINE_ONEBOX_LOADING_CSS_CLASS - end - before do SiteSetting.enable_inline_onebox_on_all_domains = true end @@ -148,12 +139,8 @@ describe CookedPostProcessor do cpp.post_process expect(cpp.html).to have_tag('a', - with: { - href: UrlHelper.cook_url(url) - }, - without: { - class: loading_css_class - }, + with: { href: UrlHelper.cook_url(url) }, + without: { class: "inline-onebox-loading" }, text: topic.title, count: 1 ) @@ -163,12 +150,8 @@ describe CookedPostProcessor do cpp.post_process expect(cpp.html).to have_tag('a', - with: { - href: UrlHelper.cook_url(url) - }, - without: { - class: loading_css_class - }, + with: { href: UrlHelper.cook_url(url) }, + without: { class: "inline-onebox-loading" }, text: topic.title, count: 1 ) @@ -235,33 +218,21 @@ describe CookedPostProcessor do html = cpp.html expect(html).to_not have_tag('a', - with: { - href: url_no_path - }, - without: { - class: loading_css_class - }, + with: { href: url_no_path }, + without: { class: "inline-onebox-loading" }, text: title ) expect(html).to have_tag('a', - with: { - href: url_with_path - }, - without: { - class: loading_css_class - }, + with: { href: url_with_path }, + without: { class: "inline-onebox-loading" }, text: title, count: 2 ) expect(html).to have_tag('a', - with: { - href: url_with_query_param - }, - without: { - class: loading_css_class - }, + with: { href: url_with_query_param }, + without: { class: "inline-onebox-loading" }, text: title, count: 1 ) diff --git a/test/javascripts/acceptance/composer-onebox-test.js b/test/javascripts/acceptance/composer-onebox-test.js index 6e047238b03..ab2484d1e9e 100644 --- a/test/javascripts/acceptance/composer-onebox-test.js +++ b/test/javascripts/acceptance/composer-onebox-test.js @@ -1,5 +1,4 @@ import { acceptance } from "helpers/qunit-helpers"; -import { INLINE_ONEBOX_CSS_CLASS } from "pretty-text/context/inline-onebox-css-classes"; acceptance("Composer - Onebox", { loggedIn: true, @@ -36,10 +35,10 @@ http://www.example.com/has-title.html .trim(), `
-This is another test This is a great title
http://www.example.com/no-title.html
This is another test http://www.example.com/no-title.html
-This is another test This is a great title
Youtube: ${link}
`, + `Youtube: ${link}
`, "allows links to contain query params" ); @@ -222,7 +221,7 @@ QUnit.test("Links", assert => { assert.cooked( "Derpy: http://derp.com?__test=1", - `Derpy: http://derp.com?__test=1
`, + `Derpy: http://derp.com?__test=1
`, "works with double underscores in urls" ); @@ -252,7 +251,7 @@ QUnit.test("Links", assert => { assert.cooked( "Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)", - `Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)
`, + `Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)
`, "autolinks a URL with parentheses (like Wikipedia)" ); @@ -264,7 +263,7 @@ QUnit.test("Links", assert => { assert.cooked( "1. View @eviltrout's profile here: http://meta.discourse.org/u/eviltrout/activityhttp://discourse.org and ' + - `http://discourse.org/another_url and ` + - `http://www.imdb.com/name/nm2225369
`, + `http://discourse.org/another_url and ` + + `http://www.imdb.com/name/nm2225369`, "allows multiple links on one line" );