fix: computeSnap throws when ctx.otherLayers is not an array (#1716)

This commit is contained in:
Afonso Coutinho
2026-06-03 05:34:25 +01:00
committed by GitHub
parent 319ba50a44
commit 55c7a4a546
2 changed files with 46 additions and 1 deletions

View File

@@ -37,7 +37,8 @@ export function computeSnap(layer, nx, ny, ctx) {
{ y: ch, label: 'canvas-b' },
{ y: ch / 2, label: 'canvas-cy' },
];
for (const other of ctx.otherLayers) {
const otherLayers = Array.isArray(ctx.otherLayers) ? ctx.otherLayers : [];
for (const other of otherLayers) {
if (!other.visible || other.id === layer.id) continue;
const o = other.offset || { x: 0, y: 0 };
const ow = other.canvas.width, oh = other.canvas.height;

View File

@@ -0,0 +1,44 @@
"""Pin computeSnap (static/js/editor/snap.js) against a non-array otherLayers.
Driven through `node --input-type=module`; skips without node.
"""
import json
import shutil
import subprocess
from pathlib import Path
import pytest
_REPO = Path(__file__).resolve().parent.parent
_HELPER = _REPO / "static" / "js" / "editor" / "snap.js"
_HAS_NODE = shutil.which("node") is not None
def _snap(other_layers):
js = f"""
import {{ computeSnap }} from '{_HELPER.as_posix()}';
const layer = {{ id: 'L1', canvas: {{ width: 100, height: 50 }} }};
const ctx = {{ zoom: 1, canvasW: 800, canvasH: 600, otherLayers: {json.dumps(other_layers)} }};
console.log(JSON.stringify(computeSnap(layer, 10, 10, ctx)));
"""
proc = subprocess.run(
["node", "--input-type=module"],
input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30,
)
assert proc.returncode == 0, proc.stderr
return json.loads(proc.stdout.strip())
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_compute_snap_tolerates_non_array_other_layers():
# ctx.otherLayers should be an array, but during init / error recovery it
# can be missing or wrong-typed; the old `for...of` threw on a non-iterable.
r = _snap(123)
assert r["x"] == 10 and r["y"] == 10 and r["guides"] == []
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_compute_snap_still_snaps_to_a_layer_edge():
other = [{"id": "L2", "visible": True, "offset": {"x": 12, "y": 300},
"canvas": {"width": 100, "height": 50}}]
r = _snap(other)
assert r["x"] == 12