Skip to content

Removed code duplication between 2 files. #3364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions client/modules/Preview/EmbedFrame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { resolvePathToFile } from '../../../server/utils/filePath';
import getConfig from '../../utils/getConfig';
import {
MEDIA_FILE_REGEX,
MEDIA_FILE_QUOTED_REGEX,
STRING_REGEX,
PLAINTEXT_FILE_REGEX,
Expand All @@ -18,6 +17,7 @@
import { getAllScriptOffsets } from '../../utils/consoleUtils';
import { registerFrame } from '../../utils/dispatcher';
import { createBlobUrl } from './filesReducer';
import resolvePathsForElementsWithAttribute from '../../../shared/resolveUtils';

Check failure on line 20 in client/modules/Preview/EmbedFrame.jsx

View workflow job for this annotation

GitHub Actions / Test and lint code base

Missing file extension for "../../../shared/resolveUtils"

let objectUrls = {};
let objectPaths = {};
Expand All @@ -34,19 +34,6 @@
`}
`;

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);
Expand Down Expand Up @@ -218,7 +205,12 @@
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., <img>, <script>) to their resolved URLs
resolvePathsForElementsWithAttribute('src', sketchDoc, resolvedFiles);

// Resolve paths for elements with the 'href' attribute
// This updates the 'href' attribute of elements (e.g., <a>, <link>) to their resolved URLs
resolvePathsForElementsWithAttribute('href', sketchDoc, resolvedFiles);
// should also include background, data, poster, but these are used way less often

Expand Down
18 changes: 5 additions & 13 deletions server/utils/previewGeneration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolvePathsForElementsWithAttribute } from '../../shared/resolveUtils';

Check failure on line 1 in server/utils/previewGeneration.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Missing file extension for "../../shared/resolveUtils"
import { resolvePathToFile } from '../utils/filePath';

import {
MEDIA_FILE_REGEX,
STRING_REGEX,
PLAINTEXT_FILE_REGEX,
EXTERNAL_LINK_REGEX,
Expand All @@ -13,8 +13,8 @@
let fileStrings = content.match(STRING_REGEX);
const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i;
fileStrings = fileStrings || [];
// if string does not begin with http or https
fileStrings.forEach((fileString) => {
// if string does not begin with http or https
if (fileString.match(fileStringRegex)) {
const filePath = fileString.substr(1, fileString.length - 2);
const resolvedFile = resolvePathToFile(filePath, files);
Expand Down Expand Up @@ -44,17 +44,9 @@
});
}

export 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);
}
}
});
// Wrapped the function in a proper call
export function resolveMediaElements(sketchDoc, files) {
resolvePathsForElementsWithAttribute('src', sketchDoc, files);
}

export function resolveScripts(sketchDoc, files, projectId) {
Expand Down
Loading