File tree Expand file tree Collapse file tree 2 files changed +14
-5
lines changed Expand file tree Collapse file tree 2 files changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,11 @@ import (
13
13
"unicode/utf8"
14
14
)
15
15
16
+ // jsWhitespace contains all of the JS whitespace characters, as defined
17
+ // by the \s character class.
18
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes.
19
+ const jsWhitespace = "\f \n \r \t \v \u0020 \u00a0 \u1680 \u2000 \u2001 \u2002 \u2003 \u2004 \u2005 \u2006 \u2007 \u2008 \u2009 \u200a \u2028 \u2029 \u202f \u205f \u3000 \ufeff "
20
+
16
21
// nextJSCtx returns the context that determines whether a slash after the
17
22
// given run of tokens starts a regular expression instead of a division
18
23
// operator: / or /=.
@@ -26,7 +31,8 @@ import (
26
31
// JavaScript 2.0 lexical grammar and requires one token of lookbehind:
27
32
// https://www.mozilla.org/js/language/js20-2000-07/rationale/syntax.html
28
33
func nextJSCtx (s []byte , preceding jsCtx ) jsCtx {
29
- s = bytes .TrimRight (s , "\t \n \f \r \u2028 \u2029 " )
34
+ // Trim all JS whitespace characters
35
+ s = bytes .TrimRight (s , jsWhitespace )
30
36
if len (s ) == 0 {
31
37
return preceding
32
38
}
Original file line number Diff line number Diff line change @@ -80,14 +80,17 @@ func TestNextJsCtx(t *testing.T) {
80
80
{jsCtxDivOp , "0" },
81
81
// Dots that are part of a number are div preceders.
82
82
{jsCtxDivOp , "0." },
83
+ // Some JS interpreters treat NBSP as a normal space, so
84
+ // we must too in order to properly escape things.
85
+ {jsCtxRegexp , "=\u00A0 " },
83
86
}
84
87
85
88
for _ , test := range tests {
86
- if nextJSCtx ([]byte (test .s ), jsCtxRegexp ) != test .jsCtx {
87
- t .Errorf ("want %s got %q " , test .jsCtx , test .s )
89
+ if ctx := nextJSCtx ([]byte (test .s ), jsCtxRegexp ); ctx != test .jsCtx {
90
+ t .Errorf ("%q: want %s got %s " , test .s , test .jsCtx , ctx )
88
91
}
89
- if nextJSCtx ([]byte (test .s ), jsCtxDivOp ) != test .jsCtx {
90
- t .Errorf ("want %s got %q " , test .jsCtx , test .s )
92
+ if ctx := nextJSCtx ([]byte (test .s ), jsCtxDivOp ); ctx != test .jsCtx {
93
+ t .Errorf ("%q: want %s got %s " , test .s , test .jsCtx , ctx )
91
94
}
92
95
}
93
96
You can’t perform that action at this time.
0 commit comments