Make DOM geometry structs serializable (#38828)

Makes the following DOM geometry structs serializable:
- `DOMRect`
- `DOMRectReadOnly`
- `DOMQuad`
- `DOMMatrix`
- `DOMMatrixReadOnly`

Testing: Covered by WPT (`css/geometry/structured-serialization.html`).

---------

Signed-off-by: lumiscosity <averyrudelphe@gmail.com>
This commit is contained in:
lumiscosity 2025-08-22 01:19:42 +02:00 committed by GitHub
parent e00f39d827
commit 39f3ce7a2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 529 additions and 89 deletions

View file

@ -3,7 +3,10 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::collections::HashMap;
use base::id::{DomRectId, DomRectIndex};
use constellation_traits::DomRect;
use dom_struct::dom_struct;
use js::rust::HandleObject;
@ -15,6 +18,8 @@ use crate::dom::bindings::reflector::{
Reflector, reflect_dom_object, reflect_dom_object_with_proto,
};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::Serializable;
use crate::dom::bindings::structuredclone::StructuredData;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
@ -196,3 +201,48 @@ pub(super) fn create_a_domrectreadonly_from_the_dictionary(other: &DOMRectInit)
height: Cell::new(other.height),
}
}
type Type = DomRectId;
impl Serializable for DOMRectReadOnly {
type Index = DomRectIndex;
type Data = DomRect;
fn serialize(&self) -> Result<(DomRectId, Self::Data), ()> {
let serialized = DomRect {
x: self.X(),
y: self.Y(),
width: self.Width(),
height: self.Height(),
};
Ok((DomRectId::new(), serialized))
}
fn deserialize(
owner: &GlobalScope,
serialized: Self::Data,
can_gc: CanGc,
) -> Result<DomRoot<Self>, ()>
where
Self: Sized,
{
Ok(Self::new(
owner,
None,
serialized.x,
serialized.y,
serialized.width,
serialized.height,
can_gc,
))
}
fn serialized_storage<'a>(
data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<Type, Self::Data>> {
match data {
StructuredData::Reader(reader) => &mut reader.rects,
StructuredData::Writer(writer) => &mut writer.rects,
}
}
}