Skip to content

Lightpanda: the headless browser designed for AI and automation

Angle/Lenght value setter unit

Karl Seguin93d23050417c
diff --git a/src/browser/tests/element/svg/svgsvg.html b/src/browser/tests/element/svg/svgsvg.html
index c81845a9b..111883e4e 100644
--- a/src/browser/tests/element/svg/svgsvg.html
+++ b/src/browser/tests/element/svg/svgsvg.html
@@ -44,10 +44,13 @@
testing.expectEqual(96, Math.round(length.value));
testing.expectEqual('2.54cm', length.valueAsString);
- // `value` is an absolute user-unit setter and resets the unit to NUMBER.
+ // `value` is an absolute user-unit setter; the unit is preserved and
+ // valueInSpecifiedUnits converts (12 user units = 0.3175cm).
length.value = 12;
- testing.expectEqual(SVGLength.SVG_LENGTHTYPE_NUMBER, length.unitType);
- testing.expectEqual('12', length.valueAsString);
+ testing.expectEqual(SVGLength.SVG_LENGTHTYPE_CM, length.unitType);
+ testing.expectEqual(0.3175, length.valueInSpecifiedUnits);
+ testing.expectEqual(12, length.value);
+ testing.expectEqual('0.3175cm', length.valueAsString);
length.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX, 4.5);
testing.expectEqual('4.5px', length.valueAsString);
testing.expectError('NotSupportedError', () => length.newValueSpecifiedUnits(0, 1));
@@ -66,8 +69,9 @@
testing.expectEqual(180, Math.round(angle.valueInSpecifiedUnits));
testing.expectEqual('180deg', angle.valueAsString);
angle.value = 45;
- testing.expectEqual(SVGAngle.SVG_ANGLETYPE_UNSPECIFIED, angle.unitType);
- testing.expectEqual('45', angle.valueAsString);
+ testing.expectEqual(SVGAngle.SVG_ANGLETYPE_DEG, angle.unitType);
+ testing.expectEqual(45, angle.valueInSpecifiedUnits);
+ testing.expectEqual('45deg', angle.valueAsString);
testing.expectError('NotSupportedError', () => angle.convertToSpecifiedUnits(0));
testing.expectError('TypeError', () => { angle.value = NaN; });
@@ -155,8 +159,8 @@
svg.x.baseVal.valueInSpecifiedUnits = 4.5;
testing.expectEqual('4.5px', svg.getAttribute('x'));
svg.x.baseVal.value = 7;
- testing.expectEqual('7', svg.getAttribute('x'));
- testing.expectEqual(SVGLength.SVG_LENGTHTYPE_NUMBER, svg.x.animVal.unitType);
+ testing.expectEqual('7px', svg.getAttribute('x'));
+ testing.expectEqual(SVGLength.SVG_LENGTHTYPE_PX, svg.x.animVal.unitType);
testing.expectError('NoModificationAllowedError', () => { svg.x.animVal.value = 3; });
testing.expectError('NoModificationAllowedError', () => { svg.x.animVal.valueAsString = '3px'; });
diff --git a/src/browser/webapi/svg/Angle.zig b/src/browser/webapi/svg/Angle.zig
index feeb8cd6c..163ba6e39 100644
--- a/src/browser/webapi/svg/Angle.zig
+++ b/src/browser/webapi/svg/Angle.zig
@@ -78,11 +78,16 @@ pub fn getValue(self: *Angle) f64 {
return toDegrees(self._value, self._unit);
}
+// Sets the value in degrees; the stored unit is preserved and
+// valueInSpecifiedUnits converts, matching Blink and Gecko.
pub fn setValue(self: *Angle, value: f64, frame: *Frame) !void {
try self.ensureWritable();
try ensureFinite(value);
- self._value = value;
- self._unit = .unspecified;
+ self.syncFromAttribute();
+ if (self._unit == .unknown) {
+ self._unit = .unspecified;
+ }
+ self._value = fromDegrees(value, self._unit);
try self.writeBack(frame);
}
diff --git a/src/browser/webapi/svg/Length.zig b/src/browser/webapi/svg/Length.zig
index 933de0cc7..6bb0dd817 100644
--- a/src/browser/webapi/svg/Length.zig
+++ b/src/browser/webapi/svg/Length.zig
@@ -120,11 +120,17 @@ pub fn getValue(self: *Length, frame: *Frame) f64 {
return self._value * self.unitToUserUnits(self._unit, frame);
}
+// Sets the value in user units; the stored unit is preserved and
+// valueInSpecifiedUnits converts, matching Blink and Gecko.
pub fn setValue(self: *Length, value: f64, frame: *Frame) !void {
try self.ensureWritable();
try ensureFinite(value);
- self._value = value;
- self._unit = .number;
+ self.syncFromAttribute();
+ if (self._unit == .unknown) {
+ self._unit = .number;
+ }
+ const factor = self.unitToUserUnits(self._unit, frame);
+ self._value = if (factor == 0) 0 else value / factor;
try self.writeBack(frame);
}