Integrate CSS with bundler (#14281)

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Zack Radisic <zackradisic@Mac.attlocal.net>
Co-authored-by: zackradisic <zackradisic@users.noreply.github.com>
Co-authored-by: Zack Radisic <zackradisic@Zacks-MBP.attlocal.net>
This commit is contained in:
Zack Radisic
2024-10-04 20:23:10 -07:00
committed by GitHub
parent 3ab3dec34d
commit a01f9d8e1b
49 changed files with 3803 additions and 1455 deletions

View File

@@ -11,6 +11,7 @@ const Calc = css.css_values.calc.Calc;
const DimensionPercentage = css.css_values.percentage.DimensionPercentage;
const LengthPercentage = css.css_values.length.LengthPercentage;
const Length = css.css_values.length.Length;
const LengthOrNumber = css.css_values.length.LengthOrNumber;
const Percentage = css.css_values.percentage.Percentage;
const CssColor = css.css_values.color.CssColor;
const Image = css.css_values.image.Image;
@@ -24,12 +25,25 @@ const CustomIdent = css.css_values.ident.CustomIdent;
const CustomIdentFns = css.css_values.ident.CustomIdentFns;
const Ident = css.css_values.ident.Ident;
fn needsDeinit(comptime T: type) bool {
return switch (T) {
f32, i32, u32, []const u8 => false,
LengthPercentage => true,
LengthOrNumber => true,
css.css_values.percentage.NumberOrPercentage => false,
css.css_properties.border_image.BorderImageSideWidth => true,
*const css.css_values.percentage.DimensionPercentage(css.css_values.length.LengthValue) => true,
else => @compileError("Don't know if " ++ @typeName(T) ++ " needs deinit. Please add it to this switch statement."),
};
}
/// A generic value that represents a value for four sides of a box,
/// e.g. border-width, margin, padding, etc.
///
/// When serialized, as few components as possible are written when
/// there are duplicate values.
pub fn Rect(comptime T: type) type {
const needs_deinit = needsDeinit(T);
return struct {
/// The top component.
top: T,
@@ -42,11 +56,41 @@ pub fn Rect(comptime T: type) type {
const This = @This();
pub fn eql(this: *const This, other: *const This) bool {
return css.generic.eql(T, &this.top, &other.top) and css.generic.eql(T, &this.right, &other.right) and css.generic.eql(T, &this.bottom, &other.bottom) and css.generic.eql(T, &this.left, &other.left);
}
pub fn deepClone(this: *const This, allocator: std.mem.Allocator) This {
if (comptime needs_deinit or T == *const css.css_values.percentage.DimensionPercentage(css.css_values.length.LengthValue)) {
return This{
.top = this.top.deepClone(allocator),
.right = this.right.deepClone(allocator),
.bottom = this.bottom.deepClone(allocator),
.left = this.left.deepClone(allocator),
};
}
return This{
.top = this.top,
.right = this.right,
.bottom = this.bottom,
.left = this.left,
};
}
pub fn deinit(this: *const This, allocator: std.mem.Allocator) void {
if (comptime needs_deinit) {
this.top.deinit(allocator);
this.right.deinit(allocator);
this.bottom.deinit(allocator);
this.left.deinit(allocator);
}
}
pub fn parse(input: *css.Parser) Result(This) {
return This.parseWith(input, valParse);
}
pub fn parseWith(input: *css.Parser, comptime parse_fn: *const fn (*css.Parser) Result(T)) Result(This) {
pub fn parseWith(input: *css.Parser, comptime parse_fn: anytype) Result(This) {
const first = switch (parse_fn(input)) {
.result => |vv| vv,
.err => |e| return .{ .err = e },
@@ -59,12 +103,12 @@ pub fn Rect(comptime T: type) type {
const third = switch (input.tryParse(parse_fn, .{})) {
.result => |v| v,
// <first> <second>
.err => return This{ .top = first, .right = second, .bottom = first, .left = second },
.err => return .{ .result = This{ .top = first, .right = second, .bottom = first, .left = second } },
};
const fourth = switch (input.tryParse(parse_fn, .{})) {
.result => |v| v,
// <first> <second> <third>
.err => return This{ .top = first, .right = second, .bottom = third, .left = second },
.err => return .{ .result = This{ .top = first, .right = second, .bottom = third, .left = second } },
};
// <first> <second> <third> <fourth>
return .{ .result = This{ .top = first, .right = second, .bottom = third, .left = fourth } };