-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: if the second args of string.replace is a string, it would as Re…
…gExp. (#5069)
- Loading branch information
1 parent
923fee7
commit e05e496
Showing
3 changed files
with
31 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@modern-js/runtime': patch | ||
--- | ||
|
||
fix: if the second args of string.replace is a string, it would as RegExp. so we use function to replace | ||
fix: 如果 string.replace 第二个参数是字符串,他若有特殊字符将会被当作正则处理,所以我们用函数去替换他 |
32 changes: 24 additions & 8 deletions
32
packages/runtime/plugin-runtime/src/ssr/serverRender/renderToString/buildHtml.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,41 @@ | ||
export type BuildHtmlCb = (tempalte: string) => string; | ||
|
||
/** | ||
* It is unsafe unsafeReplace, only support serachValue exsit one time. | ||
* @param source | ||
* @param searchValue | ||
* @param replaceValue | ||
* @returns | ||
*/ | ||
function unsafeReplace( | ||
source: string, | ||
searchValue: RegExp | string, | ||
replaceValue: string, | ||
) { | ||
const [s1, s2] = source.split(searchValue); | ||
return s1 + replaceValue + s2; | ||
} | ||
|
||
export function buildHtml(template: string, callbacks: BuildHtmlCb[]) { | ||
return callbacks.reduce((tmp, cb) => cb(tmp), template); | ||
} | ||
|
||
export function createReplaceHtml(html: string): BuildHtmlCb { | ||
const HTML_REG = /<!--<\?-\s*html\s*\?>-->/; | ||
return (template: string) => template.replace(HTML_REG, html); | ||
const HTML_REMARK = '<!--<?- html ?>-->'; | ||
return (template: string) => unsafeReplace(template, HTML_REMARK, html); | ||
} | ||
|
||
export function createReplaceSSRDataScript(data: string): BuildHtmlCb { | ||
const SSR_DATA_REG = /<!--<\?-\s*SSRDataScript\s*\?>-->/; | ||
return (template: string) => template.replace(SSR_DATA_REG, data); | ||
const SSR_DATA_REMARK = '<!--<?- SSRDataScript ?>-->'; | ||
return (template: string) => unsafeReplace(template, SSR_DATA_REMARK, data); | ||
} | ||
|
||
export function createReplaceChunkJs(js: string): BuildHtmlCb { | ||
const CHUNK_JS_REG = /<!--<\?-\s*chunksMap\.js\s*\?>-->/; | ||
return (template: string) => template.replace(CHUNK_JS_REG, js); | ||
const CHUNK_JS_REMARK = '<!--<?- chunksMap.js ?>-->'; | ||
return (template: string) => unsafeReplace(template, CHUNK_JS_REMARK, js); | ||
} | ||
|
||
export function createReplaceChunkCss(css: string): BuildHtmlCb { | ||
const CHUNK_CSS_REG = /<!--<\?-\s*chunksMap\.css\s*\?>-->/; | ||
return (template: string) => template.replace(CHUNK_CSS_REG, css); | ||
const CHUNK_CSS_REG = '<!--<?- chunksMap.css ?>-->'; | ||
return (template: string) => unsafeReplace(template, CHUNK_CSS_REG, css); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters