Auto merge of #18262 - KiChjang:value-sanitization, r=nox

Implement value sanitization on HTMLInputElement

https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm

<!-- 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/18262)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-11-09 18:35:07 -06:00 committed by GitHub
commit 338e2ae520
15 changed files with 213 additions and 285 deletions

View file

@ -185,6 +185,29 @@ impl DOMString {
pub fn bytes(&self) -> Bytes {
self.0.bytes()
}
/// Removes newline characters according to <https://infra.spec.whatwg.org/#strip-newlines>.
pub fn strip_newlines(&mut self) {
self.0.retain(|c| c != '\r' && c != '\n');
}
/// Removes leading and trailing ASCII whitespaces according to
/// <https://infra.spec.whatwg.org/#strip-leading-and-trailing-ascii-whitespace>.
pub fn strip_leading_and_trailing_ascii_whitespace(&mut self) {
if self.0.len() == 0 { 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();
self.0.truncate(last_non_whitespace);
let _ = self.0.splice(0..first_non_whitespace, "");
}
}
impl Borrow<str> for DOMString {