mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-04 06:23:45 +00:00
Merge pull request #1452 from lightpanda-io/css_escape
Some checks failed
e2e-test / zig build release (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Some checks failed
e2e-test / zig build release (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
improve correctness of CSS.escape
This commit is contained in:
@@ -20,8 +20,10 @@
|
|||||||
{
|
{
|
||||||
testing.expectEqual('\\30 abc', CSS.escape('0abc'));
|
testing.expectEqual('\\30 abc', CSS.escape('0abc'));
|
||||||
testing.expectEqual('\\31 23', CSS.escape('123'));
|
testing.expectEqual('\\31 23', CSS.escape('123'));
|
||||||
testing.expectEqual('\\-test', CSS.escape('-test'));
|
testing.expectEqual('\\-', CSS.escape('-'));
|
||||||
testing.expectEqual('\\--test', CSS.escape('--test'));
|
testing.expectEqual('-test', CSS.escape('-test'));
|
||||||
|
testing.expectEqual('--test', CSS.escape('--test'));
|
||||||
|
testing.expectEqual('-\\33 ', CSS.escape('-3'));
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -42,16 +42,24 @@ pub fn parseDimension(value: []const u8) ?f64 {
|
|||||||
/// https://drafts.csswg.org/cssom/#the-css.escape()-method
|
/// https://drafts.csswg.org/cssom/#the-css.escape()-method
|
||||||
pub fn escape(_: *const CSS, value: []const u8, page: *Page) ![]const u8 {
|
pub fn escape(_: *const CSS, value: []const u8, page: *Page) ![]const u8 {
|
||||||
if (value.len == 0) {
|
if (value.len == 0) {
|
||||||
return error.InvalidCharacterError;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const first = value[0];
|
const first = value[0];
|
||||||
|
if (first == '-' and value.len == 1) {
|
||||||
|
return "\\-";
|
||||||
|
}
|
||||||
|
|
||||||
// Count how many characters we need for the output
|
// Count how many characters we need for the output
|
||||||
var out_len: usize = escapeLen(true, first);
|
var out_len: usize = escapeLen(true, first);
|
||||||
for (value[1..]) |c| {
|
for (value[1..], 0..) |c, i| {
|
||||||
|
// Second char (i==0) is a digit and first is '-', needs hex escape
|
||||||
|
if (i == 0 and first == '-' and c >= '0' and c <= '9') {
|
||||||
|
out_len += 2 + hexDigitsNeeded(c);
|
||||||
|
} else {
|
||||||
out_len += escapeLen(false, c);
|
out_len += escapeLen(false, c);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (out_len == value.len) {
|
if (out_len == value.len) {
|
||||||
return value;
|
return value;
|
||||||
@@ -67,8 +75,13 @@ pub fn escape(_: *const CSS, value: []const u8, page: *Page) ![]const u8 {
|
|||||||
pos = 1;
|
pos = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (value[1..]) |c| {
|
for (value[1..], 0..) |c, i| {
|
||||||
if (!needsEscape(false, c)) {
|
// Second char (i==0) is a digit and first is '-', needs hex escape
|
||||||
|
if (i == 0 and first == '-' and c >= '0' and c <= '9') {
|
||||||
|
result[pos] = '\\';
|
||||||
|
const hex_str = std.fmt.bufPrint(result[pos + 1 ..], "{x} ", .{c}) catch unreachable;
|
||||||
|
pos += 1 + hex_str.len;
|
||||||
|
} else if (!needsEscape(false, c)) {
|
||||||
result[pos] = c;
|
result[pos] = c;
|
||||||
pos += 1;
|
pos += 1;
|
||||||
} else {
|
} else {
|
||||||
@@ -105,9 +118,6 @@ fn needsEscape(comptime is_first: bool, c: u8) bool {
|
|||||||
if (c >= '0' and c <= '9') {
|
if (c >= '0' and c <= '9') {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (c == '-') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Characters that need escaping
|
// Characters that need escaping
|
||||||
|
|||||||
Reference in New Issue
Block a user