|
| 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 | +const std = @import("std"); |
| 19 | +const parser = @import("../netsurf.zig"); |
| 20 | +const Page = @import("../page.zig").Page; |
| 21 | + |
| 22 | +const Allocator = std.mem.Allocator; |
| 23 | + |
| 24 | +const DataSet = @This(); |
| 25 | + |
| 26 | +element: *parser.Element, |
| 27 | + |
| 28 | +const GetResult = union(enum) { |
| 29 | + value: []const u8, |
| 30 | + undefined: void, |
| 31 | +}; |
| 32 | +pub fn named_get(self: *const DataSet, name: []const u8, _: *bool, page: *Page) !GetResult { |
| 33 | + const normalized_name = try normalize(page.call_arena, name); |
| 34 | + if (try parser.elementGetAttribute(self.element, normalized_name)) |value| { |
| 35 | + return .{ .value = value }; |
| 36 | + } |
| 37 | + return .{ .undefined = {} }; |
| 38 | +} |
| 39 | + |
| 40 | +pub fn named_set(self: *DataSet, name: []const u8, value: []const u8, _: *bool, page: *Page) !void { |
| 41 | + const normalized_name = try normalize(page.call_arena, name); |
| 42 | + try parser.elementSetAttribute(self.element, normalized_name, value); |
| 43 | +} |
| 44 | + |
| 45 | +pub fn named_delete(self: *DataSet, name: []const u8, _: *bool, page: *Page) !void { |
| 46 | + const normalized_name = try normalize(page.call_arena, name); |
| 47 | + try parser.elementRemoveAttribute(self.element, normalized_name); |
| 48 | +} |
| 49 | + |
| 50 | +fn normalize(allocator: Allocator, name: []const u8) ![]const u8 { |
| 51 | + var upper_count: usize = 0; |
| 52 | + for (name) |c| { |
| 53 | + if (std.ascii.isUpper(c)) { |
| 54 | + upper_count += 1; |
| 55 | + } |
| 56 | + } |
| 57 | + // for every upper-case letter, we'll probably need a dash before it |
| 58 | + // and we need the 'data-' prefix |
| 59 | + var normalized = try allocator.alloc(u8, name.len + upper_count + 5); |
| 60 | + |
| 61 | + @memcpy(normalized[0..5], "data-"); |
| 62 | + if (upper_count == 0) { |
| 63 | + @memcpy(normalized[5..], name); |
| 64 | + return normalized; |
| 65 | + } |
| 66 | + |
| 67 | + var pos: usize = 5; |
| 68 | + for (name) |c| { |
| 69 | + if (std.ascii.isUpper(c)) { |
| 70 | + normalized[pos] = '-'; |
| 71 | + pos += 1; |
| 72 | + normalized[pos] = c + 32; |
| 73 | + } else { |
| 74 | + normalized[pos] = c; |
| 75 | + } |
| 76 | + pos += 1; |
| 77 | + } |
| 78 | + return normalized; |
| 79 | +} |
| 80 | + |
| 81 | +const testing = @import("../../testing.zig"); |
| 82 | +test "Browser.HTML.DataSet" { |
| 83 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "" }); |
| 84 | + defer runner.deinit(); |
| 85 | + |
| 86 | + try runner.testCases(&.{ |
| 87 | + .{ "let el1 = document.createElement('div')", null }, |
| 88 | + .{ "el1.dataset.x", "undefined" }, |
| 89 | + .{ "el1.dataset.x = '123'", "123" }, |
| 90 | + .{ "delete el1.dataset.x", "true" }, |
| 91 | + .{ "el1.dataset.x", "undefined" }, |
| 92 | + .{ "delete el1.dataset.other", "true" }, // yes, this is right |
| 93 | + |
| 94 | + .{ "let ds1 = el1.dataset", null }, |
| 95 | + .{ "ds1.helloWorld = 'yes'", null }, |
| 96 | + .{ "el1.getAttribute('data-hello-world')", "yes" }, |
| 97 | + .{ "el1.setAttribute('data-this-will-work', 'positive')", null }, |
| 98 | + .{ "ds1.thisWillWork", "positive" }, |
| 99 | + }, .{}); |
| 100 | +} |
0 commit comments