简体   繁体   English

在ECMA-262严格模式下,是否允许在字符串中使用\\\\后跟十进制数字?

[英]Is '\0' followed by a decimal digit in string allowed in ECMA-262 strict mode?

According to the ECMA-262 specification (6th edition) in strict mode in single or double quoted strings after '\\' it is possible to have EscapeSequence or LineTerminatorSequence, and EscapeSequence must be one of the following: CharacterEscapeSequence, 0 [lookahead ∉ DecimalDigit], HexEscapeSequence, UnicodeEscapeSequence (see 11.8.4). 根据ECMA-262规范(第6版),在严格模式下,在\\后面的单引号或双引号字符串中可以使用EscapeSequence或LineTerminatorSequence,并且EscapeSequence必须为以下值之一:CharacterEscapeSequence,0 [lookahead∉DecimalDigit] ,HexEscapeSequence,UnicodeEscapeSequence(请参见11.8.4)。

Does it mean that it is completely incorrect to have any DecimalDigit after '\\0' at all? 这是否意味着在'\\ 0'之后有任何DecimalDigit完全不正确?

I understand that it is done that way to avoid confusion with LegacyOctalEscapeSequence (from B.1.2), but it required only octal digits being placed after the first '\\0', and V8 engine seems to support this in that way (see below). 我知道这样做是为了避免与LegacyOctalEscapeSequence(来自B.1.2)混淆,但是它只需要在第一个'\\ 0'之后放置八位数字,并且V8引擎似乎以这种方式支持此操作(请参见下文) 。

After checking up with the implementations it turns out that V8 engine allows having '\\0' followed only by DecimalDigit which is not an OctalDigit. 在检查完实现之后,事实证明V8引擎允许具有'\\ 0',之后仅是DecimalDigit,而不是OctalDigit。 In that case, it resolves it into the string with the string values of 0 at first position and then code point value of next digit as a SourceCharacter. 在这种情况下,它将解析为字符串,该字符串的第一个位置的字符串值为0,然后将下一位的代码点值作为SourceCharacter。 When it is given an OctalDigit after the '\\0', it throws a SyntaxError with message "Octal escape sequences are not allowed in strict mode.", which is a bit misleading. 在'\\ 0'之后给OctalDigit赋值时,它会引发SyntaxError并显示消息“严格模式下不允许八进制转义序列。”,这有点误导。 Chakra and SpiderMonkey seem to throw SyntaxError on any DecimalDigit after the '\\0', but with the similar message about octal escape sequences, which looks especially strange in some cases (with '8' or '9' after the '\\0', which couldn't be an octal escape sequence outside of strict mode). Chakra和SpiderMonkey似乎在'\\ 0'之后的任何DecimalDigit上抛出SyntaxError,但是带有类似八进制转义序列的消息,在某些情况下看起来尤为奇怪(在'\\ 0'之后带有'8'或'9',这不能是严格模式之外的八进制转义序列)。

So, my question is what is the correct interpretation of the specification? 那么,我的问题是对规范的正确解释是什么?

"0 [lookahead ∉ DecimalDigit]" sounds pretty unambiguous to me: no decimal digit is allowed after \\0 . “ 0 [lookahead∉DecimalDigit]”对我来说似乎很明确: \\0之后不允许使用十进制数字。

Which means the fact that V8 allows "\\08" and "\\09" in strict mode is a bug. 这意味着V8在严格模式下允许"\\08""\\09"是一个错误。 Would you please file a bug at https://bugs.chromium.org/p/v8/ ? 您能否在https://bugs.chromium.org/p/v8/上提交错误?

Interestingly, test262 does not seem to cover this case at all... 有趣的是,test262似乎根本没有涵盖这种情况。

Somewhat related: all browsers accept "\\8" and "\\9" in strict mode, which according to the spec should throw a SyntaxError. 有点相关:所有浏览器都在严格模式下接受"\\8""\\9" ,根据规范,这应引发SyntaxError。 There's a thread on esdiscuss.org indicating that this spec violation is (or at least used to be) required for web compatibility though: when existing websites depend on non-spec-compliant behavior, then browsers usually can't just update their behavior to be spec-compliant, because doing so would break such websites, and having websites work correctly is more important for users (and, hence, browser developers) than being spec-compliant. esdiscuss.org上有一个线程,它指示(但至少过去是)Web兼容性需要违反规范:当现有网站依赖于不符合规范的行为时,浏览器通常不能仅将其行为更新为遵守规范,因为这样做会破坏此类网站,并且网站对用户(以及浏览器开发人员)的正确运作比遵守规范更为重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM