codebase refactoring (split utils across files)
This commit is contained in:
@@ -179,57 +179,57 @@ func parse_utility_class(rule: CSSRule, utility_name: String) -> void:
|
||||
static func parse_utility_class_internal(rule: CSSRule, utility_name: String) -> void:
|
||||
# Handle color classes like text-[#ff0000]
|
||||
if utility_name.begins_with("text-[") and utility_name.ends_with("]"):
|
||||
var color_value = extract_bracket_content(utility_name, 5) # after 'text-'
|
||||
rule.properties["color"] = parse_color(color_value)
|
||||
var color_value = SizeUtils.extract_bracket_content(utility_name, 5) # after 'text-'
|
||||
rule.properties["color"] = ColorUtils.parse_color(color_value)
|
||||
return
|
||||
|
||||
# Handle standard text color classes like text-white, text-black, etc.
|
||||
# But exclude text alignment classes
|
||||
if utility_name.begins_with("text-") and not utility_name in ["text-left", "text-center", "text-right", "text-justify"]:
|
||||
var color_name = utility_name.substr(5) # after 'text-'
|
||||
var color = get_color(color_name)
|
||||
var color = ColorUtils.get_color(color_name)
|
||||
if color != null:
|
||||
rule.properties["color"] = color
|
||||
return
|
||||
|
||||
# Handle background color classes like bg-[#ff0000]
|
||||
if utility_name.begins_with("bg-[") and utility_name.ends_with("]"):
|
||||
var color_value = extract_bracket_content(utility_name, 3) # after 'bg-'
|
||||
var color = parse_color(color_value)
|
||||
var color_value = SizeUtils.extract_bracket_content(utility_name, 3) # after 'bg-'
|
||||
var color = ColorUtils.parse_color(color_value)
|
||||
rule.properties["background-color"] = color
|
||||
return
|
||||
|
||||
# Handle standard background color classes like bg-white, bg-black, etc.
|
||||
if utility_name.begins_with("bg-"):
|
||||
var color_name = utility_name.substr(3) # after 'bg-'
|
||||
var color = get_color(color_name)
|
||||
var color = ColorUtils.get_color(color_name)
|
||||
if color != null:
|
||||
rule.properties["background-color"] = color
|
||||
return
|
||||
|
||||
# e.g. max-w-[123px], w-[50%], h-[2rem]
|
||||
if utility_name.match("^max-w-\\[.*\\]$"):
|
||||
var val = extract_bracket_content(utility_name, 6)
|
||||
var val = SizeUtils.extract_bracket_content(utility_name, 6)
|
||||
rule.properties["max-width"] = val
|
||||
return
|
||||
if utility_name.match("^max-h-\\[.*\\]$"):
|
||||
var val = extract_bracket_content(utility_name, 6)
|
||||
var val = SizeUtils.extract_bracket_content(utility_name, 6)
|
||||
rule.properties["max-height"] = val
|
||||
return
|
||||
if utility_name.match("^min-w-\\[.*\\]$"):
|
||||
var val = extract_bracket_content(utility_name, 6)
|
||||
var val = SizeUtils.extract_bracket_content(utility_name, 6)
|
||||
rule.properties["min-width"] = val
|
||||
return
|
||||
if utility_name.match("^min-h-\\[.*\\]$"):
|
||||
var val = extract_bracket_content(utility_name, 6)
|
||||
var val = SizeUtils.extract_bracket_content(utility_name, 6)
|
||||
rule.properties["min-height"] = val
|
||||
return
|
||||
if utility_name.match("^w-\\[.*\\]$"):
|
||||
var val = extract_bracket_content(utility_name, 2)
|
||||
var val = SizeUtils.extract_bracket_content(utility_name, 2)
|
||||
rule.properties["width"] = val
|
||||
return
|
||||
if utility_name.match("^h-\\[.*\\]$"):
|
||||
var val = extract_bracket_content(utility_name, 2)
|
||||
var val = SizeUtils.extract_bracket_content(utility_name, 2)
|
||||
rule.properties["height"] = val
|
||||
return
|
||||
|
||||
@@ -277,34 +277,34 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) ->
|
||||
var val = utility_name.substr(2)
|
||||
if val.begins_with("[") and val.ends_with("]"):
|
||||
val = val.substr(1, val.length() - 2)
|
||||
rule.properties["width"] = parse_size(val)
|
||||
rule.properties["width"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
# Height
|
||||
if utility_name.begins_with("h-"):
|
||||
var val = utility_name.substr(2)
|
||||
if val.begins_with("[") and val.ends_with("]"):
|
||||
val = val.substr(1, val.length() - 2)
|
||||
rule.properties["height"] = parse_size(val)
|
||||
rule.properties["height"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
# Min width
|
||||
if utility_name.begins_with("min-w-"):
|
||||
var val = utility_name.substr(6)
|
||||
rule.properties["min-width"] = parse_size(val)
|
||||
rule.properties["min-width"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
# Min height
|
||||
if utility_name.begins_with("min-h-"):
|
||||
var val = utility_name.substr(6)
|
||||
rule.properties["min-height"] = parse_size(val)
|
||||
rule.properties["min-height"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
# Max width
|
||||
if utility_name.begins_with("max-w-"):
|
||||
var val = utility_name.substr(6)
|
||||
rule.properties["max-width"] = parse_size(val)
|
||||
rule.properties["max-width"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
# Max height
|
||||
if utility_name.begins_with("max-h-"):
|
||||
var val = utility_name.substr(6)
|
||||
rule.properties["max-height"] = parse_size(val)
|
||||
rule.properties["max-height"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
|
||||
# Flex container
|
||||
@@ -357,15 +357,15 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) ->
|
||||
# Gap
|
||||
if utility_name.begins_with("gap-"):
|
||||
var val = utility_name.substr(4)
|
||||
rule.properties["gap"] = parse_size(val)
|
||||
rule.properties["gap"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
if utility_name.begins_with("row-gap-"):
|
||||
var val = utility_name.substr(8)
|
||||
rule.properties["row-gap"] = parse_size(val)
|
||||
rule.properties["row-gap"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
if utility_name.begins_with("col-gap-"):
|
||||
var val = utility_name.substr(8)
|
||||
rule.properties["column-gap"] = parse_size(val)
|
||||
rule.properties["column-gap"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
|
||||
# FLEX ITEM PROPERTIES
|
||||
@@ -379,7 +379,7 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) ->
|
||||
return
|
||||
if utility_name.begins_with("basis-"):
|
||||
var val = utility_name.substr(6)
|
||||
rule.properties["flex-basis"] = parse_size(val)
|
||||
rule.properties["flex-basis"] = SizeUtils.parse_size(val)
|
||||
return
|
||||
|
||||
# Align self
|
||||
@@ -404,39 +404,39 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) ->
|
||||
# Handle padding classes like p-8, px-4, py-2, etc.
|
||||
if utility_name.begins_with("p-"):
|
||||
var val = utility_name.substr(2)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding"] = padding_value
|
||||
return
|
||||
if utility_name.begins_with("px-"):
|
||||
var val = utility_name.substr(3)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding-left"] = padding_value
|
||||
rule.properties["padding-right"] = padding_value
|
||||
return
|
||||
if utility_name.begins_with("py-"):
|
||||
var val = utility_name.substr(3)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding-top"] = padding_value
|
||||
rule.properties["padding-bottom"] = padding_value
|
||||
return
|
||||
if utility_name.begins_with("pt-"):
|
||||
var val = utility_name.substr(3)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding-top"] = padding_value
|
||||
return
|
||||
if utility_name.begins_with("pr-"):
|
||||
var val = utility_name.substr(3)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding-right"] = padding_value
|
||||
return
|
||||
if utility_name.begins_with("pb-"):
|
||||
var val = utility_name.substr(3)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding-bottom"] = padding_value
|
||||
return
|
||||
if utility_name.begins_with("pl-"):
|
||||
var val = utility_name.substr(3)
|
||||
var padding_value = parse_size(val)
|
||||
var padding_value = SizeUtils.parse_size(val)
|
||||
rule.properties["padding-left"] = padding_value
|
||||
return
|
||||
|
||||
@@ -471,7 +471,7 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) ->
|
||||
|
||||
# Handle custom border radius like rounded-[12px]
|
||||
if utility_name.begins_with("rounded-[") and utility_name.ends_with("]"):
|
||||
var radius_value = extract_bracket_content(utility_name, 8) # after 'rounded-'
|
||||
var radius_value = SizeUtils.extract_bracket_content(utility_name, 8) # after 'rounded-'
|
||||
rule.properties["border-radius"] = radius_value
|
||||
return
|
||||
|
||||
@@ -485,72 +485,6 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) ->
|
||||
# Handle more utility classes as needed
|
||||
# Add more cases here for other utilities
|
||||
|
||||
static func parse_size(val: String) -> String:
|
||||
var named = {
|
||||
"0": "0px", "1": "4px", "2": "8px", "3": "12px", "4": "16px", "5": "20px", "6": "24px", "8": "32px", "10": "40px",
|
||||
"12": "48px", "16": "64px", "20": "80px", "24": "96px", "28": "112px", "32": "128px", "36": "144px", "40": "160px",
|
||||
"44": "176px", "48": "192px", "52": "208px", "56": "224px", "60": "240px", "64": "256px", "72": "288px", "80": "320px", "96": "384px",
|
||||
"3xs": "256px", "2xs": "288px", "xs": "320px", "sm": "384px", "md": "448px", "lg": "512px",
|
||||
"xl": "576px", "2xl": "672px", "3xl": "768px", "4xl": "896px", "5xl": "1024px", "6xl": "1152px", "7xl": "1280px"
|
||||
}
|
||||
if named.has(val):
|
||||
return named[val]
|
||||
# Fractional (e.g. 1/2, 1/3)
|
||||
if val.find("/") != -1:
|
||||
var parts = val.split("/")
|
||||
if parts.size() == 2 and parts[1].is_valid_int() and parts[0].is_valid_int():
|
||||
var frac = float(parts[0]) / float(parts[1])
|
||||
return str(frac * 100.0) + "%"
|
||||
if val.is_valid_int():
|
||||
return str(int(val) * 16) + "px"
|
||||
return val
|
||||
|
||||
# Helper to extract content inside first matching brackets after a given index
|
||||
static func extract_bracket_content(string: String, start_idx: int) -> String:
|
||||
var open_idx = string.find("[", start_idx)
|
||||
if open_idx == -1:
|
||||
return ""
|
||||
var close_idx = string.find("]", open_idx)
|
||||
if close_idx == -1:
|
||||
return ""
|
||||
return string.substr(open_idx + 1, close_idx - open_idx - 1)
|
||||
|
||||
static func parse_color(color_string: String) -> Color:
|
||||
color_string = color_string.strip_edges()
|
||||
|
||||
# Handle hex colors
|
||||
if color_string.begins_with("#"):
|
||||
return Color.from_string(color_string, Color.WHITE)
|
||||
|
||||
# Handle rgb/rgba
|
||||
if color_string.begins_with("rgb"):
|
||||
var regex = RegEx.new()
|
||||
regex.compile("rgba?\\(([^)]+)\\)")
|
||||
var result = regex.search(color_string)
|
||||
if result:
|
||||
var values = result.get_string(1).split(",")
|
||||
if values.size() >= 3:
|
||||
var r = values[0].strip_edges().to_float() / 255.0
|
||||
var g = values[1].strip_edges().to_float() / 255.0
|
||||
var b = values[2].strip_edges().to_float() / 255.0
|
||||
var a = 1.0
|
||||
if values.size() >= 4:
|
||||
a = values[3].strip_edges().to_float()
|
||||
return Color(r, g, b, a)
|
||||
|
||||
# Handle named colors
|
||||
# TODO: map to actual Tailwind colors
|
||||
match color_string.to_lower():
|
||||
"red": return Color.RED
|
||||
"green": return Color.GREEN
|
||||
"blue": return Color.BLUE
|
||||
"white": return Color.WHITE
|
||||
"black": return Color.BLACK
|
||||
"yellow": return Color.YELLOW
|
||||
"cyan": return Color.CYAN
|
||||
"magenta": return Color.MAGENTA
|
||||
_: return Color.from_string(color_string, Color.WHITE)
|
||||
|
||||
static func parse_inline_style(style_string: String) -> Dictionary:
|
||||
var rule = CSSRule.new()
|
||||
rule.selector = ""
|
||||
@@ -564,116 +498,3 @@ static func parse_inline_style(style_string: String) -> Dictionary:
|
||||
parse_utility_class_internal(rule, utility_name)
|
||||
|
||||
return rule.properties
|
||||
|
||||
# TODO: probably a better idea to precompile the patterns, though idk how much more efficient that would be, if at all
|
||||
static func is_utility_class(cls: String) -> bool:
|
||||
var utility_patterns = [
|
||||
"^text-(xs|sm|base|lg|xl|2xl|3xl|4xl|5xl|6xl)$", # font sizes
|
||||
"^text-(left|center|right|justify)$", # text alignment
|
||||
"^text-\\[.*\\]$", # custom text colors
|
||||
"^text-(white|black|transparent|slate-\\d+|gray-\\d+|red-\\d+|green-\\d+|blue-\\d+|yellow-\\d+)$", # text colors
|
||||
"^bg-\\[.*\\]$", # custom bg colors
|
||||
"^bg-(white|black|transparent|slate-\\d+|gray-\\d+|red-\\d+|green-\\d+|blue-\\d+|yellow-\\d+)$", # bg colors
|
||||
"^(w|h|min-w|min-h|max-w|max-h)-", # sizing
|
||||
"^font-(bold|mono|italic)$", # font styles
|
||||
"^underline$",
|
||||
"^flex", # flex utilities
|
||||
"^items-", # align items
|
||||
"^justify-", # justify content
|
||||
"^content-", # align content
|
||||
"^self-", # align self
|
||||
"^order-", # order
|
||||
"^gap-", # gap
|
||||
"^(p|px|py|pt|pr|pb|pl)-", # padding
|
||||
"^rounded", # border radius
|
||||
"^basis-", # flex basis
|
||||
"^(hover|active):", # pseudo classes
|
||||
]
|
||||
|
||||
for pattern in utility_patterns:
|
||||
var regex = RegEx.new()
|
||||
regex.compile(pattern)
|
||||
if regex.search(cls):
|
||||
return true
|
||||
|
||||
return false
|
||||
|
||||
static func get_color(color_name: String) -> Color:
|
||||
# Common colors
|
||||
match color_name:
|
||||
"white": return Color.WHITE
|
||||
"black": return Color.BLACK
|
||||
"transparent": return Color.TRANSPARENT
|
||||
|
||||
# Gray scale
|
||||
"slate-50": return Color.from_string("#f8fafc", Color.WHITE)
|
||||
"slate-100": return Color.from_string("#f1f5f9", Color.WHITE)
|
||||
"slate-200": return Color.from_string("#e2e8f0", Color.WHITE)
|
||||
"slate-300": return Color.from_string("#cbd5e1", Color.WHITE)
|
||||
"slate-400": return Color.from_string("#94a3b8", Color.WHITE)
|
||||
"slate-500": return Color.from_string("#64748b", Color.WHITE)
|
||||
"slate-600": return Color.from_string("#475569", Color.WHITE)
|
||||
"slate-700": return Color.from_string("#334155", Color.WHITE)
|
||||
"slate-800": return Color.from_string("#1e293b", Color.WHITE)
|
||||
"slate-900": return Color.from_string("#0f172a", Color.WHITE)
|
||||
|
||||
"gray-50": return Color.from_string("#f9fafb", Color.WHITE)
|
||||
"gray-100": return Color.from_string("#f3f4f6", Color.WHITE)
|
||||
"gray-200": return Color.from_string("#e5e7eb", Color.WHITE)
|
||||
"gray-300": return Color.from_string("#d1d5db", Color.WHITE)
|
||||
"gray-400": return Color.from_string("#9ca3af", Color.WHITE)
|
||||
"gray-500": return Color.from_string("#6b7280", Color.WHITE)
|
||||
"gray-600": return Color.from_string("#4b5563", Color.WHITE)
|
||||
"gray-700": return Color.from_string("#374151", Color.WHITE)
|
||||
"gray-800": return Color.from_string("#1f2937", Color.WHITE)
|
||||
"gray-900": return Color.from_string("#111827", Color.WHITE)
|
||||
|
||||
# Red
|
||||
"red-50": return Color.from_string("#fef2f2", Color.WHITE)
|
||||
"red-100": return Color.from_string("#fee2e2", Color.WHITE)
|
||||
"red-200": return Color.from_string("#fecaca", Color.WHITE)
|
||||
"red-300": return Color.from_string("#fca5a5", Color.WHITE)
|
||||
"red-400": return Color.from_string("#f87171", Color.WHITE)
|
||||
"red-500": return Color.from_string("#ef4444", Color.WHITE)
|
||||
"red-600": return Color.from_string("#dc2626", Color.WHITE)
|
||||
"red-700": return Color.from_string("#b91c1c", Color.WHITE)
|
||||
"red-800": return Color.from_string("#991b1b", Color.WHITE)
|
||||
"red-900": return Color.from_string("#7f1d1d", Color.WHITE)
|
||||
|
||||
# Green
|
||||
"green-50": return Color.from_string("#f0fdf4", Color.WHITE)
|
||||
"green-100": return Color.from_string("#dcfce7", Color.WHITE)
|
||||
"green-200": return Color.from_string("#bbf7d0", Color.WHITE)
|
||||
"green-300": return Color.from_string("#86efac", Color.WHITE)
|
||||
"green-400": return Color.from_string("#4ade80", Color.WHITE)
|
||||
"green-500": return Color.from_string("#22c55e", Color.WHITE)
|
||||
"green-600": return Color.from_string("#16a34a", Color.WHITE)
|
||||
"green-700": return Color.from_string("#15803d", Color.WHITE)
|
||||
"green-800": return Color.from_string("#166534", Color.WHITE)
|
||||
"green-900": return Color.from_string("#14532d", Color.WHITE)
|
||||
|
||||
# Blue
|
||||
"blue-50": return Color.from_string("#eff6ff", Color.WHITE)
|
||||
"blue-100": return Color.from_string("#dbeafe", Color.WHITE)
|
||||
"blue-200": return Color.from_string("#bfdbfe", Color.WHITE)
|
||||
"blue-300": return Color.from_string("#93c5fd", Color.WHITE)
|
||||
"blue-400": return Color.from_string("#60a5fa", Color.WHITE)
|
||||
"blue-500": return Color.from_string("#3b82f6", Color.WHITE)
|
||||
"blue-600": return Color.from_string("#2563eb", Color.WHITE)
|
||||
"blue-700": return Color.from_string("#1d4ed8", Color.WHITE)
|
||||
"blue-800": return Color.from_string("#1e40af", Color.WHITE)
|
||||
"blue-900": return Color.from_string("#1e3a8a", Color.WHITE)
|
||||
|
||||
# Yellow
|
||||
"yellow-50": return Color.from_string("#fefce8", Color.WHITE)
|
||||
"yellow-100": return Color.from_string("#fef9c3", Color.WHITE)
|
||||
"yellow-200": return Color.from_string("#fef08a", Color.WHITE)
|
||||
"yellow-300": return Color.from_string("#fde047", Color.WHITE)
|
||||
"yellow-400": return Color.from_string("#facc15", Color.WHITE)
|
||||
"yellow-500": return Color.from_string("#eab308", Color.WHITE)
|
||||
"yellow-600": return Color.from_string("#ca8a04", Color.WHITE)
|
||||
"yellow-700": return Color.from_string("#a16207", Color.WHITE)
|
||||
"yellow-800": return Color.from_string("#854d0e", Color.WHITE)
|
||||
"yellow-900": return Color.from_string("#713f12", Color.WHITE)
|
||||
|
||||
_: return Color.BLACK
|
||||
|
||||
@@ -206,7 +206,7 @@ func extract_class_names_from_style(element: HTMLElement) -> Array[String]:
|
||||
var style_tokens = style_attr.split(" ")
|
||||
for token in style_tokens:
|
||||
token = token.strip_edges()
|
||||
if token.length() > 0 and not CSSParser.is_utility_class(token):
|
||||
if token.length() > 0 and not UtilityClassValidator.is_utility_class(token):
|
||||
class_names.append(token)
|
||||
return class_names
|
||||
|
||||
|
||||
Reference in New Issue
Block a user