Skip to content

Commit

Permalink
🐛fix the scene where the url contains escape characters (#88)
Browse files Browse the repository at this point in the history
Co-authored-by: Kuitos <[email protected]>
  • Loading branch information
gongshun and kuitos authored Feb 27, 2023
1 parent 732a5f3 commit 67f07a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/__tests__/test-process-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('test process-tpl', () => {
'<link rel="preload" href="//gw.alipayobjects.com/as/g/antcloud-fe/antd-cloud-nav/0.2.22/antd-cloud-nav.min.js">\n' +
'<link rel="prefetch" href="/a3-ie6-polyfill.js">\n' +
'<link rel="stylesheet" href="/umi.css">\n' +
'<link rel="stylesheet" href="/escape-character&nbsp;.css?a=&amp;&b=2">\n' +
'<link rel="preload" as="font" href="/static/fonts/iconfont.woff" type="font/woff" crossorigin="anonymous">\n' +
'\n' +
'<meta charset="utf-8">\n' +
Expand Down Expand Up @@ -62,6 +63,7 @@ test('test process-tpl', () => {
'<div id="root"></div>\n' +
'\n' +
'<script src="/umi.js"></script>\n' +
'<script src="/escape-character&amp;.js"></script>' +
'<!-- <script src="/a1.js"></script>' +
'-->' +
'<script src="/comment.js"></script>\n' +
Expand Down Expand Up @@ -89,10 +91,13 @@ test('test process-tpl', () => {
src: 'http://kuitos.me/test-async.js',
},
'http://kuitos.me/umi.js',
'http://kuitos.me/escape-character&.js',
'http://kuitos.me/comment.js']);
expect(template.indexOf(genScriptReplaceSymbol('http://kuitos.me/test-async.js', true)) !== -1).toBeTruthy();
expect(template.indexOf(genLinkReplaceSymbol('http://kuitos.me/umi.css')) !== -1).toBeTruthy();
expect(template.indexOf(genLinkReplaceSymbol('http://kuitos.me/escape-character%C2%A0.css?a=&&b=2')) !== -1).toBeTruthy();
expect(template.indexOf(genScriptReplaceSymbol('http://kuitos.me/umi.js')) !== -1).toBeTruthy();
expect(template.indexOf(genScriptReplaceSymbol('http://kuitos.me/escape-character&.js')) !== -1).toBeTruthy();
expect(template.indexOf(genScriptReplaceSymbol('http://kuitos.me/comment.js')) !== -1).toBeTruthy();
expect(template.indexOf(genScriptReplaceSymbol('http://kuitos.me/main-es5.js')) !== -1).toBeTruthy();
expect(template.indexOf('<script src="/test-type.json" type="test"></script>') !== -1).toBeTruthy();
Expand All @@ -111,6 +116,7 @@ test('test process-tpl', () => {

const { styles, template: template2 } = processTpl(tpl, 'http://kuitos.me/cdn/');
expect(styles[0]).toBe('http://kuitos.me/umi.css');
expect(styles[1]).toBe('http://kuitos.me/escape-character%C2%A0.css?a=&&b=2');
expect(template2.indexOf(genLinkReplaceSymbol('http://kuitos.me/umi.css')) !== -1).toBeTruthy();

});
Expand Down
4 changes: 3 additions & 1 deletion src/process-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @homepage https://github.com/kuitos/
* @since 2018-09-03 15:04
*/
import { getInlineCode, isModuleScriptSupported } from './utils';
import { getInlineCode, isModuleScriptSupported, parseUrl } from './utils';

const ALL_SCRIPT_REGEX = /(<script[\s\S]*?>)[\s\S]*?<\/script>/gi;
const SCRIPT_TAG_REGEX = /<(script)\s+((?!type=('|")text\/ng-template\3).)*?>.*?<\/\1>/is;
Expand Down Expand Up @@ -92,6 +92,7 @@ export default function processTpl(tpl, baseURI, postProcessTemplate) {
return genIgnoreAssetReplaceSymbol(newHref);
}

newHref = parseUrl(newHref);
styles.push(newHref);
return genLinkReplaceSymbol(newHref);
}
Expand Down Expand Up @@ -156,6 +157,7 @@ export default function processTpl(tpl, baseURI, postProcessTemplate) {

if (matchedScriptSrc) {
const asyncScript = !!scriptTag.match(SCRIPT_ASYNC_REGEX);
matchedScriptSrc = parseUrl(matchedScriptSrc);
scripts.push(asyncScript ? { async: true, src: matchedScriptSrc } : matchedScriptSrc);
return genScriptReplaceSymbol(matchedScriptSrc, asyncScript);
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ export function evalCode(scriptSrc, code) {
const evalFunc = evalCache[key];
evalFunc.call(window);
}

// 转换 url 中的转义字符,例如 &amp; => &
export function parseUrl(url){
const parser = new DOMParser();
const html = `<script src="${url}"></script>`;
const doc = parser.parseFromString(html, "text/html");
return doc.scripts[0].src;
}

0 comments on commit 67f07a0

Please sign in to comment.