diff --git a/layout/style/nsStyleUtil.cpp b/layout/style/nsStyleUtil.cpp index 6f6ceeccd65..74dac0f1ae9 100644 --- a/layout/style/nsStyleUtil.cpp +++ b/layout/style/nsStyleUtil.cpp @@ -81,13 +81,16 @@ void nsStyleUtil::AppendEscapedCSSString(const nsAString& aString, nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) { // The relevant parts of the CSS grammar are: - // ident [-]?{nmstart}{nmchar}* + // ident ([-]?{nmstart}|[-][-]){nmchar}* // nmstart [_a-z]|{nonascii}|{escape} // nmchar [_a-z0-9-]|{nonascii}|{escape} // nonascii [^\0-\177] // escape {unicode}|\\[^\n\r\f0-9a-f] // unicode \\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])? - // from http://www.w3.org/TR/CSS21/syndata.html#tokenization + // from http://www.w3.org/TR/CSS21/syndata.html#tokenization but + // modified for idents by + // http://dev.w3.org/csswg/cssom/#serialize-an-identifier and + // http://dev.w3.org/csswg/css-syntax/#would-start-an-identifier const char16_t* in = aIdent.BeginReading(); const char16_t* const end = aIdent.EndReading(); @@ -97,7 +100,13 @@ nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) // A leading dash does not need to be escaped as long as it is not the // *only* character in the identifier. - if (in + 1 != end && *in == '-') { + if (*in == '-') { + if (in + 1 == end) { + aReturn.Append(char16_t('\\')); + aReturn.Append(char16_t('-')); + return true; + } + aReturn.Append(char16_t('-')); ++in; } @@ -105,16 +114,8 @@ nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) // Escape a digit at the start (including after a dash), // numerically. If we didn't escape it numerically, it would get // interpreted as a numeric escape for the wrong character. - // A second dash immediately after a leading dash must also be - // escaped, but this may be done symbolically. - if (in != end && (*in == '-' || - ('0' <= *in && *in <= '9'))) { - if (*in == '-') { - aReturn.Append(char16_t('\\')); - aReturn.Append(char16_t('-')); - } else { - aReturn.AppendPrintf("\\%hX ", *in); - } + if (in != end && ('0' <= *in && *in <= '9')) { + aReturn.AppendPrintf("\\%hX ", *in); ++in; } diff --git a/layout/style/test/test_css_escape_api.html b/layout/style/test/test_css_escape_api.html index d262df09500..852c00a3b91 100644 --- a/layout/style/test/test_css_escape_api.html +++ b/layout/style/test/test_css_escape_api.html @@ -66,7 +66,7 @@ is(CSS.escape('-7a'), '-\\37 a', "escapingFailed Char: -7a"); is(CSS.escape('-8a'), '-\\38 a', "escapingFailed Char: -8a"); is(CSS.escape('-9a'), '-\\39 a', "escapingFailed Char: -9a"); -is(CSS.escape('--a'), '-\\-a', "escapingFailed Char: --a"); +is(CSS.escape('--a'), '--a', 'Should not need to escape leading "--"'); is(CSS.escape('\x80\x2D\x5F\xA9'), '\\80 \x2D\x5F\xA9', "escapingFailed Char: \\x80\\x2D\\x5F\\xA9"); is(CSS.escape('\xA0\xA1\xA2'), '\xA0\xA1\xA2', "escapingFailed Char: \\xA0\\xA1\\xA2"); diff --git a/layout/style/test/test_parser_diagnostics_unprintables.html b/layout/style/test/test_parser_diagnostics_unprintables.html index e532c2f49bf..e59989e94c0 100644 --- a/layout/style/test/test_parser_diagnostics_unprintables.html +++ b/layout/style/test/test_parser_diagnostics_unprintables.html @@ -74,7 +74,7 @@ const substitutions = [ { t: "\\37 ", i: "\\37 ", s: "7" }, { t: "\\38 ", i: "\\38 ", s: "8" }, { t: "\\39 ", i: "\\39 ", s: "9" }, - { t: "-\\-", i: "-\\-", s: "--" }, + { t: "-\\-", i: "--", s: "--" }, { t: "-\\30 ", i: "-\\30 ", s: "-0" }, { t: "-\\31 ", i: "-\\31 ", s: "-1" }, { t: "-\\32 ", i: "-\\32 ", s: "-2" },