Skip to content

Commit c1774cf

Browse files
committed
update names, remove extra comments, add documentation
1 parent 2059b32 commit c1774cf

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

client/modules/IDE/components/Header/resolveUtils.unit.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// resolveUtils.unit.test.jsx
22

3-
import resolvePathsForElementsWithAttribute from '../../../../../shared_file/resolveUtils';
3+
import resolvePathsForElementsWithAttribute from '../../../../../common_utils/resolveUtils';
44
import { resolvePathToFile } from '../../../../../server/utils/filePath';
55

66
// Mock the dependencies

client/modules/Preview/EmbedFrame.jsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { getAllScriptOffsets } from '../../utils/consoleUtils';
1818
import { registerFrame } from '../../utils/dispatcher';
1919
import { createBlobUrl } from './filesReducer';
20-
import resolvePathsForElementsWithAttribute from '../../../shared_file/resolveUtils';
20+
import resolvePathsForElementsWithAttribute from '../../../common_utils/resolveUtils';
2121

2222
let objectUrls = {};
2323
let objectPaths = {};
@@ -205,12 +205,7 @@ function injectLocalFiles(files, htmlFile, options) {
205205
base.href = `${window.origin}${basePath}${basePath.length > 1 && '/'}`;
206206
sketchDoc.head.appendChild(base);
207207

208-
// Resolve paths for elements with the 'src' attribute
209-
// This updates the 'src' attribute of elements (e.g., <img>, <script>) to their resolved URLs
210208
resolvePathsForElementsWithAttribute('src', sketchDoc, resolvedFiles);
211-
212-
// Resolve paths for elements with the 'href' attribute
213-
// This updates the 'href' attribute of elements (e.g., <a>, <link>) to their resolved URLs
214209
resolvePathsForElementsWithAttribute('href', sketchDoc, resolvedFiles);
215210
// should also include background, data, poster, but these are used way less often
216211

common_utils/resolveUtils.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { resolvePathToFile } from '../server/utils/filePath';
2+
import { MEDIA_FILE_REGEX } from '../server/utils/fileUtils';
3+
4+
/**
5+
* Resolves paths for elements with a specific attribute.
6+
*
7+
* @param {string} attr - The attribute name to search for in elements, such as "src" or "href".
8+
* @param {Document} sketchDoc - The document to search for elements with the attribute.
9+
* @param {Array} files - The files to search for the resolved paths.
10+
*/
11+
12+
export default function resolvePathsForElementsWithAttribute(
13+
attr,
14+
sketchDoc,
15+
files
16+
) {
17+
const elements = sketchDoc.querySelectorAll(`[${attr}]`);
18+
19+
const elementsArray = Array.prototype.slice.call(elements);
20+
21+
elementsArray.forEach((element) => {
22+
const attrValue = element.getAttribute(attr);
23+
24+
if (MEDIA_FILE_REGEX.test(attrValue)) {
25+
const resolvedFile = resolvePathToFile(attrValue, files);
26+
27+
if (resolvedFile && resolvedFile.url) {
28+
element.setAttribute(attr, resolvedFile.url);
29+
}
30+
}
31+
});
32+
}

server/utils/previewGeneration.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { resolvePathToFile } from '../utils/filePath';
2-
import { resolvePathsForElementsWithAttribute } from '../../shared_file/resolveUtils';
2+
import { resolvePathsForElementsWithAttribute } from '../../common_utils/resolveUtils';
33

44
import {
55
STRING_REGEX,
@@ -44,7 +44,6 @@ export function injectMediaUrls(filesToInject, allFiles, projectId) {
4444
});
4545
}
4646

47-
// Wrapped the function in a proper call
4847
export function resolveMediaElements(sketchDoc, files) {
4948
resolvePathsForElementsWithAttribute('src', sketchDoc, files);
5049
}

shared_file/resolveUtils.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)