diff --git a/src/app/services/projectService.spec.ts b/src/app/services/projectService.spec.ts index ce46fc48886..6dfbb48375d 100644 --- a/src/app/services/projectService.spec.ts +++ b/src/app/services/projectService.spec.ts @@ -47,6 +47,7 @@ describe('ProjectService', () => { shouldReplaceAssetPathsInNonHtmlComponentContent(); shouldReplaceAssetPathsInHtmlComponentContent(); shouldNotReplaceAssetPathsInHtmlComponentContent(); + injectAssetPaths(); shouldRetrieveProjectWhenConfigProjectURLIsValid(); shouldGetDefaultThemePath(); shouldReturnTheStartNodeOfTheProject(); @@ -150,6 +151,55 @@ function shouldNotReplaceAssetPathsInHtmlComponentContent() { }); } +function injectAssetPaths() { + describe('injectAssetPaths()', () => { + beforeEach(() => { + createNormalSpy(); + }); + it('should inject a file name with parentheses followed by another file name', () => { + expect(service.injectAssetPaths(['image (1).png', 'heart.png'])).toEqual([ + `${projectBaseURL}assets/image (1).png`, + `${projectBaseURL}assets/heart.png` + ]); + }); + it('should not break the JSON when a file name it cannot match precedes one it can', () => { + const content = { stamps: { Stamps: ['image#1.png', 'b.png'] } }; + expect(service.injectAssetPaths(content).stamps.Stamps).toEqual([ + 'image#1.png', + `${projectBaseURL}assets/b.png` + ]); + }); + it('should inject file names with parentheses and CJK punctuation', () => { + const content = { + stamps: { Stamps: ['示意圖 (chatgpt生成) 1.png', '細胞(核)【卵】.png', 'b.png'] } + }; + expect(service.injectAssetPaths(content).stamps.Stamps).toEqual([ + `${projectBaseURL}assets/示意圖 (chatgpt生成) 1.png`, + `${projectBaseURL}assets/細胞(核)【卵】.png`, + `${projectBaseURL}assets/b.png` + ]); + }); + it('should leave a match alone when it closes on a different quote than it opened with', () => { + const content = { prompt: "Hi: ok: it's x.png" }; + expect(service.injectAssetPaths(content)).toEqual(content); + }); + it('should not let parentheses in HTML text swallow the next image', () => { + const html = '

(see) a.png

'; + const expected = `

(see) a.png

`; + expect(service.injectAssetPaths({ html }).html).toEqual(expected); + expect(service.replaceAssetPaths(html)).toEqual(expected); + }); + it('should not prefix prose that contains punctuation and mentions a file', () => { + const content = { a: 'Hello, see b.png', b: '請看下圖,再回答 a.png' }; + expect(service.injectAssetPaths(content)).toEqual(content); + }); + it('should not change absolute paths', () => { + const content = { src: '/curriculum/1/assets/a (1).png' }; + expect(service.injectAssetPaths(content).src).toEqual('/curriculum/1/assets/a (1).png'); + }); + }); +} + 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 0875809b4bb..55aa647da4f 100644 --- a/src/assets/wise5/services/projectService.ts +++ b/src/assets/wise5/services/projectService.ts @@ -22,6 +22,17 @@ 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 +// mistaken for one. +const RELATIVE_ASSET_FILE_NAME = new RegExp( + '^(?!/)[a-zA-Z0-9@%&;()._/ \\-' + + '\\u3008-\\u3011\\u3014-\\u301b\\u3040-\\u30ff\\u3400-\\u4dbf\\u4e00-\\u9fff\\uac00-\\ud7af' + + '\\uff08\\uff09\\uff10-\\uff19\\uff21-\\uff3a\\uff3b\\uff3d\\uff41-\\uff5a]+' + + '\\.(png|jpe?g|pdf|gif|mov|mp4|mp3|wav|swf|css|txt|json|xlsx?|doc|html?|js)$', + 'i' +); + @Injectable() export class ProjectService { achievements: any = []; @@ -414,11 +425,9 @@ export class ProjectService { injectAssetPaths(content: any): any { if (content != null) { if (typeof content === 'object') { - let contentString = JSON.stringify(content); + const contentString = JSON.stringify(content); if (contentString != null) { - // replace the relative asset paths with the absolute paths - contentString = this.replaceAssetPaths(contentString); - content = JSON.parse(contentString); + content = this.injectAssetPathsInValue(JSON.parse(contentString)); } } else if (typeof content === 'string') { // replace the relative asset paths with the absolute paths @@ -428,6 +437,23 @@ export class ProjectService { return content; } + // Each string is handled on its own so that a match cannot run from one JSON string into the + // next, which would put the prefix outside any string and make JSON.parse throw. + private injectAssetPathsInValue(value: any): any { + if (typeof value === 'string') { + if (RELATIVE_ASSET_FILE_NAME.test(value)) { + return this.configService.getConfigParam('projectBaseURL') + 'assets/' + value; + } + return JSON.parse(this.replaceAssetPaths(JSON.stringify(value))); + } + if (value != null && typeof value === 'object') { + for (const key of Object.keys(value)) { + value[key] = this.injectAssetPathsInValue(value[key]); + } + } + return value; + } + /** * Replace the relative asset paths with absolute paths * @param contentString the content string @@ -444,52 +470,18 @@ export class ProjectService { // note that this also works for \"abc.png and \'abc.png, where the quotes are escaped contentString = contentString.replace( new RegExp( - "('|\"|\\\\'|\\\\\")[^:][^/]?[^/]?[a-zA-Z0-9\u4e00-\u9fa5@%&;\\._\\/\\s\\-']*[.]" + + "('|\"|\\\\'|\\\\\")([^:][^/]?[^/]?[a-zA-Z0-9\u4e00-\u9fa5@%&;\\._\\/\\s\\-']*[.]" + '(png|jpe?g|pdf|gif|mov|mp4|mp3|wav|swf|css|txt|json|xlsx?|doc|html.*?|js).*?' + - '(\'|"|\\\\\'|\\\\")', + '(\'|"|\\\\\'|\\\\"))', 'gi' ), - (matchedString) => { - /* - * once found, we prepend the contentBaseURL + "assets/" to the string within the quotes - * and keep everything else the same. - */ - let delimiter = ''; - let matchedStringWithoutQuotes = ''; - - if (matchedString.length > 2 && matchedString.substr(0, 1) == '\\') { - // the string has escaped quotes for example \"hello.png\" - - // get everything between the escaped quotes - matchedStringWithoutQuotes = matchedString.substr(2, matchedString.length - 4); - - // get the delimiter which will be \' or \" - delimiter = matchedString.substr(0, 2); - } else { - // the string does not have escaped quotes for example "hello.png" - - // get everything between the quotes - matchedStringWithoutQuotes = matchedString.substr(1, matchedString.length - 2); - - // get the delimiter which will be ' or " - delimiter = matchedString.substr(0, 1); - } - - if ( - matchedStringWithoutQuotes != null && - matchedStringWithoutQuotes.length > 0 && - matchedStringWithoutQuotes.charAt(0) == '/' - ) { - /* - * the matched string starts with a "/" which means it's - * an absolute path and does not require path prepending - * so we will just return the original unmodified string - */ - return delimiter + matchedStringWithoutQuotes + delimiter; - } else { - // make a new string with the contentBaseURL + assets/ prepended to the path - return delimiter + contentBaseURL + 'assets/' + matchedStringWithoutQuotes + delimiter; + (matchedString, delimiter: string, rest: string) => { + // Rebuilding a match that closes on a different quote with the opening one rewrites a + // quote and can leave a JSON string unterminated. + if (rest.charAt(0) == '/' || !matchedString.endsWith(delimiter)) { + return matchedString; } + return delimiter + contentBaseURL + 'assets/' + rest; } ); }