Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Minor refactorings for 'data-' attribute logic.
  • Loading branch information
frewsxcv committed Jul 29, 2017
1 parent 23e5bfa commit 6238035
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions components/script/dom/htmlelement.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -394,19 +397,19 @@ 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..];
let has_uppercase = name.chars().any(|curr_char| curr_char.is_ascii_uppercase());
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());
Expand Down

0 comments on commit 6238035

Please sign in to comment.