|
| 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 CSSRuleList = @import("css_rule_list.zig").CSSRuleList; |
| 25 | +const CSSImportRule = @import("css_rule.zig").CSSImportRule; |
| 26 | + |
| 27 | +pub const CSSStyleSheet = struct { |
| 28 | + pub const prototype = *StyleSheet; |
| 29 | + |
| 30 | + proto: StyleSheet, |
| 31 | + css_rules: CSSRuleList, |
| 32 | + owner_rule: ?*CSSImportRule, |
| 33 | + |
| 34 | + const CSSStyleSheetOpts = struct { |
| 35 | + base_url: ?[]const u8 = null, |
| 36 | + // TODO: Suupport media |
| 37 | + disabled: bool = false, |
| 38 | + }; |
| 39 | + |
| 40 | + pub fn constructor(_opts: ?CSSStyleSheetOpts) !CSSStyleSheet { |
| 41 | + const opts = _opts orelse CSSStyleSheetOpts{}; |
| 42 | + return .{ |
| 43 | + .proto = StyleSheet{ .disabled = opts.disabled }, |
| 44 | + .css_rules = .constructor(), |
| 45 | + .owner_rule = null, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + pub fn get_ownerRule(_: *CSSStyleSheet) ?*CSSImportRule { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + pub fn get_cssRules(self: *CSSStyleSheet) *CSSRuleList { |
| 54 | + return &self.css_rules; |
| 55 | + } |
| 56 | + |
| 57 | + pub fn _insertRule(self: *CSSStyleSheet, rule: []const u8, _index: ?usize, page: *Page) !usize { |
| 58 | + const index = _index orelse 0; |
| 59 | + if (index > self.css_rules.list.items.len) { |
| 60 | + return error.IndexSize; |
| 61 | + } |
| 62 | + |
| 63 | + const arena = page.arena; |
| 64 | + try self.css_rules.list.insert(arena, index, try arena.dupe(u8, rule)); |
| 65 | + return index; |
| 66 | + } |
| 67 | + |
| 68 | + pub fn _deleteRule(self: *CSSStyleSheet, index: usize) !void { |
| 69 | + if (index > self.css_rules.list.items.len) { |
| 70 | + return error.IndexSize; |
| 71 | + } |
| 72 | + |
| 73 | + _ = self.css_rules.list.orderedRemove(index); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +const testing = @import("../../testing.zig"); |
| 78 | +test "Browser.CSS.StyleSheet" { |
| 79 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{}); |
| 80 | + defer runner.deinit(); |
| 81 | + |
| 82 | + try runner.testCases(&.{ |
| 83 | + .{ "let css = new CSSStyleSheet()", "undefined" }, |
| 84 | + .{ "css instanceof CSSStyleSheet", "true" }, |
| 85 | + .{ "css.cssRules.length", "0" }, |
| 86 | + .{ "css.ownerRule", "null" }, |
| 87 | + .{ "let index1 = css.insertRule('body { color: red; }', 0)", "undefined" }, |
| 88 | + .{ "index1", "0" }, |
| 89 | + .{ "css.cssRules.length", "1" }, |
| 90 | + }, .{}); |
| 91 | +} |
0 commit comments