Skip to content
Open
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
67 changes: 67 additions & 0 deletions src/app/services/projectService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,76 @@ function injectAssetPaths() {
const content = { src: '/curriculum/1/assets/a (1).png' };
expect(service.injectAssetPaths(content).src).toEqual('/curriculum/1/assets/a (1).png');
});
it('should inject a src with parentheses as multiple choice authoring writes it', () => {
expectHtmlInjected(
'<img src="image (1).png" alt="image (1).png" />',
`<img src="${projectBaseURL}assets/image (1).png" alt="image (1).png" />`
);
});
it('should inject an upper-case, single-quoted attribute with spaces around =', () => {
expectHtmlInjected(
"<IMG SRC = 'a (1).jpg'>",
`<IMG SRC = '${projectBaseURL}assets/a (1).jpg'>`
);
});
it('should inject href, poster and source src with parentheses', () => {
expectHtmlInjected(
'<a href="doc (2).pdf">doc</a><video poster="p (1).png">' +
'<source src="v (1).mp4" type="video/mp4"></video>',
`<a href="${projectBaseURL}assets/doc (2).pdf">doc</a>` +
`<video poster="${projectBaseURL}assets/p (1).png">` +
`<source src="${projectBaseURL}assets/v (1).mp4" type="video/mp4"></video>`
);
});
it('should inject quoted CSS url() values with parentheses', () => {
expectHtmlInjected(
`<div style="background-image: url('bg (1).jpg')"></div>` +
'<style>div { background: url("bg (2).jpg"); }</style>',
`<div style="background-image: url('${projectBaseURL}assets/bg (1).jpg')"></div>` +
`<style>div { background: url("${projectBaseURL}assets/bg (2).jpg"); }</style>`
);
});
it('should inject an attribute that follows a newline', () => {
expectHtmlInjected(
'<img\nsrc="a (1).png">',
`<img\nsrc="${projectBaseURL}assets/a (1).png">`
);
});
it('should inject a src with CJK brackets', () => {
expectHtmlInjected(
'<img src="細胞(核)【卵】.png">',
`<img src="${projectBaseURL}assets/細胞(核)【卵】.png">`
);
});
it('should not change absolute URLs, data URIs or other attributes in HTML', () => {
const html =
'<img src="/curriculum/1/assets/a (1).png"><a href="https://example.com/a (1).pdf">a</a>' +
'<img src="data:image/png;base64,AAAA"><img data-filename="a (1).png" data-src="a (1).png">';
expectHtmlInjected(html, html);
});
it('should not change prose or an unquoted url() with parentheses', () => {
const html = '<p>(see) a (1).png</p><div style="background: url(a (1).png)"></div>';
expectHtmlInjected(html, html);
});
it('should not inject twice with a root-relative base URL', () => {
(configService.getConfigParam as jasmine.Spy).and.returnValue('/curriculum/12345/');
const html = '<img src="a (1).png"><a href="b.pdf">b</a>';
const once = service.replaceAssetPaths(html);
expect(once).toEqual(
'<img src="/curriculum/12345/assets/a (1).png">' +
'<a href="/curriculum/12345/assets/b.pdf">b</a>'
);
expect(service.replaceAssetPaths(once)).toEqual(once);
expect(service.injectAssetPaths(service.injectAssetPaths({ html })).html).toEqual(once);
});
});
}

function expectHtmlInjected(html: string, expected: string): void {
expect(service.replaceAssetPaths(html)).toEqual(expected);
expect(service.injectAssetPaths({ html }).html).toEqual(expected);
}

function shouldRetrieveProjectWhenConfigProjectURLIsValid() {
it('should retrieve project when Config.projectURL is valid', () => {
spyOn(configService, 'getConfigParam').withArgs('projectURL').and.returnValue(projectURL);
Expand Down
18 changes: 16 additions & 2 deletions src/assets/wise5/services/projectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { ProjectLocale } from '../../../app/domain/projectLocale';
import { Language } from '../../../app/domain/language';
import { ThemeSettings } from '../common/ThemeSettings';

// Wider than the name class in replaceAssetPaths because nothing surrounds a standalone value for
// a match to run into. Sentence punctuation is excluded so prose ending in a file name is not
// Tested against a whole value: a standalone string, or a quoted attribute or url() value in
// replaceAssetPaths. Wider than the name class of the regex there because nothing around a whole
// value can be swallowed. Sentence punctuation is excluded so prose ending in a file name is not
// mistaken for one.
const RELATIVE_ASSET_FILE_NAME = new RegExp(
'^(?!/)[a-zA-Z0-9@%&;()._/ \\-' +
Expand All @@ -33,6 +34,12 @@ const RELATIVE_ASSET_FILE_NAME = new RegExp(
'i'
);

// A quoted URL attribute or CSS url() value. The matching closing quote bounds the value, so a
// name with parentheses can be tested whole without running into the next string. \\[nrt] covers
// whitespace that JSON.stringify has escaped.
const QUOTED_ASSET_REFERENCE =
/((?:\s|\\[nrt])(?:src|href|poster)\s*=\s*|url\(\s*)(\\?["'])(.*?)\2/gi;

@Injectable()
export class ProjectService {
achievements: any = [];
Expand Down Expand Up @@ -484,6 +491,13 @@ export class ProjectService {
return delimiter + contentBaseURL + 'assets/' + rest;
}
);
contentString = contentString.replace(
QUOTED_ASSET_REFERENCE,
(matchedString, lead: string, quote: string, value: string) =>
RELATIVE_ASSET_FILE_NAME.test(value)
? lead + quote + contentBaseURL + 'assets/' + value + quote
: matchedString
);
}
return contentString;
}
Expand Down
Loading