Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Mar 18, 2024
1 parent 68b7de6 commit 556ab43
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 206 deletions.
67 changes: 37 additions & 30 deletions crates/gosub_styling/src/css_node_tree.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use gosub_shared::types::Result;
use crate::css_colors::RgbColor;
use core::fmt::Debug;
use std::cell::RefCell;
use gosub_css3::stylesheet::{
CssOrigin, CssSelector, CssSelectorPart, CssSelectorType, MatcherType,
Specificity,
CssOrigin, CssSelector, CssSelectorPart, CssSelectorType, MatcherType, Specificity,
};
use gosub_html5::node::NodeId;
use gosub_html5::parser::document::{DocumentHandle, TreeIterator};
use gosub_shared::types::Result;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::HashMap;

Expand All @@ -22,15 +21,18 @@ pub fn generate_css_node_tree(document: DocumentHandle) -> Result<CssNodeTree> {
let mut css_map_entry = CssProperties::new();

let binding = document.get();
let node = binding.get_node_by_id(current_node_id).expect("node not found");
let node = binding
.get_node_by_id(current_node_id)
.expect("node not found");
if !node.is_element() {
continue;
}

for sheet in document.get().stylesheets.iter() {
for rule in sheet.rules.iter() {
for selector in rule.selectors().iter() {
if !match_selector(DocumentHandle::clone(&document), current_node_id, selector) {
if !match_selector(DocumentHandle::clone(&document), current_node_id, selector)
{
continue;
}

Expand All @@ -39,7 +41,7 @@ pub fn generate_css_node_tree(document: DocumentHandle) -> Result<CssNodeTree> {
let property = declaration.property.clone();

let declaration = DeclarationProperty {
value: CssValue::String(declaration.value.clone()), // @TODO: parse the value into the correct CSSValue
value: CssValue::String(declaration.value.clone()), // @TODO: parse the value into the correct CSSValue
origin: sheet.origin.clone(),
important: declaration.important,
location: "".into(),
Expand Down Expand Up @@ -77,7 +79,11 @@ fn match_selector(document: DocumentHandle, node_id: NodeId, selector: &CssSelec

#[allow(dead_code)]
/// Returns true when the given node matches the part(s)
fn match_selector_part(document: DocumentHandle, node_id: NodeId, selector_parts: &mut Vec<CssSelectorPart>) -> bool {
fn match_selector_part(
document: DocumentHandle,
node_id: NodeId,
selector_parts: &mut Vec<CssSelectorPart>,
) -> bool {
let binding = document.get();
let mut next_current_node = Some(binding.get_node_by_id(node_id).expect("node not found"));

Expand Down Expand Up @@ -164,7 +170,7 @@ fn match_selector_part(document: DocumentHandle, node_id: NodeId, selector_parts
// Contains
got_attr_value.contains(&wanted_attr_value)
}
}
};
}
CssSelectorType::PseudoClass => {
// @Todo: implement pseudo classes
Expand All @@ -187,14 +193,22 @@ fn match_selector_part(document: DocumentHandle, node_id: NodeId, selector_parts
// @todo: Namespace combinator ('|')
" " => {
// Descendant combinator, any parent that matches the previous selector will do
if !match_selector_part(DocumentHandle::clone(&document), current_node.id, selector_parts) {
if !match_selector_part(
DocumentHandle::clone(&document),
current_node.id,
selector_parts,
) {
// we insert the combinator back so we the next loop will match against the parent node
selector_parts.insert(0, part);
}
}
">" => {
// Child combinator. Only matches the direct child
if !match_selector_part(DocumentHandle::clone(&document), current_node.id, selector_parts) {
if !match_selector_part(
DocumentHandle::clone(&document),
current_node.id,
selector_parts,
) {
return false;
}
}
Expand Down Expand Up @@ -337,7 +351,7 @@ impl CssProperty {
self.calculate_value();
}

return &self.actual;
&self.actual
}

fn calculate_value(&mut self) {
Expand All @@ -352,16 +366,10 @@ impl CssProperty {
// Step 5: Find actual value by rounding numbers (when possible)
// @TODO: stuff like clipping and such should occur as well
self.actual = match &self.used {
CssValue::Number(len) => {
CssValue::Number(len.round())
}
CssValue::Percentage(perc) => {
CssValue::Percentage(perc.round())
}
CssValue::Unit(value, unit, ) => {
CssValue::Unit(value.round(), unit.clone())
}
_ => self.used.clone()
CssValue::Number(len) => CssValue::Number(len.round()),
CssValue::Percentage(perc) => CssValue::Percentage(perc.round()),
CssValue::Unit(value, unit) => CssValue::Unit(value.round(), unit.clone()),
_ => self.used.clone(),
}
}
}
Expand Down Expand Up @@ -438,7 +446,7 @@ impl CssNodeTree {
Some(e) => {
let prop = e.take().clone();
Some(prop)
},
}
None => None,
}
}
Expand Down Expand Up @@ -467,7 +475,6 @@ impl CssNodeTree {
.find(|entry| entry.name == prop_name)
.map(|entry| entry.initial)
}

}

#[cfg(test)]
Expand All @@ -477,42 +484,42 @@ mod tests {
#[test]
fn compare_declared() {
let a = DeclarationProperty {
value: "red".into(),
value: CssValue::String("red".into()),
origin: CssOrigin::Author,
important: false,
location: "".into(),
specificity: Specificity::new(1, 0, 0),
};
let b = DeclarationProperty {
value: "blue".into(),
value: CssValue::String("blue".into()),
origin: CssOrigin::UserAgent,
important: false,
location: "".into(),
specificity: Specificity::new(1, 0, 0),
};
let c = DeclarationProperty {
value: "green".into(),
value: CssValue::String("green".into()),
origin: CssOrigin::User,
important: false,
location: "".into(),
specificity: Specificity::new(1, 0, 0),
};
let d = DeclarationProperty {
value: "yellow".into(),
value: CssValue::String("yellow".into()),
origin: CssOrigin::Author,
important: true,
location: "".into(),
specificity: Specificity::new(1, 0, 0),
};
let e = DeclarationProperty {
value: "orange".into(),
value: CssValue::String("orange".into()),
origin: CssOrigin::UserAgent,
important: true,
location: "".into(),
specificity: Specificity::new(1, 0, 0),
};
let f = DeclarationProperty {
value: "purple".into(),
value: CssValue::String("purple".into()),
origin: CssOrigin::User,
important: true,
location: "".into(),
Expand Down
6 changes: 3 additions & 3 deletions crates/gosub_styling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! This crate connects CSS3 and HTML5 into a styling pipeline
//!
use std::fs;
use gosub_css3::convert::ast_converter::convert_ast_to_stylesheet;
use gosub_css3::Css3;
use gosub_css3::parser_config::ParserConfig;
use gosub_css3::stylesheet::{CssOrigin, CssStylesheet};
use gosub_css3::Css3;
use std::fs;

pub mod css_node_tree;
pub mod css_colors;
pub mod css_node_tree;
pub mod pipeline;
mod property_list;

Expand Down
10 changes: 2 additions & 8 deletions crates/gosub_styling/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ impl Pipeline {
}

/// Generates a render tree by duplicating the DOM tree and removing all nodes that are not renderable or hidden.
pub fn generate_render_tree(
&self,
doc_handle: DocumentHandle,
) -> DocumentHandle {
pub fn generate_render_tree(&self, doc_handle: DocumentHandle) -> DocumentHandle {
// Create a complete copy of the document tree into the render tree
let mut rendertree_handle = doc_handle.deep_clone();

Expand All @@ -32,10 +29,7 @@ impl Pipeline {
}
}

fn remove_unrenderable_nodes(
rendertree_handle: &mut DocumentHandle,
node_id: NodeId,
) {
fn remove_unrenderable_nodes(rendertree_handle: &mut DocumentHandle, node_id: NodeId) {
let node;
{
let binding = rendertree_handle.get();
Expand Down
165 changes: 0 additions & 165 deletions src/bin/style-parser.rs

This file was deleted.

0 comments on commit 556ab43

Please sign in to comment.