Module | Sass::SCSS::RX
In: | lib/sass/scss/rx.rb

A module containing regular expressions used for lexing tokens in an SCSS document. Most of these are taken from [the CSS3 spec](www.w3.org/TR/css3-syntax/#lexical), although some have been modified for various reasons.

### Methods

escape_char escape_ident quote

### Constants

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]*/
RANGE | = | /(?:#{H}|\?){1,6}/ |  | This is more liberal than the spec‘s definition, but that definition didn‘t work well with the greediness rules
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}\+/ |  | Defined in www.w3.org/TR/css3-selectors/#lex
GREATER | = | /#{W}>/
TILDE | = | /#{W}~/
NOT | = | quote(":not(", Regexp::IGNORECASE)
HEXCOLOR | = | /\#[0-9a-fA-F]+/ |  | Custom
INTERP_START | = | /#\{/
STRING1_NOINTERP | = | /\"((?:[^\n\r\f\\"#]|#(?!\{)|\\#{NL}|#{ESCAPE})*)\"/
STRING2_NOINTERP | = | /\'((?:[^\n\r\f\\'#]|#(?!\{)|\\#{NL}|#{ESCAPE})*)\'/
STRING_NOINTERP | = | /#{STRING1_NOINTERP}|#{STRING2_NOINTERP}/
STATIC_VALUE | = | /(-?#{NMSTART}|#{STRING_NOINTERP}|\s(?!%)|#[a-f0-9]|[,%]|#{NUM}|\!important)+(?=[;}])/i |  | 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.
STATIC_SELECTOR | = | /(#{NMCHAR}|\s|[,>+*]|[:#.]#{NMSTART})+(?=[{])/i

### Public Class methods

escape_char(c)

Escapes a single character for a CSS identifier.

@param c [String] The character to escape. Should have length 1 @return [String] The escaped character @private

[Source]

        # File lib/sass/scss/rx.rb, line 33
    33:       def self.escape_char(c)
    34:         return "\\%06x" % Haml::Util.ord(c) unless c =~ /[ -\/:-~]/
    35:         return "\\#{c}"
    36:       end

escape_ident(str)

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

@param str [String] The string to escape @return [String] The escaped string

[Source]

        # File lib/sass/scss/rx.rb, line 13
    13:       def self.escape_ident(str)
    14:         return "" if str.empty?
    15:         return "\\#{str}" if str == '-' || str == '_'
    16:         out = ""
    17:         value = str.dup
    18:         out << value.slice!(0...1) if value =~ /^[-_]/
    19:         if value[0...1] =~ NMSTART
    20:           out << value.slice!(0...1)
    21:         else
    22:           out << escape_char(value.slice!(0...1))
    23:         end
    24:         out << value.gsub(/[^a-zA-Z0-9_-]/) {|c| escape_char c}
    25:         return out
    26:       end

quote(str, flags = 0)

Creates a Regexp from a plain text string, escaping all significant characters.

@param str [String] The text of the regexp @param flags [Fixnum] Flags for the created regular expression @return [Regexp] @private

[Source]

        # File lib/sass/scss/rx.rb, line 45
    45:       def self.quote(str, flags = 0)
    46:         Regexp.new(Regexp.quote(str), flags)
    47:       end

[Validate]