diff --git a/moli-renderer-v8/src/context_bootstrap/bridge_descriptor/descriptors.rs b/moli-renderer-v8/src/context_bootstrap/bridge_descriptor/descriptors.rs index cf8ba0e410c..7941a1a860f 100644 --- a/moli-renderer-v8/src/context_bootstrap/bridge_descriptor/descriptors.rs +++ b/moli-renderer-v8/src/context_bootstrap/bridge_descriptor/descriptors.rs @@ -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, diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/builders.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/builders.rs index 583b7c40d8a..519e47714a2 100644 --- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/builders.rs +++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/builders.rs @@ -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, diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs index 61b6e6cb6cd..9a4c06bdf5d 100644 --- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs +++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs @@ -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::(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::(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>, diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs index 39d671adefc..909593c905d 100644 --- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs +++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs @@ -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>( diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/geometry_and_svg.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/geometry_and_svg.rs index c672279a820..68910e3b405 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/geometry_and_svg.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/geometry_and_svg.rs @@ -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/", + "", + ); + + 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(