Skip to content

Commit

Permalink
fmt, clippy and some git weirdness
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Sep 13, 2024
1 parent f589134 commit 54a9167
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 231 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ members = ["packages/blitz", "packages/dom", "packages/dioxus-blitz", "packages/
resolver = "2"

[workspace.dependencies]
style = { git = "https://github.com/dioxuslabs/stylo", branch = "enable-table-moz-center-style-adjust" }
style_config = { git = "https://github.com/dioxuslabs/stylo", branch = "enable-table-moz-center-style-adjust" }
style_traits = { git = "https://github.com/dioxuslabs/stylo", branch = "enable-table-moz-center-style-adjust" }
style_dom = { git = "https://github.com/dioxuslabs/stylo", package = "dom", branch = "enable-table-moz-center-style-adjust" }
selectors = { git = "https://github.com/dioxuslabs/stylo", branch = "enable-table-moz-center-style-adjust" }
html5ever = "0.27" # needs to match stylo markup5ever version
style = { git = "https://github.com/dioxuslabs/stylo", branch = "blitz" }
style_config = { git = "https://github.com/dioxuslabs/stylo", branch = "blitz" }
style_traits = { git = "https://github.com/dioxuslabs/stylo", branch = "blitz" }
style_dom = { git = "https://github.com/dioxuslabs/stylo", package = "dom", branch = "blitz" }
selectors = { git = "https://github.com/dioxuslabs/stylo", branch = "blitz" }
html5ever = "0.29" # needs to match stylo markup5ever version
taffy = { git = "https://github.com/dioxuslabs/taffy", rev = "950a0eb1322f15e5d1083f4793b55d52061718de" }
parley = { git = "https://github.com/nicoburns/parley", rev = "029bf1df3e1829935fa6d25b875d3138f79a62c1" }
dioxus = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef" }
Expand Down
144 changes: 74 additions & 70 deletions packages/dom/src/htmlsink.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::node::{Attribute, ElementNodeData, Node, NodeData};
use crate::util::{CssHandler, ImageHandler, Resource};
use std::borrow::Cow;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::HashSet;

use crate::node::{Attribute, ElementNodeData, Node, NodeData};
use crate::Document;
use blitz_traits::net::SharedProvider;
use html5ever::local_name;
use html5ever::{
tendril::{StrTendril, TendrilSink},
tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink},
ExpandedName, QualName,
QualName,
};
use std::borrow::Cow;
use std::collections::HashSet;

/// Convert an html5ever Attribute which uses tendril for its value to a blitz Attribute
/// which uses String.
Expand All @@ -21,26 +23,26 @@ fn html5ever_to_blitz_attr(attr: html5ever::Attribute) -> Attribute {
}

pub struct DocumentHtmlParser<'a> {
doc: &'a mut Document,
doc: RefCell<&'a mut Document>,

style_nodes: Vec<usize>,
style_nodes: RefCell<Vec<usize>>,

/// Errors that occurred during parsing.
pub errors: Vec<Cow<'static, str>>,
pub errors: RefCell<Vec<Cow<'static, str>>>,

/// The document's quirks mode.
pub quirks_mode: QuirksMode,
pub quirks_mode: Cell<QuirksMode>,

net_provider: SharedProvider<Resource>,
}

