From 1f99bf4631cc0b5bbd2c4407c98bcd547a63905e Mon Sep 17 00:00:00 2001 From: Jim Berlage Date: Wed, 17 Oct 2018 13:54:55 -0500 Subject: [PATCH] Fixes panic on DOMString::strip_leading_and_trailing_ascii_whitespace --- components/script/dom/bindings/str.rs | 18 ++++++------- tests/wpt/mozilla/meta/MANIFEST.json | 10 ++++++++ .../mozilla/tests/mozilla/input_value.html | 25 +++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 tests/wpt/mozilla/tests/mozilla/input_value.html diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index 9f038026562..251a03b2d66 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -182,16 +182,16 @@ impl DOMString { return; } - let last_non_whitespace = match self.0.rfind(|ref c| !char::is_ascii_whitespace(c)) { - Some(idx) => idx + 1, - None => { - self.0.clear(); - return; - }, - }; - let first_non_whitespace = self.0.find(|ref c| !char::is_ascii_whitespace(c)).unwrap(); + let trailing_whitespace_len = self + .0 + .trim_end_matches(|ref c| char::is_ascii_whitespace(c)) + .len(); + self.0.truncate(trailing_whitespace_len); + if self.0.is_empty() { + return; + } - self.0.truncate(last_non_whitespace); + let first_non_whitespace = self.0.find(|ref c| !char::is_ascii_whitespace(c)).unwrap(); let _ = self.0.replace_range(0..first_non_whitespace, ""); } diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index 42349d2c34f..af116a9143a 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -14006,6 +14006,12 @@ {} ] ], + "mozilla/input_value.html": [ + [ + "/_mozilla/mozilla/input_value.html", + {} + ] + ], "mozilla/interface_member_exposed.html": [ [ "/_mozilla/mozilla/interface_member_exposed.html", @@ -27049,6 +27055,10 @@ "031e67e0c3bfd25bb32a8c1727864cdcf8bd641b", "testharness" ], + "mozilla/input_value.html": [ + "a2a12d44d0331164651816710eeb2ddcb1738735", + "testharness" + ], "mozilla/interface_member_exposed.html": [ "dd637cf92a894e4569e8fb0baf11eea6968033af", "testharness" diff --git a/tests/wpt/mozilla/tests/mozilla/input_value.html b/tests/wpt/mozilla/tests/mozilla/input_value.html new file mode 100644 index 00000000000..a2a12d44d03 --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/input_value.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + +