A platform for community discussion. Free, open, simple.
FEATURE: Match media and oneboxes when highlighting quoted text
Régis Hanolc099d0d77796
diff --git a/app/assets/stylesheets/common/base/topic-post.scss b/app/assets/stylesheets/common/base/topic-post.scssindex 66d42614505..e9f1b876a7c 100644--- a/app/assets/stylesheets/common/base/topic-post.scss+++ b/app/assets/stylesheets/common/base/topic-post.scss@@ -1983,6 +1983,10 @@ span.highlighted {.quote-highlight {background-color: var(--highlight-bg);scroll-margin-top: 3lh;++ &:not(span) {+ outline: 0.25em solid var(--highlight);+ }}.pausable-animated-image {diff --git a/frontend/discourse/app/lib/quote.js b/frontend/discourse/app/lib/quote.jsindex ee9e00b7d4b..e40cefb4f8b 100644--- a/frontend/discourse/app/lib/quote.js+++ b/frontend/discourse/app/lib/quote.js@@ -6,7 +6,12 @@ import { stripQuotationMarks } from "discourse-markdown-it/features/bbcode-blockconst WHITESPACE = /\s/;const WORD_CHARACTER = /[\p{L}\p{N}_]/u;const MIN_PARTIAL_MATCH_LENGTH = 20;+const MEDIA_START = "\uE000";+const MEDIA_END = "\uE001";+const SKIPPED = "img.emoji, .lightbox-wrapper .meta, aside.quote > .title";const HIGHLIGHT_CLASS = "quote-highlight";+const UPLOAD_SHA1 = /[0-9a-f]{40}/;+const URL_ORIGIN = /^([a-z]+:)?\/\/[^/]+/i;export const QUOTE_HIGHLIGHT_ID = "quote-highlight-start";@@ -102,6 +107,33 @@ function findLongestPrefix(haystack, needle, isBoundary) {return best;}+function mediaSource(element) {+ if (element.matches("img")) {+ const src = element.getAttribute("src") ?? "";+ return (+ src.match(UPLOAD_SHA1)?.[0] ??+ element.dataset.base62Sha1 ??+ src.replace(URL_ORIGIN, "")+ );+ }++ if (element.matches("aside.onebox")) {+ return element.dataset.oneboxSrc;+ }++ if (+ element.matches("a.inline-onebox, a.inline-onebox-loading") ||+ (element.matches("a") &&+ element.textContent === element.getAttribute("href"))+ ) {+ return element.getAttribute("href");+ }++ if (element.matches("[data-video-src]")) {+ return element.dataset.videoSrc;+ }+}+function collectText(node, visit) {for (const child of node.childNodes) {if (child.nodeType === Node.TEXT_NODE) {@@ -109,6 +141,16 @@ function collectText(node, visit) {continue;}+ if (child.nodeType !== Node.ELEMENT_NODE || child.matches(SKIPPED)) {+ continue;+ }++ const source = mediaSource(child);+ if (source) {+ visit(child, `${MEDIA_START}${source}${MEDIA_END}`);+ continue;+ }+collectText(child, visit);}}@@ -142,6 +184,15 @@ function indexText(node) {}function wrapMatch(target, from, to) {+ if (target.nodeType === Node.ELEMENT_NODE) {+ const visible =+ getComputedStyle(target).display === "none"+ ? target.parentElement+ : target;+ visible.classList.add(HIGHLIGHT_CLASS);+ return visible;+ }+const parent = target.parentElement;if (parent.classList.contains(HIGHLIGHT_CLASS) &&diff --git a/frontend/discourse/tests/unit/lib/quote-test.js b/frontend/discourse/tests/unit/lib/quote-test.jsindex 14b6acf54fc..19533d31907 100644--- a/frontend/discourse/tests/unit/lib/quote-test.js+++ b/frontend/discourse/tests/unit/lib/quote-test.js@@ -1,6 +1,7 @@import { setupTest } from "ember-qunit";import { module, test } from "qunit";-import { highlightQuote } from "discourse/lib/quote";+import { highlightQuote, quotableText } from "discourse/lib/quote";+import { fixture } from "discourse/tests/helpers/qunit-helpers";module("Unit | Utility | quote", function (hooks) {setupTest(hooks);@@ -28,6 +29,90 @@ module("Unit | Utility | quote", function (hooks) {);});+ test("highlightQuote matches media and oneboxes by their source", function (assert) {+ const quote = document.createElement("div");+ quote.innerHTML =+ "<p>Look</p><p><img src='/uploads/a.png' data-base62-sha1='abc'><img src='/uploads/b.png'></p><div class='video-placeholder-container' data-video-src='/uploads/c.mp4'></div><p><a href='https://example.com'>https://example.com</a></p><p><a class='inline-onebox' href='https://example.com/page'>Page</a></p><p>done</p>";+ const elem = document.createElement("div");+ elem.innerHTML =+ "<p>Look</p><div class='lightbox-wrapper'><a class='lightbox'><img src='/optimized/a.png' data-base62-sha1='abc'><div class='meta'>a.png 10×10</div></a></div><img src='//cdn/uploads/b.png'><div class='video-container' data-video-src='/uploads/c.mp4'><video><source src='/uploads/c.mp4'><a href='/uploads/c.mp4'>/uploads/c.mp4</a></video></div><aside class='onebox' data-onebox-src='https://example.com'>Example Domain</aside><aside class='onebox' data-onebox-src='https://example.com/page'>Page</aside><p>done</p>";+ fixture().append(elem);++ const highlights = highlightQuote(elem, quotableText(quote));++ assert.deepEqual(+ highlights.map((highlight) => highlight.tagName),+ ["SPAN", "IMG", "IMG", "DIV", "ASIDE", "ASIDE", "SPAN"],+ "highlights text, images, videos and oneboxes"+ );+ });++ test("highlightQuote matches an upload across its sizes", function (assert) {+ const sha1 = "5e820b8990e256f3625c184f514507b597b52d74";+ const quote = document.createElement("div");+ quote.innerHTML = `<p><img src='/uploads/default/original/1X/${sha1}.jpeg' data-base62-sha1='du3v9AE'></p><p><img src='http://exa mple.com/a.png'></p>`;+ const elem = document.createElement("div");+ elem.innerHTML = `<p><img src='//cdn/optimized/1X/${sha1}_2_690x388.jpeg'></p><p><img src='http://exa mple.com/a.png'></p>`;+ fixture().append(elem);++ const highlights = highlightQuote(elem, quotableText(quote));++ assert.deepEqual(+ highlights.map((highlight) => highlight.tagName),+ ["IMG", "IMG"],+ "matches the optimized copy and tolerates a malformed source"+ );+ });++ test("highlightQuote highlights media inside closed details", function (assert) {+ const elem = document.createElement("div");+ elem.innerHTML =+ "<details><summary>More</summary><p><img src='/uploads/a.png'></p></details>";+ fixture().append(elem);++ const [highlight] = highlightQuote(+ elem,+ quotableText(elem.querySelector("p"))+ );++ assert.strictEqual(highlight.tagName, "IMG", "highlights the image itself");+ });++ test("highlightQuote highlights the container of hidden media", function (assert) {+ const elem = document.createElement("div");+ elem.innerHTML =+ "<p class='player'><img src='/thumb.jpg' style='display: none'><iframe></iframe></p>";+ fixture().append(elem);++ const [highlight] = highlightQuote(elem, quotableText(elem));++ assert+ .dom(highlight)+ .hasClass("player", "highlights the visible container");+ });++ test("highlightQuote ignores emoji", function (assert) {+ const elem = document.createElement("div");+ elem.innerHTML =+ "<p>Read the docs <img src='/images/emoji/link.png' class='emoji'> before asking</p>";++ const highlights = highlightQuote(elem, "Read the docs before asking");++ assert.deepEqual(+ highlights.map((highlight) => highlight.textContent),+ ["Read the docs", "before asking"],+ "matches across the emoji"+ );+ });++ test("quotableText ignores quote titles", function (assert) {+ const quote = document.createElement("div");+ quote.innerHTML =+ "<aside class='quote'><div class='title'>Someone said:</div><blockquote>Hi</blockquote></aside>";++ assert.strictEqual(quotableText(quote), "Hi", "skips the title");+ });+test("highlightQuote prefers whole words", function (assert) {const elem = document.createElement("div");elem.innerHTML = "<p>Start with the artist, then the art section</p>";