mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Minor refactorings for 'data-' attribute logic.
This commit is contained in:
parent
23e5bfaf27
commit
6238035612
1 changed files with 9 additions and 6 deletions
|
@ -32,7 +32,6 @@ use dom::virtualmethods::VirtualMethods;
|
|||
use dom_struct::dom_struct;
|
||||
use html5ever::{LocalName, Prefix};
|
||||
use std::ascii::AsciiExt;
|
||||
use std::borrow::ToOwned;
|
||||
use std::default::Default;
|
||||
use std::rc::Rc;
|
||||
use style::attr::AttrValue;
|
||||
|
@ -374,11 +373,15 @@ impl HTMLElementMethods for HTMLElement {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#attr-data-*
|
||||
|
||||
static DATA_PREFIX: &str = "data-";
|
||||
static DATA_HYPHEN_SEPARATOR: char = '\x2d';
|
||||
|
||||
fn to_snake_case(name: DOMString) -> DOMString {
|
||||
let mut attr_name = "data-".to_owned();
|
||||
let mut attr_name = String::with_capacity(name.len() + DATA_PREFIX.len());
|
||||
attr_name.push_str(DATA_PREFIX);
|
||||
for ch in name.chars() {
|
||||
if ch.is_ascii_uppercase() {
|
||||
attr_name.push('\x2d');
|
||||
attr_name.push(DATA_HYPHEN_SEPARATOR);
|
||||
attr_name.push(ch.to_ascii_lowercase());
|
||||
} else {
|
||||
attr_name.push(ch);
|
||||
|
@ -394,7 +397,7 @@ fn to_snake_case(name: DOMString) -> DOMString {
|
|||
// without the data prefix.
|
||||
|
||||
fn to_camel_case(name: &str) -> Option<DOMString> {
|
||||
if !name.starts_with("data-") {
|
||||
if !name.starts_with(DATA_PREFIX) {
|
||||
return None;
|
||||
}
|
||||
let name = &name[5..];
|
||||
|
@ -402,11 +405,11 @@ fn to_camel_case(name: &str) -> Option<DOMString> {
|
|||
if has_uppercase {
|
||||
return None;
|
||||
}
|
||||
let mut result = "".to_owned();
|
||||
let mut result = String::with_capacity(name.len().saturating_sub(DATA_PREFIX.len()));
|
||||
let mut name_chars = name.chars();
|
||||
while let Some(curr_char) = name_chars.next() {
|
||||
//check for hyphen followed by character
|
||||
if curr_char == '\x2d' {
|
||||
if curr_char == DATA_HYPHEN_SEPARATOR {
|
||||
if let Some(next_char) = name_chars.next() {
|
||||
if next_char.is_ascii_lowercase() {
|
||||
result.push(next_char.to_ascii_uppercase());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue