LibWeb: Do nothing when calling CanvasPath.closePath() with empty path
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

This commit is contained in:
Tim Ledbetter 2025-09-06 02:20:48 +01:00 committed by Tim Flynn
commit 3c6472dc00
Notes: github-actions[bot] 2025-09-06 12:48:46 +00:00
3 changed files with 31 additions and 0 deletions

View file

@ -23,8 +23,14 @@ void CanvasPath::ensure_subpath(float x, float y)
m_path.move_to(Gfx::FloatPoint { x, y });
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-closepath
void CanvasPath::close_path()
{
// The closePath() method, when invoked, must do nothing if the object's path has no subpaths. Otherwise, it must
// mark the last subpath as closed, create a new subpath whose first point is the same as the previous subpath's
// first point, and finally add this new subpath to the path.
if (m_path.is_empty())
return;
m_path.close();
}

View file

@ -0,0 +1,3 @@
<!DOCTYPE html>
<div>There should be no diagonal line drawn to the canvas</div>
<canvas width="100" height="100" style="border:1px solid black"></canvas>

View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<link rel="match" href="../../expected/HTML/canvas-close-empty-path-ref.html" />
<div>There should be no diagonal line drawn to the canvas</div>
<canvas width="100" height="100" style="border:1px solid black"></canvas>
<script>
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
// Start a fresh path but DO NOT moveTo() first.
ctx.beginPath();
// Should be a no-op per spec if there's no current subpath.
ctx.closePath();
// Because there's no current point, lineTo(50,50) should start a new subpath at (50,50)
// and draw nothing until a subsequent lineTo/moveTo.
// If an unintended moveTo(0,0) happened, this will draw from (0,0)->(50,50).
ctx.lineTo(50, 50);
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.stroke();
</script>