Skip to content

Commit

Permalink
Merge pull request #688 from Sharktheone/v2
Browse files Browse the repository at this point in the history
begin migration to engine v2
  • Loading branch information
Sharktheone authored Dec 5, 2024
2 parents c845980 + d97b973 commit 8df1048
Show file tree
Hide file tree
Showing 96 changed files with 2,535 additions and 2,446 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions benches/tree_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@ use std::fs::File;
use criterion::{criterion_group, criterion_main, Criterion};
use gosub_css3::system::Css3System;
use gosub_html5::document::builder::DocumentBuilderImpl;
use gosub_html5::document::document_impl::TreeIterator;
use gosub_html5::document::document_impl::{DocumentImpl, TreeIterator};
use gosub_html5::document::fragment::DocumentFragmentImpl;
use gosub_html5::parser::Html5Parser;
use gosub_shared::byte_stream::{ByteStream, Encoding};
use gosub_shared::document::DocumentHandle;
use gosub_shared::node::NodeId;
use gosub_shared::traits::config::{HasCssSystem, HasDocument, HasHtmlParser};
use gosub_shared::traits::document::DocumentBuilder;

#[derive(Clone, Debug, PartialEq)]
struct Config;

impl HasCssSystem for Config {
type CssSystem = Css3System;
}
impl HasDocument for Config {
type Document = DocumentImpl<Self>;
type DocumentFragment = DocumentFragmentImpl<Self>;
type DocumentBuilder = DocumentBuilderImpl;
}

