Skip to content

Commit

Permalink
tmp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Sep 16, 2024
1 parent adc8895 commit 8094bba
Show file tree
Hide file tree
Showing 23 changed files with 254 additions and 205 deletions.
1 change: 1 addition & 0 deletions crates/gosub_css3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod stylesheet;
pub mod tokenizer;
mod unicode;
pub mod walker;
#[allow(dead_code)]
pub mod matcher;
mod errors;

Expand Down
11 changes: 0 additions & 11 deletions crates/gosub_css3/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ pub struct CssStylesheet {
pub parse_log: Vec<CssLog>
}

/// Defines the origin of the stylesheet (or declaration)
#[derive(Debug, PartialEq, Clone)]
pub enum CssOrigin {
/// Browser/user agent defined stylesheets
UserAgent,
/// Author defined stylesheets that are linked or embedded in the HTML files
Author,
/// User defined stylesheets that will override the author and user agent stylesheets (for instance, custom user styles or extensions)
User,
}

/// A CSS rule, which contains a list of selectors and a list of declarations
#[derive(Debug, PartialEq, Clone)]
pub struct CssRule {
Expand Down
13 changes: 7 additions & 6 deletions crates/gosub_css3/src/matcher/styling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use gosub_shared::node::NodeId;
use gosub_shared::traits::document::Document;
use gosub_shared::traits::node::Node;
use gosub_shared::document::DocumentHandle;
use gosub_shared::traits::css3::CssSystem;

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

// Matches a complete selector (all parts) against the given node(id)
pub(crate) fn match_selector<D: Document>(
document: DocumentHandle<D>,
pub(crate) fn match_selector<D: Document<S>, S: CssSystem>(
document: DocumentHandle<D, S>,
node_id: NodeId,
selector: &CssSelector,
) -> (bool, Specificity) {
Expand All @@ -38,8 +39,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>(
handle: DocumentHandle<D>,
fn match_selector_parts<D: Document<S>, S: CssSystem>(
handle: DocumentHandle<D, S>,
node_id: NodeId,
mut parts: &[CssSelectorPart],
) -> bool {
Expand Down Expand Up @@ -77,7 +78,7 @@ fn match_selector_parts<D: Document>(
true
}

fn match_selector_part<'a, D: Document>(
fn match_selector_part<'a, D: Document<S>, S: CssSystem>(
part: &CssSelectorPart,
current_node: &D::Node,
doc: &'a D,
Expand Down Expand Up @@ -235,7 +236,7 @@ fn match_selector_part<'a, D: Document>(
}
Combinator::NextSibling => {
let parent_node = doc.node_by_id(current_node.parent_id().unwrap());
let Some(children) = parent_node.map(|p| p.children().clone()) else {
let Some(children) = parent_node.map(|p| p.children()) else {
return false;
};

Expand Down
1 change: 1 addition & 0 deletions crates/gosub_css3/src/matcher/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Group {
pub components: Vec<SyntaxComponent>,
}

#[allow(dead_code)]
#[derive(PartialEq, Debug, Clone)]
pub enum GroupCombinators {
/// All elements must be matched in order (space delimited)
Expand Down
10 changes: 6 additions & 4 deletions crates/gosub_html5/src/document/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use url::Url;
use gosub_shared::traits::css3::CssSystem;

use gosub_shared::traits::document::{Document, DocumentType};
use gosub_shared::traits::node::{Node, QuirksMode};
Expand All @@ -12,16 +13,17 @@ pub struct DocumentBuilder {}

impl DocumentBuilder {
/// Creates a new document with a document root node
pub fn new_document<D: Document>(url: Option<Url>) -> DocumentHandle<D> {
pub fn new_document<D: Document<C>, C: CssSystem>(url: Option<Url>) -> DocumentHandle<D, C> {
let doc = D::new(DocumentType::HTML, url, None);
DocumentHandle::create(doc)
}

/// Creates a new document fragment with the context as the root node
pub fn new_document_fragment<D>(context_node: &D::Node) -> DocumentHandle<D>
pub fn new_document_fragment<D, C>(context_node: &D::Node) -> DocumentHandle<D, C>
where
D: Document,
D::Node: Node<Document=D>
C: CssSystem,
D: Document<C>,
D::Node: Node<C, Document=D>
{
let handle = context_node.handle();

Expand Down
62 changes: 29 additions & 33 deletions crates/gosub_html5/src/document/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use gosub_css3::stylesheet::CssStylesheet;
use gosub_shared::traits::document::{Document as OtherDocument, Document, DocumentType};
use crate::DocumentHandle;
use core::fmt::Debug;
Expand All @@ -7,6 +6,7 @@ use url::Url;

use gosub_shared::byte_stream::Location;
use gosub_shared::node::NodeId;
use gosub_shared::traits::css3::CssSystem;
use gosub_shared::traits::node::Node;
use gosub_shared::traits::node::QuirksMode;
use crate::node::arena::NodeArena;
Expand All @@ -26,24 +26,24 @@ pub(crate) fn is_valid_id_attribute_value(value: &str) -> bool {

/// Defines a document
#[derive(Debug)]
pub struct DocumentImpl {
pub struct DocumentImpl<C: CssSystem> {
// pub handle: Weak<DocumentHandle<Self>>,

/// URL of the given document (if any)
pub url: Option<Url>,
/// Holds and owns all nodes in the document
pub(crate) arena: NodeArena<NodeImpl>,
pub(crate) arena: NodeArena<NodeImpl<C>, C>,
/// HTML elements with ID (e.g., <div id="myid">)
named_id_elements: HashMap<String, NodeId>,
/// Document type of this document
pub doctype: DocumentType,
/// Quirks mode of this document
pub quirks_mode: QuirksMode,
/// Loaded stylesheets as extracted from the document
pub stylesheets: Vec<CssStylesheet>,
pub stylesheets: Vec<C::Stylesheet>,
}

impl PartialEq for DocumentImpl {
impl<C: CssSystem> PartialEq for DocumentImpl<C> {
fn eq(&self, other: &Self) -> bool {
self.url == other.url
&& self.arena == other.arena
Expand All @@ -54,15 +54,15 @@ impl PartialEq for DocumentImpl {
}
}

impl Default for DocumentImpl {
impl<C: CssSystem> Default for DocumentImpl<C> {
/// Returns a default document
fn default() -> Self {
Self::new(DocumentType::HTML, None, None)
}
}

impl Document for DocumentImpl {
type Node = NodeImpl;
impl<C: CssSystem> Document<C> for DocumentImpl<C> {
type Node = NodeImpl<C>;

// fn new_with_handle(document_type: DocumentType, url: Option<Url>, location: &Location, root_node: Option<&Self::Node>) -> DocumentHandle<Self> {
// let mut doc = Self::new(document_type, url);
Expand Down Expand Up @@ -97,11 +97,15 @@ impl Document for DocumentImpl {
doc
}

fn handle(&self) -> DocumentHandle<DocumentImpl> {
fn handle(&self) -> DocumentHandle<DocumentImpl<C>, C> {
todo!("handle() implementation")
// self.handle.upgrade().expect("failure").get().unwrap()
}

fn add_stylesheet(&mut self, stylesheet: C::Stylesheet) {
self.stylesheets.push(stylesheet);
}


/// Returns the URL of the document, or "" when no location is set
fn url(&self) -> Option<Url> {
Expand Down Expand Up @@ -266,15 +270,15 @@ impl Document for DocumentImpl {
}

/// Creates a new document node
fn new_document_node(handle: DocumentHandle<Self>, quirks_mode: QuirksMode, location: Location) -> Self::Node {
fn new_document_node(handle: DocumentHandle<Self, C>, quirks_mode: QuirksMode, location: Location) -> Self::Node {
NodeImpl::new(
handle.clone(),
location,
&NodeDataTypeInternal::Document(DocumentData::new(quirks_mode))
)
}

fn new_doctype_node(handle: DocumentHandle<Self>, name: &str, public_id: Option<&str>, system_id: Option<&str>, location: Location) -> Self::Node {
fn new_doctype_node(handle: DocumentHandle<Self, C>, name: &str, public_id: Option<&str>, system_id: Option<&str>, location: Location) -> Self::Node {
NodeImpl::new(
handle.clone(),
location,
Expand All @@ -283,7 +287,7 @@ impl Document for DocumentImpl {
}

/// Creates a new comment node
fn new_comment_node(handle: DocumentHandle<Self>, comment: &str, location: Location) -> Self::Node {
fn new_comment_node(handle: DocumentHandle<Self, C>, comment: &str, location: Location) -> Self::Node {
NodeImpl::new(
handle.clone(),
location,
Expand All @@ -292,7 +296,7 @@ impl Document for DocumentImpl {
}

/// Creates a new text node
fn new_text_node(handle: DocumentHandle<Self>, value: &str, location: Location) -> Self::Node {
fn new_text_node(handle: DocumentHandle<Self, C>, value: &str, location: Location) -> Self::Node {
NodeImpl::new(
handle.clone(),
location,
Expand All @@ -301,7 +305,7 @@ impl Document for DocumentImpl {
}

/// Creates a new element node
fn new_element_node(handle: DocumentHandle<Self>, name: &str, namespace: Option<&str>, attributes: HashMap<String, String>, location: Location) -> Self::Node {
fn new_element_node(handle: DocumentHandle<Self, C>, name: &str, namespace: Option<&str>, attributes: HashMap<String, String>, location: Location) -> Self::Node {
NodeImpl::new(
handle.clone(),
location,
Expand All @@ -310,22 +314,16 @@ impl Document for DocumentImpl {
}
}

impl DocumentImpl {
impl<C: CssSystem> DocumentImpl<C> {

/// Fetches a node by named id (string) or returns None when no node with this ID is found
pub fn get_node_by_named_id<D>(&self, named_id: &str) -> Option<&D::Node>
where
D: Document<Node=NodeImpl>
{
pub fn get_node_by_named_id(&self, named_id: &str) -> Option<&<DocumentImpl<C> as Document<C>>::Node> {
let node_id = self.named_id_elements.get(named_id)?;
self.arena.node(*node_id)
}

/// Fetches a mutable node by named id (string) or returns None when no node with this ID is found
pub fn get_node_by_named_id_mut<D>(&mut self, named_id: &str) -> Option<&mut D::Node>
where
D: Document<Node=NodeImpl>
{
pub fn get_node_by_named_id_mut<D>(&mut self, named_id: &str) -> Option<&mut <DocumentImpl<C> as Document<C>>::Node> {
let node_id = self.named_id_elements.get(named_id)?;
self.arena.node_mut(*node_id)
}
Expand Down Expand Up @@ -372,22 +370,20 @@ impl DocumentImpl {
false
}

pub fn nodes<D>(&self) -> &HashMap<NodeId, D::Node>
where
D: Document<Node=NodeImpl>
pub fn nodes(&self) -> &HashMap<NodeId, <DocumentImpl<C> as Document<C>>::Node>
{
self.arena.nodes()
}
}

// Walk the document tree with the given visitor
pub fn walk_document_tree<D: Document>(handle: DocumentHandle<D>, visitor: &mut Box<dyn Visitor<D::Node>>) {
pub fn walk_document_tree<C: CssSystem>(handle: DocumentHandle<DocumentImpl<C>, C>, visitor: &mut Box<dyn Visitor<<DocumentImpl<C> as Document<C>>::Node, C>>) {
let binding = handle.get();
let root = binding.get_root();
internal_visit(handle.clone(), root, visitor);
}

fn internal_visit<D: Document>(handle: DocumentHandle<D>, node: &D::Node, visitor: &mut Box<dyn Visitor<D::Node>>) {
fn internal_visit<C: CssSystem>(handle: DocumentHandle<DocumentImpl<C>, C>, node: &<DocumentImpl<C> as Document<C>>::Node, visitor: &mut Box<dyn Visitor<<DocumentImpl<C> as Document<C>>::Node, C>>) {
visitor.document_enter(&node);

let binding = handle.get();
Expand All @@ -404,15 +400,15 @@ fn internal_visit<D: Document>(handle: DocumentHandle<D>, node: &D::Node, visito
/// WARNING: mutations in the document would be reflected
/// in the iterator. It's advised to consume the entire iterator
/// before mutating the document again.
pub struct TreeIterator<D: Clone + Document> {
pub struct TreeIterator<D: Clone + Document<S>, S: CssSystem> {
current_node_id: Option<NodeId>,
node_stack: Vec<NodeId>,
document: DocumentHandle<D>,
document: DocumentHandle<D, S>,
}

impl<D: Document + Clone> TreeIterator<D> {
impl<D: Document<S> + Clone, S: CssSystem> TreeIterator<D, S> {
#[must_use]
pub fn new(doc: DocumentHandle<D>) -> Self {
pub fn new(doc: DocumentHandle<D, S>) -> Self {
Self {
current_node_id: None,
document: doc.clone(),
Expand All @@ -421,7 +417,7 @@ impl<D: Document + Clone> TreeIterator<D> {
}
}

impl<D: Document + Clone> Iterator for TreeIterator<D> {
impl<D: Document<S> + Clone, S: CssSystem> Iterator for TreeIterator<D, S> {
type Item = NodeId;

fn next(&mut self) -> Option<NodeId> {
Expand Down
21 changes: 11 additions & 10 deletions crates/gosub_html5/src/document/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ use core::fmt;
use core::fmt::Debug;

use gosub_shared::node::NodeId;
use gosub_shared::traits::css3::CssSystem;
use gosub_shared::traits::document::DocumentFragment;
use crate::document::document::DocumentImpl;
use crate::node::arena::NodeArena;
use crate::node::node::NodeImpl;

/// Defines a document fragment which can be attached to for instance a <template> element
#[derive(PartialEq)]
pub struct DocumentFragmentImpl {
pub struct DocumentFragmentImpl<C: CssSystem> {
/// Node elements inside this fragment
arena: NodeArena<NodeImpl>,
arena: NodeArena<NodeImpl<C>, C>,
/// Document handle of the parent
pub handle: DocumentHandle<DocumentImpl>,
pub handle: DocumentHandle<DocumentImpl<C>, C>,
/// Host node on which this fragment is attached
host: NodeId,
}

impl Clone for DocumentFragmentImpl {
impl<C: CssSystem> Clone for DocumentFragmentImpl<C> {
/// Clones the document fragment
fn clone(&self) -> Self {
Self {
Expand All @@ -30,16 +31,16 @@ impl Clone for DocumentFragmentImpl {
}
}

impl Debug for DocumentFragmentImpl {
impl<C: CssSystem> Debug for DocumentFragmentImpl<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DocumentFragment")
}
}

impl DocumentFragmentImpl {
impl<C: CssSystem> DocumentFragmentImpl<C> {
/// Creates a new document fragment and attaches it to "host" node inside "handle"
#[must_use]
pub(crate) fn new(handle: DocumentHandle<DocumentImpl>, host: NodeId) -> Self {
pub(crate) fn new(handle: DocumentHandle<DocumentImpl<C>, C>, host: NodeId) -> Self {
Self {
arena: NodeArena::new(),
handle,
Expand All @@ -48,11 +49,11 @@ impl DocumentFragmentImpl {
}
}

impl DocumentFragment for DocumentFragmentImpl {
type Document = DocumentImpl;
impl<C: CssSystem> DocumentFragment<C> for DocumentFragmentImpl<C> {
type Document = DocumentImpl<C>;

/// Returns the document handle for this document
fn handle(&self) -> DocumentHandle<Self::Document> {
fn handle(&self) -> DocumentHandle<Self::Document, C> {
self.handle.clone()
}
}
Loading

0 comments on commit 8094bba

Please sign in to comment.