File tree 4 files changed +41
-10
lines changed
4 files changed +41
-10
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,25 @@ test('strings', () => {
32
32
expect ( clean ) . toMatch ( 'const b = "\0\0\0\0"' )
33
33
} )
34
34
35
+ test ( 'escape character' , ( ) => {
36
+ const clean = emptyString ( `
37
+ '1\\'1'
38
+ "1\\"1"
39
+ "1\\"1\\"1"
40
+ "1\\'1'\\"1"
41
+ "1'1'"
42
+ "1'\\'1\\''\\"1\\"\\""
43
+ '1"\\"1\\""\\"1\\"\\"'
44
+ '""1""'
45
+ '"""1"""'
46
+ '""""1""""'
47
+ "''1''"
48
+ "'''1'''"
49
+ "''''1''''"
50
+ ` )
51
+ expect ( clean ) . not . toMatch ( '1' )
52
+ } )
53
+
35
54
test ( 'strings comment nested' , ( ) => {
36
55
expect (
37
56
emptyString ( `
Original file line number Diff line number Diff line change @@ -2,7 +2,8 @@ import type { RollupError } from 'rollup'
2
2
// bank on the non-overlapping nature of regex matches and combine all filters into one giant regex
3
3
// /`([^`\$\{\}]|\$\{(`|\g<1>)*\})*`/g can match nested string template
4
4
// but js not support match expression(\g<0>). so clean string template(`...`) in other ways.
5
- const cleanerRE = / " [ ^ " ] * " | ' [ ^ ' ] * ' | \/ \* ( .| [ \r \n ] ) * ?\* \/ | \/ \/ .* / g
5
+ const cleanerRE =
6
+ / " ( [ ^ " ] | (?< = \\ ) " ) * " | ' ( [ ^ ' ] | (?< = \\ ) ' ) * ' | \/ \* ( .| [ \r \n ] ) * ?\* \/ | \/ \/ .* / g
6
7
7
8
const blankReplacer = ( s : string ) => ' ' . repeat ( s . length )
8
9
const stringBlankReplacer = ( s : string ) =>
@@ -25,9 +26,16 @@ export function emptyString(raw: string): string {
25
26
}
26
27
27
28
const enum LexerState {
29
+ // template string
28
30
inTemplateString ,
29
31
inInterpolationExpression ,
30
- inObjectExpression
32
+ inObjectExpression ,
33
+ // strings
34
+ inSingleQuoteString ,
35
+ inDoubleQuoteString ,
36
+ // comments
37
+ inMultilineCommentsRE ,
38
+ inSinglelineCommentsRE
31
39
}
32
40
33
41
function replaceAt (
Original file line number Diff line number Diff line change @@ -1122,7 +1122,7 @@ export async function hoistAtRules(css: string) {
1122
1122
// to top when multiple files are concatenated.
1123
1123
// match until semicolon that's not in quotes
1124
1124
s . replace (
1125
- / @ i m p o r t \s * (?: u r l \( [ ^ \) ] * \) | " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ ; ] * ) .* ?; / gm,
1125
+ / @ i m p o r t \s * (?: u r l \( [ ^ \) ] * \) | " ( [ ^ " ] | (?< = \\ ) " ) * " | ' ( [ ^ ' ] | (?< = \\ ) ' ) * ' | [ ^ ; ] * ) .* ?; / gm,
1126
1126
( match ) => {
1127
1127
s . appendLeft ( 0 , match )
1128
1128
return ''
@@ -1131,13 +1131,16 @@ export async function hoistAtRules(css: string) {
1131
1131
// #6333
1132
1132
// CSS @charset must be the top-first in the file, hoist the first to top
1133
1133
let foundCharset = false
1134
- s . replace ( / @ c h a r s e t \s * (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ ; ] * ) .* ?; / gm, ( match ) => {
1135
- if ( ! foundCharset ) {
1136
- s . prepend ( match )
1137
- foundCharset = true
1134
+ s . replace (
1135
+ / @ c h a r s e t \s * (?: " ( [ ^ " ] | (?< = \\ ) " ) * " | ' ( [ ^ ' ] | (?< = \\ ) ' ) * ' | [ ^ ; ] * ) .* ?; / gm,
1136
+ ( match ) => {
1137
+ if ( ! foundCharset ) {
1138
+ s . prepend ( match )
1139
+ foundCharset = true
1140
+ }
1141
+ return ''
1138
1142
}
1139
- return ''
1140
- } )
1143
+ )
1141
1144
return s . toString ( )
1142
1145
}
1143
1146
Original file line number Diff line number Diff line change @@ -46,7 +46,8 @@ interface ScriptAssetsUrl {
46
46
const htmlProxyRE = / \? h t m l - p r o x y = ? [ & i n l i n e \- c s s ] * & i n d e x = ( \d + ) \. ( j s | c s s ) $ /
47
47
const inlineCSSRE = / _ _ V I T E _ I N L I N E _ C S S _ _ ( [ ^ _ ] + _ \d + ) _ _ / g
48
48
// Do not allow preceding '.', but do allow preceding '...' for spread operations
49
- const inlineImportRE = / (?< ! (?< ! \. \. ) \. ) \b i m p o r t \s * \( ( " [ ^ " ] * " | ' [ ^ ' ] * ' ) \) / g
49
+ const inlineImportRE =
50
+ / (?< ! (?< ! \. \. ) \. ) \b i m p o r t \s * \( ( " ( [ ^ " ] | (?< = \\ ) " ) * " | ' ( [ ^ ' ] | (?< = \\ ) ' ) * ' ) \) / g
50
51
const htmlLangRE = / \. ( h t m l | h t m ) $ /
51
52
52
53
export const isHTMLProxy = ( id : string ) : boolean => htmlProxyRE . test ( id )
You can’t perform that action at this time.
0 commit comments