Skip to content

Commit

Permalink
Move SVG processing to construct pass
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Nov 11, 2024
1 parent 3c531bc commit 6604185
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
18 changes: 0 additions & 18 deletions packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::events::{EventData, HitResult, RendererEvent};
use crate::node::{ImageData, NodeSpecificData, TextBrush};
use crate::util::parse_svg;
use crate::{ElementNodeData, Node, NodeData, TextNodeData, Viewport};
use app_units::Au;
use html5ever::local_name;
Expand Down Expand Up @@ -676,23 +675,6 @@ impl Document {
self.add_stylesheet_for_node(sheet, target_id);
}

pub fn process_svg_element(&mut self, target_id: usize) {
let outer_html = self.nodes[target_id].outer_html();
println!("{}", outer_html);
match parse_svg(outer_html.as_bytes()) {
Ok(svg) => {
println!("SVG parsed successfully");
self.nodes[target_id]
.element_data_mut()
.unwrap()
.node_specific_data = NodeSpecificData::Svg(svg);
}
Err(err) => {
dbg!(err);
}
};
}

pub fn remove_user_agent_stylesheet(&mut self, contents: &str) {
if let Some(sheet) = self.ua_stylesheets.remove(contents) {
self.stylist.remove_stylesheet(sheet, &self.guard.read());
Expand Down
8 changes: 0 additions & 8 deletions packages/blitz-dom/src/htmlsink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ fn html5ever_to_blitz_attr(attr: html5ever::Attribute) -> Attribute {
pub struct DocumentHtmlParser<'a> {
doc: RefCell<&'a mut Document>,
style_nodes: RefCell<Vec<usize>>,
svg_nodes: RefCell<Vec<usize>>,

/// Errors that occurred during parsing.
pub errors: RefCell<Vec<Cow<'static, str>>>,
Expand All @@ -41,7 +40,6 @@ impl DocumentHtmlParser<'_> {
DocumentHtmlParser {
doc: RefCell::new(doc),
style_nodes: RefCell::new(Vec::new()),
svg_nodes: RefCell::new(Vec::new()),
errors: RefCell::new(Vec::new()),
quirks_mode: Cell::new(QuirksMode::NoQuirks),
net_provider,
Expand Down Expand Up @@ -181,11 +179,6 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
doc.process_style_element(*id);
}

// Parse inline SVGs (<svg> elements)
for id in self.svg_nodes.borrow().iter() {
doc.process_svg_element(*id);
}

for error in self.errors.borrow().iter() {
println!("ERROR: {}", error);
}
Expand Down Expand Up @@ -240,7 +233,6 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
"img" => self.load_image(id),
"input" => self.process_button_input(id),
"style" => self.style_nodes.borrow_mut().push(id),
"svg" => self.svg_nodes.borrow_mut().push(id),
_ => {}
}

Expand Down
26 changes: 24 additions & 2 deletions packages/blitz-dom/src/layout/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use crate::{
ListItemLayout, ListItemLayoutPosition, Marker, NodeKind, NodeSpecificData, TextBrush,
TextInputData, TextLayout,
},
stylo_to_parley, Document, ElementNodeData, Node, NodeData,
stylo_to_parley,
util::parse_svg,
Document, ElementNodeData, Node, NodeData,
};

use super::table::build_table_context;
Expand Down Expand Up @@ -64,6 +66,23 @@ pub(crate) fn collect_layout_children(
}
}

if matches!(tag_name, "svg") {
let outer_html = doc.get_node(container_node_id).unwrap().outer_html();
match parse_svg(outer_html.as_bytes()) {
Ok(svg) => {
doc.get_node_mut(container_node_id)
.unwrap()
.element_data_mut()
.unwrap()
.node_specific_data = NodeSpecificData::Svg(svg);
}
Err(err) => {
dbg!(err);
}
};
return;
}

//Only ol tags have start and reversed attributes
let (mut index, reversed) = if tag_name == "ol" {
(
Expand Down Expand Up @@ -759,7 +778,10 @@ pub(crate) fn build_inline_layout(
(DisplayOutside::Inline, DisplayInside::Flow) => {
let tag_name = &element_data.name.local;

if *tag_name == local_name!("img") || *tag_name == local_name!("input") {
if *tag_name == local_name!("img")
|| *tag_name == local_name!("svg")
|| *tag_name == local_name!("input")
{
builder.push_inline_box(InlineBox {
id: node_id as u64,
// Overridden by push_inline_box method
Expand Down

0 comments on commit 6604185

Please sign in to comment.