Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSS import support #139

Merged
merged 12 commits into from
Nov 3, 2024
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/assets/servo.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<meta property="og:logo" content="/img/servo-symbol-color-no-container.png">
<meta property="og:url" content="/">

<link rel="stylesheet" href="file:///Users/nico/code/oss/blitz/examples/assets/servo.css">
kokoISnoTarget marked this conversation as resolved.
Show resolved Hide resolved
<link rel="stylesheet" href="servo.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.0/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Fira+Sans:300,400,600,700|Fira+Mono:300,400,600,700|Fire+Sans:100,200,300,400,500,600,700">
<link rel="shortcut icon" href="/img/servo-symbol-color-no-container.png">
Expand All @@ -36,7 +36,7 @@
<div class="container">
<div class="navbar-brand">
<a class="navbar-item" href="/">
<img src="/img/servo-color-negative-no-container.png" alt="Home">
<img src="servo-color-negative-no-container.png" alt="Home">
</a>

<a role="button" class="navbar-burger" data-target="navbar-menu" aria-label="menu" aria-expanded="false">
Expand Down
10 changes: 4 additions & 6 deletions examples/screenshot.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Load first CLI argument as a url. Fallback to google.com if no CLI argument is provided.

use blitz_dom::util::Resource;
use blitz_dom::net::Resource;
use blitz_dom::{HtmlDocument, Viewport};
use blitz_net::{MpscCallback, Provider};
use blitz_renderer_vello::render_to_buffer;
use blitz_traits::net::{SharedCallback, SharedProvider};
use blitz_traits::net::SharedProvider;
use reqwest::Url;
use std::sync::Arc;
use std::{
Expand Down Expand Up @@ -52,10 +52,8 @@ async fn main() {
.unwrap_or(1200);

let (mut recv, callback) = MpscCallback::new();
let net = Arc::new(Provider::new(
Handle::current(),
Arc::new(callback) as SharedCallback<Resource>,
));
let callback = Arc::new(callback);
let net = Arc::new(Provider::new(Handle::current(), callback));

timer.time("Setup document prerequisites");

Expand Down
5 changes: 4 additions & 1 deletion examples/servo.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use dioxus_native::Config;
use std::path::Path;

fn main() {
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/assets/servo.html");
let url = format!("file://{}", dir.display());
dioxus_native::launch_static_html_cfg(
include_str!("./assets/servo.html"),
Config {
stylesheets: Vec::new(),
base_url: Some(String::from("https://servo.org/")),
base_url: Some(url),
},
);
}
2 changes: 1 addition & 1 deletion packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use style::values::computed::Overflow;
use style::values::GenericAtomIdent;
use winit::keyboard::{Key, NamedKey};
// use quadtree_rs::Quadtree;
use crate::util::Resource;
use crate::net::Resource;
use selectors::{matching::QuirksMode, Element};
use slab::Slab;
use std::any::Any;
Expand Down
6 changes: 3 additions & 3 deletions packages/blitz-dom/src/html_document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::events::RendererEvent;
use crate::{Document, DocumentHtmlParser, DocumentLike, Viewport};

use crate::util::Resource;
use crate::net::Resource;
use crate::DEFAULT_CSS;
use blitz_traits::net::SharedProvider;

Expand Down Expand Up @@ -37,7 +37,7 @@ impl HtmlDocument {
html: &str,
base_url: Option<String>,
stylesheets: Vec<String>,
net: SharedProvider<Resource>,
net_provider: SharedProvider<Resource>,
) -> Self {
// Spin up the virtualdom and include the default stylesheet
let viewport = Viewport::new(0, 0, 1.0);
Expand All @@ -55,7 +55,7 @@ impl HtmlDocument {
}

// Parse HTML string into document
DocumentHtmlParser::parse_into_doc(&mut dom, net, html);
DocumentHtmlParser::parse_into_doc(&mut dom, html, net_provider);

HtmlDocument { inner: dom }
}
Expand Down
20 changes: 11 additions & 9 deletions packages/blitz-dom/src/htmlsink.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::util::{CssHandler, ImageHandler, Resource};
use crate::net::{CssHandler, ImageHandler, Resource};
use std::borrow::Cow;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::HashSet;
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct DocumentHtmlParser<'a> {
/// The document's quirks mode.
pub quirks_mode: Cell<QuirksMode>,

pub net_provider: SharedProvider<Resource>,
net_provider: SharedProvider<Resource>,
}

impl DocumentHtmlParser<'_> {
Expand All @@ -49,10 +49,10 @@ impl DocumentHtmlParser<'_> {

pub fn parse_into_doc<'d>(
doc: &'d mut Document,
net: SharedProvider<Resource>,
html: &str,
net_provider: SharedProvider<Resource>,
) -> &'d mut Document {
let sink = Self::new(doc, net);
let sink = Self::new(doc, net_provider);
html5ever::parse_document(sink, Default::default())
.from_utf8()
.read_from(&mut html.as_bytes())
Expand Down Expand Up @@ -106,13 +106,12 @@ impl DocumentHtmlParser<'_> {

