Skip to content

Commit 2656cc7

Browse files
committed
Add basic tests for CSSStyleSheet
1 parent ba94818 commit 2656cc7

File tree

4 files changed

+86
-49
lines changed

4 files changed

+86
-49
lines changed

src/browser/cssom/css_rule.zig

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,3 @@ pub const CSSImportRule = struct {
4343
style_sheet: CSSStyleSheet,
4444
supports_text: ?[]const u8,
4545
};
46-
47-
// pub const CSSGroupingRule = struct {
48-
// pub const prototype = *CSSRule;
49-
// list: std.ArrayListUnmanaged(CSSRule),
50-
51-
// pub fn _insertRule(self: *CSSGroupingRule, rule: []const u8, _index: ?usize, page: *Page) !usize {
52-
// const index = _index orelse 0;
53-
// if (index > self.list.items.len) return error.IndexSizeError;
54-
55-
// const css_rule: CSSRule = .{ .css_text = rule, .parent_rule = null, .parent_stylesheet = null };
56-
// try self.list.insert(page.arena, index, css_rule);
57-
// return self.list.items.len;
58-
// }
59-
60-
// pub fn _deleteRule(self: *CSSGroupingRule, index: usize) !void {
61-
// if (index > self.list.items.len) return error.IndexSizeError;
62-
// _ = self.list.orderedRemove(index);
63-
// }
64-
// };
65-
66-
// pub const CSSStyleRule = struct {
67-
// pub const prototype = *CSSGroupingRule;
68-
// selector_text: []const u8,
69-
// style: CSSStyleDeclaration,
70-
// };
71-
72-
// pub const CSSFontFaceRule = struct {
73-
// pub const prototype = *CSSRule;
74-
// };
75-
76-
// pub const CSSPageRule = struct {
77-
// pub const prototype = *CSSGroupingRule;
78-
// selector_text: []const u8,
79-
// style: CSSStyleDeclaration,
80-
// };
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <francis@lightpanda.io>
4+
// Pierre Tachoire <pierre@lightpanda.io>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
21+
const Page = @import("../page.zig").Page;
22+
const StyleSheet = @import("stylesheet.zig").StyleSheet;
23+
24+
const CSSRule = @import("css_rule.zig").CSSRule;
25+
const CSSImportRule = @import("css_rule.zig").CSSImportRule;
26+
27+
pub const CSSRuleList = struct {
28+
list: std.ArrayListUnmanaged([]const u8),
29+
30+
pub fn constructor() CSSRuleList {
31+
return .{ .list = .empty };
32+
}
33+
34+
pub fn _item(self: *CSSRuleList, _index: u32) ?CSSRule {
35+
const index: usize = @intCast(_index);
36+
37+
if (index > self.list.items.len) {
38+
return null;
39+
}
40+
41+
// todo: for now, just return null.
42+
// this depends on properly parsing CSSRule
43+
return null;
44+
}
45+
46+
pub fn get_length(self: *CSSRuleList) u32 {
47+
return @intCast(self.list.items.len);
48+
}
49+
};
50+
51+
const testing = @import("../../testing.zig");
52+
test "Browser.CSS.CSSRuleList" {
53+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
54+
defer runner.deinit();
55+
56+
try runner.testCases(&.{
57+
.{ "let list = new CSSRuleList()", "undefined" },
58+
.{ "list instanceof CSSRuleList", "true" },
59+
.{ "list.length", "0" },
60+
.{ "list.item(0)", "null" },
61+
}, .{});
62+
}

src/browser/cssom/css_stylesheet.zig

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,57 @@ const std = @import("std");
2121
const Page = @import("../page.zig").Page;
2222
const StyleSheet = @import("stylesheet.zig").StyleSheet;
2323

24-
const CSSRule = @import("css_rule.zig").CSSRule;
24+
const CSSRuleList = @import("css_rule_list.zig").CSSRuleList;
2525
const CSSImportRule = @import("css_rule.zig").CSSImportRule;
2626

