Auto merge of #21978 - jimberlage:input-value-fix, r=jdm

Fixes panic on DOMString::strip_leading_and_trailing_ascii_whitespace

<!-- Please describe your changes on the following line: -->
This changes `DOMString::strip_leading_and_trailing_ascii_whitespace` to handle multi-byte unicode characters.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #21963 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21978)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-11-16 11:10:32 -05:00 committed by GitHub
commit 831f966b0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 9 deletions

View file

@ -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, "");
}