From 7c998caae3f6a027118c5c627a625a624b036582 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 31 Jan 2024 15:06:43 +0300 Subject: embed.js: process all links, stop on first embed --- vnext/src/utils/embed.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'vnext') diff --git a/vnext/src/utils/embed.js b/vnext/src/utils/embed.js index 11434938..79674e28 100644 --- a/vnext/src/utils/embed.js +++ b/vnext/src/utils/embed.js @@ -1,8 +1,8 @@ function htmlEscape(html) { return html.replace(/&/g, '&') - .replace(/"/g, '"') - .replace(//g, '>') + .replace(/"/g, '"') + .replace(//g, '>') } function insertAfter(newNode, referenceNode) { @@ -352,15 +352,17 @@ function embedLink(aNode, linkTypes, container, afterNode = false) { function embedLinks(aNodes, container) { let anyEmbed = false let embeddableLinkTypes = getEmbeddableLinkTypes() - Array.from(aNodes).forEach(aNode => { + Array.from(aNodes).every(aNode => { let isEmbedded = embedLink(aNode, embeddableLinkTypes, container) + // stop on first embedded link anyEmbed = anyEmbed || isEmbedded + return !(anyEmbed) }) return anyEmbed } /** - * Embed all the links inside element "x" that match to "allLinksSelector". + * Embed first link from supplied links inside element "x" that match to "allLinksSelector". * All the embedded media is placed inside "div.embedContainer". * "div.embedContainer" is inserted before an element matched by "beforeNodeSelector" * if not present. Existing container is used otherwise. @@ -369,7 +371,7 @@ function embedLinks(aNodes, container) { * @param {string} allLinksSelector */ export function embedLinksToX(x, beforeNodeSelector, allLinksSelector) { - let allLinks = [x.querySelector(allLinksSelector)] + let allLinks = x.querySelectorAll(allLinksSelector) let existingContainer = x.querySelector('div.embedContainer') if (existingContainer) { -- cgit v1.2.3