diff --git a/dom/webidl/CSS.webidl b/dom/webidl/CSS.webidl index 9965554a881..b5cdab2d185 100644 --- a/dom/webidl/CSS.webidl +++ b/dom/webidl/CSS.webidl @@ -20,6 +20,5 @@ interface CSS { // http://dev.w3.org/csswg/cssom/#the-css.escape%28%29-method partial interface CSS { - [Throws] static DOMString escape(DOMString ident); }; diff --git a/layout/style/CSS.cpp b/layout/style/CSS.cpp index a0ec3a2f04d..55a426de394 100644 --- a/layout/style/CSS.cpp +++ b/layout/style/CSS.cpp @@ -85,14 +85,9 @@ CSS::Supports(const GlobalObject& aGlobal, /* static */ void CSS::Escape(const GlobalObject& aGlobal, const nsAString& aIdent, - nsAString& aReturn, - ErrorResult& aRv) + nsAString& aReturn) { - bool success = nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn); - - if (!success) { - aRv.Throw(NS_ERROR_DOM_INVALID_CHARACTER_ERR); - } + nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn); } } // namespace dom diff --git a/layout/style/CSS.h b/layout/style/CSS.h index adf583d9509..ce3e38da41e 100644 --- a/layout/style/CSS.h +++ b/layout/style/CSS.h @@ -35,8 +35,7 @@ public: static void Escape(const GlobalObject& aGlobal, const nsAString& aIdent, - nsAString& aReturn, - ErrorResult& aRv); + nsAString& aReturn); }; } // namespace dom diff --git a/layout/style/nsStyleUtil.cpp b/layout/style/nsStyleUtil.cpp index d1f9b4461a5..e6cbdd8a93f 100644 --- a/layout/style/nsStyleUtil.cpp +++ b/layout/style/nsStyleUtil.cpp @@ -79,7 +79,7 @@ void nsStyleUtil::AppendEscapedCSSString(const nsAString& aString, aReturn.Append(quoteChar); } -/* static */ bool +/* static */ void nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) { // The relevant parts of the CSS grammar are: @@ -98,7 +98,7 @@ nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) const char16_t* const end = aIdent.EndReading(); if (in == end) - return true; + return; // A leading dash does not need to be escaped as long as it is not the // *only* character in the identifier. @@ -106,7 +106,7 @@ nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) if (in + 1 == end) { aReturn.Append(char16_t('\\')); aReturn.Append(char16_t('-')); - return true; + return; } aReturn.Append(char16_t('-')); @@ -124,9 +124,8 @@ nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) for (; in != end; ++in) { char16_t ch = *in; if (ch == 0x00) { - return false; - } - if (ch < 0x20 || (0x7F <= ch && ch < 0xA0)) { + aReturn.Append(char16_t(0xFFFD)); + } else if (ch < 0x20 || (0x7F <= ch && ch < 0xA0)) { // Escape U+0000 through U+001F and U+007F through U+009F numerically. aReturn.AppendPrintf("\\%hx ", *in); } else { @@ -142,7 +141,6 @@ nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn) aReturn.Append(ch); } } - return true; } // unquoted family names must be a sequence of idents diff --git a/layout/style/nsStyleUtil.h b/layout/style/nsStyleUtil.h index 196563a554f..2fb098f403a 100644 --- a/layout/style/nsStyleUtil.h +++ b/layout/style/nsStyleUtil.h @@ -39,10 +39,9 @@ public: // Append the identifier given by |aIdent| to |aResult|, with // appropriate escaping so that it can be reparsed to the same - // identifier. - // Returns false if |aIdent| contains U+0000 - // Returns true for all other cases - static bool AppendEscapedCSSIdent(const nsAString& aIdent, + // identifier. An exception is if aIdent contains U+0000, which + // will be escaped as U+FFFD and then reparsed back to U+FFFD. + static void AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aResult); static void diff --git a/layout/style/test/test_css_escape_api.html b/layout/style/test/test_css_escape_api.html index 3154f4dca5a..00ec240c70d 100644 --- a/layout/style/test/test_css_escape_api.html +++ b/layout/style/test/test_css_escape_api.html @@ -21,11 +21,18 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=955860 // Tests taken from: // https://github.com/mathiasbynens/CSS.escape/blob/master/tests/tests.js -SimpleTest.doesThrow(() => CSS.escape('\0'), "InvalidCharacterError Character :\\0"); -SimpleTest.doesThrow(() => CSS.escape('a\0'), "InvalidCharacterError Character : a\\0"); -SimpleTest.doesThrow(() => CSS.escape('\0b'), "InvalidCharacterError Character : \\0b"); -SimpleTest.doesThrow(() => CSS.escape('a\0b'), "InvalidCharacterError Character : a\\0b"); SimpleTest.doesThrow(() => CSS.escape(), 'undefined'); + +is(CSS.escape('\0'), '\uFFFD', "escaping for 0 char (1)"); +is(CSS.escape('a\0'), 'a\uFFFD', "escaping for 0 char (2)"); +is(CSS.escape('\0b'), '\uFFFDb', "escaping for 0 char (3)"); +is(CSS.escape('a\0b'), 'a\uFFFDb', "escaping for 0 char (4)"); + +is(CSS.escape('\uFFFD'), '\uFFFD', "escaping for replacement char (1)"); +is(CSS.escape('a\uFFFD'), 'a\uFFFD', "escaping replacement char (2)"); +is(CSS.escape('\uFFFDb'), '\uFFFDb', "escaping replacement char (3)"); +is(CSS.escape('a\uFFFDb'), 'a\uFFFDb', "escaping replacement char (4)"); + is(CSS.escape(true), 'true', "escapingFailed Character : true(bool)"); is(CSS.escape(false), 'false', "escapingFailed Character : false(bool)"); is(CSS.escape(null), 'null', "escapingFailed Character : null");