impl HasHtmlParser for Config {
type HtmlParser = Html5Parser<'static, Self>;
}
fn wikipedia_main_page(c: &mut Criterion) {
// Criterion can report inconsistent results from run to run in some cases. We attempt to
// minimize that in this setup.
Expand All @@ -20,7 +38,7 @@ fn wikipedia_main_page(c: &mut Criterion) {
let mut stream = ByteStream::new(Encoding::UTF8, None);
let _ = stream.read_from_file(html_file);

let doc_handle = <DocumentBuilderImpl as DocumentBuilder<Css3System>>::new_document(None);
let doc_handle: DocumentHandle<Config> = DocumentBuilderImpl::new_document(None);
let _ = Html5Parser::parse_document(&mut stream, doc_handle.clone(), None);

group.bench_function("wikipedia main page", |b| {
Expand All @@ -45,7 +63,7 @@ fn stackoverflow_home(c: &mut Criterion) {
let mut bytestream = ByteStream::new(Encoding::UTF8, None);
let _ = bytestream.read_from_file(html_file);

let doc_handle = <DocumentBuilderImpl as DocumentBuilder<Css3System>>::new_document(None);
let doc_handle: DocumentHandle<Config> = DocumentBuilderImpl::new_document(None);
let _ = Html5Parser::parse_document(&mut bytestream, doc_handle.clone(), None);

group.bench_function("stackoverflow home", |b| {
Expand Down
4 changes: 2 additions & 2 deletions crates/gosub_css3/src/functions/attr.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::stylesheet::CssValue;
use gosub_shared::traits::css3::CssSystem;
use gosub_shared::traits::config::HasDocument;
use gosub_shared::traits::node::{ElementDataType, Node};

// Probably this shouldn't quite be in gosub_css3
#[allow(dead_code)]
pub fn resolve_attr<N: Node<C>, C: CssSystem>(values: &[CssValue], node: &N) -> Vec<CssValue> {
pub fn resolve_attr<C: HasDocument>(values: &[CssValue], node: &C::Node) -> Vec<CssValue> {
let Some(attr_name) = values.first().map(|v| v.to_string()) else {
return vec![];
};
Expand Down
5 changes: 3 additions & 2 deletions crates/gosub_css3/src/functions/calc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::stylesheet::CssValue;

#[allow(dead_code)]
pub fn resolve_calc(_values: &[CssValue]) -> Vec<CssValue> {
todo!()
pub fn resolve_calc(values: &[CssValue]) -> Vec<CssValue> {
println!("Calc called with {values:?}");
vec![CssValue::None]
}
12 changes: 5 additions & 7 deletions crates/gosub_css3/src/functions/var.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;

use crate::stylesheet::CssValue;
use gosub_shared::traits::css3::CssSystem;
use gosub_shared::traits::document::Document;
use gosub_shared::traits::config::HasDocument;
use std::collections::HashMap;

#[allow(dead_code)]
#[derive(Clone, Debug, Default)]
Expand All @@ -12,7 +10,7 @@ pub struct VariableEnvironment {

#[allow(dead_code)]
impl VariableEnvironment {
pub fn get<D: Document<C>, C: CssSystem>(&self, name: &str, _doc: &D, _node: &D::Node) -> Option<CssValue> {
pub fn get<C: HasDocument>(&self, name: &str, _doc: &C::Document, _node: &C::Node) -> Option<CssValue> {
let mut current = Some(self);

while let Some(env) = current {
Expand All @@ -32,7 +30,7 @@ impl VariableEnvironment {
}

#[allow(dead_code)]
pub fn resolve_var<D: Document<C>, C: CssSystem>(values: &[CssValue], doc: &D, node: &D::Node) -> Vec<CssValue> {
pub fn resolve_var<C: HasDocument>(values: &[CssValue], doc: &C::Document, node: &C::Node) -> Vec<CssValue> {
let Some(name) = values.first().map(|v| {
let mut str = v.to_string();

Expand All @@ -50,7 +48,7 @@ pub fn resolve_var<D: Document<C>, C: CssSystem>(values: &[CssValue], doc: &D, n

let environment = VariableEnvironment::default(); //TODO: get from node

let Some(value) = environment.get(&name, doc, node) else {
let Some(value) = environment.get::<C>(&name, doc, node) else {
let Some(default) = values.get(1).cloned() else {
return vec![];
};
Expand Down
2 changes: 2 additions & 0 deletions crates/gosub_css3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate core;

use crate::ast::convert_ast_to_stylesheet;
use crate::stylesheet::CssStylesheet;
use crate::tokenizer::Tokenizer;
Expand Down
55 changes: 27 additions & 28 deletions crates/gosub_css3/src/matcher/styling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ use std::fmt::Display;

use gosub_shared::document::DocumentHandle;
use gosub_shared::node::NodeId;
use gosub_shared::traits::css3::{CssOrigin, CssPropertyMap, CssSystem};
use gosub_shared::traits::config::HasDocument;
use gosub_shared::traits::css3;
use gosub_shared::traits::css3::{CssOrigin, CssPropertyMap};
use gosub_shared::traits::document::Document;
use gosub_shared::traits::node::ClassList;
use gosub_shared::traits::node::ElementDataType;
use gosub_shared::traits::node::Node;

use crate::matcher::property_definitions::get_css_definitions;
use crate::stylesheet::{Combinator, CssSelector, CssSelectorPart, CssValue, MatcherType, Specificity};
use crate::system::Css3System;

// Matches a complete selector (all parts) against the given node(id)
pub(crate) fn match_selector<D: Document<C>, C: CssSystem>(
document: DocumentHandle<D, C>,
pub(crate) fn match_selector<C: HasDocument>(
document: DocumentHandle<C>,
node_id: NodeId,
selector: &CssSelector,
) -> (bool, Specificity) {
Expand All @@ -41,8 +44,8 @@ fn consume<'a, T>(this: &mut &'a [T]) -> Option<&'a T> {
}

/// Returns true when the given node matches the part(s)
fn match_selector_parts<D: Document<C>, C: CssSystem>(
handle: DocumentHandle<D, C>,
fn match_selector_parts<C: HasDocument>(
handle: DocumentHandle<C>,
node_id: NodeId,
mut parts: &[CssSelectorPart],
) -> bool {
Expand All @@ -61,7 +64,7 @@ fn match_selector_parts<D: Document<C>, C: CssSystem>(
return false;
}

if !match_selector_part(part, current_node, &*binding, &mut next_current_node, &mut parts) {
if !match_selector_part::<C>(part, current_node, &*binding, &mut next_current_node, &mut parts) {
return false;
}

Expand All @@ -74,11 +77,11 @@ fn match_selector_parts<D: Document<C>, C: CssSystem>(
true
}

fn match_selector_part<'a, D: Document<C>, C: CssSystem>(
fn match_selector_part<'a, C: HasDocument>(
part: &CssSelectorPart,
current_node: &D::Node,
doc: &'a D,
next_node: &mut Option<&'a D::Node>,
current_node: &C::Node,
doc: &'a C::Document,
next_node: &mut Option<&'a C::Node>,
parts: &mut &[CssSelectorPart],
) -> bool {
match part {
Expand Down Expand Up @@ -198,7 +201,7 @@ fn match_selector_part<'a, D: Document<C>, C: CssSystem>(

*next_node = Some(parent);

if match_selector_part(last, parent, doc, next_node, parts) {
if match_selector_part::<C>(last, parent, doc, next_node, parts) {
return true;
}

Expand Down Expand Up @@ -227,7 +230,7 @@ fn match_selector_part<'a, D: Document<C>, C: CssSystem>(

*next_node = Some(parent);

match_selector_part(last, parent, doc, next_node, parts)
match_selector_part::<C>(last, parent, doc, next_node, parts)
}
Combinator::NextSibling => {
let parent_node = doc.node_by_id(current_node.parent_id().unwrap());
Expand Down Expand Up @@ -261,7 +264,7 @@ fn match_selector_part<'a, D: Document<C>, C: CssSystem>(

*next_node = Some(prev);

match_selector_part(last, prev, doc, next_node, parts)
match_selector_part::<C>(last, prev, doc, next_node, parts)
}
Combinator::SubsequentSibling => {
let parent_node = doc.node_by_id(current_node.parent_id().unwrap());
Expand All @@ -282,7 +285,7 @@ fn match_selector_part<'a, D: Document<C>, C: CssSystem>(
continue;
};

if match_selector_part(last, child, doc, next_node, parts) {
if match_selector_part::<C>(last, child, doc, next_node, parts) {
return true;
}
}
Expand Down Expand Up @@ -534,9 +537,7 @@ impl Display for CssProperty {
}
}

impl gosub_shared::traits::css3::CssProperty for CssProperty {
type Value = CssValue;

impl css3::CssProperty<Css3System> for CssProperty {
fn compute_value(&mut self) {
self.compute_value();
}
Expand Down Expand Up @@ -588,15 +589,15 @@ impl gosub_shared::traits::css3::CssProperty for CssProperty {
}
}

fn as_list(&self) -> Option<&[Self::Value]> {
fn as_list(&self) -> Option<&[CssValue]> {
if let CssValue::List(list) = &self.actual {
Some(list)
} else {
None
}
}

fn as_function(&self) -> Option<(&str, &[Self::Value])> {
fn as_function(&self) -> Option<(&str, &[CssValue])> {
if let CssValue::Function(name, args) = &self.actual {
Some((name.as_str(), args))
} else {
Expand Down Expand Up @@ -636,34 +637,32 @@ impl CssProperties {
}
}

impl CssPropertyMap for CssProperties {
type Property = CssProperty;

fn insert_inherited(&mut self, name: &str, value: Self::Property) {
impl CssPropertyMap<Css3System> for CssProperties {
fn insert_inherited(&mut self, name: &str, value: CssProperty) {
self.properties.entry(name.to_string()).or_insert(value);
}

fn insert(&mut self, name: &str, value: Self::Property) {
fn insert(&mut self, name: &str, value: CssProperty) {
self.properties.insert(name.to_string(), value);
}

fn get(&self, name: &str) -> Option<&Self::Property> {
fn get(&self, name: &str) -> Option<&CssProperty> {
self.properties.get(name)
}

fn get_mut(&mut self, name: &str) -> Option<&mut Self::Property> {
fn get_mut(&mut self, name: &str) -> Option<&mut CssProperty> {
self.properties.get_mut(name)
}

fn make_dirty(&mut self) {
self.dirty = true;
}

fn iter(&self) -> impl Iterator<Item = (&str, &Self::Property)> + '_ {
fn iter(&self) -> impl Iterator<Item = (&str, &CssProperty)> + '_ {
self.properties.iter().map(|(k, v)| (k.as_str(), v))
}

fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Self::Property)> + '_ {
fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut CssProperty)> + '_ {
self.properties.iter_mut().map(|(k, v)| (k.as_str(), v))
}

Expand Down
Loading

0 comments on commit 8df1048

Please sign in to comment.