Skip to content

Commit ba94818

Browse files
committed
add CSSStyleSheet
1 parent ac759a6 commit ba94818

File tree

6 files changed

+249
-8
lines changed

6 files changed

+249
-8
lines changed

src/browser/cssom/css_rule.zig

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
23+
const CSSStyleDeclaration = @import("css_style_declaration.zig").CSSStyleDeclaration;
24+
const CSSStyleSheet = @import("css_stylesheet.zig").CSSStyleSheet;
25+
26+
pub const Interfaces = .{
27+
CSSRule,
28+
CSSImportRule,
29+
};
30+
31+
// https://developer.mozilla.org/en-US/docs/Web/API/CSSRule
32+
pub const CSSRule = struct {
33+
css_text: []const u8,
34+
parent_rule: ?*CSSRule = null,
35+
parent_stylesheet: ?*CSSStyleSheet = null,
36+
};
37+
38+
pub const CSSImportRule = struct {
39+
pub const prototype = *CSSRule;
40+
href: []const u8,
41+
layer_name: ?[]const u8,
42+
media: void,
43+
style_sheet: CSSStyleSheet,
44+
supports_text: ?[]const u8,
45+
};
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+
// };

src/browser/cssom/css_style_declaration.zig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ const std = @import("std");
2020

2121
const CSSParser = @import("./css_parser.zig").CSSParser;
2222
const CSSValueAnalyzer = @import("./css_value_analyzer.zig").CSSValueAnalyzer;
23+
const CSSRule = @import("css_rule.zig").CSSRule;
2324
const Page = @import("../page.zig").Page;
2425

25-
pub const Interfaces = .{
26-
CSSStyleDeclaration,
27-
CSSRule,
28-
};
29-
30-
const CSSRule = struct {};
31-
3226
pub const CSSStyleDeclaration = struct {
3327
store: std.StringHashMapUnmanaged(Property),
3428
order: std.ArrayListUnmanaged([]const u8),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 CSSStyleSheet = struct {
28+
pub const prototype = *StyleSheet;
29+
30+
// TODO: For now, we won't parse any rules.
31+
css_rules: std.ArrayListUnmanaged([]const u8),
32+
33+
// TODO: Support owner_rule here.
34+
35+
const CSSStyleSheetOpts = struct {
36+
base_url: ?[]const u8,
37+
// TODO: Suupport media
38+
disabled: bool = false,
39+
};
40+
41+
pub fn constructor(_opts: ?CSSStyleSheetOpts) CSSStyleSheet {
42+
const opts = _opts orelse CSSStyleSheetOpts{};
43+
_ = opts;
44+
45+
return .{ .css_rules = .empty, .owner_rule = null };
46+
}
47+
48+
pub fn get_ownerRule(_: *CSSStyleSheet) ?*CSSImportRule {
49+
return null;
50+
}
51+
52+
pub fn get_cssRules(self: *CSSStyleSheet) *std.ArrayListUnmanaged([]const u8) {
53+
return self.css_rules;
54+
}
55+
56+
pub fn _insertRule(self: *CSSStyleSheet, rule: []const u8, _index: ?usize, page: *Page) !usize {
57+
const index = _index orelse 0;
58+
if (index > self.css_rules.items.len) {
59+
return error.IndexSize;
60+
}
61+
62+
const arena = page.arena;
63+
try self.css_rules.insert(arena, index, arena.dupe(u8, rule));
64+
return index;
65+
}
66+
67+
pub fn _deleteRule(self: *CSSStyleSheet, index: usize) !void {
68+
if (index > self.css_rules.items.len) {
69+
return error.IndexSize;
70+
}
71+
72+
_ = self.css_rules.orderedRemove(index);
73+
}
74+
};
75+
76+
const testing = @import("../../testing.zig");
77+
test "Browser.CSS.StyleSheet" {
78+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
79+
defer runner.deinit();
80+
81+
try runner.testCases(&.{
82+
.{ "let css = new CSSStylesheet()", "" },
83+
}, .{});
84+
}

src/browser/cssom/cssom.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
pub const Stylesheet = @import("stylesheet.zig").StyleSheet;
20+
pub const CSSStylesheet = @import("css_stylesheet.zig").CSSStyleSheet;
21+
pub const CSSStyleDeclaration = @import("css_style_declaration.zig").CSSStyleDeclaration;
22+
23+
pub const Interfaces = .{
24+
Stylesheet,
25+
CSSStylesheet,
26+
CSSStyleDeclaration,
27+
@import("css_rule.zig").Interfaces,
28+
};

src/browser/cssom/stylesheet.zig

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 parser = @import("../netsurf.zig");
20+
21+
// https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet#specifications
22+
pub const StyleSheet = struct {
23+
disabled: bool,
24+
href: []const u8,
25+
owner_node: *parser.Node,
26+
parent_stylesheet: ?*StyleSheet,
27+
title: []const u8,
28+
type: []const u8,
29+
30+
pub fn get_disabled(self: *StyleSheet) bool {
31+
return self.disabled;
32+
}
33+
34+
pub fn get_href(self: *StyleSheet) []const u8 {
35+
return self.href;
36+
}
37+
38+
// TODO: media
39+
40+
pub fn get_ownerNode(self: *StyleSheet) *parser.Node {
41+
return self.owner_node;
42+
}
43+
44+
pub fn get_parentStyleSheet(self: *StyleSheet) ?*StyleSheet {
45+
return self.parent_stylesheet;
46+
}
47+
48+
pub fn get_title(self: *StyleSheet) []const u8 {
49+
return self.title;
50+
}
51+
52+
pub fn get_type(self: *StyleSheet) []const u8 {
53+
return self.type;
54+
}
55+
};

src/browser/env.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const WebApis = struct {
2222
pub const Interfaces = generate.Tuple(.{
2323
@import("crypto/crypto.zig").Crypto,
2424
@import("console/console.zig").Console,
25-
@import("cssom/css_style_declaration.zig").Interfaces,
25+
@import("cssom/cssom.zig").Interfaces,
2626
@import("dom/dom.zig").Interfaces,
2727
@import("encoding/text_encoder.zig").Interfaces,
2828
@import("events/event.zig").Interfaces,

0 commit comments

Comments
 (0)