Module: Sass::SCSS::RX

Included in:
Parser, Sass::Script::Lexer
Defined in:
lib/sass/scss/rx.rb

Overview

A module containing regular expressions used for lexing tokens in an SCSS document. Most of these are taken from the CSS3 spec, although some have been modified for various reasons.

Constant Summary collapse

H =
/[0-9a-fA-F]/
NL =
/\n|\r\n|\r|\f/
UNICODE =
/\\#{H}{1,6}[ \t\r\n\f]?/
NONASCII =
/[#{s}]/
ESCAPE =
/#{UNICODE}|\\[ -~#{s}]/
NMSTART =
/[_a-zA-Z]|#{NONASCII}|#{ESCAPE}/
NMCHAR =
/[a-zA-Z0-9_-]|#{NONASCII}|#{ESCAPE}/
STRING1 =
/\"((?:[^\n\r\f\\"]|\\#{NL}|#{ESCAPE})*)\"/
STRING2 =
/\'((?:[^\n\r\f\\']|\\#{NL}|#{ESCAPE})*)\'/
IDENT =
/-?#{NMSTART}#{NMCHAR}*/
NAME =
/#{NMCHAR}+/
NUM =
/[0-9]+|[0-9]*\.[0-9]+/
STRING =
/#{STRING1}|#{STRING2}/
URLCHAR =
/[#%&*-~]|#{NONASCII}|#{ESCAPE}/
URL =
/(#{URLCHAR}*)/
W =
/[ \t\r\n\f]*/
VARIABLE =
/(\$)(#{Sass::SCSS::RX::IDENT})/
RANGE =

This is more liberal than the spec's definition, but that definition didn't work well with the greediness rules

/(?:#{H}|\?){1,6}/
S =
/[ \t\r\n\f]+/
COMMENT =
/\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\//
SINGLE_LINE_COMMENT =
/\/\/.*(\n[ \t]*\/\/.*)*/
CDO =
quote("<!--")
CDC =
quote("-->")
INCLUDES =
quote("~=")
DASHMATCH =
quote("|=")
PREFIXMATCH =
quote("^=")
SUFFIXMATCH =
quote("$=")
SUBSTRINGMATCH =
quote("*=")
HASH =
/##{NAME}/
IMPORTANT =
/!#{W}important/i
DEFAULT =
/!#{W}default/i
NUMBER =
/#{NUM}(?:#{IDENT}|%)?/
URI =
/url\(#{W}(?:#{STRING}|#{URL})#{W}\)/i
FUNCTION =
/#{IDENT}\(/
UNICODERANGE =
/u\+(?:#{H}{1,6}-#{H}{1,6}|#{RANGE})/i
PLUS =
/#{W}\+/
GREATER =
/#{W}>/
TILDE =
/#{W}~/
NOT =
quote(":not(", Regexp::IGNORECASE)
HEXCOLOR =

Custom

/\#[0-9a-fA-F]+/
INTERP_START =
/#\{/
MOZ_ANY =
quote(":-moz-any(", Regexp::IGNORECASE)
IDENT_HYPHEN_INTERP =
/-(#\{)/
STRING1_NOINTERP =
/\"((?:[^\n\r\f\\"#]|#(?!\{)|\\#{NL}|#{ESCAPE})*)\"/
STRING2_NOINTERP =
/\'((?:[^\n\r\f\\'#]|#(?!\{)|\\#{NL}|#{ESCAPE})*)\'/
STRING_NOINTERP =
/#{STRING1_NOINTERP}|#{STRING2_NOINTERP}/
STATIC_VALUE =

Can't use IDENT here, because it seems to take exponential time on 1.8. We could use it for 1.9 only, but I don't want to introduce a cross-version behavior difference. In any case, almost all CSS idents will be matched by this.

We explicitly avoid parsing newlines or values/selectors longer than about 50 characters. This mitigates the problem of exponential parsing time when a value has a long string of valid, parsable content followed by something invalid.

/(-?#{NMSTART}|#{STRING_NOINTERP}|[ \t](?!%)|#[a-f0-9]|[,%]|#{NUM}|\!important){0,50}([;}])/i
STATIC_SELECTOR =
/(#{NMCHAR}|[ \t]|[,>+*]|[:#.]#{NMSTART}){0,50}([{])/i

Class Method Summary collapse

Class Method Details

.escape_ident(str) ⇒ String

Takes a string and returns a CSS identifier that will have the value of the given string.

Parameters:

  • str (String)

    The string to escape

Returns:

  • (String)

    The escaped string



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sass/scss/rx.rb', line 13

def self.escape_ident(str)
  return "" if str.empty?
  return "\\#{str}" if str == '-' || str == '_'
  out = ""
  value = str.dup
  out << value.slice!(0...1) if value =~ /^[-_]/
  if value[0...1] =~ NMSTART
    out << value.slice!(0...1)
  else
    out << escape_char(value.slice!(0...1))
  end
  out << value.gsub(/[^a-zA-Z0-9_-]/) {|c| escape_char c}
  return out
end