mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Add DOMString floating point string test and fix
Fixes an issue where DOMString::is_valid_floating_point_number_string was returning true for strings that began with whitespace characters- TAB, LF, FF, or CR. Also added a unit test to cover this since the corresponding web-platform-tests are incomplete.
This commit is contained in:
parent
23359c5868
commit
576f51f598
3 changed files with 27 additions and 5 deletions
|
@ -3,11 +3,11 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
//! The `ByteString` struct.
|
//! The `ByteString` struct.
|
||||||
|
|
||||||
use chrono::prelude::{Utc, Weekday};
|
use chrono::prelude::{Utc, Weekday};
|
||||||
use chrono::{Datelike, TimeZone};
|
use chrono::{Datelike, TimeZone};
|
||||||
use cssparser::CowRcStr;
|
use cssparser::CowRcStr;
|
||||||
use html5ever::{LocalName, Namespace};
|
use html5ever::{LocalName, Namespace};
|
||||||
|
use regex::Regex;
|
||||||
use servo_atoms::Atom;
|
use servo_atoms::Atom;
|
||||||
use std::borrow::{Borrow, Cow, ToOwned};
|
use std::borrow::{Borrow, Cow, ToOwned};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
@ -337,11 +337,11 @@ impl DOMString {
|
||||||
|
|
||||||
/// https://html.spec.whatwg.org/multipage/#valid-floating-point-number
|
/// https://html.spec.whatwg.org/multipage/#valid-floating-point-number
|
||||||
pub fn is_valid_floating_point_number_string(&self) -> bool {
|
pub fn is_valid_floating_point_number_string(&self) -> bool {
|
||||||
// for the case that `parse_floating_point_number` cannot handle
|
lazy_static! {
|
||||||
if self.0.contains(" ") {
|
static ref RE: Regex =
|
||||||
return false;
|
Regex::new(r"^-?(?:\d+\.\d+|\d+|\.\d+)(?:(e|E)(\+|\-)?\d+)?$").unwrap();
|
||||||
}
|
}
|
||||||
parse_floating_point_number(&self.0).is_ok()
|
RE.is_match(&self.0) && parse_floating_point_number(&self.0).is_ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number
|
/// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number
|
||||||
|
|
20
tests/unit/script/domstring.rs
Normal file
20
tests/unit/script/domstring.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use script::test::DOMString;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_domstring_is_valid_floating_point_number_string_leading_whitespace() {
|
||||||
|
assert!(!DOMString::from("\t1").is_valid_floating_point_number_string());
|
||||||
|
assert!(!DOMString::from("\n1").is_valid_floating_point_number_string());
|
||||||
|
// \x0C - form feed
|
||||||
|
assert!(!DOMString::from("\x0C1").is_valid_floating_point_number_string());
|
||||||
|
assert!(!DOMString::from("\r1").is_valid_floating_point_number_string());
|
||||||
|
assert!(!DOMString::from(" 1").is_valid_floating_point_number_string());
|
||||||
|
}
|
|
@ -2,6 +2,8 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod domstring;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod headers;
|
mod headers;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue