mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
Use fetchRemove and getOrPut to streamline map manipulation
This commit is contained in:
@@ -48,7 +48,7 @@ pub const CSSStyleDeclaration = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_cssText(self: *const CSSStyleDeclaration, state: *SessionState) ![]const u8 {
|
pub fn get_cssText(self: *const CSSStyleDeclaration, state: *SessionState) ![]const u8 {
|
||||||
var buffer = std.ArrayListUnmanaged(u8){};
|
var buffer: std.ArrayListUnmanaged(u8) = .empty;
|
||||||
const writer = buffer.writer(state.call_arena);
|
const writer = buffer.writer(state.call_arena);
|
||||||
for (self.order.items) |name| {
|
for (self.order.items) |name| {
|
||||||
const prop = self.store.get(name).?;
|
const prop = self.store.get(name).?;
|
||||||
@@ -65,6 +65,8 @@ pub const CSSStyleDeclaration = struct {
|
|||||||
self.store.clearRetainingCapacity();
|
self.store.clearRetainingCapacity();
|
||||||
self.order.clearRetainingCapacity();
|
self.order.clearRetainingCapacity();
|
||||||
|
|
||||||
|
// call_arena is safe here, because _setProperty will dupe the name
|
||||||
|
// using the state's longer-living arena.
|
||||||
const declarations = try CSSParser.parseDeclarations(state.call_arena, text);
|
const declarations = try CSSParser.parseDeclarations(state.call_arena, text);
|
||||||
|
|
||||||
for (declarations) |decl| {
|
for (declarations) |decl| {
|
||||||
@@ -95,34 +97,30 @@ pub const CSSStyleDeclaration = struct {
|
|||||||
return if (index < self.order.items.len) self.order.items[index] else "";
|
return if (index < self.order.items.len) self.order.items[index] else "";
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _removeProperty(self: *CSSStyleDeclaration, name: []const u8, state: *SessionState) ![]const u8 {
|
pub fn _removeProperty(self: *CSSStyleDeclaration, name: []const u8) ![]const u8 {
|
||||||
if (self.store.get(name)) |prop| {
|
const prop = self.store.fetchRemove(name) orelse return "";
|
||||||
const value_copy = try state.call_arena.dupe(u8, prop.value);
|
for (self.order.items, 0..) |item, i| {
|
||||||
_ = self.store.remove(name);
|
if (std.mem.eql(u8, item, name)) {
|
||||||
var i: usize = 0;
|
|
||||||
while (i < self.order.items.len) : (i += 1) {
|
|
||||||
if (std.mem.eql(u8, self.order.items[i], name)) {
|
|
||||||
_ = self.order.orderedRemove(i);
|
_ = self.order.orderedRemove(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value_copy;
|
// safe to return, since it's in our state.arena
|
||||||
}
|
return prop.value.value;
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _setProperty(self: *CSSStyleDeclaration, name: []const u8, value: []const u8, priority: ?[]const u8, state: *SessionState) !void {
|
pub fn _setProperty(self: *CSSStyleDeclaration, name: []const u8, value: []const u8, priority: ?[]const u8, state: *SessionState) !void {
|
||||||
|
const owned_value = try state.arena.dupe(u8, value);
|
||||||
const is_important = priority != null and std.ascii.eqlIgnoreCase(priority.?, "important");
|
const is_important = priority != null and std.ascii.eqlIgnoreCase(priority.?, "important");
|
||||||
|
|
||||||
const value_copy = try state.arena.dupe(u8, value);
|
const gop = try self.store.getOrPut(state.arena, name);
|
||||||
|
if (!gop.found_existing) {
|
||||||
if (!self.store.contains(name)) {
|
const owned_name = try state.arena.dupe(u8, name);
|
||||||
const name_copy = try state.arena.dupe(u8, name);
|
gop.key_ptr.* = owned_name;
|
||||||
try self.order.append(state.arena, name_copy);
|
try self.order.append(state.arena, owned_name);
|
||||||
try self.store.put(state.arena, name_copy, Property{ .value = value_copy, .priority = is_important });
|
|
||||||
} else {
|
|
||||||
try self.store.put(state.arena, name, Property{ .value = value_copy, .priority = is_important });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gop.value_ptr.* = .{ .value = owned_value, .priority = is_important };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user