-
Notifications
You must be signed in to change notification settings - Fork 285
Minimal CSSStyleSheet #790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (C) 2023-2024 Lightpanda (Selecy SAS) | ||
| // | ||
| // Francis Bouvier <francis@lightpanda.io> | ||
| // Pierre Tachoire <pierre@lightpanda.io> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| const std = @import("std"); | ||
|
|
||
| const CSSStyleSheet = @import("css_stylesheet.zig").CSSStyleSheet; | ||
|
|
||
| pub const Interfaces = .{ | ||
| CSSRule, | ||
| CSSImportRule, | ||
| }; | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/CSSRule | ||
| pub const CSSRule = struct { | ||
| css_text: []const u8, | ||
| parent_rule: ?*CSSRule = null, | ||
| parent_stylesheet: ?*CSSStyleSheet = null, | ||
| }; | ||
|
|
||
| pub const CSSImportRule = struct { | ||
| pub const prototype = *CSSRule; | ||
| href: []const u8, | ||
| layer_name: ?[]const u8, | ||
| media: void, | ||
| style_sheet: CSSStyleSheet, | ||
| supports_text: ?[]const u8, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (C) 2023-2024 Lightpanda (Selecy SAS) | ||
| // | ||
| // Francis Bouvier <francis@lightpanda.io> | ||
| // Pierre Tachoire <pierre@lightpanda.io> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| const std = @import("std"); | ||
|
|
||
| const StyleSheet = @import("stylesheet.zig").StyleSheet; | ||
| const CSSRule = @import("css_rule.zig").CSSRule; | ||
| const CSSImportRule = @import("css_rule.zig").CSSImportRule; | ||
mookums marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| pub const CSSRuleList = struct { | ||
| list: std.ArrayListUnmanaged([]const u8), | ||
|
|
||
| pub fn constructor() CSSRuleList { | ||
| return .{ .list = .empty }; | ||
| } | ||
|
|
||
| pub fn _item(self: *CSSRuleList, _index: u32) ?CSSRule { | ||
| const index: usize = @intCast(_index); | ||
|
|
||
| if (index > self.list.items.len) { | ||
| return null; | ||
| } | ||
|
|
||
| // todo: for now, just return null. | ||
| // this depends on properly parsing CSSRule | ||
| return null; | ||
| } | ||
|
|
||
| pub fn get_length(self: *CSSRuleList) u32 { | ||
| return @intCast(self.list.items.len); | ||
| } | ||
| }; | ||
|
|
||
| const testing = @import("../../testing.zig"); | ||
| test "Browser.CSS.CSSRuleList" { | ||
| var runner = try testing.jsRunner(testing.tracking_allocator, .{}); | ||
| defer runner.deinit(); | ||
|
|
||
| try runner.testCases(&.{ | ||
| .{ "let list = new CSSRuleList()", "undefined" }, | ||
| .{ "list instanceof CSSRuleList", "true" }, | ||
| .{ "list.length", "0" }, | ||
| .{ "list.item(0)", "null" }, | ||
| }, .{}); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||
| // Copyright (C) 2023-2024 Lightpanda (Selecy SAS) | ||||||
| // | ||||||
| // Francis Bouvier <francis@lightpanda.io> | ||||||
| // Pierre Tachoire <pierre@lightpanda.io> | ||||||
| // | ||||||
| // This program is free software: you can redistribute it and/or modify | ||||||
| // it under the terms of the GNU Affero General Public License as | ||||||
| // published by the Free Software Foundation, either version 3 of the | ||||||
| // License, or (at your option) any later version. | ||||||
| // | ||||||
| // This program is distributed in the hope that it will be useful, | ||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
| // GNU Affero General Public License for more details. | ||||||
| // | ||||||
| // You should have received a copy of the GNU Affero General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
|
|
||||||
| const std = @import("std"); | ||||||
|
|
||||||
| const Page = @import("../page.zig").Page; | ||||||
| const StyleSheet = @import("stylesheet.zig").StyleSheet; | ||||||
|
|
||||||
| const CSSRuleList = @import("css_rule_list.zig").CSSRuleList; | ||||||
| const CSSImportRule = @import("css_rule.zig").CSSImportRule; | ||||||
|
|
||||||
| pub const CSSStyleSheet = struct { | ||||||
| pub const prototype = *StyleSheet; | ||||||
mookums marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| proto: StyleSheet, | ||||||
| css_rules: CSSRuleList, | ||||||
| owner_rule: ?*CSSImportRule, | ||||||
|
|
||||||
| const CSSStyleSheetOpts = struct { | ||||||
| base_url: ?[]const u8 = null, | ||||||
| // TODO: Suupport media | ||||||
| disabled: bool = false, | ||||||
| }; | ||||||
|
|
||||||
| pub fn constructor(_opts: ?CSSStyleSheetOpts) !CSSStyleSheet { | ||||||
| const opts = _opts orelse CSSStyleSheetOpts{}; | ||||||
| return .{ | ||||||
| .proto = StyleSheet{ .disabled = opts.disabled }, | ||||||
| .css_rules = .constructor(), | ||||||
| .owner_rule = null, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| pub fn get_ownerRule(_: *CSSStyleSheet) ?*CSSImportRule { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not a big deal. Can also be done in |
||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| pub fn get_cssRules(self: *CSSStyleSheet) *CSSRuleList { | ||||||
| return &self.css_rules; | ||||||
| } | ||||||
|
|
||||||
| pub fn _insertRule(self: *CSSStyleSheet, rule: []const u8, _index: ?usize, page: *Page) !usize { | ||||||
| const index = _index orelse 0; | ||||||
| if (index > self.css_rules.list.items.len) { | ||||||
| return error.IndexSize; | ||||||
| } | ||||||
|
|
||||||
| const arena = page.arena; | ||||||
| try self.css_rules.list.insert(arena, index, try arena.dupe(u8, rule)); | ||||||
| return index; | ||||||
| } | ||||||
|
|
||||||
| pub fn _deleteRule(self: *CSSStyleSheet, index: usize) !void { | ||||||
| if (index > self.css_rules.list.items.len) { | ||||||
| return error.IndexSize; | ||||||
| } | ||||||
|
|
||||||
| _ = self.css_rules.list.orderedRemove(index); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| const testing = @import("../../testing.zig"); | ||||||
| test "Browser.CSS.StyleSheet" { | ||||||
| var runner = try testing.jsRunner(testing.tracking_allocator, .{}); | ||||||
| defer runner.deinit(); | ||||||
|
|
||||||
| try runner.testCases(&.{ | ||||||
| .{ "let css = new CSSStyleSheet()", "undefined" }, | ||||||
| .{ "css instanceof CSSStyleSheet", "true" }, | ||||||
| .{ "css.cssRules.length", "0" }, | ||||||
| .{ "css.ownerRule", "null" }, | ||||||
| .{ "let index1 = css.insertRule('body { color: red; }', 0)", "undefined" }, | ||||||
| .{ "index1", "0" }, | ||||||
| .{ "css.cssRules.length", "1" }, | ||||||
| }, .{}); | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (C) 2023-2024 Lightpanda (Selecy SAS) | ||
| // | ||
| // Francis Bouvier <francis@lightpanda.io> | ||
| // Pierre Tachoire <pierre@lightpanda.io> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| pub const Stylesheet = @import("stylesheet.zig").StyleSheet; | ||
| pub const CSSStylesheet = @import("css_stylesheet.zig").CSSStyleSheet; | ||
| pub const CSSStyleDeclaration = @import("css_style_declaration.zig").CSSStyleDeclaration; | ||
| pub const CSSRuleList = @import("css_rule_list.zig").CSSRuleList; | ||
|
|
||
| pub const Interfaces = .{ | ||
| Stylesheet, | ||
| CSSStylesheet, | ||
| CSSStyleDeclaration, | ||
| CSSRuleList, | ||
| @import("css_rule.zig").Interfaces, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright (C) 2023-2024 Lightpanda (Selecy SAS) | ||
| // | ||
| // Francis Bouvier <francis@lightpanda.io> | ||
| // Pierre Tachoire <pierre@lightpanda.io> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| const parser = @import("../netsurf.zig"); | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet#specifications | ||
| pub const StyleSheet = struct { | ||
| disabled: bool = false, | ||
| href: []const u8 = "", | ||
| owner_node: ?*parser.Node = null, | ||
| parent_stylesheet: ?*StyleSheet = null, | ||
| title: []const u8 = "", | ||
| type: []const u8 = "text/css", | ||
|
|
||
| pub fn get_disabled(self: *const StyleSheet) bool { | ||
| return self.disabled; | ||
| } | ||
|
|
||
| pub fn get_href(self: *const StyleSheet) []const u8 { | ||
| return self.href; | ||
| } | ||
|
|
||
| // TODO: media | ||
|
|
||
| pub fn get_ownerNode(self: *const StyleSheet) ?*parser.Node { | ||
| return self.owner_node; | ||
| } | ||
|
|
||
| pub fn get_parentStyleSheet(self: *const StyleSheet) ?*StyleSheet { | ||
| return self.parent_stylesheet; | ||
| } | ||
|
|
||
| pub fn get_title(self: *const StyleSheet) []const u8 { | ||
| return self.title; | ||
| } | ||
|
|
||
| pub fn get_type(self: *const StyleSheet) []const u8 { | ||
| return self.type; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.