Skip to content

Lightpanda: the headless browser designed for AI and automation

Align with Chrome/Firefox

Karl Seguine50c3e9a3bb4
diff --git a/src/browser/tests/element/svg/collections.html b/src/browser/tests/element/svg/collections.html
index 96e78095b..715157080 100644
--- a/src/browser/tests/element/svg/collections.html
+++ b/src/browser/tests/element/svg/collections.html
@@ -3,277 +3,718 @@
<svg id=root>
<polygon id=polygon points="0,0 10,20"></polygon>
- <polyline id=polyline points="1 2, 3 4"></polyline>
+ <polyline id=polylineLive points="0,0 10,20"></polyline>
+ <polyline id=polylineExternal points="0,0 10,20"></polyline>
+ <polyline id=polylineInvalid points="0,0 10,20"></polyline>
+ <polyline id=polylineIdentity points="1 2 3 4"></polyline>
+ <polyline id=polylineMutators points="1 2 3 4"></polyline>
+ <polyline id=polylineStale points="1 2 3 4"></polyline>
+ <polyline id=polylineErrors points="1 2 3 4"></polyline>
+ <polygon id=polygonReadOnly points="1 2 3 4"></polygon>
<polyline id=many></polyline>
- <g id=group transform="translate(10 20) rotate(45 1 2)"
- requiredExtensions="urn:one urn:two" systemLanguage="en, fr"></g>
+
+ <g id=group transform="translate(10 20) rotate(45 1 2)"></g>
+ <g id=transformLive transform="translate(10 20) rotate(45 1 2)"></g>
+ <g id=transformExternal transform="translate(10 20)"></g>
+ <g id=transformIdentity transform="translate(1 2)"></g>
+ <g id=transformMutators transform="translate(1 2)"></g>
+ <g id=transformStale transform="translate(10 20)"></g>
+ <g id=transformConsolidate transform="translate(10 20) scale(2)"></g>
+ <g id=transformConsolidateCommit transform="rotate(30 5 6) scale(2 3)"></g>
+ <g id=matrixIs2D transform="translate(1 2)"></g>
+ <g id=matrixStaleMatrix transform="translate(1 2)"></g>
+ <g id=transformReadOnly transform="translate(1 2)"></g>
+ <g id=transformParse transform="translate(1 2) scale(3,4) rotate(30 5 6)"></g>
<g id=matrixGroup transform="translate(1 2)"></g>
- <g id=replaceGroup transform="scale(1) translate(0 0)"></g>
+ <g id=matrixReadOnly transform="translate(1 2)"></g>
+
+ <g id=strings requiredExtensions="urn:one urn:two" systemLanguage="en, fr"></g>
+ <g id=stringMutators requiredExtensions="urn:one urn:two"></g>
+ <g id=stringReturns requiredExtensions="urn:one urn:two"></g>
+ <g id=stringClear requiredExtensions="urn:one urn:two"></g>
+ <g id=stringLanguages systemLanguage="en, fr"></g>
+ <g id=stringExternal systemLanguage="en, fr"></g>
+ <g id=stringTokens systemLanguage="en, fr"></g>
+ <g id=stringNoValidation requiredExtensions="urn:one"></g>
</svg>
-<script id=pointLists>
+<!--
+ These fail in Chrome, deliberately. SVG 2 builds the list interfaces over a
+ list of real objects — getItem returns "a reference to that object and not a
+ copy of it" — so two getItem calls hand back the same object. Firefox does
+ this; Chrome creates a fresh tear-off per call and per mutator return, and
+ honours identity only where the IDL says [SameObject] (SVGTransform.matrix,
+ asserted in transformMatrixLive).
+
+ WPT sits this one out: every list test in svg/types/scripted compares .value
+ rather than the objects. We follow the spec and Firefox. Kept in its own
+ script so the rest of the file still runs in Chrome.
+-->
+<script id=pointListIdentity>
{
const root = $('#root');
const polygon = $('#polygon');
const points = polygon.points;
- const animated = polygon.animatedPoints;
- testing.expectEqual(true, points instanceof SVGPointList);
- testing.expectEqual(true, points === polygon.points);
- testing.expectEqual(true, animated === polygon.animatedPoints);
- testing.expectEqual(false, points === animated);
+ testing.expectTrue(points === polygon.points);
+ testing.expectTrue(polygon.animatedPoints === polygon.animatedPoints);
+ testing.expectFalse(points === polygon.animatedPoints);
+ testing.expectTrue(points.getItem(0) === points.getItem(0));
+
+ // "The inserted item is the item itself and not a copy", so every mutator
+ // hands back the object it was given.
+ const list = $('#polylineIdentity').points;
+ const appended = root.createSVGPoint();
+ testing.expectEqual(appended, list.appendItem(appended));
+ const inserted = root.createSVGPoint();
+ testing.expectEqual(inserted, list.insertItemBefore(inserted, 0));
+ const replacement = root.createSVGPoint();
+ testing.expectEqual(replacement, list.replaceItem(replacement, 1));
+ testing.expectEqual(replacement, list.removeItem(1));
+ const initialized = root.createSVGPoint();
+ testing.expectEqual(initialized, list.initialize(initialized));
+}
+</script>
+
+<script id=pointListBasics>
+{
+ const polygon = $('#polygon');
+ const points = polygon.points;
+
+ testing.expectTrue(points instanceof SVGPointList);
testing.expectEqual(2, points.length);
testing.expectEqual(2, points.numberOfItems);
- testing.expectEqual(true, points.getItem(0) === points.getItem(0));
+ testing.expectEqual(0, points.getItem(0).x);
+ testing.expectEqual(0, points.getItem(0).y);
testing.expectEqual(10, points.getItem(1).x);
testing.expectEqual(20, points.getItem(1).y);
+}
+</script>
+
+<script id=pointListLive>
+{
+ const polyline = $('#polylineLive');
+ const points = polyline.points;
- const oldPoint = points.getItem(0);
- oldPoint.x = 5;
- testing.expectEqual('5,0 10,20', polygon.getAttribute('points'));
- testing.expectEqual(5, animated.getItem(0).x);
+ points.getItem(0).x = 5;
+ testing.expectEqual('5 0 10 20', polyline.getAttribute('points'));
+ testing.expectEqual(5, polyline.animatedPoints.getItem(0).x);
- polygon.setAttribute('points', '1 2, 3 4');
+ points.getItem(1).y = -1;
+ testing.expectEqual('5 0 10 -1', polyline.getAttribute('points'));
+}
+</script>
+
+<script id=pointListExternalMutation>
+{
+ const polyline = $('#polylineExternal');
+ const points = polyline.points;
+
+ polyline.setAttribute('points', '1 2, 3 4');
testing.expectEqual(2, points.length);
testing.expectEqual(1, points.getItem(0).x);
- oldPoint.x = 99;
- testing.expectEqual('1 2, 3 4', polygon.getAttribute('points'));
+ testing.expectEqual(2, points.getItem(0).y);
+ testing.expectEqual(3, points.getItem(1).x);
+ testing.expectEqual('1 2, 3 4', polyline.getAttribute('points'));
+}
+</script>
- polygon.setAttribute('points', '1,2,3');
+<script id=pointListInvalidAttribute>
+{
+ const polyline = $('#polylineInvalid');
+ const points = polyline.points;
+
+ // A malformed number invalidates the whole attribute...
+ polyline.setAttribute('points', '0,0 100,0 INVALID');
testing.expectEqual(0, points.length);
- testing.expectEqual('1,2,3', polygon.getAttribute('points'));
- polygon.setAttribute('points', '0,0');
+ testing.expectEqual('0,0 100,0 INVALID', polyline.getAttribute('points'));
+
+ // ...but a trailing coordinate with no pair only truncates it.
+ polyline.setAttribute('points', '0,0 100,0 20');
+ testing.expectEqual(2, points.length);
+ polyline.setAttribute('points', '0,0 100,0 20,');
+ testing.expectEqual(2, points.length);
+ polyline.setAttribute('points', '1,2,3');
+ testing.expectEqual(1, points.length);
+ testing.expectEqual('1,2,3', polyline.getAttribute('points'));
+}
+</script>
+
+<script id=pointListMutators>
+{
+ const root = $('#root');
+ const polyline = $('#polylineMutators');
+ const points = polyline.points;
const detached = root.createSVGPoint();
detached.x = 7;
detached.y = 8;
- testing.expectEqual(detached, points.appendItem(detached));
- const copied = points.appendItem(detached);
- testing.expectEqual(false, copied === detached);
- testing.expectEqual(3, points.length);
+ points.appendItem(detached);
+ testing.expectEqual('1 2 3 4 7 8', polyline.getAttribute('points'));
+
+ // Already in a list, so this one gets copied.
+ points.appendItem(detached);
+ testing.expectEqual(4, points.length);
detached.x = 9;
- testing.expectEqual(9, points.getItem(1).x);
- testing.expectEqual(7, copied.x);
+ testing.expectEqual(9, points.getItem(2).x);
+ testing.expectEqual(7, points.getItem(3).x);
+
+ const removed = points.removeItem(2);
+ testing.expectEqual(9, removed.x);
+ testing.expectEqual('1 2 3 4 7 8', polyline.getAttribute('points'));
+
+ const inserted = root.createSVGPoint();
+ inserted.x = 100;
+ inserted.y = 200;
+ points.insertItemBefore(inserted, 0);
+ testing.expectEqual('100 200 1 2 3 4 7 8', polyline.getAttribute('points'));
+
+ const replacement = root.createSVGPoint();
+ replacement.x = 5;
+ replacement.y = 6;
+ points.replaceItem(replacement, 1);
+ testing.expectEqual('100 200 5 6 3 4 7 8', polyline.getAttribute('points'));
+
+ const initialized = root.createSVGPoint();
+ initialized.x = 4;
+ initialized.y = 5;
+ points.initialize(initialized);
+ testing.expectEqual('4 5', polyline.getAttribute('points'));
+
+ points.clear();
+ testing.expectEqual('', polyline.getAttribute('points'));
+}
+</script>
+
+<script id=pointListErrors>
+{
+ const root = $('#root');
+ const points = $('#polylineErrors').points;
- testing.expectEqual(detached, points.removeItem(1));
- detached.y = 12;
- testing.expectEqual(12, detached.y);
testing.expectError('IndexSizeError', () => points.getItem(99));
testing.expectError('IndexSizeError', () => points.replaceItem(root.createSVGPoint(), 99));
testing.expectError('IndexSizeError', () => points.removeItem(99));
+}
+</script>
+
+<!-- SVGPoint's coordinates are a restricted float, unlike DOMPoint's. -->
+<script id=pointCoordinatesAreRestricted>
+{
+ const point = $('#root').createSVGPoint();
+ point.x = 100;
+ testing.expectError('TypeError', () => { point.x = NaN; });
+ testing.expectError('TypeError', () => { point.x = Infinity; });
+ testing.expectError('TypeError', () => { point.x = point; });
+ testing.expectEqual(100, point.x);
+
+ point.y = null;
+ testing.expectEqual(0, point.y);
+}
+</script>
+
+<script id=pointListReadOnly>
+{
+ const root = $('#root');
+ const polygon = $('#polygonReadOnly');
+ const animated = polygon.animatedPoints;
testing.expectError('NoModificationAllowedError', () => animated.clear());
testing.expectError('NoModificationAllowedError', () => animated.appendItem(root.createSVGPoint()));
testing.expectError('NoModificationAllowedError', () => { animated.getItem(0).x = 4; });
+
+ // Read-only is sticky: an external attribute change drops the item from the
+ // list, but it must not become a writable orphan.
+ const stale = animated.getItem(0);
+ polygon.setAttribute('points', '9 9');
+ testing.expectEqual(1, animated.length);
+ testing.expectError('NoModificationAllowedError', () => { stale.x = 4; });
}
</script>
-<script id=pointListCapacityAndPolyline>
+<script id=pointListCapacity>
{
const root = $('#root');
- const list = $('#many').points;
+ const points = $('#many').points;
for (let i = 0; i < 70; i++) {
const point = root.createSVGPoint();
point.x = i;
point.y = -i;
- list.appendItem(point);
+ points.appendItem(point);
}
- testing.expectEqual(70, list.numberOfItems);
- testing.expectEqual(69, list.getItem(69).x);
- testing.expectEqual(-69, list.getItem(69).y);
-
- const polyline = $('#polyline');
- testing.expectEqual(true, polyline.points instanceof SVGPointList);
- testing.expectEqual(2, polyline.points.length);
- const initialized = root.createSVGPoint();
- initialized.x = 4;
- initialized.y = 5;
- polyline.points.initialize(initialized);
- testing.expectEqual('4,5', polyline.getAttribute('points'));
- polyline.points.clear();
- testing.expectEqual('', polyline.getAttribute('points'));
+ testing.expectEqual(70, points.numberOfItems);
+ testing.expectEqual(69, points.getItem(69).x);
+ testing.expectEqual(-69, points.getItem(69).y);
}
</script>
-<script id=transformLists>
+<!-- Fails in Chrome, passes in Firefox; see pointListIdentity. -->
+<script id=transformListIdentity>
{
const root = $('#root');
const group = $('#group');
const animated = group.transform;
+
+ testing.expectTrue(animated === group.transform);
+ testing.expectTrue(animated.baseVal === animated.baseVal);
+ testing.expectTrue(animated.animVal === animated.animVal);
+ testing.expectFalse(animated.baseVal === animated.animVal);
+ testing.expectTrue(animated.baseVal.getItem(1) === animated.baseVal.getItem(1));
+ testing.expectTrue(animated.baseVal.getItem(1).matrix === animated.baseVal.getItem(1).matrix);
+
+ const list = $('#transformIdentity').transform.baseVal;
+ const appended = root.createSVGTransform();
+ testing.expectEqual(appended, list.appendItem(appended));
+ const inserted = root.createSVGTransform();
+ testing.expectEqual(inserted, list.insertItemBefore(inserted, 0));
+ const replacement = root.createSVGTransform();
+ testing.expectEqual(replacement, list.replaceItem(replacement, 1));
+ testing.expectEqual(replacement, list.removeItem(1));
+ const initialized = root.createSVGTransform();
+ testing.expectEqual(initialized, list.initialize(initialized));
+}
+</script>
+
+<script id=transformListBasics>
+{
+ const group = $('#group');
+ const animated = group.transform;
const base = animated.baseVal;
- const anim = animated.animVal;
-
- testing.expectEqual(true, animated instanceof SVGAnimatedTransformList);
- testing.expectEqual(true, animated === group.transform);
- testing.expectEqual(true, base === animated.baseVal);
- testing.expectEqual(true, anim === animated.animVal);
- testing.expectEqual(false, base === anim);
- testing.expectEqual(true, base instanceof SVGTransformList);
+
+ testing.expectTrue(animated instanceof SVGAnimatedTransformList);
+ testing.expectTrue(base instanceof SVGTransformList);
testing.expectEqual(2, base.numberOfItems);
+ testing.expectEqual(2, base.length);
testing.expectEqual(SVGTransform.SVG_TRANSFORM_TRANSLATE, base.getItem(0).type);
testing.expectEqual(10, base.getItem(0).matrix.e);
+ testing.expectEqual(20, base.getItem(0).matrix.f);
testing.expectEqual(SVGTransform.SVG_TRANSFORM_ROTATE, base.getItem(1).type);
testing.expectEqual(45, base.getItem(1).angle);
- testing.expectEqual(true, base.getItem(1).matrix === base.getItem(1).matrix);
+}
+</script>
- const oldTransform = base.getItem(0);
- oldTransform.setTranslate(3, 4);
- testing.expectEqual(true, group.getAttribute('transform').startsWith('translate(3 4)'));
- testing.expectEqual(3, anim.getItem(0).matrix.e);
+<script id=transformListLive>
+{
+ const group = $('#transformLive');
+ const base = group.transform.baseVal;
+
+ base.getItem(0).setTranslate(3, 4);
+ testing.expectEqual('translate(3 4)', group.getAttribute('transform').slice(0, 14));
+ testing.expectEqual(3, group.transform.animVal.getItem(0).matrix.e);
+
+ base.getItem(1).setSkewX(15);
+ testing.expectEqual(SVGTransform.SVG_TRANSFORM_SKEWX, base.getItem(1).type);
+ testing.expectEqual('translate(3 4) skewX(15)', group.getAttribute('transform'));
+}
+</script>
+
+<script id=transformListExternalMutation>
+{
+ const group = $('#transformExternal');
+ const base = group.transform.baseVal;
group.setAttribute('transform', 'scale(2)');
testing.expectEqual(1, base.length);
+ testing.expectEqual(SVGTransform.SVG_TRANSFORM_SCALE, base.getItem(0).type);
testing.expectEqual(2, base.getItem(0).matrix.a);
- oldTransform.setTranslate(8, 9);
+ testing.expectEqual(2, base.getItem(0).matrix.d);
+}
+</script>
+
+<!--
+ All three engines differ on writing an item that an external attribute change
+ has dropped from its list:
+
+ us the write lands on the orphan; the list and attribute are untouched
+ Chrome lands on the orphan, but the list is re-committed anyway, so the
+ attribute comes back reserialized ("scale(2)" -> "scale(2 2)")
+ Firefox lands on the *list* at that index, because its tear-off proxies
+ slot n rather than the object ("scale(2)" -> "translate(8, 9)")
+
+ SVG 2 reparses the attribute into a fresh set of objects, so the item that was
+ removed is no longer in the list and a write to it should not reach it.
+-->
+<script id=staleItemWrites>
+{
+ const group = $('#transformStale');
+ const base = group.transform.baseVal;
+ const stale = base.getItem(0);
+
+ group.setAttribute('transform', 'scale(2)');
+ testing.expectEqual(1, base.length);
+ stale.setTranslate(8, 9);
testing.expectEqual('scale(2)', group.getAttribute('transform'));
+ testing.expectEqual(SVGTransform.SVG_TRANSFORM_SCALE, base.getItem(0).type);
+ testing.expectEqual(2, base.getItem(0).matrix.a);
- const detached = root.createSVGTransform();
- detached.setSkewX(15);
- testing.expectEqual(detached, base.appendItem(detached));
- const copied = base.appendItem(detached);
- testing.expectEqual(false, copied === detached);
- testing.expectEqual(3, base.length);
+ const polyline = $('#polylineStale');
+ const points = polyline.points;
+ const stalePoint = points.getItem(0);
+ polyline.setAttribute('points', '9 9');
+ testing.expectEqual(1, points.length);
+ stalePoint.x = 5;
+ testing.expectEqual('9 9', polyline.getAttribute('points'));
+ testing.expectEqual(9, points.getItem(0).x);
+
+ // A live matrix orphaned the same way keeps working as a plain matrix.
+ const matrixGroup = $('#matrixStaleMatrix');
+ const matrixBase = matrixGroup.transform.baseVal;
+ const matrix = matrixBase.getItem(0).matrix;
+ matrixGroup.setAttribute('transform', 'scale(4)');
+ testing.expectEqual(1, matrixBase.length);
+ matrix.e = 11;
+ testing.expectEqual(11, matrix.e);
+ testing.expectEqual(0, matrixBase.getItem(0).matrix.e);
+ testing.expectEqual(4, matrixBase.getItem(0).matrix.a);
+}
+</script>
- testing.expectError('IndexSizeError', () => base.getItem(99));
- testing.expectError('IndexSizeError', () => base.replaceItem(root.createSVGTransform(), 99));
- testing.expectError('IndexSizeError', () => base.removeItem(99));
- testing.expectError('NoModificationAllowedError', () => anim.clear());
- testing.expectError('NoModificationAllowedError', () => anim.appendItem(root.createSVGTransform()));
- testing.expectError('NoModificationAllowedError', () => anim.getItem(0).setScale(3, 3));
+<script id=transformListMutators>
+{
+ const root = $('#root');
+ const group = $('#transformMutators');
+ const base = group.transform.baseVal;
+
+ const scale = root.createSVGTransform();
+ scale.setScale(2, 3);
+ base.appendItem(scale);
+ testing.expectEqual('translate(1 2) scale(2 3)', group.getAttribute('transform'));
+
+ const removed = base.removeItem(0);
+ testing.expectEqual(SVGTransform.SVG_TRANSFORM_TRANSLATE, removed.type);
+ testing.expectEqual('scale(2 3)', group.getAttribute('transform'));
+
+ const rotate = root.createSVGTransform();
+ rotate.setRotate(30, 5, 6);
+ base.insertItemBefore(rotate, 0);
+ testing.expectEqual('rotate(30 5 6) scale(2 3)', group.getAttribute('transform'));
+
+ const initialized = root.createSVGTransform();
+ initialized.setTranslate(7, 8);
+ base.initialize(initialized);
+ testing.expectEqual('translate(7 8)', group.getAttribute('transform'));
+ base.clear();
+ testing.expectEqual('', group.getAttribute('transform'));
+}
+</script>
+
+<script id=transformListConsolidate>
+{
+ const base = $('#transformConsolidate').transform.baseVal;
+
+ testing.expectEqual(2, base.numberOfItems);
const consolidated = base.consolidate();
- testing.expectEqual(true, consolidated instanceof SVGTransform);
testing.expectEqual(SVGTransform.SVG_TRANSFORM_MATRIX, consolidated.type);
- testing.expectEqual(1, base.length);
- testing.expectEqual(true, group.getAttribute('transform').startsWith('matrix('));
+ testing.expectEqual(1, base.numberOfItems);
+ testing.expectEqual(2, consolidated.matrix.a);
+ testing.expectEqual(2, consolidated.matrix.d);
+ testing.expectEqual(10, consolidated.matrix.e);
+ testing.expectEqual(20, consolidated.matrix.f);
}
</script>
-<script id=transformMatrixReflection>
+<!--
+ Chrome updates the list but leaves the attribute alone, so after consolidating
+ to a single matrix the attribute still reads "rotate(...)" while the DOM says
+ one SVG_TRANSFORM_MATRIX item. We commit, like every other mutator does: our
+ list is rebuilt from the attribute on the next read, so not committing would
+ undo the consolidate on the very next access.
+-->
+<script id=transformListConsolidateCommits>
+{
+ const group = $('#transformConsolidateCommit');
+ group.transform.baseVal.consolidate();
+ testing.expectEqual('matrix(', group.getAttribute('transform').slice(0, 7));
+ testing.expectEqual(1, group.transform.baseVal.numberOfItems);
+}
+</script>
+
+<script id=transformListReadOnly>
{
const root = $('#root');
+ const group = $('#transformReadOnly');
+ const anim = group.transform.animVal;
+
+ testing.expectError('NoModificationAllowedError', () => anim.clear());
+ testing.expectError('NoModificationAllowedError', () => anim.appendItem(root.createSVGTransform()));
+ testing.expectError('NoModificationAllowedError', () => anim.getItem(0).setTranslate(1, 1));
+
+ // Read-only is sticky, see pointListReadOnly.
+ const stale = anim.getItem(0);
+ group.setAttribute('transform', 'scale(3)');
+ testing.expectEqual(1, anim.length);
+ testing.expectError('NoModificationAllowedError', () => stale.setTranslate(1, 1));
+}
+</script>
+
+<script id=transformMatrixLive>
+{
const group = $('#matrixGroup');
const base = group.transform.baseVal;
const anim = group.transform.animVal;
const transform = base.getItem(0);
const matrix = transform.matrix;
- testing.expectEqual(true, matrix === transform.matrix);
+ testing.expectTrue(matrix === transform.matrix);
+
matrix.e = 5;
testing.expectEqual(SVGTransform.SVG_TRANSFORM_MATRIX, transform.type);
testing.expectEqual(0, transform.angle);
- testing.expectEqual(true, group.getAttribute('transform').startsWith('matrix('));
+ testing.expectEqual('matrix(1 0 0 1 5 2)', group.getAttribute('transform'));
testing.expectEqual(5, anim.getItem(0).matrix.e);
+ testing.expectEqual(1, base.length);
+}
+</script>
+
+<script id=transformMatrixReadOnly>
+{
+ const group = $('#matrixReadOnly');
+ const animMatrix = group.transform.animVal.getItem(0).matrix;
+
+ testing.expectError('NoModificationAllowedError', () => { animMatrix.e = 99; });
+ testing.expectEqual('translate(1 2)', group.getAttribute('transform'));
+ testing.expectEqual(1, animMatrix.e);
+}
+</script>
+
+<!--
+ SVG 2 types SVGTransform.matrix as a DOMMatrix (SVGMatrix is a typedef for it).
+ Chrome still returns the legacy SVGMatrix, which has none of these members, so
+ this block and the next one only run here.
+-->
+<script id=transformMatrixDomMatrixApi>
+{
+ const root = $('#root');
+ const transform = root.createSVGTransform();
+ const matrix = transform.matrix;
testing.expectEqual(matrix, matrix.translateSelf(2, 3));
- testing.expectEqual(7, matrix.e);
- testing.expectEqual(5, matrix.f);
- testing.expectEqual(7, anim.getItem(0).matrix.e);
- testing.expectEqual(5, anim.getItem(0).matrix.f);
+ testing.expectEqual(2, matrix.e);
+ testing.expectEqual(3, matrix.f);
+ testing.expectEqual(SVGTransform.SVG_TRANSFORM_MATRIX, transform.type);
testing.expectEqual(matrix, matrix.setMatrixValue('matrix(1, 0, 0, 1, 9, 10)'));
testing.expectEqual(9, matrix.e);
testing.expectEqual(10, matrix.f);
- testing.expectEqual(9, anim.getItem(0).matrix.e);
- testing.expectEqual(10, anim.getItem(0).matrix.f);
+}
+</script>
- const animMatrix = anim.getItem(0).matrix;
- const beforeReadOnlyWrites = group.getAttribute('transform');
- testing.expectError('NoModificationAllowedError', () => { animMatrix.e = 99; });
- testing.expectError('NoModificationAllowedError', () => animMatrix.translateSelf(1, 1));
- testing.expectError('NoModificationAllowedError', () => animMatrix.setMatrixValue('scale(2)'));
- testing.expectEqual(beforeReadOnlyWrites, group.getAttribute('transform'));
- testing.expectEqual(9, animMatrix.e);
+<script id=transformMatrixIs2D>
+{
+ const root = $('#root');
+ const group = $('#matrixIs2D');
+ const transform = root.createSVGTransform();
+ const matrix = transform.matrix;
- group.setAttribute('transform', 'scale(4)');
- testing.expectEqual(1, base.length);
- matrix.e = 11;
- testing.expectEqual('scale(4)', group.getAttribute('transform'));
+ // An SVGTransform is a 2D affine transform and the `transform` attribute has
+ // no syntax for anything else, so the 3D components are dropped.
+ matrix.m13 = 2;
testing.expectEqual(SVGTransform.SVG_TRANSFORM_MATRIX, transform.type);
- testing.expectEqual(11, matrix.e);
+ testing.expectTrue(matrix.is2D);
+ testing.expectEqual(0, matrix.m13);
- const detached = root.createSVGTransform();
- const detachedMatrix = detached.matrix;
- detachedMatrix.e = 13;
- testing.expectEqual(SVGTransform.SVG_TRANSFORM_MATRIX, detached.type);
- testing.expectEqual(true, detachedMatrix === detached.matrix);
- detachedMatrix.m13 = 2;
- testing.expectEqual(SVGTransform.SVG_TRANSFORM_UNKNOWN, detached.type);
- testing.expectEqual(false, detachedMatrix.is2D);
-
- testing.expectEqual(detached, base.initialize(detached));
- testing.expectEqual(true, group.getAttribute('transform').startsWith('matrix3d('));
- testing.expectEqual(2, base.getItem(0).matrix.m13);
- testing.expectEqual(2, anim.getItem(0).matrix.m13);
- testing.expectError('NoModificationAllowedError', () => { anim.getItem(0).matrix.m13 = 3; });
+ matrix.setMatrixValue('translate3d(1px, 2px, 3px)');
+ testing.expectTrue(matrix.is2D);
+ testing.expectEqual(1, matrix.e);
+ testing.expectEqual(2, matrix.f);
+ testing.expectEqual(0, matrix.m43);
- const consolidated = base.consolidate();
- testing.expectEqual(SVGTransform.SVG_TRANSFORM_UNKNOWN, consolidated.type);
- testing.expectEqual(false, consolidated.matrix.is2D);
- testing.expectEqual(true, group.getAttribute('transform').startsWith('matrix3d('));
-
- const replaceGroup = $('#replaceGroup');
- const replacement = root.createSVGTransform();
- replacement.setScale(10, 1);
- testing.expectEqual(replacement, replaceGroup.transform.baseVal.replaceItem(replacement, 0));
- testing.expectEqual(replacement, replaceGroup.transform.baseVal.getItem(0));
- testing.expectEqual(10, replacement.matrix.a);
- testing.expectEqual(true, replaceGroup.getAttribute('transform').startsWith('scale(10 1)'));
+ group.transform.baseVal.initialize(transform);
+ testing.expectEqual('matrix(1 0 0 1 1 2)', group.getAttribute('transform'));
}
</script>
-<script id=transformParserRegression>
+<script id=transformParser>
{
- const group = $('#group');
+ const group = $('#transformParse');
+ const base = group.transform.baseVal;
+
+ testing.expectEqual(3, base.length);
+ testing.expectEqual(2, base.getItem(0).matrix.f);
+ testing.expectEqual(4, base.getItem(1).matrix.d);
+ testing.expectEqual(30, base.getItem(2).angle);
+
group.setAttribute('transform', 'translate()');
- testing.expectEqual(0, group.transform.baseVal.length);
+ testing.expectEqual(0, base.length);
testing.expectEqual('translate()', group.getAttribute('transform'));
- group.setAttribute('transform', 'translate(1 2) scale(3,4) rotate(30 5 6)');
- testing.expectEqual(3, group.transform.baseVal.length);
- testing.expectEqual(2, group.transform.baseVal.getItem(0).matrix.f);
- testing.expectEqual(4, group.transform.baseVal.getItem(1).matrix.d);
- testing.expectEqual(30, group.transform.baseVal.getItem(2).angle);
+ // No 3D function is part of the SVG transform grammar.
+ group.setAttribute('transform', 'matrix3d(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1)');
+ testing.expectEqual(0, base.length);
+}
+</script>
+<script id=domMatrixTransformString>
+{
testing.expectError('SyntaxError', () => new DOMMatrix('translate()'));
testing.expectError('SyntaxError', () => new DOMMatrix('scale()'));
- const matrix = new DOMMatrix('translate(3px 4px) scale(2 3)');
+
+ const matrix = new DOMMatrix('translate(3px, 4px) scale(2, 3)');
testing.expectEqual(3, matrix.e);
testing.expectEqual(4, matrix.f);
+ testing.expectEqual(2, matrix.a);
}
</script>
-<script id=stringLists>
+<!--
+ The SVG grammar's separators are not CSS syntax: a DOMMatrix string wants
+ commas between arguments, even though `transform="translate(3 4)"` does not.
+-->
+<script id=domMatrixRejectsSvgSeparators>
{
- const group = $('#group');
+ testing.expectError('SyntaxError', () => new DOMMatrix('translate(3px 4px) scale(2 3)'));
+ testing.expectError('SyntaxError', () => new DOMMatrix('translate(3 4)'));
+ testing.expectError('SyntaxError', () => new DOMMatrix('matrix(1 0 0 1 5 6)'));
+ testing.expectError('SyntaxError', () => new DOMMatrix('translate(3px-4px)'));
+
+ const matrix = new DOMMatrix('translate(3px, 4px) scale(2, 3)');
+ testing.expectEqual(3, matrix.e);
+ testing.expectEqual(2, matrix.a);
+}
+</script>
+
+<script id=stringListBasics>
+{
+ const group = $('#strings');
const required = group.requiredExtensions;
const languages = group.systemLanguage;
- testing.expectEqual(true, required instanceof SVGStringList);
- testing.expectEqual(true, required === group.requiredExtensions);
- testing.expectEqual(true, languages === group.systemLanguage);
+ testing.expectTrue(required instanceof SVGStringList);
+ testing.expectTrue(required === group.requiredExtensions);
+ testing.expectTrue(languages === group.systemLanguage);
testing.expectEqual(2, required.numberOfItems);
testing.expectEqual('urn:one', required.getItem(0));
+ testing.expectEqual('urn:two', required.getItem(1));
testing.expectEqual(2, languages.length);
+ testing.expectEqual('en', languages.getItem(0));
testing.expectEqual('fr', languages.getItem(1));
+ testing.expectError('IndexSizeError', () => required.getItem(99));
+}
+</script>
+
+<script id=stringListMutators>
+{
+ const group = $('#stringMutators');
+ const required = group.requiredExtensions;
required.appendItem('urn:three');
testing.expectEqual('urn:one urn:two urn:three', group.getAttribute('requiredExtensions'));
- testing.expectEqual('urn:two', required.removeItem(1));
+ required.removeItem(1);
testing.expectEqual('urn:one urn:three', group.getAttribute('requiredExtensions'));
- testing.expectError('SyntaxError', () => required.appendItem('two words'));
- testing.expectError('IndexSizeError', () => required.getItem(99));
+ required.insertItemBefore('urn:zero', 0);
+ testing.expectEqual('urn:zero urn:one urn:three', group.getAttribute('requiredExtensions'));
+ required.replaceItem('urn:x', 1);
+ testing.expectEqual('urn:zero urn:x urn:three', group.getAttribute('requiredExtensions'));
+ required.initialize('urn:only');
+ testing.expectEqual('urn:only', group.getAttribute('requiredExtensions'));
+
+ required.clear();
+ testing.expectEqual(0, required.length);
+}
+</script>
+
+<!-- Emptying a string list removes the attribute in Chrome but blanks it in
+ Firefox, which blanks it for point and transform lists too. Chrome is the
+ inconsistent one here; we follow it. -->
+<script id=stringListClearRemovesAttribute>
+{
+ const group = $('#stringClear');
+ group.requiredExtensions.clear();
+ testing.expectEqual(null, group.getAttribute('requiredExtensions'));
+}
+</script>
+
+<!-- Firefox returns "" from SVGStringList.removeItem; Chrome and the spec
+ return the removed item. -->
+<script id=stringListMutatorReturns>
+{
+ const required = $('#stringReturns').requiredExtensions;
+
+ testing.expectEqual('urn:three', required.appendItem('urn:three'));
+ testing.expectEqual('urn:two', required.removeItem(1));
+ testing.expectEqual('urn:zero', required.insertItemBefore('urn:zero', 0));
+ testing.expectEqual('urn:x', required.replaceItem('urn:x', 1));
+ testing.expectEqual('urn:only', required.initialize('urn:only'));
+}
+</script>
+
+<script id=stringListDelimiter>
+{
+ const group = $('#stringLanguages');
+ const languages = group.systemLanguage;
languages.insertItemBefore('de', 1);
- testing.expectEqual('en, de, fr', group.getAttribute('systemLanguage'));
+ testing.expectEqual('en,de,fr', group.getAttribute('systemLanguage'));
+}
+</script>
+
+<!--
+ systemLanguage is a set of comma-separated tokens: every segment is a token,
+ including an empty one, and each is trimmed. Mirrors the cases in
+ wpt/svg/struct/systemLanguage-parsing.html, which we can't run yet because
+ <text> is not an SVGGraphicsElement here.
+-->
+<script id=stringListCommaTokens>
+{
+ const group = $('#stringTokens');
+ const languages = group.systemLanguage;
+
+ const parsesAs = (input, expected) => {
+ group.setAttribute('systemLanguage', input);
+ testing.expectEqual(expected.length, languages.length);
+ for (let i = 0; i < expected.length; i++) {
+ testing.expectEqual(expected[i], languages.getItem(i));
+ }
+ };
+
+ parsesAs('en,fr,de', ['en', 'fr', 'de']);
+ parsesAs('en , fr , de', ['en', 'fr', 'de']);
+ parsesAs(' en, fr ', ['en', 'fr']);
+ parsesAs(' \t\nen, fr\t\n ', ['en', 'fr']);
+ parsesAs('en', ['en']);
+ parsesAs('en-US, zh-Hans', ['en-US', 'zh-Hans']);
+ parsesAs('en,,fr', ['en', '', 'fr']);
+ parsesAs('', ['']);
+ parsesAs(',', ['', '']);
+ parsesAs('not-a-lang, ???', ['not-a-lang', '???']);
+
+ // An absent attribute is an empty list, unlike an empty one.
+ group.removeAttribute('systemLanguage');
+ testing.expectEqual(0, languages.length);
+}
+</script>
+
+<script id=stringListExternalMutation>
+{
+ const group = $('#stringExternal');
+ const languages = group.systemLanguage;
+
group.setAttribute('systemLanguage', 'ja, ko');
testing.expectEqual(2, languages.length);
testing.expectEqual('ko', languages.getItem(1));
- group.setAttribute('systemLanguage', 'en,,fr');
- testing.expectEqual(0, languages.length);
- testing.expectEqual('en,,fr', group.getAttribute('systemLanguage'));
+
group.setAttribute('systemLanguage', 'it, pt');
testing.expectEqual(2, languages.length);
testing.expectEqual('pt', languages.getItem(1));
}
</script>
+
+<script id=stringListNoValidation>
+{
+ const group = $('#stringNoValidation');
+ const required = group.requiredExtensions;
+
+ // Neither the spec nor Chrome validates the inserted string against the
+ // list's delimiter, and the list stays authoritative over its own
+ // serialization, so this remains one item.
+ required.appendItem('two words');
+ testing.expectEqual('urn:one two words', group.getAttribute('requiredExtensions'));
+ testing.expectEqual(2, required.length);
+ testing.expectEqual('two words', required.getItem(1));
+
+ // Until the attribute is written from the outside, which reparses.
+ group.setAttribute('requiredExtensions', 'urn:one three more words');
+ testing.expectEqual(4, required.length);
+ testing.expectEqual('three', required.getItem(1));
+}
+</script>
diff --git a/src/browser/webapi/DOMMatrixReadOnly.zig b/src/browser/webapi/DOMMatrixReadOnly.zig
index d86f8dd66..e70658e25 100644
--- a/src/browser/webapi/DOMMatrixReadOnly.zig
+++ b/src/browser/webapi/DOMMatrixReadOnly.zig
@@ -56,9 +56,10 @@ pub const State = struct {
is_2d: bool,
};
+// The owner decides whether a write is allowed; a matrix that belongs to an
+// animVal SVGTransform is rejected by the transform, not here.
pub const Attachment = struct {
owner: *anyopaque,
- read_only: bool,
mutate: *const fn (*anyopaque, *DOMMatrixReadOnly, State) anyerror!void,
};
@@ -103,7 +104,6 @@ pub fn getState(self: *const DOMMatrixReadOnly) State {
pub fn applyState(self: *DOMMatrixReadOnly, state: State) !void {
if (self._attachment) |attachment| {
- if (attachment.read_only) return error.NoModificationAllowed;
return attachment.mutate(attachment.owner, self, state);
}
self.applyStateRaw(state);
@@ -118,16 +118,6 @@ pub fn attach(self: *DOMMatrixReadOnly, attachment: Attachment) void {
self._attachment = attachment;
}
-pub fn detach(self: *DOMMatrixReadOnly, owner: *anyopaque) void {
- const attachment = self._attachment orelse return;
- if (attachment.owner == owner) self._attachment = null;
-}
-
-pub fn isAttachedTo(self: *const DOMMatrixReadOnly, owner: *anyopaque) bool {
- const attachment = self._attachment orelse return false;
- return attachment.owner == owner;
-}
-
pub const DOMMatrixInit = struct {
a: ?f64 = null,
b: ?f64 = null,
@@ -462,7 +452,7 @@ pub fn parseTransformList(input: []const u8, m: *[16]f64, is_2d: *bool) !void {
pub fn parseTransformFunction(function: TransformFunction, syntax: TransformSyntax) !ParsedTransform {
var values: [16]f64 = undefined;
var units: [16]ParsedValue.Unit = undefined;
- const count = try parseArguments(function.arguments, &values, &units);
+ const count = try parseArguments(function.arguments, syntax, &values, &units);
const name = function.name;
const Eql = std.mem.eql;
@@ -478,6 +468,7 @@ pub fn parseTransformFunction(function: TransformFunction, syntax: TransformSynt
}
if (Eql(u8, name, "matrix3d")) {
+ if (syntax == .svg) return error.SyntaxError;
try requireCount(count, 16, 16);
try requireUnitless(units[0..count]);
return makeParsedTransform(.matrix3d, values, values, count, false);
@@ -613,8 +604,8 @@ fn requireAngle(unit: ParsedValue.Unit) !void {
if (unit == .other) return error.SyntaxError;
}
-fn parseArguments(arguments: []const u8, values: *[16]f64, units: *[16]ParsedValue.Unit) !usize {
- var scanner = ArgumentScanner{ .input = arguments };
+fn parseArguments(arguments: []const u8, syntax: TransformSyntax, values: *[16]f64, units: *[16]ParsedValue.Unit) !usize {
+ var scanner = ArgumentScanner{ .input = arguments, .syntax = syntax };
var count: usize = 0;
while (try scanner.next()) |value| {
if (count == values.len) return error.SyntaxError;
@@ -873,6 +864,7 @@ const ParsedValue = struct {
const ArgumentScanner = struct {
input: []const u8,
+ syntax: TransformSyntax,
index: usize = 0,
first: bool = true,
@@ -887,10 +879,14 @@ const ArgumentScanner = struct {
self.index += 1;
while (self.index < self.input.len and std.ascii.isWhitespace(self.input[self.index])) self.index += 1;
if (self.index == self.input.len) return error.SyntaxError;
- } else if (!self.first and self.index < self.input.len and !had_whitespace and
- self.input[self.index] != '+' and self.input[self.index] != '-')
- {
- return error.SyntaxError;
+ } else if (!self.first and self.index < self.input.len) {
+ // CSS separates arguments with a comma and nothing else. The SVG
+ // grammar also takes whitespace, or no separator at all when the
+ // next number carries its own sign.
+ if (self.syntax == .css) return error.SyntaxError;
+ if (!had_whitespace and self.input[self.index] != '+' and self.input[self.index] != '-') {
+ return error.SyntaxError;
+ }
}
if (self.index == self.input.len) return null;
diff --git a/src/browser/webapi/DOMPointReadOnly.zig b/src/browser/webapi/DOMPointReadOnly.zig
index 525d0c662..a0bf84a2f 100644
--- a/src/browser/webapi/DOMPointReadOnly.zig
+++ b/src/browser/webapi/DOMPointReadOnly.zig
@@ -40,11 +40,19 @@ _z: f64,
_w: f64,
_attachment: ?Attachment = null,
+// Sticky. An animVal item that a later external attribute change detaches from
+// its list must not turn into a writable orphan.
+_read_only: bool = false,
+
+// SVGPoint's coordinates are a restricted float: NaN and infinity are a
+// TypeError rather than a stored value. Only points that reach an SVG list are
+// restricted; a plain DOMPoint takes an unrestricted double.
+_restricted: bool = false,
+
pub const Coordinate = enum { x, y, z, w };
pub const Attachment = struct {
owner: *anyopaque,
- read_only: bool,
mutate: *const fn (*anyopaque, *DOMPointReadOnly, Coordinate, f64) anyerror!void,
};
@@ -142,8 +150,9 @@ pub fn getW(self: *const DOMPointReadOnly) f64 {
}
pub fn setCoordinate(self: *DOMPointReadOnly, coordinate: Coordinate, value: f64) !void {
+ if (self._read_only) return error.NoModificationAllowed;
+ if (self._restricted and !std.math.isFinite(value)) return error.TypeError;
if (self._attachment) |attachment| {
- if (attachment.read_only) return error.NoModificationAllowed;
return attachment.mutate(attachment.owner, self, coordinate, value);
}
self.setCoordinateRaw(coordinate, value);
@@ -158,8 +167,13 @@ pub fn setCoordinateRaw(self: *DOMPointReadOnly, coordinate: Coordinate, value:
}
}
-pub fn attach(self: *DOMPointReadOnly, attachment: Attachment) void {
+pub fn restrict(self: *DOMPointReadOnly) void {
+ self._restricted = true;
+}
+
+pub fn attach(self: *DOMPointReadOnly, attachment: Attachment, read_only: bool) void {
self._attachment = attachment;
+ if (read_only) self._read_only = true;
}
pub fn detach(self: *DOMPointReadOnly, owner: *anyopaque) void {
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index a81b82b66..665b9402c 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -856,6 +856,10 @@ pub fn removeAttribute(self: *Element, name: String, frame: *Frame) !void {
return self._attributes.delete(name, self, frame);
}
+pub fn removeAttributeSafe(self: *Element, name: String, frame: *Frame) void {
+ self._attributes.deleteSafe(name, self, frame);
+}
+
pub fn toggleAttribute(self: *Element, name: String, force: ?bool, frame: *Frame) !bool {
try Attribute.validateAttributeName(name);
const has = try self.hasAttribute(name, frame);
diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zig
index 983c6aa5f..c2023a1ef 100644
--- a/src/browser/webapi/element/Attribute.zig
+++ b/src/browser/webapi/element/Attribute.zig
@@ -315,9 +315,17 @@ pub const List = struct {
pub fn delete(self: *List, name: String, element: *Element, frame: *Frame) !void {
const result = try self.getEntryAndNormalizedName(name, frame);
const entry = result.entry orelse return;
+ return self._delete(entry, result.normalized, element, frame);
+ }
+
+ pub fn deleteSafe(self: *List, name: String, element: *Element, frame: *Frame) void {
+ const entry = self.getEntryWithNormalizedName(name) orelse return;
+ self._delete(entry, name, element, frame);
+ }
+ fn _delete(self: *List, entry: *Entry, normalized: String, element: *Element, frame: *Frame) void {
const owner = element.ownerFrame(frame);
- const is_id = shouldAddToIdMap(result.normalized, element);
+ const is_id = shouldAddToIdMap(normalized, element);
const old_value = entry.value();
if (is_id) {
@@ -333,7 +341,7 @@ pub const List = struct {
self._len -= 1;
owner.domChanged();
- owner.attributeRemove(element, result.normalized, .wrap(old_value));
+ owner.attributeRemove(element, normalized, .wrap(old_value));
}
pub fn getNames(self: *const List, allocator: Allocator) ![][]const u8 {
diff --git a/src/browser/webapi/element/svg/Svg.zig b/src/browser/webapi/element/svg/Svg.zig
index ae6ae220b..42419255d 100644
--- a/src/browser/webapi/element/svg/Svg.zig
+++ b/src/browser/webapi/element/svg/Svg.zig
@@ -82,7 +82,9 @@ pub fn getPreserveAspectRatio(self: *Svg, frame: *Frame) !*AnimatedPreserveAspec
}
pub fn createSVGPoint(_: *Svg, frame: *Frame) !*DOMPoint {
- return DOMPoint.create(0, 0, 0, 1, frame._page);
+ const point = try DOMPoint.create(0, 0, 0, 1, frame._page);
+ point._proto.restrict();
+ return point;
}
pub fn createSVGMatrix(_: *Svg, frame: *Frame) !*DOMMatrix {
diff --git a/src/browser/webapi/svg/PointList.zig b/src/browser/webapi/svg/PointList.zig
index e412b8ed1..6c9a645a4 100644
--- a/src/browser/webapi/svg/PointList.zig
+++ b/src/browser/webapi/svg/PointList.zig
@@ -72,9 +72,8 @@ pub fn getNumberOfItems(self: *PointList, frame: *Frame) !u32 {
pub fn clear(self: *PointList, frame: *Frame) !void {
try self.requireMutable();
try self.sync(frame);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
+ try self.retireAll(frame);
try self.setAttribute(&.{}, frame);
- self.retireAllAssumeCapacity();
}
pub fn initialize(self: *PointList, item: *DOMPoint, frame: *Frame) !*DOMPoint {
@@ -84,10 +83,9 @@ pub fn initialize(self: *PointList, item: *DOMPoint, frame: *Frame) !*DOMPoint {
const prepared = try self.prepareItem(item, frame);
errdefer prepared._proto.releaseRef(frame._page);
+ try self.retireAll(frame);
try self._items.ensureTotalCapacity(frame.arena, 1);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
try self.setAttribute(&.{prepared}, frame);
- self.retireAllAssumeCapacity();
self._items.appendAssumeCapacity(prepared);
self.attach(prepared);
return prepared;
@@ -174,11 +172,11 @@ fn prepareItem(_: *PointList, item: *DOMPoint, frame: *Frame) !*DOMPoint {
}
fn attach(self: *PointList, point: *DOMPoint) void {
+ point._proto.restrict();
point._proto.attach(.{
.owner = self,
- .read_only = self._read_only,
.mutate = PointList.mutatePoint,
- });
+ }, self._read_only);
}
fn mutatePoint(
@@ -220,20 +218,17 @@ fn sync(self: *PointList, frame: *Frame) !void {
return;
}
+ self._synced = false;
var parsed = parse(raw, frame) catch |err| switch (err) {
error.SyntaxError => std.ArrayList(*DOMPoint).empty,
else => return err,
};
errdefer for (parsed.items) |point| point._proto.releaseRef(frame._page);
- try self._snapshot.ensureTotalCapacity(frame.arena, raw.len);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
- try self._items.ensureTotalCapacity(frame.arena, parsed.items.len);
-
- self._synced = false;
self._snapshot.clearRetainingCapacity();
- self._snapshot.appendSliceAssumeCapacity(raw);
- self.retireAllAssumeCapacity();
+ try self._snapshot.appendSlice(frame.arena, raw);
+ try self.retireAll(frame);
+ try self._items.ensureTotalCapacity(frame.arena, parsed.items.len);
for (parsed.items) |point| {
self._items.appendAssumeCapacity(point);
self.attach(point);
@@ -258,7 +253,9 @@ fn parse(raw: []const u8, frame: *Frame) !std.ArrayList(*DOMPoint) {
errdefer for (parsed.items) |point| point._proto.releaseRef(frame._page);
while (try scanner.next()) |x| {
- const y = (try scanner.next()) orelse return error.SyntaxError;
+ // A trailing coordinate with no pair truncates the list; only a
+ // malformed number invalidates the whole attribute.
+ const y = (try scanner.next()) orelse break;
const point = try DOMPoint.create(x, y, 0, 1, frame._page);
point._proto.acquireRef();
parsed.append(frame.local_arena, point) catch |err| {
@@ -269,7 +266,9 @@ fn parse(raw: []const u8, frame: *Frame) !std.ArrayList(*DOMPoint) {
return parsed;
}
-fn retireAllAssumeCapacity(self: *PointList) void {
+fn retireAll(self: *PointList, frame: *Frame) !void {
+ self._synced = false;
+ try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
for (self._items.items) |point| {
point._proto.detach(self);
self._retired.appendAssumeCapacity(point);
@@ -282,7 +281,7 @@ fn setAttribute(self: *PointList, items: []const *DOMPoint, frame: *Frame) !void
const writer = &serialized.writer;
for (items, 0..) |point, i| {
if (i != 0) try writer.writeByte(' ');
- try writer.print("{d},{d}", .{ point._proto._x, point._proto._y });
+ try writer.print("{d} {d}", .{ point._proto._x, point._proto._y });
}
try self.commitAttribute(serialized.written(), frame);
}
@@ -300,17 +299,16 @@ fn setAttributeWithOverride(
if (i != 0) try writer.writeByte(' ');
const x = if (i == index and coordinate == .x) value else point._proto._x;
const y = if (i == index and coordinate == .y) value else point._proto._y;
- try writer.print("{d},{d}", .{ x, y });
+ try writer.print("{d} {d}", .{ x, y });
}
try self.commitAttribute(serialized.written(), frame);
}
fn commitAttribute(self: *PointList, serialized: []const u8, frame: *Frame) !void {
- try self._snapshot.ensureTotalCapacity(frame.arena, serialized.len);
- try self._element.setAttributeSafe(comptime .wrap("points"), .wrap(serialized), frame);
self._synced = false;
+ try self._element.setAttributeSafe(comptime .wrap("points"), .wrap(serialized), frame);
self._snapshot.clearRetainingCapacity();
- self._snapshot.appendSliceAssumeCapacity(serialized);
+ try self._snapshot.appendSlice(frame.arena, serialized);
self._synced = true;
}
@@ -329,7 +327,6 @@ const NumberScanner = struct {
if (!self.first and self.index < self.input.len and self.input[self.index] == ',') {
self.index += 1;
while (self.index < self.input.len and std.ascii.isWhitespace(self.input[self.index])) self.index += 1;
- if (self.index == self.input.len) return error.SyntaxError;
} else if (!self.first and self.index < self.input.len and !had_whitespace and
self.input[self.index] != '+' and self.input[self.index] != '-')
{
diff --git a/src/browser/webapi/svg/StringList.zig b/src/browser/webapi/svg/StringList.zig
index 68a66b05a..ccd217063 100644
--- a/src/browser/webapi/svg/StringList.zig
+++ b/src/browser/webapi/svg/StringList.zig
@@ -20,10 +20,11 @@ _element: *Element,
_attribute_name: lp.String,
_delimiter: Delimiter,
_synced: bool = false,
+// An absent attribute is an empty list, but an empty attribute is not: a
+// comma-separated value parses "" as a single empty token.
+_present: bool = false,
_snapshot: std.ArrayList(u8) = .empty,
_items: std.ArrayList([]const u8) = .empty,
-_scratch_snapshot: std.ArrayList(u8) = .empty,
-_scratch_items: std.ArrayList([]const u8) = .empty,
pub const Kind = enum {
required_extensions,
@@ -82,9 +83,8 @@ pub fn clear(self: *StringList, frame: *Frame) !void {
}
pub fn initialize(self: *StringList, item: []const u8, frame: *Frame) ![]const u8 {
- try self.validateItem(item);
try self.commit(&.{item}, frame);
- return self._items.items[0];
+ return item;
}
pub fn getItem(self: *StringList, index: u32, frame: *Frame) ![]const u8 {
@@ -94,7 +94,6 @@ pub fn getItem(self: *StringList, index: u32, frame: *Frame) ![]const u8 {
}
pub fn insertItemBefore(self: *StringList, item: []const u8, index: u32, frame: *Frame) ![]const u8 {
- try self.validateItem(item);
try self.sync(frame);
const at = @min(@as(usize, index), self._items.items.len);
const next = try frame.local_arena.alloc([]const u8, self._items.items.len + 1);
@@ -102,17 +101,16 @@ pub fn insertItemBefore(self: *StringList, item: []const u8, index: u32, frame:
next[at] = item;
@memcpy(next[at + 1 ..], self._items.items[at..]);
try self.commit(next, frame);
- return self._items.items[at];
+ return item;
}
pub fn replaceItem(self: *StringList, item: []const u8, index: u32, frame: *Frame) ![]const u8 {
- try self.validateItem(item);
try self.sync(frame);
if (index >= self._items.items.len) return error.IndexSizeError;
const next = try frame.local_arena.dupe([]const u8, self._items.items);
next[index] = item;
try self.commit(next, frame);
- return self._items.items[index];
+ return item;
}
pub fn removeItem(self: *StringList, index: u32, frame: *Frame) ![]const u8 {
@@ -130,76 +128,82 @@ pub fn appendItem(self: *StringList, item: []const u8, frame: *Frame) ![]const u
return self.insertItemBefore(item, std.math.maxInt(u32), frame);
}
-fn validateItem(self: *const StringList, item: []const u8) !void {
- if (item.len == 0) return error.SyntaxError;
- for (item) |byte| {
- if (std.ascii.isWhitespace(byte) or (self._delimiter == .comma and byte == ',')) {
- return error.SyntaxError;
- }
- }
-}
-
fn sync(self: *StringList, frame: *Frame) !void {
- const raw = self._element.getAttributeSafe(self._attribute_name) orelse "";
- if (self._synced and std.mem.eql(u8, self._snapshot.items, raw)) return;
- try self.prepare(raw, frame);
- self.publishPrepared();
+ const raw = self._element.getAttributeSafe(self._attribute_name);
+ if (self._synced and self._present == (raw != null) and
+ std.mem.eql(u8, self._snapshot.items, raw orelse ""))
+ {
+ return;
+ }
+ return self.rebuild(raw, frame);
}
+// The list is authoritative: an item keeps its identity even when it contains
+// the delimiter, so we record where each one landed rather than reparsing our
+// own serialization.
fn commit(self: *StringList, items: []const []const u8, frame: *Frame) !void {
+ if (items.len == 0) {
+ self._synced = false;
+ self._element.removeAttributeSafe(self._attribute_name, frame);
+ self._present = false;
+ self._snapshot.clearRetainingCapacity();
+ self._items.clearRetainingCapacity();
+ self._synced = true;
+ return;
+ }
+
+ const separator: []const u8 = if (self._delimiter == .comma) "," else " ";
var serialized: std.Io.Writer.Allocating = .init(frame.local_arena);
const writer = &serialized.writer;
- for (items, 0..) |item, i| {
- try self.validateItem(item);
- if (i != 0) try writer.writeAll(if (self._delimiter == .comma) ", " else " ");
+ const bounds = try frame.local_arena.alloc([2]usize, items.len);
+ for (items, bounds, 0..) |item, *bound, i| {
+ if (i != 0) try writer.writeAll(separator);
+ const start = serialized.written().len;
try writer.writeAll(item);
+ bound.* = .{ start, serialized.written().len };
}
+ const bytes = serialized.written();
- const serialized_bytes = serialized.written();
- try self.prepare(serialized_bytes, frame);
- try self._element.setAttributeSafe(self._attribute_name, .wrap(self._scratch_snapshot.items), frame);
- self.publishPrepared();
-}
-
-fn prepare(self: *StringList, raw: []const u8, frame: *Frame) !void {
- self._scratch_snapshot.clearRetainingCapacity();
- try self._scratch_snapshot.appendSlice(frame.arena, raw);
- const parsed = parse(self._scratch_snapshot.items, self._delimiter, frame.local_arena) catch |err| switch (err) {
- error.SyntaxError => std.ArrayList([]const u8).empty,
- else => return err,
- };
- self._scratch_items.clearRetainingCapacity();
- try self._scratch_items.appendSlice(frame.arena, parsed.items);
-}
-
-fn publishPrepared(self: *StringList) void {
self._synced = false;
- std.mem.swap(std.ArrayList(u8), &self._snapshot, &self._scratch_snapshot);
- std.mem.swap(std.ArrayList([]const u8), &self._items, &self._scratch_items);
+ try self._element.setAttributeSafe(self._attribute_name, .wrap(bytes), frame);
+ self._present = true;
+ self._snapshot.clearRetainingCapacity();
+ try self._snapshot.appendSlice(frame.arena, bytes);
+ self._items.clearRetainingCapacity();
+ for (bounds) |bound| {
+ try self._items.append(frame.arena, self._snapshot.items[bound[0]..bound[1]]);
+ }
self._synced = true;
}
-fn parse(raw: []const u8, delimiter: Delimiter, allocator: std.mem.Allocator) !std.ArrayList([]const u8) {
- var items: std.ArrayList([]const u8) = .empty;
- switch (delimiter) {
- .whitespace => {
- var iterator = std.mem.tokenizeAny(u8, raw, " \t\r\n\x0c");
- while (iterator.next()) |item| try items.append(allocator, item);
- },
- .comma => {
- if (std.mem.trim(u8, raw, " \t\r\n\x0c").len == 0) return items;
- var iterator = std.mem.splitScalar(u8, raw, ',');
- while (iterator.next()) |part| {
- const item = std.mem.trim(u8, part, " \t\r\n\x0c");
- if (item.len == 0) return error.SyntaxError;
- for (item) |byte| if (std.ascii.isWhitespace(byte)) return error.SyntaxError;
- try items.append(allocator, item);
- }
- },
+fn rebuild(self: *StringList, raw: ?[]const u8, frame: *Frame) !void {
+ self._synced = false;
+ self._present = raw != null;
+ self._snapshot.clearRetainingCapacity();
+ self._items.clearRetainingCapacity();
+
+ if (raw) |value| {
+ try self._snapshot.appendSlice(frame.arena, value);
+ switch (self._delimiter) {
+ .whitespace => {
+ var iterator = std.mem.tokenizeAny(u8, self._snapshot.items, WHITESPACE);
+ while (iterator.next()) |item| try self._items.append(frame.arena, item);
+ },
+ // A set of comma-separated tokens: every segment is a token, even
+ // an empty one, and each is trimmed of surrounding whitespace.
+ .comma => {
+ var iterator = std.mem.splitScalar(u8, self._snapshot.items, ',');
+ while (iterator.next()) |part| {
+ try self._items.append(frame.arena, std.mem.trim(u8, part, WHITESPACE));
+ }
+ },
+ }
}
- return items;
+ self._synced = true;
}
+const WHITESPACE = " \t\r\n\x0c";
+
pub const JsApi = struct {
pub const bridge = js.Bridge(StringList);
diff --git a/src/browser/webapi/svg/Transform.zig b/src/browser/webapi/svg/Transform.zig
index d79ab1839..2ebe7dc02 100644
--- a/src/browser/webapi/svg/Transform.zig
+++ b/src/browser/webapi/svg/Transform.zig
@@ -48,6 +48,10 @@ _cy: f64 = 0,
_matrix: *DOMMatrix,
_attachment: ?Attachment = null,
+// Sticky. An animVal item that a later external attribute change detaches from
+// its list must not turn into a writable orphan.
+_read_only: bool = false,
+
pub const State = struct {
typ: u16,
angle: f64,
@@ -59,7 +63,6 @@ pub const State = struct {
pub const Attachment = struct {
owner: *anyopaque,
- read_only: bool,
mutate: *const fn (*anyopaque, *Transform, State) anyerror!void,
};
@@ -78,7 +81,7 @@ pub fn detached(frame: *Frame) !*Transform {
const matrix = try DOMMatrix.create(RO.identity(), true, frame._page);
errdefer matrix._proto.deinit(frame._page);
const self = try frame._factory.create(Transform{ ._matrix = matrix });
- self.attachMatrix(false);
+ self.attachMatrix();
return self;
}
@@ -87,14 +90,13 @@ pub fn fromMatrix(init: ?DOMMatrix2DInit, frame: *Frame) !*Transform {
const matrix = try DOMMatrix.create(parsed.m, true, frame._page);
errdefer matrix._proto.deinit(frame._page);
const self = try frame._factory.create(Transform{ ._matrix = matrix });
- self.attachMatrix(false);
+ self.attachMatrix();
return self;
}
pub fn fromParsed(parsed: RO.ParsedTransform, frame: *Frame) !*Transform {
const typ: u16 = switch (parsed.kind) {
.matrix => 1,
- .matrix3d => 0,
.translate => 2,
.scale => 3,
.rotate => 4,
@@ -111,7 +113,7 @@ pub fn fromParsed(parsed: RO.ParsedTransform, frame: *Frame) !*Transform {
._cy = if (typ == 4 and parsed.count == 3) parsed.values[2] else 0,
._matrix = matrix,
});
- self.attachMatrix(false);
+ self.attachMatrix();
return self;
}
@@ -126,7 +128,7 @@ pub fn clone(self: *const Transform, frame: *Frame) !*Transform {
._cy = current.cy,
._matrix = matrix,
});
- cloned.attachMatrix(false);
+ cloned.attachMatrix();
return cloned;
}
@@ -199,25 +201,22 @@ pub fn applyStateRaw(self: *Transform, state: State) void {
}
fn applyState(self: *Transform, state: State) !void {
+ if (self._read_only) return error.NoModificationAllowed;
try ensureFinite(&state.matrix);
if (self._attachment) |attachment| {
- if (attachment.read_only) return error.NoModificationAllowed;
return attachment.mutate(attachment.owner, self, state);
}
self.applyStateRaw(state);
}
-pub fn attach(self: *Transform, attachment: Attachment) void {
+pub fn attach(self: *Transform, attachment: Attachment, read_only: bool) void {
self._attachment = attachment;
- self.attachMatrix(attachment.read_only);
+ if (read_only) self._read_only = true;
}
pub fn detach(self: *Transform, owner: *anyopaque) void {
const attachment = self._attachment orelse return;
- if (attachment.owner == owner) {
- self._attachment = null;
- self.attachMatrix(false);
- }
+ if (attachment.owner == owner) self._attachment = null;
}
pub fn isAttached(self: *const Transform) bool {
@@ -231,12 +230,6 @@ pub fn isAttachedTo(self: *const Transform, owner: *anyopaque) bool {
pub fn writeState(state: State, writer: anytype) !void {
switch (state.typ) {
- 0 => try writer.print("matrix3d({d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d}, {d})", .{
- state.matrix[0], state.matrix[1], state.matrix[2], state.matrix[3],
- state.matrix[4], state.matrix[5], state.matrix[6], state.matrix[7],
- state.matrix[8], state.matrix[9], state.matrix[10], state.matrix[11],
- state.matrix[12], state.matrix[13], state.matrix[14], state.matrix[15],
- }),
1 => try writer.print("matrix({d} {d} {d} {d} {d} {d})", .{
state.matrix[0], state.matrix[1], state.matrix[4], state.matrix[5], state.matrix[12], state.matrix[13],
}),
@@ -252,23 +245,31 @@ pub fn writeState(state: State, writer: anytype) !void {
}
}
-fn attachMatrix(self: *Transform, read_only: bool) void {
+fn attachMatrix(self: *Transform) void {
self._matrix._proto.attach(.{
.owner = self,
- .read_only = read_only,
.mutate = Transform.mutateMatrix,
});
}
+// An SVGTransform is a 2D affine transform, and the `transform` attribute has
+// no syntax for anything else, so a write through the live matrix keeps only
+// the six components the transform can represent.
fn mutateMatrix(context: *anyopaque, _: *RO, state: RO.State) anyerror!void {
const self: *Transform = @ptrCast(@alignCast(context));
+ const m = state.matrix;
try self.applyState(.{
- .typ = if (state.is_2d) 1 else 0,
+ .typ = 1,
.angle = 0,
.cx = 0,
.cy = 0,
- .matrix = state.matrix,
- .is_2d = state.is_2d,
+ .matrix = .{
+ m[0], m[1], 0, 0,
+ m[4], m[5], 0, 0,
+ 0, 0, 1, 0,
+ m[12], m[13], 0, 1,
+ },
+ .is_2d = true,
});
}
diff --git a/src/browser/webapi/svg/TransformList.zig b/src/browser/webapi/svg/TransformList.zig
index 729e33223..8baf90052 100644
--- a/src/browser/webapi/svg/TransformList.zig
+++ b/src/browser/webapi/svg/TransformList.zig
@@ -53,9 +53,8 @@ pub fn getNumberOfItems(self: *TransformList, frame: *Frame) !u32 {
pub fn clear(self: *TransformList, frame: *Frame) !void {
try self.requireMutable();
try self.sync(frame);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
+ try self.retireAll(frame);
try self.setAttribute(&.{}, frame);
- self.retireAllAssumeCapacity();
}
pub fn initialize(self: *TransformList, item: *Transform, frame: *Frame) !*Transform {
@@ -64,10 +63,9 @@ pub fn initialize(self: *TransformList, item: *Transform, frame: *Frame) !*Trans
const prepared = try self.prepareItem(item, frame);
errdefer prepared.releaseRef(frame._page);
+ try self.retireAll(frame);
try self._items.ensureTotalCapacity(frame.arena, 1);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
try self.setAttribute(&.{prepared}, frame);
- self.retireAllAssumeCapacity();
self._items.appendAssumeCapacity(prepared);
self.attach(prepared);
return prepared;
@@ -142,36 +140,28 @@ pub fn consolidate(self: *TransformList, frame: *Frame) !?*Transform {
if (self._items.items.len == 0) return null;
var matrix = DOMMatrixReadOnly.identity();
- var is_2d = true;
- for (self._items.items) |item| {
- const state = item.getState();
- matrix = DOMMatrixReadOnly.multiplyMatrix(matrix, state.matrix);
- is_2d = is_2d and state.is_2d;
- }
+ for (self._items.items) |item| matrix = DOMMatrixReadOnly.multiplyMatrix(matrix, item.getState().matrix);
for (matrix) |value| if (!std.math.isFinite(value)) return error.TypeError;
- var values: [16]f64 = matrix;
- if (is_2d) {
- values[0] = matrix[0];
- values[1] = matrix[1];
- values[2] = matrix[4];
- values[3] = matrix[5];
- values[4] = matrix[12];
- values[5] = matrix[13];
- }
+ var values: [16]f64 = undefined;
+ values[0] = matrix[0];
+ values[1] = matrix[1];
+ values[2] = matrix[4];
+ values[3] = matrix[5];
+ values[4] = matrix[12];
+ values[5] = matrix[13];
const consolidated = try Transform.fromParsed(.{
- .kind = if (is_2d) .matrix else .matrix3d,
+ .kind = .matrix,
.matrix = matrix,
.values = values,
- .count = if (is_2d) 6 else 16,
- .is_2d = is_2d,
+ .count = 6,
+ .is_2d = true,
}, frame);
consolidated.acquireRef();
errdefer consolidated.releaseRef(frame._page);
+ try self.retireAll(frame);
try self._items.ensureTotalCapacity(frame.arena, 1);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
try self.setAttribute(&.{consolidated}, frame);
- self.retireAllAssumeCapacity();
self._items.appendAssumeCapacity(consolidated);
self.attach(consolidated);
return consolidated;
@@ -190,9 +180,8 @@ fn prepareItem(_: *TransformList, item: *Transform, frame: *Frame) !*Transform {
fn attach(self: *TransformList, transform: *Transform) void {
transform.attach(.{
.owner = self,
- .read_only = self._read_only,
.mutate = TransformList.mutateTransform,
- });
+ }, self._read_only);
}
fn mutateTransform(context: *anyopaque, transform: *Transform, state: Transform.State) anyerror!void {
@@ -216,20 +205,17 @@ fn sync(self: *TransformList, frame: *Frame) !void {
const raw = self._element.getAttributeSafe(comptime .wrap("transform")) orelse "";
if (self._synced and std.mem.eql(u8, self._snapshot.items, raw)) return;
+ self._synced = false;
var parsed = parse(raw, frame) catch |err| switch (err) {
error.SyntaxError => std.ArrayList(*Transform).empty,
else => return err,
};
errdefer for (parsed.items) |transform| transform.releaseRef(frame._page);
- try self._snapshot.ensureTotalCapacity(frame.arena, raw.len);
- try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
- try self._items.ensureTotalCapacity(frame.arena, parsed.items.len);
-
- self._synced = false;
self._snapshot.clearRetainingCapacity();
- self._snapshot.appendSliceAssumeCapacity(raw);
- self.retireAllAssumeCapacity();
+ try self._snapshot.appendSlice(frame.arena, raw);
+ try self.retireAll(frame);
+ try self._items.ensureTotalCapacity(frame.arena, parsed.items.len);
for (parsed.items) |transform| {
self._items.appendAssumeCapacity(transform);
self.attach(transform);
@@ -267,7 +253,9 @@ fn parse(raw: []const u8, frame: *Frame) !std.ArrayList(*Transform) {
return parsed;
}
-fn retireAllAssumeCapacity(self: *TransformList) void {
+fn retireAll(self: *TransformList, frame: *Frame) !void {
+ self._synced = false;
+ try self._retired.ensureUnusedCapacity(frame.arena, self._items.items.len);
for (self._items.items) |transform| {
transform.detach(self);
self._retired.appendAssumeCapacity(transform);
@@ -296,11 +284,10 @@ fn setAttributeWithOverride(self: *TransformList, index: usize, state: Transform
}
fn commitAttribute(self: *TransformList, serialized: []const u8, frame: *Frame) !void {
- try self._snapshot.ensureTotalCapacity(frame.arena, serialized.len);
- try self._element.setAttributeSafe(comptime .wrap("transform"), .wrap(serialized), frame);
self._synced = false;
+ try self._element.setAttributeSafe(comptime .wrap("transform"), .wrap(serialized), frame);
self._snapshot.clearRetainingCapacity();
- self._snapshot.appendSliceAssumeCapacity(serialized);
+ try self._snapshot.appendSlice(frame.arena, serialized);
self._synced = true;
}