Module: CSS::CodePoints

Included in:
Escape, Tokenizer
Defined in:
lib/css/code_points.rb

Overview

Character class predicates from CSS Syntax §4.2 Definitions, plus the U+FFFD replacement character used both during tokenization and serialization.

ASCII bytes are looked up in a precomputed boolean table (one Array access + one branch); non-ASCII code points (>= 0x80) are always ident-cp / ident-start per spec, so the helpers fall back to a single ‘c.ord >= 0x80` check. Avoids the chain of `String#<=>` calls a range-style predicate would dispatch.

Constant Summary collapse

REPLACEMENT =
"".freeze
DIGIT_TABLE =
build_table(0x30..0x39)
HEX_DIGIT_TABLE =
build_table(0x30..0x39, 0x41..0x46, 0x61..0x66)
IDENT_START_TABLE =
build_table(0x41..0x5A, 0x61..0x7A, 0x5F)
IDENT_CP_TABLE =
build_table(0x30..0x39, 0x41..0x5A, 0x61..0x7A, 0x5F, 0x2D)

Class Method Summary collapse

Class Method Details

.build_table(*ranges_or_ints) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/css/code_points.rb', line 14

def self.build_table(*ranges_or_ints)
  Array.new(128, false).tap {|a|
    ranges_or_ints.each {|r|
      if r.is_a?(Range) then r.each { a[it] = true }
      else                   a[r] = true
      end
    }
  }.freeze
end

.digit?(c) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/css/code_points.rb', line 31

def digit?(c)
  return false if c.nil?

  o = c.ord
  o < 128 && DIGIT_TABLE[o]
end

.hex_digit?(c) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/css/code_points.rb', line 38

def hex_digit?(c)
  return false if c.nil?

  o = c.ord
  o < 128 && HEX_DIGIT_TABLE[o]
end

.ident_code_point?(c) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/css/code_points.rb', line 52

def ident_code_point?(c)
  return false if c.nil?

  o = c.ord
  o >= 128 || IDENT_CP_TABLE[o]
end

.ident_start_code_point?(c) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/css/code_points.rb', line 45

def ident_start_code_point?(c)
  return false if c.nil?

  o = c.ord
  o >= 128 || IDENT_START_TABLE[o]
end