Skip to content

Commit 22787a1

Browse files
authored
Merge branch 'develop' into Branch_Kamakshi
2 parents 04407be + 8bbb2ad commit 22787a1

File tree

4 files changed

+103
-26
lines changed

4 files changed

+103
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// resolveUtils.unit.test.jsx
2+
3+
import resolvePathsForElementsWithAttribute from '../../../../../server/utils/resolveUtils';
4+
import { resolvePathToFile } from '../../../../../server/utils/filePath';
5+
6+
// Mock the dependencies
7+
jest.mock('../../../../../server/utils/filePath', () => ({
8+
resolvePathToFile: jest.fn()
9+
}));
10+
11+
jest.mock('../../../../../server/utils/fileUtils', () => ({
12+
MEDIA_FILE_REGEX: /\.(png|jpg|jpeg|gif|svg)$/i
13+
}));
14+
15+
describe('resolvePathsForElementsWithAttribute', () => {
16+
let mockSketchDoc;
17+
let mockFiles;
18+
19+
beforeEach(() => {
20+
jest.clearAllMocks();
21+
22+
// Create a mock DOM environment
23+
mockSketchDoc = document.implementation.createHTMLDocument();
24+
mockFiles = {
25+
'image.png': { url: 'https://example.com/image.png' },
26+
'missing.jpg': { url: null }
27+
};
28+
29+
resolvePathToFile.mockImplementation((fileName, files) => files[fileName]);
30+
});
31+
32+
it('should update the attribute when the file is resolved successfully', () => {
33+
const element = mockSketchDoc.createElement('img');
34+
element.setAttribute('src', 'image.png');
35+
mockSketchDoc.body.appendChild(element);
36+
37+
resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles);
38+
39+
expect(element.getAttribute('src')).toBe('https://example.com/image.png');
40+
});
41+
42+
it('should not update the attribute when the file resolution fails', () => {
43+
const element = mockSketchDoc.createElement('img');
44+
element.setAttribute('src', 'missing.jpg');
45+
mockSketchDoc.body.appendChild(element);
46+
47+
resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles);
48+
49+
expect(element.getAttribute('src')).toBe('missing.jpg');
50+
});
51+
52+
it('should not update the attribute when the value does not match MEDIA_FILE_REGEX', () => {
53+
const element = mockSketchDoc.createElement('img');
54+
element.setAttribute('src', 'document.pdf');
55+
mockSketchDoc.body.appendChild(element);
56+
57+
resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles);
58+
59+
expect(element.getAttribute('src')).toBe('document.pdf');
60+
});
61+
62+
it('should do nothing when no elements with the specified attribute are found', () => {
63+
resolvePathsForElementsWithAttribute('src', mockSketchDoc, mockFiles);
64+
65+
expect(mockSketchDoc.querySelectorAll('[src]').length).toBe(0);
66+
});
67+
});

client/modules/Preview/EmbedFrame.jsx

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import decomment from 'decomment';
88
import { resolvePathToFile } from '../../../server/utils/filePath';
99
import getConfig from '../../utils/getConfig';
1010
import {
11-
MEDIA_FILE_REGEX,
1211
MEDIA_FILE_QUOTED_REGEX,
1312
STRING_REGEX,
1413
PLAINTEXT_FILE_REGEX,
@@ -18,6 +17,7 @@ import {
1817
import { getAllScriptOffsets } from '../../utils/consoleUtils';
1918
import { registerFrame } from '../../utils/dispatcher';
2019
import { createBlobUrl } from './filesReducer';
20+
import resolvePathsForElementsWithAttribute from '../../../server/utils/resolveUtils';
2121

2222
let objectUrls = {};
2323
let objectPaths = {};
@@ -34,19 +34,6 @@ const Frame = styled.iframe`
3434
`}
3535
`;
3636

37-
function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) {
38-
const elements = sketchDoc.querySelectorAll(`[${attr}]`);
39-
const elementsArray = Array.prototype.slice.call(elements);
40-
elementsArray.forEach((element) => {
41-
if (element.getAttribute(attr).match(MEDIA_FILE_REGEX)) {
42-
const resolvedFile = resolvePathToFile(element.getAttribute(attr), files);
43-
if (resolvedFile && resolvedFile.url) {
44-
element.setAttribute(attr, resolvedFile.url);
45-
}
46-
}
47-
});
48-
}
49-
5037
function resolveCSSLinksInString(content, files) {
5138
let newContent = content;
5239
let cssFileStrings = content.match(STRING_REGEX);

server/utils/previewGeneration.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolvePathToFile } from '../utils/filePath';
2+
import { resolvePathsForElementsWithAttribute } from '../utils/resolveUtils';
23

34
import {
4-
MEDIA_FILE_REGEX,
55
STRING_REGEX,
66
PLAINTEXT_FILE_REGEX,
77
EXTERNAL_LINK_REGEX,
@@ -44,17 +44,8 @@ export function injectMediaUrls(filesToInject, allFiles, projectId) {
4444
});
4545
}
4646

47-
export function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) {
48-
const elements = sketchDoc.querySelectorAll(`[${attr}]`);
49-
const elementsArray = Array.prototype.slice.call(elements);
50-
elementsArray.forEach((element) => {
51-
if (element.getAttribute(attr).match(MEDIA_FILE_REGEX)) {
52-
const resolvedFile = resolvePathToFile(element.getAttribute(attr), files);
53-
if (resolvedFile && resolvedFile.url) {
54-
element.setAttribute(attr, resolvedFile.url);
55-
}
56-
}
57-
});
47+
export function resolveMediaElements(sketchDoc, files) {
48+
resolvePathsForElementsWithAttribute('src', sketchDoc, files);
5849
}
5950

6051
export function resolveScripts(sketchDoc, files, projectId) {

server/utils/resolveUtils.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { resolvePathToFile } from './filePath';
2+
import { MEDIA_FILE_REGEX } from './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+
}

0 commit comments

Comments
 (0)