diff --git a/components/style/str.rs b/components/style/str.rs index 96305a59f1f..92febb40824 100644 --- a/components/style/str.rs +++ b/components/style/str.rs @@ -148,6 +148,6 @@ pub fn str_join(strs: I, join: &str) -> String /// Returns true if a given string has a given prefix with case-insensitive match. pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool { - string.len() > prefix.len() && - string[0..prefix.len()].eq_ignore_ascii_case(prefix) + string.len() >= prefix.len() && + string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes()) } diff --git a/tests/unit/style/str.rs b/tests/unit/style/str.rs index dafbd8fb7da..154cf894158 100644 --- a/tests/unit/style/str.rs +++ b/tests/unit/style/str.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use style::str::{split_html_space_chars, str_join}; +use style::str::{split_html_space_chars, str_join, starts_with_ignore_ascii_case}; #[test] pub fn split_html_space_chars_whitespace() { @@ -33,3 +33,14 @@ pub fn test_str_join_many() { let expected = "-alpha--beta-gamma-"; assert_eq!(actual, expected); } + +#[test] +pub fn test_starts_with_ignore_ascii_case_basic() { + assert!(starts_with_ignore_ascii_case("-webkit-", "-webkit-")); + assert!(starts_with_ignore_ascii_case("-webkit-foo", "-webkit-")); +} + +#[test] +pub fn test_starts_with_ignore_ascii_case_char_boundary() { + assert!(!starts_with_ignore_ascii_case("aaaaa💩", "-webkit-")); +}