diff --git a/components/script/dom/dommatrix.rs b/components/script/dom/dommatrix.rs index bc57feb14fab..03e152a2f2c8 100644 --- a/components/script/dom/dommatrix.rs +++ b/components/script/dom/dommatrix.rs @@ -6,14 +6,17 @@ use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::{ DOMMatrixInit, DOMMatrixMethods, Wrap, }; use crate::dom::bindings::codegen::Bindings::DOMMatrixReadOnlyBinding::DOMMatrixReadOnlyMethods; +use crate::dom::bindings::codegen::UnionTypes::StringOrUnrestrictedDoubleSequence; +use crate::dom::bindings::error; use crate::dom::bindings::error::Fallible; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::reflector::reflect_dom_object; use crate::dom::bindings::root::DomRoot; use crate::dom::dommatrixreadonly::{ - dommatrixinit_to_matrix, entries_to_matrix, DOMMatrixReadOnly, + dommatrixinit_to_matrix, entries_to_matrix, transform_to_matrix, DOMMatrixReadOnly, }; use crate::dom::globalscope::GlobalScope; +use crate::dom::window::Window; use dom_struct::dom_struct; use euclid::Transform3D; use js::rust::CustomAutoRooterGuard; @@ -37,14 +40,32 @@ impl DOMMatrix { } } - // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-dommatrix - pub fn Constructor(global: &GlobalScope) -> Fallible> { - Self::Constructor_(global, vec![1.0, 0.0, 0.0, 1.0, 0.0, 0.0]) - } - - // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-dommatrix-numbersequence - pub fn Constructor_(global: &GlobalScope, entries: Vec) -> Fallible> { - entries_to_matrix(&entries[..]).map(|(is2D, matrix)| Self::new(global, is2D, matrix)) + // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-dommatrixreadonly + pub fn Constructor( + global: &GlobalScope, + init: Option, + ) -> Fallible> { + if init.is_none() { + return Ok(Self::new(global, true, Transform3D::identity())); + } + match init.unwrap() { + StringOrUnrestrictedDoubleSequence::String(ref s) => { + if global.downcast::().is_none() { + return Err(error::Error::Type( + "String constructor is only supported in the main thread.".to_owned(), + )); + } + if s.is_empty() { + return Ok(Self::new(global, true, Transform3D::identity())); + } + transform_to_matrix(s.to_string()) + .map(|(is2D, matrix)| Self::new(global, is2D, matrix)) + }, + StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(ref entries) => { + entries_to_matrix(&entries[..]) + .map(|(is2D, matrix)| Self::new(global, is2D, matrix)) + }, + } } // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-frommatrix @@ -62,7 +83,10 @@ impl DOMMatrix { array: CustomAutoRooterGuard, ) -> Fallible> { let vec: Vec = array.to_vec().iter().map(|&x| x as f64).collect(); - DOMMatrix::Constructor_(global, vec) + DOMMatrix::Constructor( + global, + Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)), + ) } // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-fromfloat64array @@ -71,7 +95,10 @@ impl DOMMatrix { array: CustomAutoRooterGuard, ) -> Fallible> { let vec: Vec = array.to_vec(); - DOMMatrix::Constructor_(global, vec) + DOMMatrix::Constructor( + global, + Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)), + ) } } diff --git a/components/script/dom/dommatrixreadonly.rs b/components/script/dom/dommatrixreadonly.rs index 010f823e78c7..fae6afe1649f 100644 --- a/components/script/dom/dommatrixreadonly.rs +++ b/components/script/dom/dommatrixreadonly.rs @@ -8,13 +8,17 @@ use crate::dom::bindings::codegen::Bindings::DOMMatrixReadOnlyBinding::{ DOMMatrixReadOnlyMethods, Wrap, }; use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit; +use crate::dom::bindings::codegen::UnionTypes::StringOrUnrestrictedDoubleSequence; use crate::dom::bindings::error; use crate::dom::bindings::error::Fallible; +use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; use crate::dom::bindings::root::DomRoot; use crate::dom::dommatrix::DOMMatrix; use crate::dom::dompoint::DOMPoint; use crate::dom::globalscope::GlobalScope; +use crate::dom::window::Window; +use cssparser::{Parser, ParserInput}; use dom_struct::dom_struct; use euclid::{Angle, Transform3D}; use js::jsapi::{JSContext, JSObject}; @@ -25,6 +29,7 @@ use std::cell::{Cell, Ref}; use std::f64; use std::ptr; use std::ptr::NonNull; +use style::parser::ParserContext; #[dom_struct] pub struct DOMMatrixReadOnly { @@ -49,13 +54,31 @@ impl DOMMatrixReadOnly { } // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-dommatrixreadonly - pub fn Constructor(global: &GlobalScope) -> Fallible> { - Ok(Self::new(global, true, Transform3D::identity())) - } - - // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-dommatrixreadonly-numbersequence - pub fn Constructor_(global: &GlobalScope, entries: Vec) -> Fallible> { - entries_to_matrix(&entries[..]).map(|(is2D, matrix)| Self::new(global, is2D, matrix)) + pub fn Constructor( + global: &GlobalScope, + init: Option, + ) -> Fallible> { + if init.is_none() { + return Ok(Self::new(global, true, Transform3D::identity())); + } + match init.unwrap() { + StringOrUnrestrictedDoubleSequence::String(ref s) => { + if global.downcast::().is_none() { + return Err(error::Error::Type( + "String constructor is only supported in the main thread.".to_owned(), + )); + } + if s.is_empty() { + return Ok(Self::new(global, true, Transform3D::identity())); + } + transform_to_matrix(s.to_string()) + .map(|(is2D, matrix)| Self::new(global, is2D, matrix)) + }, + StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(ref entries) => { + entries_to_matrix(&entries[..]) + .map(|(is2D, matrix)| Self::new(global, is2D, matrix)) + }, + } } // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-frommatrix @@ -372,7 +395,10 @@ impl DOMMatrixReadOnly { array: CustomAutoRooterGuard, ) -> Fallible> { let vec: Vec = array.to_vec().iter().map(|&x| x as f64).collect(); - DOMMatrixReadOnly::Constructor_(global, vec) + DOMMatrixReadOnly::Constructor( + global, + Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)), + ) } // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-fromfloat64array @@ -382,7 +408,10 @@ impl DOMMatrixReadOnly { array: CustomAutoRooterGuard, ) -> Fallible> { let vec: Vec = array.to_vec(); - DOMMatrixReadOnly::Constructor_(global, vec) + DOMMatrixReadOnly::Constructor( + global, + Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)), + ) } } @@ -759,3 +788,32 @@ fn normalize_point(x: f64, y: f64, z: f64) -> (f64, f64, f64) { (x / len, y / len, z / len) } } + +pub fn transform_to_matrix(value: String) -> Fallible<(bool, Transform3D)> { + use style::properties::longhands::transform; + + let mut input = ParserInput::new(&value); + let mut parser = Parser::new(&mut input); + let url = ::servo_url::ServoUrl::parse("about:blank").unwrap(); + let context = ParserContext::new( + ::style::stylesheets::Origin::Author, + &url, + Some(::style::stylesheets::CssRuleType::Style), + ::style_traits::ParsingMode::DEFAULT, + ::style::context::QuirksMode::NoQuirks, + None, + None, + ); + + let transform = match parser.parse_entirely(|t| transform::parse(&context, t)) { + Ok(result) => result, + Err(..) => return Err(error::Error::Syntax), + }; + + let (m, is_3d) = match transform.to_transform_3d_matrix_f64(None) { + Ok(result) => result, + Err(..) => return Err(error::Error::Syntax), + }; + + Ok((!is_3d, m)) +} diff --git a/components/script/dom/webidls/DOMMatrix.webidl b/components/script/dom/webidls/DOMMatrix.webidl index 6facefec0569..d617f20af99c 100644 --- a/components/script/dom/webidls/DOMMatrix.webidl +++ b/components/script/dom/webidls/DOMMatrix.webidl @@ -10,9 +10,7 @@ * related or neighboring rights to this work. */ -[Constructor, - // Constructor(DOMString transformList), - Constructor(sequence numberSequence), +[Constructor(optional (DOMString or sequence) init), Exposed=(Window,Worker)] interface DOMMatrix : DOMMatrixReadOnly { diff --git a/components/script/dom/webidls/DOMMatrixReadOnly.webidl b/components/script/dom/webidls/DOMMatrixReadOnly.webidl index 2382275e2494..dcd9229b3d74 100644 --- a/components/script/dom/webidls/DOMMatrixReadOnly.webidl +++ b/components/script/dom/webidls/DOMMatrixReadOnly.webidl @@ -10,9 +10,7 @@ * related or neighboring rights to this work. */ -[Constructor, - // Constructor(DOMString transformList) - Constructor(sequence numberSequence), +[Constructor(optional (DOMString or sequence) init), Exposed=(Window,Worker)] interface DOMMatrixReadOnly { diff --git a/components/style/values/generics/transform.rs b/components/style/values/generics/transform.rs index ba0e320865a6..64fb1e254a5d 100644 --- a/components/style/values/generics/transform.rs +++ b/components/style/values/generics/transform.rs @@ -542,6 +542,19 @@ impl Transform { ) }; + let (m, is_3d) = match self.to_transform_3d_matrix_f64(reference_box) { + Ok(result) => result, + Err(err) => return Err(err), + }; + + Ok((cast_3d_transform(m), is_3d)) + } + + /// Same as Transform::to_transform_3d_matrix but a f64 version. + pub fn to_transform_3d_matrix_f64( + &self, + reference_box: Option<&Rect>, + ) -> Result<(Transform3D, bool), ()> { // We intentionally use Transform3D during computation to avoid error propagation // because using f32 to compute triangle functions (e.g. in create_rotation()) is not // accurate enough. In Gecko, we also use "double" to compute the triangle functions. @@ -556,7 +569,7 @@ impl Transform { transform = transform.pre_mul(&matrix); } - Ok((cast_3d_transform(transform), contain_3d)) + Ok((transform, contain_3d)) } } diff --git a/tests/wpt/metadata/css/geometry/DOMMatrix-001.html.ini b/tests/wpt/metadata/css/geometry/DOMMatrix-001.html.ini index f6cc19731440..c2c132ec2622 100644 --- a/tests/wpt/metadata/css/geometry/DOMMatrix-001.html.ini +++ b/tests/wpt/metadata/css/geometry/DOMMatrix-001.html.ini @@ -1,293 +1,19 @@ [DOMMatrix-001.html] type: testharness - [testConstructor1] - expected: FAIL - - [testConstructor2] - expected: FAIL - - [testConstructor3] - expected: FAIL - - [testConstructor5] - expected: FAIL - - [testConstructor7] - expected: FAIL - - [testConstructor8] - expected: FAIL - - [testConstructor9] - expected: FAIL - - [testConstructor10] - expected: FAIL - - [testConstructor11] - expected: FAIL - - [testConstructor13] - expected: FAIL - - [testConstructorIllegal0] - expected: FAIL - - [new DOMMatrix(undefined)] - expected: FAIL - [new DOMMatrix(new DOMMatrix())] expected: FAIL - [new DOMMatrix("none")] - expected: FAIL - - [new DOMMatrix(" none")] - expected: FAIL - - [new DOMMatrix("none ")] - expected: FAIL - - [new DOMMatrix("NONE")] - expected: FAIL - - [new DOMMatrix("none/**/")] - expected: FAIL - - [new DOMMatrix("/**/none")] - expected: FAIL - - [new DOMMatrix("")] - expected: FAIL - - [new DOMMatrix("scale(2) translateX(5px) translateY(5px)")] - expected: FAIL - - [new DOMMatrix("scale(2, 2) translateX(5px) translateY(5px)")] - expected: FAIL - - [new DOMMatrix("scale(2)translateX(5px)translateY(5px)")] - expected: FAIL - - [new DOMMatrix("scale(2) translateX(calc(2 * 2.5px)) translateY(5px)")] - expected: FAIL - [new DOMMatrix("scale(2) translateX(5px) translateY(5px) rotate(5deg) rotate(-5deg)")] expected: FAIL - [new DOMMatrix("translateX (5px)")] - expected: FAIL - - [new DOMMatrix("scale(2 2) translateX(5) translateY(5)")] - expected: FAIL - - [new DOMMatrix("scale(2, 2), translateX(5) ,translateY(5)")] - expected: FAIL - - [new DOMMatrix("translateX(5em)")] - expected: FAIL - - [new DOMMatrix("translateX(5ex)")] - expected: FAIL - - [new DOMMatrix("translateX(5ch)")] - expected: FAIL - - [new DOMMatrix("translateX(5rem)")] - expected: FAIL - - [new DOMMatrix("translateX(5vw)")] - expected: FAIL - - [new DOMMatrix("translateX(5vh)")] - expected: FAIL - - [new DOMMatrix("translateX(5vmin)")] - expected: FAIL - - [new DOMMatrix("translateX(5vmax)")] - expected: FAIL - - [new DOMMatrix("translateX(5%)")] - expected: FAIL - - [new DOMMatrix("rotate(5)")] - expected: FAIL - - [new DOMMatrix("rotate(5, 5, 5)")] - expected: FAIL - - [new DOMMatrix("rotate(5, 5px, 5px)")] - expected: FAIL - - [new DOMMatrix("rotate(5deg, 5px, 5px)")] - expected: FAIL - - [new DOMMatrix(" ")] - expected: FAIL - - [new DOMMatrix("/**/")] - expected: FAIL - - [new DOMMatrix("\\0")] - expected: FAIL - - [new DOMMatrix(";")] - expected: FAIL - - [new DOMMatrix("none;")] - expected: FAIL - - [new DOMMatrix("null")] - expected: FAIL - - [new DOMMatrix(null)] - expected: FAIL - - [new DOMMatrix("undefined")] - expected: FAIL - - [new DOMMatrix("inherit")] - expected: FAIL - - [new DOMMatrix("initial")] - expected: FAIL - - [new DOMMatrix("unset")] - expected: FAIL - [new DOMMatrix(matrix)] expected: FAIL - [new DOMMatrix("scale(2, 2), translateX(5px) translateY(5px)")] - expected: FAIL - - [new DOMMatrixReadOnly(undefined)] - expected: FAIL - [new DOMMatrixReadOnly(new DOMMatrixReadOnly())] expected: FAIL - [new DOMMatrixReadOnly("none")] - expected: FAIL - - [new DOMMatrixReadOnly(" none")] - expected: FAIL - - [new DOMMatrixReadOnly("none ")] - expected: FAIL - - [new DOMMatrixReadOnly("NONE")] - expected: FAIL - - [new DOMMatrixReadOnly("none/**/")] - expected: FAIL - - [new DOMMatrixReadOnly("/**/none")] - expected: FAIL - - [new DOMMatrixReadOnly("")] - expected: FAIL - - [new DOMMatrixReadOnly("scale(2) translateX(5px) translateY(5px)")] - expected: FAIL - - [new DOMMatrixReadOnly("scale(2, 2) translateX(5px) translateY(5px)")] - expected: FAIL - - [new DOMMatrixReadOnly("scale(2)translateX(5px)translateY(5px)")] - expected: FAIL - - [new DOMMatrixReadOnly("scale(2) translateX(calc(2 * 2.5px)) translateY(5px)")] - expected: FAIL - [new DOMMatrixReadOnly("scale(2) translateX(5px) translateY(5px) rotate(5deg) rotate(-5deg)")] expected: FAIL - [new DOMMatrixReadOnly("translateX (5px)")] - expected: FAIL - - [new DOMMatrixReadOnly("scale(2 2) translateX(5) translateY(5)")] - expected: FAIL - - [new DOMMatrixReadOnly("scale(2, 2), translateX(5) ,translateY(5)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5em)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5ex)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5ch)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5rem)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5vw)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5vh)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5vmin)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5vmax)")] - expected: FAIL - - [new DOMMatrixReadOnly("translateX(5%)")] - expected: FAIL - - [new DOMMatrixReadOnly("rotate(5)")] - expected: FAIL - - [new DOMMatrixReadOnly("rotate(5, 5, 5)")] - expected: FAIL - - [new DOMMatrixReadOnly("rotate(5, 5px, 5px)")] - expected: FAIL - - [new DOMMatrixReadOnly("rotate(5deg, 5px, 5px)")] - expected: FAIL - - [new DOMMatrixReadOnly(" ")] - expected: FAIL - - [new DOMMatrixReadOnly("/**/")] - expected: FAIL - - [new DOMMatrixReadOnly("\\0")] - expected: FAIL - - [new DOMMatrixReadOnly(";")] - expected: FAIL - - [new DOMMatrixReadOnly("none;")] - expected: FAIL - - [new DOMMatrixReadOnly("null")] - expected: FAIL - - [new DOMMatrixReadOnly(null)] - expected: FAIL - - [new DOMMatrixReadOnly("undefined")] - expected: FAIL - - [new DOMMatrixReadOnly("inherit")] - expected: FAIL - - [new DOMMatrixReadOnly("initial")] - expected: FAIL - - [new DOMMatrixReadOnly("unset")] - expected: FAIL - [new DOMMatrixReadOnly(matrix)] expected: FAIL - - [new DOMMatrixReadOnly("scale(2, 2), translateX(5px) translateY(5px)")] - expected: FAIL -