Module: TailwindMerge::ParseClassName

Included in:
Merger
Defined in:
lib/tailwind_merge/parse_class_name.rb

Constant Summary collapse

IMPORTANT_MODIFIER =
"!"
MODIFIER_SEPARATOR =
":"
MODIFIER_SEPARATOR_LENGTH =
MODIFIER_SEPARATOR.length
MODIFIER_SEPARATOR_BYTE =
MODIFIER_SEPARATOR.ord
POSTFIX_SEPARATOR_BYTE =
"/".ord
OPEN_BRACKET_BYTE =
"[".ord
CLOSE_BRACKET_BYTE =
"]".ord
OPEN_PAREN_BYTE =
"(".ord
CLOSE_PAREN_BYTE =
")".ord

Instance Method Summary collapse

Instance Method Details

#parse_class_name(class_name, prefix: nil) ⇒ Object

Parse class name into parts.

Inspired by splitAtTopLevelOnly used in Tailwind CSS



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tailwind_merge/parse_class_name.rb', line 24

def parse_class_name(class_name, prefix: nil)
  unless prefix.nil?
    full_prefix = "#{prefix}#{MODIFIER_SEPARATOR}"
    if class_name.start_with?(full_prefix)
      return parse_class_name(class_name[full_prefix.length..])
    else
      return TailwindClass.new(
        is_external: true,
        modifiers: [],
        has_important_modifier: false,
        base_class_name: class_name,
        maybe_postfix_modifier_position: nil,
      )
    end
  end

  modifiers = []

  bracket_depth = 0
  paren_depth = 0
  modifier_start = 0
  postfix_modifier_position = nil

  # Byte-wise scan: all separators are ASCII, so byte positions are safe
  # for multibyte class names (UTF-8 continuation bytes never match ASCII).
  # Positions produced here (including maybe_postfix_modifier_position)
  # are byte offsets and must be consumed with String#byteslice.
  index = 0
  size = class_name.bytesize
  while index < size
    byte = class_name.getbyte(index)

    if bracket_depth.zero? && paren_depth.zero?
      if byte == MODIFIER_SEPARATOR_BYTE
        modifiers << class_name.byteslice(modifier_start, index - modifier_start)
        modifier_start = index + MODIFIER_SEPARATOR_LENGTH
        index += 1
        next
      elsif byte == POSTFIX_SEPARATOR_BYTE
        postfix_modifier_position = index
        index += 1
        next
      end
    end

    case byte
    when OPEN_BRACKET_BYTE then bracket_depth += 1
    when CLOSE_BRACKET_BYTE then bracket_depth -= 1
    when OPEN_PAREN_BYTE then paren_depth += 1
    when CLOSE_PAREN_BYTE then paren_depth -= 1
    end

    index += 1
  end

  base_class_name_with_important_modifier = modifiers.empty? ? class_name : class_name.byteslice(modifier_start, size - modifier_start)

  base_class_name, has_important_modifier = strip_important_modifier(base_class_name_with_important_modifier)

  maybe_postfix_modifier_position = if postfix_modifier_position && postfix_modifier_position > modifier_start
    postfix_modifier_position - modifier_start
  end

  TailwindClass.new(
    is_external: false,
    modifiers:,
    has_important_modifier:,
    base_class_name: base_class_name,
    maybe_postfix_modifier_position:,
  )
end

#strip_important_modifier(base_class_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tailwind_merge/parse_class_name.rb', line 96

def strip_important_modifier(base_class_name)
  if base_class_name.end_with?(IMPORTANT_MODIFIER)
    return [base_class_name[0...-IMPORTANT_MODIFIER.length], true]
  end

  # In Tailwind CSS v3 the important modifier was at the start of the base class name. This is still supported for legacy reasons.
  # @see https://github.com/dcastil/tailwind-merge/issues/513#issuecomment-2614029864
  if base_class_name.start_with?(IMPORTANT_MODIFIER)
    return [base_class_name[1..], true]
  end

  [base_class_name, false]
end