if let (Some("stylesheet"), Some(href)) = (rel_attr, href_attr) {
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,
guard: self.doc.borrow().guard.clone(),
provider: self.net_provider.clone(),
}),
);
Expand All @@ -125,7 +124,7 @@ impl DocumentHtmlParser<'_> {
if !raw_src.is_empty() {
let src = self.doc.borrow().resolve_url(raw_src);
self.net_provider
.fetch(src, Box::new(ImageHandler::new(target_id)));
.fetch(src, Box::new(ImageHandler(target_id)));
}
}
}
Expand Down Expand Up @@ -159,7 +158,10 @@ 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;
type ElemName<'a>
= Ref<'a, QualName>
where
Self: 'a;

fn finish(self) -> Self::Output {
let doc = self.doc.into_inner();
Expand Down Expand Up @@ -372,7 +374,7 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
#[test]
fn parses_some_html() {
use crate::Viewport;
use blitz_traits::net::DummyProvider;
use blitz_traits::net::{DummyCallback, DummyProvider};
use std::sync::Arc;

let html = "<!DOCTYPE html><html><body><h1>hello world</h1></body></html>";
Expand Down
28 changes: 20 additions & 8 deletions packages/blitz-dom/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ impl TraversePartialTree for Document {
impl TraverseTree for Document {}

impl LayoutPartialTree for Document {
type CoreContainerStyle<'a> = &'a taffy::Style where Self : 'a;
type CacheMut<'b> = &'b mut Cache where Self: 'b;
type CoreContainerStyle<'a>
= &'a taffy::Style
where
Self: 'a;
type CacheMut<'b>
= &'b mut Cache
where
Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> &Style {
&self.node_from_id(node_id).style
Expand Down Expand Up @@ -297,11 +303,13 @@ impl LayoutPartialTree for Document {
}

impl taffy::LayoutBlockContainer for Document {
type BlockContainerStyle<'a> = &'a Style
type BlockContainerStyle<'a>
= &'a Style
where
Self: 'a;

type BlockItemStyle<'a> = &'a Style
type BlockItemStyle<'a>
= &'a Style
where
Self: 'a;

Expand All @@ -315,11 +323,13 @@ impl taffy::LayoutBlockContainer for Document {
}

impl taffy::LayoutFlexboxContainer for Document {
type FlexboxContainerStyle<'a> = &'a Style
type FlexboxContainerStyle<'a>
= &'a Style
where
Self: 'a;

type FlexboxItemStyle<'a> = &'a Style
type FlexboxItemStyle<'a>
= &'a Style
where
Self: 'a;

Expand All @@ -333,11 +343,13 @@ impl taffy::LayoutFlexboxContainer for Document {
}

impl taffy::LayoutGridContainer for Document {
type GridContainerStyle<'a> = &'a Style
type GridContainerStyle<'a>
= &'a Style
where
Self: 'a;

type GridItemStyle<'a> = &'a Style
type GridItemStyle<'a>
= &'a Style
where
Self: 'a;

Expand Down
21 changes: 16 additions & 5 deletions packages/blitz-dom/src/layout/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ impl Iterator for RangeIter {
}

impl taffy::TraversePartialTree for TableTreeWrapper<'_> {
type ChildIter<'a> = RangeIter where Self : 'a;
type ChildIter<'a>
= RangeIter
where
Self: 'a;

#[inline(always)]
fn child_ids(&self, _node_id: taffy::NodeId) -> Self::ChildIter<'_> {
Expand All @@ -186,8 +189,14 @@ impl taffy::TraversePartialTree for TableTreeWrapper<'_> {
impl taffy::TraverseTree for TableTreeWrapper<'_> {}

impl taffy::LayoutPartialTree for TableTreeWrapper<'_> {
type CoreContainerStyle<'a> = &'a taffy::Style where Self : 'a;
type CacheMut<'b> = &'b mut taffy::Cache where Self: 'b;
type CoreContainerStyle<'a>
= &'a taffy::Style
where
Self: 'a;
type CacheMut<'b>
= &'b mut taffy::Cache
where
Self: 'b;

fn get_core_container_style(&self, _node_id: taffy::NodeId) -> &taffy::Style {
&self.ctx.style
Expand Down Expand Up @@ -222,11 +231,13 @@ impl taffy::LayoutPartialTree for TableTreeWrapper<'_> {
}

impl taffy::LayoutGridContainer for TableTreeWrapper<'_> {
type GridContainerStyle<'a> = &'a taffy::Style
type GridContainerStyle<'a>
= &'a taffy::Style
where
Self: 'a;

type GridItemStyle<'a> = &'a taffy::Style
type GridItemStyle<'a>
= &'a taffy::Style
where
Self: 'a;

Expand Down
2 changes: 2 additions & 0 deletions packages/blitz-dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub mod debug;

pub mod events;

pub mod net;

pub mod viewport;

pub use document::{Document, DocumentLike};
Expand Down
Loading