fix(svg): implement marker orientation angle APIs

This commit is contained in:
ldm0
2026-11-19 00:16:20 +08:00
parent b754c2e539
commit 320d8a148b
5 changed files with 273 additions and 0 deletions
@@ -328,6 +328,10 @@ const NODE_BRIDGE_DESCRIPTORS: &[BridgeDescriptor] = &[
web_api_interfaces::SVGMaskElement::DESCRIPTOR,
ELEMENT_GROUPS,
),
descriptor(
web_api_interfaces::SVGMarkerElement::DESCRIPTOR,
ELEMENT_GROUPS,
),
specialized_descriptor(
web_api_interfaces::SVGPathElement::DESCRIPTOR,
ELEMENT_GROUPS,
@@ -1498,6 +1498,44 @@ pub(super) fn build_svg_angle_from_parsed<'s>(
.expect("SVGAngle declaration should bind")
}
pub(super) fn build_svg_animated_angle_for_attribute<'s>(
scope: &mut v8::PinScope<'s, '_>,
owner: v8::Local<'s, v8::Object>,
attribute: &str,
) -> v8::Local<'s, v8::Object> {
let parsed = svg_owner_attribute_value(scope, owner, attribute)
.as_deref()
.and_then(parse_svg_orient_angle_value)
.unwrap_or_default();
let base_val = build_svg_angle_from_parsed(scope, &parsed, false);
set_svg_angle_owner_attribute(scope, base_val, owner, attribute);
let anim_val = build_svg_angle_from_parsed(scope, &parsed, true);
set_svg_angle_owner_attribute(scope, anim_val, owner, attribute);
SvgAnimatedAngleObjectDeclaration::new(base_val, anim_val)
.bind(scope)
.expect("SVGAnimatedAngle declaration should bind")
}
pub(super) fn build_svg_angle<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Object> {
build_svg_angle_from_parsed(scope, &SvgParsedAngle::default(), false)
}
pub(super) fn build_svg_angle_from_parsed<'s>(
scope: &mut v8::PinScope<'s, '_>,
parsed: &SvgParsedAngle,
read_only: bool,
) -> v8::Local<'s, v8::Object> {
SvgAngleObjectDeclaration::new(
parsed.unit_type,
parsed.value,
parsed.value_in_specified_units,
parsed.value_as_string.clone(),
read_only,
)
.bind(scope)
.expect("SVGAngle declaration should bind")
}
pub(super) fn build_svg_length<'s>(
scope: &mut v8::PinScope<'s, '_>,
value: f64,
@@ -3397,6 +3397,14 @@ pub(super) fn svg_svg_element_create_point_callback<'s>(
}
}
pub(super) fn svg_svg_element_create_angle_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
_args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
rv.set(build_svg_angle(scope).into());
}
pub(super) fn svg_svg_element_deselect_all_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -3834,6 +3842,46 @@ pub(super) fn svg_angle_new_value_specified_units_callback<'s>(
rv.set_undefined();
}
pub(super) fn svg_angle_new_value_specified_units_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
if !require_svg_receiver(
scope,
args.this(),
SVG_ANGLE_UNIT_TYPE_SLOT,
"SVGAngle",
"newValueSpecifiedUnits",
) {
return;
}
if svg_angle_is_read_only(scope, args.this()) {
throw_dom_exception(
scope,
"NoModificationAllowedError",
7,
"The SVG angle is read-only.",
);
return;
}
let Some(parsed) = webidl::parse_args::<SvgAngleNewValueSpecifiedUnitsArgs>(scope, &args)
else {
return;
};
if !set_svg_angle_new_value(scope, args.this(), parsed.unit_type as u32, parsed.value) {
throw_dom_exception(
scope,
"NotSupportedError",
9,
"The SVG angle unit type is not supported.",
);
return;
}
reflect_svg_angle_to_owner_attribute(scope, args.this());
rv.set_undefined();
}
pub(super) fn svg_length_convert_to_specified_units_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -3912,6 +3960,47 @@ pub(super) fn svg_angle_convert_to_specified_units_callback<'s>(
rv.set_undefined();
}
pub(super) fn svg_angle_convert_to_specified_units_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
if !require_svg_receiver(
scope,
args.this(),
SVG_ANGLE_UNIT_TYPE_SLOT,
"SVGAngle",
"convertToSpecifiedUnits",
) {
return;
}
if svg_angle_is_read_only(scope, args.this()) {
throw_dom_exception(
scope,
"NoModificationAllowedError",
7,
"The SVG angle is read-only.",
);
return;
}
sync_svg_angle_from_owner_attribute(scope, args.this());
let Some(parsed) = webidl::parse_args::<SvgAngleConvertToSpecifiedUnitsArgs>(scope, &args)
else {
return;
};
if !convert_svg_angle_to_unit(scope, args.this(), parsed.unit_type as u32) {
throw_dom_exception(
scope,
"NotSupportedError",
9,
"The SVG angle unit type is not supported.",
);
return;
}
reflect_svg_angle_to_owner_attribute(scope, args.this());
rv.set_undefined();
}
pub(super) fn svg_graphics_get_bbox_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -235,6 +235,8 @@ enum SvgAnimatedEnumerationKind {
UnitType,
SpreadMethod,
LengthAdjust,
MarkerUnits,
MarkerOrient,
}
#[derive(Clone, Copy)]
@@ -1285,6 +1287,20 @@ const SVG_ANIMATED_INTEGER_PROPERTIES: &[SvgAnimatedIntegerProperty] = &[
initial_value: 1,
component: SvgAnimatedIntegerComponent::Scalar,
},
SvgAnimatedEnumerationProperty {
index: 10,
attribute: "markerUnits",
cache_slot: SVG_MARKER_UNITS_SLOT,
initial_value: SVG_MARKER_UNITS_STROKE_WIDTH,
kind: SvgAnimatedEnumerationKind::MarkerUnits,
},
SvgAnimatedEnumerationProperty {
index: 11,
attribute: "orient",
cache_slot: SVG_MARKER_ORIENT_TYPE_SLOT,
initial_value: SVG_MARKER_ORIENT_ANGLE,
kind: SvgAnimatedEnumerationKind::MarkerOrient,
},
];
pub(in crate::context_bootstrap) fn install_svg_template_bindings<'s>(
@@ -670,6 +670,132 @@ fn svg_animated_boolean_reflects_preserve_alpha() {
assert_eq!(result, "ok");
}
#[test]
fn svg_marker_orient_angle_is_a_live_animated_angle() {
let mut vm = new_parsed_test_vm(
"https://svg-animated-angle.test/",
"<!doctype html><html><body></body></html>",
);
let result = vm
.eval(
r#"
(() => {
const assert = (condition, message) => {
if (!condition) throw new Error(message);
};
const close = (actual, expected) => Math.abs(actual - expected) < 1e-9;
const ns = "http://www.w3.org/2000/svg";
const marker = document.createElementNS(ns, "marker");
for (const constructor of [SVGMarkerElement, SVGAnimatedAngle, SVGAngle]) {
assert(typeof constructor === "function", `${constructor.name} constructor`);
let illegalConstructor = false;
try {
new constructor();
} catch (error) {
illegalConstructor = error instanceof TypeError;
}
assert(illegalConstructor, `${constructor.name} illegal constructor`);
}
assert(marker instanceof SVGMarkerElement, "marker interface");
assert(Object.getPrototypeOf(SVGMarkerElement.prototype) === SVGElement.prototype,
"marker prototype parent");
const markerDescriptor = Object.getOwnPropertyDescriptor(
SVGMarkerElement.prototype,
"orientAngle",
);
assert(typeof markerDescriptor.get === "function" &&
markerDescriptor.set === undefined, "orientAngle readonly descriptor");
assert(markerDescriptor.enumerable && markerDescriptor.configurable,
"orientAngle descriptor flags");
const animated = marker.orientAngle;
const base = animated.baseVal;
const anim = animated.animVal;
assert(animated instanceof SVGAnimatedAngle, "animated angle interface");
assert(base instanceof SVGAngle && anim instanceof SVGAngle, "angle interfaces");
assert(base !== anim, "base and animated values are distinct");
assert(marker.orientAngle === animated, "orientAngle SameObject");
assert(base.value === 0 && anim.value === 0, "initial values");
assert(base.unitType === SVGAngle.SVG_ANGLETYPE_UNSPECIFIED, "initial unit");
assert(!marker.hasAttribute("orient"), "getter does not create attribute");
assert(marker.orientType instanceof SVGAnimatedEnumeration, "orientType interface");
assert(marker.orientType.baseVal === SVGMarkerElement.SVG_MARKER_ORIENT_ANGLE,
"initial orientType");
assert(marker.markerUnits instanceof SVGAnimatedEnumeration,
"markerUnits interface");
assert(marker.markerUnits.baseVal ===
SVGMarkerElement.SVG_MARKERUNITS_STROKEWIDTH, "initial markerUnits");
marker.markerUnits.baseVal = SVGMarkerElement.SVG_MARKERUNITS_USERSPACEONUSE;
assert(marker.getAttribute("markerUnits") === "userSpaceOnUse",
"markerUnits reflection");
base.value = 100;
assert(base.value === 100 && marker.orientAngle.baseVal.value === 100,
"cached base value is live");
assert(marker.getAttribute("orient") === "100", "value reflects to orient");
marker.orientAngle.baseVal = -1;
assert(marker.orientAngle.baseVal === base && base.value === 100,
"baseVal assignment is ignored");
marker.setAttribute("orient", "1.5707963267948966rad");
assert(close(base.value, 90), "content attribute converts to degrees");
assert(base.unitType === SVGAngle.SVG_ANGLETYPE_RAD, "content attribute unit");
assert(close(base.valueInSpecifiedUnits, Math.PI / 2), "specified value");
assert(anim.unitType === SVGAngle.SVG_ANGLETYPE_RAD && close(anim.value, 90),
"animVal tracks content attribute");
assert(marker.orientType.baseVal === SVGMarkerElement.SVG_MARKER_ORIENT_ANGLE,
"angle orientType");
marker.setOrientToAuto();
assert(marker.getAttribute("orient") === "auto", "setOrientToAuto reflection");
assert(marker.orientType.baseVal === SVGMarkerElement.SVG_MARKER_ORIENT_AUTO,
"automatic orientType");
assert(base.value === 0 && base.unitType === SVGAngle.SVG_ANGLETYPE_UNSPECIFIED,
"automatic orient angle");
marker.setAttribute("orient", "400grad");
assert(base.value === 360 && base.unitType === SVGAngle.SVG_ANGLETYPE_GRAD,
"grad content attribute");
const svg = document.createElementNS(ns, "svg");
const standalone = svg.createSVGAngle();
assert(standalone instanceof SVGAngle, "createSVGAngle result");
standalone.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD, Math.PI);
assert(close(standalone.value, 180), "newValueSpecifiedUnits conversion");
standalone.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_GRAD);
assert(close(standalone.valueInSpecifiedUnits, 200), "unit conversion");
assert(standalone.valueAsString === "200grad", "unit serialization");
marker.setOrientToAngle(standalone);
assert(marker.getAttribute("orient") === "200grad", "setOrientToAngle reflection");
assert(marker.orientAngle.baseVal.value === 180, "setOrientToAngle value");
let readonlyAnim = false;
try {
anim.value = 1;
} catch (error) {
readonlyAnim = error.name === "NoModificationAllowedError";
}
assert(readonlyAnim, "animVal is read-only");
let incompatibleReceiver = false;
try {
markerDescriptor.get.call(document.createElementNS(ns, "rect"));
} catch (error) {
incompatibleReceiver = error instanceof TypeError;
}
assert(incompatibleReceiver, "marker receiver brand");
return "ok";
})()
"#,
)
.expect("SVG animated angle probe should evaluate");
assert_eq!(result, "ok");
}
#[test]
fn svg_string_lists_reflect_conditional_processing_attributes() {
let mut vm = new_parsed_test_vm(