22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
5+ use std:: cell:: RefCell ;
6+
7+ use base64:: Engine as _;
58use dom_struct:: dom_struct;
69use html5ever:: { LocalName , Prefix , local_name, ns} ;
710use js:: rust:: HandleObject ;
811use layout_api:: SVGSVGData ;
12+ use servo_url:: ServoUrl ;
913use style:: attr:: AttrValue ;
14+ use xml5ever:: serialize:: TraversalScope ;
1015
1116use crate :: dom:: attr:: Attr ;
1217use crate :: dom:: bindings:: inheritance:: Castable ;
@@ -25,6 +30,8 @@ const DEFAULT_HEIGHT: u32 = 150;
2530#[ dom_struct]
2631pub ( crate ) struct SVGSVGElement {
2732 svggraphicselement : SVGGraphicsElement ,
33+ #[ no_trace]
34+ cached_serialized_data : RefCell < Option < ServoUrl > > ,
2835}
2936
3037impl SVGSVGElement {
@@ -35,6 +42,7 @@ impl SVGSVGElement {
3542 ) -> SVGSVGElement {
3643 SVGSVGElement {
3744 svggraphicselement : SVGGraphicsElement :: new_inherited ( local_name, prefix, document) ,
45+ cached_serialized_data : Default :: default ( ) ,
3846 }
3947 }
4048
@@ -53,6 +61,19 @@ impl SVGSVGElement {
5361 can_gc,
5462 )
5563 }
64+
65+ pub ( crate ) fn cache_serialized_data ( & self ) {
66+ println ! ( "XXX cache_serialized_data" ) ;
67+ let source: String = self
68+ . upcast :: < Node > ( )
69+ . xml_serialize ( TraversalScope :: IncludeNode )
70+ . into ( ) ;
71+ let base64 = base64:: engine:: general_purpose:: STANDARD . encode ( source) ;
72+ let source = format ! ( "data:image/svg+xml;base64,{}" , base64) ;
73+ if let Ok ( url) = ServoUrl :: parse ( & source) {
74+ * self . cached_serialized_data . borrow_mut ( ) = Some ( url) ;
75+ }
76+ }
5677}
5778
5879pub ( crate ) trait LayoutSVGSVGElementHelpers {
@@ -70,7 +91,7 @@ impl LayoutSVGSVGElementHelpers for LayoutDom<'_, SVGSVGElement> {
7091 SVGSVGData {
7192 width : width_attr. map_or ( DEFAULT_WIDTH , |val| val. as_uint ( ) ) ,
7293 height : height_attr. map_or ( DEFAULT_HEIGHT , |val| val. as_uint ( ) ) ,
73- source : self . upcast :: < Element > ( ) . serialize_as_xml ( ) . into ( )
94+ source : self . unsafe_get ( ) . cached_serialized_data . borrow ( ) . clone ( ) ,
7495 }
7596 }
7697}
@@ -84,6 +105,7 @@ impl VirtualMethods for SVGSVGElement {
84105 self . super_type ( )
85106 . unwrap ( )
86107 . attribute_mutated ( attr, mutation, can_gc) ;
108+ * self . cached_serialized_data . borrow_mut ( ) = None ;
87109 }
88110
89111 fn parse_plain_attribute ( & self , name : & LocalName , value : DOMString ) -> AttrValue {
@@ -96,4 +118,17 @@ impl VirtualMethods for SVGSVGElement {
96118 . parse_plain_attribute ( name, value) ,
97119 }
98120 }
121+
122+ // fn children_changed(&self, mutation: &super::node::ChildrenMutation) {
123+ // self.super_type()
124+ // .map(|parent| parent.children_changed(mutation));
125+ // println!("XXX children_changed");
126+ // let source: String = self
127+ // .upcast::<Node>()
128+ // .html_serialize(TraversalScope::IncludeNode, false, vec![], CanGc::note())
129+ // .into();
130+ // let base64 = base64::engine::general_purpose::STANDARD.encode(source);
131+ // let source = format!("data:image/svg+xml;base64,{}", base64);
132+ // *self.serialized_data_url.borrow_mut() = source;
133+ // }
99134}
0 commit comments