From ae77325ad0e0ec9674dd9c5377530142066c7c06 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Thu, 2 Jul 2020 10:40:43 -0400 Subject: [PATCH] consolidate implementation of `unvendor()` --- src/common.rs | 23 +++++++++++++++++++++++ src/parse/function.rs | 22 +--------------------- src/selector/parse.rs | 24 +----------------------- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/src/common.rs b/src/common.rs index 09a21fb..7fb916d 100644 --- a/src/common.rs +++ b/src/common.rs @@ -144,3 +144,26 @@ impl Identifier { self.0 } } + +/// Returns `name` without a vendor prefix. +/// +/// If `name` has no vendor prefix, it's returned as-is. +pub(crate) fn unvendor(name: &str) -> &str { + let bytes = name.as_bytes(); + + if bytes.len() < 2 { + return name; + } + + if bytes.get(0_usize) != Some(&b'-') || bytes.get(1_usize) == Some(&b'-') { + return name; + } + + for i in 2..bytes.len() { + if bytes.get(i) == Some(&b'-') { + return &name[i + 1..]; + } + } + + name +} diff --git a/src/parse/function.rs b/src/parse/function.rs index 85da2fc..05e9852 100644 --- a/src/parse/function.rs +++ b/src/parse/function.rs @@ -4,6 +4,7 @@ use peekmore::PeekMore; use crate::{ args::CallArgs, atrule::Function, + common::unvendor, error::SassResult, utils::{read_until_closing_curly_brace, read_until_semicolon_or_closing_curly_brace}, value::Value, @@ -16,27 +17,6 @@ use super::{NeverEmptyVec, Parser, Stmt}; const FORBIDDEN_IDENTIFIERS: [&str; 7] = ["calc", "element", "expression", "url", "and", "or", "not"]; -fn unvendor(name: &str) -> &str { - let mut chars = name.chars(); - if !matches!(chars.next(), Some('-')) { - return name; - } - if matches!(chars.next(), Some('-')) { - return name; - } - if name.chars().count() < 2 { - return name; - } - let mut pos = 2; - for c in chars { - if c == '-' { - return &name[pos..]; - } - pos += 1; - } - name -} - impl<'a> Parser<'a> { pub(super) fn parse_function(&mut self) -> SassResult<()> { self.whitespace_or_comment(); diff --git a/src/selector/parse.rs b/src/selector/parse.rs index aa32b61..8490e2e 100644 --- a/src/selector/parse.rs +++ b/src/selector/parse.rs @@ -1,6 +1,7 @@ use codemap::Span; use crate::{ + common::unvendor, error::SassResult, parse::Parser, utils::{is_name, is_name_start, read_until_closing_paren}, @@ -560,29 +561,6 @@ fn is_simple_selector_start(c: char) -> bool { matches!(c, '*' | '[' | '.' | '#' | '%' | ':') } -/// Returns `name` without a vendor prefix. -/// -/// If `name` has no vendor prefix, it's returned as-is. -fn unvendor(name: &str) -> &str { - let bytes = name.as_bytes(); - - if bytes.len() < 2 { - return name; - } - - if bytes[0_usize] != b'-' || bytes[1_usize] == b'-' { - return name; - } - - for i in 2..bytes.len() { - if bytes.get(i) == Some(&b'-') { - return &name[i + 1..]; - } - } - - name -} - /// Returns whether `name` is the name of a pseudo-element that can be written /// with pseudo-class syntax (`:before`, `:after`, `:first-line`, or /// `:first-letter`)