impl<'a> DocumentHtmlParser<'a> {
pub fn new(doc: &mut Document, net_provider: SharedProvider<Resource>) -> DocumentHtmlParser {
DocumentHtmlParser {
doc,
style_nodes: Vec::new(),
errors: Vec::new(),
quirks_mode: QuirksMode::NoQuirks,
doc: RefCell::new(doc),
style_nodes: RefCell::new(Vec::new()),
errors: RefCell::new(Vec::new()),
quirks_mode: Cell::new(QuirksMode::NoQuirks),
net_provider,
}
}
Expand All @@ -57,27 +59,27 @@ impl<'a> DocumentHtmlParser<'a> {
.unwrap()
}

fn create_node(&mut self, node_data: NodeData) -> usize {
self.doc.create_node(node_data)
fn create_node(&self, node_data: NodeData) -> usize {
self.doc.borrow_mut().create_node(node_data)
}

fn create_text_node(&mut self, text: &str) -> usize {
self.doc.create_text_node(text)
fn create_text_node(&self, text: &str) -> usize {
self.doc.borrow_mut().create_text_node(text)
}

fn node(&self, id: usize) -> &Node {
&self.doc.nodes[id]
fn node(&self, id: usize) -> Ref<Node> {
Ref::map(self.doc.borrow(), |doc| &doc.nodes[id])
}

fn node_mut(&mut self, id: usize) -> &mut Node {
&mut self.doc.nodes[id]
fn node_mut(&self, id: usize) -> RefMut<Node> {
RefMut::map(self.doc.borrow_mut(), |doc| &mut doc.nodes[id])
}

fn try_append_text_to_text_node(&mut self, node_id: Option<usize>, text: &str) -> bool {
fn try_append_text_to_text_node(&self, node_id: Option<usize>, text: &str) -> bool {
let Some(node_id) = node_id else {
return false;
};
let node = self.node_mut(node_id);
let mut node = self.node_mut(node_id);

match node.text_data_mut() {
Some(data) => {
Expand All @@ -88,41 +90,42 @@ impl<'a> DocumentHtmlParser<'a> {
}
}

fn last_child(&mut self, parent_id: usize) -> Option<usize> {
fn last_child(&self, parent_id: usize) -> Option<usize> {
self.node(parent_id).children.last().copied()
}

fn load_linked_stylesheet(&mut self, target_id: usize) {
fn load_linked_stylesheet(&self, target_id: usize) {
let node = self.node(target_id);

let rel_attr = node.attr(local_name!("rel"));
let href_attr = node.attr(local_name!("href"));

if let (Some("stylesheet"), Some(href)) = (rel_attr, href_attr) {
let url = self.doc.resolve_url(href);
let url = self.doc.borrow().resolve_url(href);
let guard = self.doc.borrow().guard.clone();
self.net_provider.fetch(
url.clone(),
Box::new(CssHandler {
node: target_id,
source_url: url,
guard: self.doc.guard.clone(),
guard,
}),
);
}
}

fn load_image(&mut self, target_id: usize) {
fn load_image(&self, target_id: usize) {
let node = self.node(target_id);
if let Some(raw_src) = node.attr(local_name!("src")) {
if !raw_src.is_empty() {
let src = self.doc.resolve_url(raw_src);
let src = self.doc.borrow().resolve_url(raw_src);
self.net_provider
.fetch(src, Box::new(ImageHandler::new(target_id)));
}
}
}

fn process_button_input(&mut self, target_id: usize) {
fn process_button_input(&self, target_id: usize) {
let node = self.node(target_id);
let Some(data) = node.element_data() else {
return;
Expand Down Expand Up @@ -150,47 +153,52 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
// we use the ID of the nodes in the tree as the handle
type Handle = usize;

type ElemName<'a> = Ref<'a, QualName> where Self: 'a;

fn finish(self) -> Self::Output {
let doc = self.doc.into_inner();

// Add inline stylesheets (<style> elements)
for id in &self.style_nodes {
self.doc.process_style_element(*id);
for id in self.style_nodes.borrow().iter() {
doc.process_style_element(*id);
}

// Compute child_idx fields.
self.doc.flush_child_indexes(0, 0, 0);
doc.flush_child_indexes(0, 0, 0);

for error in self.errors {
for error in self.errors.borrow().iter() {
println!("ERROR: {}", error);
}

self.doc
doc
}

fn parse_error(&mut self, msg: Cow<'static, str>) {
self.errors.push(msg);
fn parse_error(&self, msg: Cow<'static, str>) {
self.errors.borrow_mut().push(msg);
}

fn get_document(&mut self) -> Self::Handle {
fn get_document(&self) -> Self::Handle {
0
}

fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> ExpandedName<'a> {
self.node(*target)
.element_data()
.expect("TreeSink::elem_name called on a node which is not an element!")
.name
.expanded()
fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> Self::ElemName<'a> {
Ref::map(self.doc.borrow(), |doc| {
&doc.nodes[*target]
.element_data()
.expect("TreeSink::elem_name called on a node which is not an element!")
.name
})
}

fn create_element(
&mut self,
&self,
name: QualName,
attrs: Vec<html5ever::Attribute>,
_flags: ElementFlags,
) -> Self::Handle {
let attrs = attrs.into_iter().map(html5ever_to_blitz_attr).collect();
let mut data = ElementNodeData::new(name.clone(), attrs);
data.flush_style_attribute(&self.doc.guard);
data.flush_style_attribute(&self.doc.borrow().guard);

let id = self.create_node(NodeData::Element(data));
let node = self.node(id);
Expand All @@ -200,31 +208,34 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {

// If the node has an "id" attribute, store it in the ID map.
if let Some(id_attr) = node.attr(local_name!("id")) {
self.doc.nodes_to_id.insert(id_attr.to_string(), id);
self.doc
.borrow_mut()
.nodes_to_id
.insert(id_attr.to_string(), id);
}

// Custom post-processing by element tag name
match name.local.as_ref() {
"link" => self.load_linked_stylesheet(id),
"img" => self.load_image(id),
"input" => self.process_button_input(id),
"style" => self.style_nodes.push(id),
"style" => self.style_nodes.borrow_mut().push(id),
_ => {}
}

id
}

fn create_comment(&mut self, _text: StrTendril) -> Self::Handle {
fn create_comment(&self, _text: StrTendril) -> Self::Handle {
self.create_node(NodeData::Comment)
}

fn create_pi(&mut self, _target: StrTendril, _data: StrTendril) -> Self::Handle {
fn create_pi(&self, _target: StrTendril, _data: StrTendril) -> Self::Handle {
// NOTE: html5ever does not call this method (only xml5ever does)
unimplemented!()
}

fn append(&mut self, parent_id: &Self::Handle, child: NodeOrText<Self::Handle>) {
fn append(&self, parent_id: &Self::Handle, child: NodeOrText<Self::Handle>) {
match child {
NodeOrText::AppendNode(child_id) => {
self.node_mut(*parent_id).children.push(child_id);
Expand All @@ -243,11 +254,7 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {

// Note: The tree builder promises we won't have a text node after the insertion point.
// https://github.com/servo/html5ever/blob/main/rcdom/lib.rs#L338
fn append_before_sibling(
&mut self,
sibling_id: &Self::Handle,
new_node: NodeOrText<Self::Handle>,
) {
fn append_before_sibling(&self, sibling_id: &Self::Handle, new_node: NodeOrText<Self::Handle>) {
let sibling = self.node(*sibling_id);
let parent_id = sibling.parent.expect("Sibling has not parent");
let parent = self.node(parent_id);
Expand Down Expand Up @@ -284,7 +291,7 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
}

fn append_based_on_parent_node(
&mut self,
&self,
element: &Self::Handle,
prev_element: &Self::Handle,
child: NodeOrText<Self::Handle>,
Expand All @@ -298,15 +305,15 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
}

fn append_doctype_to_document(
&mut self,
&self,
_name: StrTendril,
_public_id: StrTendril,
_system_id: StrTendril,
) {
// Ignore. We don't care about the DOCTYPE for now.
}

fn get_template_contents(&mut self, target: &Self::Handle) -> Self::Handle {
fn get_template_contents(&self, target: &Self::Handle) -> Self::Handle {
// TODO: implement templates properly. This should allow to function like regular elements.
*target
}
Expand All @@ -315,15 +322,13 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
x == y
}

fn set_quirks_mode(&mut self, mode: QuirksMode) {
self.quirks_mode = mode;
fn set_quirks_mode(&self, mode: QuirksMode) {
self.quirks_mode.set(mode);
}

fn add_attrs_if_missing(&mut self, target: &Self::Handle, attrs: Vec<html5ever::Attribute>) {
let element_data = self
.node_mut(*target)
.element_data_mut()
.expect("Not an element");
fn add_attrs_if_missing(&self, target: &Self::Handle, attrs: Vec<html5ever::Attribute>) {
let mut node = self.node_mut(*target);
let element_data = node.element_data_mut().expect("Not an element");

let existing_names = element_data
.attrs
Expand All @@ -339,18 +344,17 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
);
}

fn remove_from_parent(&mut self, target: &Self::Handle) {
let node = self.node_mut(*target);
fn remove_from_parent(&self, target: &Self::Handle) {
let mut node = self.node_mut(*target);
let parent_id = node.parent.take().expect("Node has no parent");
self.node_mut(parent_id)
.children
.retain(|child_id| child_id != target);
}

fn reparent_children(&mut self, node_id: &Self::Handle, new_parent_id: &Self::Handle) {
fn reparent_children(&self, node_id: &Self::Handle, new_parent_id: &Self::Handle) {
// Take children array from old parent
let node = self.node_mut(*node_id);
let children = std::mem::take(&mut node.children);
let children = std::mem::take(&mut self.node_mut(*node_id).children);

// Update parent reference of children
for child_id in children.iter() {
Expand Down
13 changes: 13 additions & 0 deletions packages/dom/src/stylo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ impl<'a> selectors::Element for BlitzNode<'a> {
NonTSPseudoClass::Disabled => false,
NonTSPseudoClass::Enabled => false,
NonTSPseudoClass::Focus => self.element_state.contains(ElementState::FOCUS),
NonTSPseudoClass::FocusWithin => false,
NonTSPseudoClass::FocusVisible => false,
NonTSPseudoClass::Fullscreen => false,
NonTSPseudoClass::Hover => self.element_state.contains(ElementState::HOVER),
NonTSPseudoClass::Indeterminate => false,
Expand All @@ -437,6 +439,17 @@ impl<'a> selectors::Element for BlitzNode<'a> {
NonTSPseudoClass::ServoNonZeroBorder => false,
NonTSPseudoClass::Target => false,
NonTSPseudoClass::Visited => false,
NonTSPseudoClass::Autofill => false,
NonTSPseudoClass::Default => false,

NonTSPseudoClass::InRange => false,
NonTSPseudoClass::Modal => false,
NonTSPseudoClass::Optional => false,
NonTSPseudoClass::OutOfRange => false,
NonTSPseudoClass::PopoverOpen => false,
NonTSPseudoClass::Required => false,
NonTSPseudoClass::UserInvalid => false,
NonTSPseudoClass::UserValid => false,
}
}

Expand Down
11 changes: 0 additions & 11 deletions packages/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,3 @@ tokio = { workspace = true }
reqwest = { version = "0.12.7" }
data-url = "0.3.1"
thiserror = "1.0.63"
[package]
name = "blitz-net"
version = "0.1.0"
edition = "2021"

[dependencies]
blitz-traits = { path = "../traits" }
tokio = { workspace = true }
reqwest = { version = "0.12.7" }
data-url = "0.3.1"
thiserror = "1.0.63"
Loading

0 comments on commit 54a9167

Please sign in to comment.