From 67f07a0a378b5b4d748a6c816c8dd40ddd785678 Mon Sep 17 00:00:00 2001 From: gongshun <2440606146@qq.com> Date: Mon, 27 Feb 2023 11:31:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bfix=20the=20scene=20where=20the=20u?= =?UTF-8?q?rl=20contains=20escape=20characters=20(#88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kuitos --- src/__tests__/test-process-tpl.js | 6 ++++++ src/process-tpl.js | 4 +++- src/utils.js | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/__tests__/test-process-tpl.js b/src/__tests__/test-process-tpl.js index 2e09da7..8b27f78 100644 --- a/src/__tests__/test-process-tpl.js +++ b/src/__tests__/test-process-tpl.js @@ -14,6 +14,7 @@ test('test process-tpl', () => { '\n' + '\n' + '\n' + + '\n' + '\n' + '\n' + '\n' + @@ -62,6 +63,7 @@ test('test process-tpl', () => { '
\n' + '\n' + '\n' + + '' + '' + '\n' + @@ -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('') !== -1).toBeTruthy(); @@ -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(); }); diff --git a/src/process-tpl.js b/src/process-tpl.js index e6bd160..3e6bea1 100644 --- a/src/process-tpl.js +++ b/src/process-tpl.js @@ -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 = /()[\s\S]*?<\/script>/gi; const SCRIPT_TAG_REGEX = /<(script)\s+((?!type=('|")text\/ng-template\3).)*?>.*?<\/\1>/is; @@ -92,6 +92,7 @@ export default function processTpl(tpl, baseURI, postProcessTemplate) { return genIgnoreAssetReplaceSymbol(newHref); } + newHref = parseUrl(newHref); styles.push(newHref); return genLinkReplaceSymbol(newHref); } @@ -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); } diff --git a/src/utils.js b/src/utils.js index 3700db4..91d46fd 100644 --- a/src/utils.js +++ b/src/utils.js @@ -177,3 +177,11 @@ export function evalCode(scriptSrc, code) { const evalFunc = evalCache[key]; evalFunc.call(window); } + +// 转换 url 中的转义字符,例如 & => & +export function parseUrl(url){ + const parser = new DOMParser(); + const html = ``; + const doc = parser.parseFromString(html, "text/html"); + return doc.scripts[0].src; +}