2727
pub const CSSStyleSheet = struct {
2828
pub const prototype = *StyleSheet;
2929

30-
// TODO: For now, we won't parse any rules.
31-
css_rules: std.ArrayListUnmanaged([]const u8),
32-
33-
// TODO: Support owner_rule here.
30+
css_rules: *CSSRuleList,
31+
owner_rule: ?*CSSImportRule,
3432

3533
const CSSStyleSheetOpts = struct {
36-
base_url: ?[]const u8,
34+
base_url: ?[]const u8 = null,
3735
// TODO: Suupport media
3836
disabled: bool = false,
3937
};
4038

41-
pub fn constructor(_opts: ?CSSStyleSheetOpts) CSSStyleSheet {
39+
pub fn constructor(_opts: ?CSSStyleSheetOpts, page: *Page) !CSSStyleSheet {
4240
const opts = _opts orelse CSSStyleSheetOpts{};
4341
_ = opts;
4442

45-
return .{ .css_rules = .empty, .owner_rule = null };
43+
const arena = page.arena;
44+
const rules = try arena.create(CSSRuleList);
45+
rules.* = .constructor();
46+
47+
return .{ .css_rules = rules, .owner_rule = null };
4648
}
4749

4850
pub fn get_ownerRule(_: *CSSStyleSheet) ?*CSSImportRule {
4951
return null;
5052
}
5153

52-
pub fn get_cssRules(self: *CSSStyleSheet) *std.ArrayListUnmanaged([]const u8) {
54+
pub fn get_cssRules(self: *CSSStyleSheet) *CSSRuleList {
5355
return self.css_rules;
5456
}
5557

5658
pub fn _insertRule(self: *CSSStyleSheet, rule: []const u8, _index: ?usize, page: *Page) !usize {
5759
const index = _index orelse 0;
58-
if (index > self.css_rules.items.len) {
60+
if (index > self.css_rules.list.items.len) {
5961
return error.IndexSize;
6062
}
6163

6264
const arena = page.arena;
63-
try self.css_rules.insert(arena, index, arena.dupe(u8, rule));
65+
try self.css_rules.list.insert(arena, index, try arena.dupe(u8, rule));
6466
return index;
6567
}
6668

6769
pub fn _deleteRule(self: *CSSStyleSheet, index: usize) !void {
68-
if (index > self.css_rules.items.len) {
70+
if (index > self.css_rules.list.items.len) {
6971
return error.IndexSize;
7072
}
7173

72-
_ = self.css_rules.orderedRemove(index);
74+
_ = self.css_rules.list.orderedRemove(index);
7375
}
7476
};
7577

@@ -79,6 +81,12 @@ test "Browser.CSS.StyleSheet" {
7981
defer runner.deinit();
8082

8183
try runner.testCases(&.{
82-
.{ "let css = new CSSStylesheet()", "" },
84+
.{ "let css = new CSSStyleSheet()", "undefined" },
85+
.{ "css instanceof CSSStyleSheet", "true" },
86+
.{ "css.cssRules.length", "0" },
87+
.{ "css.ownerRule", "null" },
88+
.{ "let index1 = css.insertRule('body { color: red; }', 0)", "undefined" },
89+
.{ "index1", "0" },
90+
.{ "css.cssRules.length", "1" },
8391
}, .{});
8492
}

src/browser/cssom/cssom.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
pub const Stylesheet = @import("stylesheet.zig").StyleSheet;
2020
pub const CSSStylesheet = @import("css_stylesheet.zig").CSSStyleSheet;
2121
pub const CSSStyleDeclaration = @import("css_style_declaration.zig").CSSStyleDeclaration;
22+
pub const CSSRuleList = @import("css_rule_list.zig").CSSRuleList;
2223

2324
pub const Interfaces = .{
2425
Stylesheet,
2526
CSSStylesheet,
2627
CSSStyleDeclaration,
28+
CSSRuleList,
2729
@import("css_rule.zig").Interfaces,
2830
};

0 commit comments

Comments
 (0)