diff --git a/src/app/services/projectService.spec.ts b/src/app/services/projectService.spec.ts index 6dfbb48375d..45d301d326f 100644 --- a/src/app/services/projectService.spec.ts +++ b/src/app/services/projectService.spec.ts @@ -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( + 'image (1).png', + `image (1).png` + ); + }); + it('should inject an upper-case, single-quoted attribute with spaces around =', () => { + expectHtmlInjected( + "", + `` + ); + }); + it('should inject href, poster and source src with parentheses', () => { + expectHtmlInjected( + 'doc', + `doc` + + `` + ); + }); + it('should inject quoted CSS url() values with parentheses', () => { + expectHtmlInjected( + `
` + + '', + `
` + + `` + ); + }); + it('should inject an attribute that follows a newline', () => { + expectHtmlInjected( + '', + `` + ); + }); + it('should inject a src with CJK brackets', () => { + expectHtmlInjected( + '', + `` + ); + }); + it('should not change absolute URLs, data URIs or other attributes in HTML', () => { + const html = + 'a' + + ''; + expectHtmlInjected(html, html); + }); + it('should not change prose or an unquoted url() with parentheses', () => { + const html = '

(see) a (1).png

'; + 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 = 'b'; + const once = service.replaceAssetPaths(html); + expect(once).toEqual( + '' + + 'b' + ); + 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); diff --git a/src/assets/wise5/services/projectService.ts b/src/assets/wise5/services/projectService.ts index 55aa647da4f..ecd9f47ad5d 100644 --- a/src/assets/wise5/services/projectService.ts +++ b/src/assets/wise5/services/projectService.ts @@ -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@%&;()._/ \\-' + @@ -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 = []; @@ -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; }