Skip to content

Commit 513ef9a

Browse files
kristoff-itLoris Cro
authored andcommitted
wip
1 parent bdc30e9 commit 513ef9a

File tree

1 file changed

+59
-28
lines changed

1 file changed

+59
-28
lines changed

src/html/elements/picture.zig

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn validateSrcset(
265265
// var future_h = false;
266266
_ = width_desc;
267267

268-
outer: while (position < input.len) {
268+
while (position < input.len) {
269269
// 4. Splitting loop: Collect a sequence of code points that are ASCII whitespace or U+002C COMMA characters from input given position. If any U+002C COMMA characters were collected, that is a parse error.
270270
const start = position;
271271
while (position < input.len) : (position += 1) {
@@ -312,22 +312,41 @@ fn validateSrcset(
312312
var state: enum { descriptor, parens, after } = .descriptor;
313313

314314
// 4. Let c be the character at position. Do the following depending on the value of state. For the purpose of this step, "EOF" is a special character representing that position is past the end of input.
315-
const descriptor_start: u32 = position;
315+
var descriptor_start: u32 = position;
316316
while (true) : (position += 1) {
317317
const c = if (position == input.len) 0 else input[position];
318318
state: switch (state) {
319319
.descriptor => switch (c) {
320320
' ', '\t'...'\r' => {
321321
// ASCII whitespace
322322
// If current descriptor is not empty, append current descriptor to descriptors and let current descriptor be the empty string. Set state to after descriptor.
323-
if (position - descriptor_start != 0) {
323+
const descriptor = input[descriptor_start..position];
324+
if (descriptor.len != 0) {
325+
if (validateDescriptor(
326+
node_idx,
327+
descriptor_start,
328+
descriptor,
329+
)) |err| {
330+
return errors.append(gpa, err);
331+
}
332+
descriptor_start = position;
324333
state = .after;
325334
}
326335
},
327336
',' => {
328337
// U+002C COMMA (,)
329338
// Advance position to the next character in input. If current descriptor is not empty, append current descriptor to descriptors. Jump to the step labeled descriptor parser.
330-
if (position - descriptor_start == 0) continue :outer;
339+
const descriptor = input[descriptor_start..position];
340+
if (descriptor.len != 0) {
341+
if (validateDescriptor(
342+
node_idx,
343+
descriptor_start,
344+
descriptor,
345+
)) |err| {
346+
return errors.append(gpa, err);
347+
}
348+
}
349+
position += 1;
331350
break;
332351
},
333352
'(' => {
@@ -339,7 +358,16 @@ fn validateSrcset(
339358
0 => {
340359
// EOF
341360
// If current descriptor is not empty, append current descriptor to descriptors. Jump to the step labeled descriptor parser.
342-
if (position - descriptor_start == 0) continue :outer;
361+
const descriptor = input[descriptor_start..position];
362+
if (descriptor.len != 0) {
363+
if (validateDescriptor(
364+
node_idx,
365+
descriptor_start,
366+
descriptor,
367+
)) |err| {
368+
return errors.append(gpa, err);
369+
}
370+
}
343371
break;
344372
},
345373

@@ -387,33 +415,36 @@ fn validateSrcset(
387415
}
388416
// Advance position to the next character in input. Repeat this step.
389417
}
390-
391-
const descriptor = input[descriptor_start..position];
392-
assert(descriptor.len > 0);
393-
394-
switch (descriptor[descriptor.len - 1]) {
395-
'w' => {},
396-
'x' => {},
397-
'h' => {},
398-
else => return errors.append(gpa, .{
399-
.tag = .{
400-
.invalid_attr_value = .{ .reason = "invalid descriptor" },
401-
},
402-
.main_location = .{
403-
.start = value.span.start + descriptor_start,
404-
.end = value.span.start + position,
405-
},
406-
.node_idx = node_idx,
407-
}),
408-
}
409-
410-
if (position < input.len - 1 and input[position] == ',') {
411-
position += 1;
412-
}
413418
}
414419
}
415420
}
416421

422+
fn validateDescriptor(
423+
node_idx: u32,
424+
descriptor_start: u32,
425+
descriptor: []const u8,
426+
) ?Ast.Error {
427+
assert(descriptor.len > 0);
428+
429+
switch (descriptor[descriptor.len - 1]) {
430+
'w' => {},
431+
'x' => {},
432+
'h' => {},
433+
else => return .{
434+
.tag = .{
435+
.invalid_attr_value = .{ .reason = "invalid descriptor" },
436+
},
437+
.main_location = .{
438+
.start = descriptor_start,
439+
.end = @intCast(descriptor_start + descriptor.len),
440+
},
441+
.node_idx = node_idx,
442+
},
443+
}
444+
445+
return null;
446+
}
447+
417448
fn completions(
418449
arena: Allocator,
419450
ast: Ast,

0 commit comments

Comments
 (0)