From d32fd6daa7e2fadd2c3b6e34fb4b9ee4702d85d9 Mon Sep 17 00:00:00 2001 From: Harshit-7373 Date: Wed, 5 Mar 2025 22:58:07 +0530 Subject: [PATCH 1/5] Removed code duplication between server previewGeneration.js and client EmbedFrame.jsx. #2805 --- client/modules/Preview/EmbedFrame.jsx | 19 ++++++------------- server/utils/previewGeneration.js | 17 +++++------------ shared/resolveUtils.js | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 shared/resolveUtils.js diff --git a/client/modules/Preview/EmbedFrame.jsx b/client/modules/Preview/EmbedFrame.jsx index 4b2ad60d9b..2d983f2d8a 100644 --- a/client/modules/Preview/EmbedFrame.jsx +++ b/client/modules/Preview/EmbedFrame.jsx @@ -18,6 +18,7 @@ import { import { getAllScriptOffsets } from '../../utils/consoleUtils'; import { registerFrame } from '../../utils/dispatcher'; import { createBlobUrl } from './filesReducer'; +import { resolvePathsForElementsWithAttribute } from '../../../shared/resolveUtils'; let objectUrls = {}; let objectPaths = {}; @@ -34,19 +35,6 @@ const Frame = styled.iframe` `} `; -function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) { - const elements = sketchDoc.querySelectorAll(`[${attr}]`); - const elementsArray = Array.prototype.slice.call(elements); - elementsArray.forEach((element) => { - if (element.getAttribute(attr).match(MEDIA_FILE_REGEX)) { - const resolvedFile = resolvePathToFile(element.getAttribute(attr), files); - if (resolvedFile && resolvedFile.url) { - element.setAttribute(attr, resolvedFile.url); - } - } - }); -} - function resolveCSSLinksInString(content, files) { let newContent = content; let cssFileStrings = content.match(STRING_REGEX); @@ -218,7 +206,12 @@ function injectLocalFiles(files, htmlFile, options) { base.href = `${window.origin}${basePath}${basePath.length > 1 && '/'}`; sketchDoc.head.appendChild(base); + // Resolve paths for elements with the 'src' attribute + // This updates the 'src' attribute of elements (e.g., ,