Skip to content

Commit

Permalink
ComponentValue = CssToken and ignore DOCTYPE
Browse files Browse the repository at this point in the history
  • Loading branch information
d0iasm committed Aug 31, 2024
1 parent 6a6c63a commit d6b0078
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 61 deletions.
65 changes: 18 additions & 47 deletions core/src/renderer/css/cssom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Declaration {
pub fn new() -> Self {
Self {
property: String::new(),
value: ComponentValue::Keyword(String::new()),
value: ComponentValue::Ident(String::new()),
}
}

Expand All @@ -169,17 +169,7 @@ impl Declaration {

/// https://www.w3.org/TR/css-syntax-3/#component-value
/// https://www.w3.org/TR/css-values-4/#component-types
#[derive(Debug, Clone, PartialEq)]
pub enum ComponentValue {
/// https://www.w3.org/TR/css-values-3/#keywords
Keyword(String),
/// https://www.w3.org/TR/css-values-3/#numeric-types
/// This is one of basic data types.
Number(f64),
/// https://www.w3.org/TR/css-syntax-3/#preserved-tokens
/// The token from the list of tokens produced by the tokenizer.
PreservedToken(CssToken),
}
pub type ComponentValue = CssToken;

#[derive(Debug, Clone)]
pub struct CssParser {
Expand Down Expand Up @@ -211,16 +201,9 @@ impl CssParser {

/// https://www.w3.org/TR/css-syntax-3/#consume-component-value
fn consume_component_value(&mut self) -> ComponentValue {
let token = match self.t.next() {
Some(t) => t,
None => panic!("should have a token but got None"),
};

match token {
CssToken::Ident(ident) => ComponentValue::Keyword(ident.to_string()),
CssToken::Number(num) => ComponentValue::Number(num),
_ => ComponentValue::PreservedToken(token),
}
self.t
.next()
.expect("should have a token in consume_component_value")
}

/// https://www.w3.org/TR/css-syntax-3/#qualified-rule
Expand Down Expand Up @@ -267,8 +250,6 @@ impl CssParser {

/// https://www.w3.org/TR/css-syntax-3/#consume-a-declaration
fn consume_declaration(&mut self) -> Option<Declaration> {
self.t.peek()?;

// Create a new declaration with its name set to the value of the current input token.
let mut declaration = Declaration::new();
declaration.set_property(self.consume_ident());
Expand All @@ -284,6 +265,7 @@ impl CssParser {

// "4. As long as the next input token is anything other than an <EOF-token>, consume a
// component value and append it to the declaration’s value."
// TODO: support multiple values in one declaration.
declaration.set_value(self.consume_component_value());

Some(declaration)
Expand Down Expand Up @@ -315,25 +297,16 @@ impl CssParser {
CssToken::Ident(ref _ident) => {
if let Some(declaration) = self.consume_declaration() {
declarations.push(declaration);
if self.t.peek() == Some(&CssToken::Delim(',')) {
self.t.next();
}
}
}
CssToken::StringToken(_) => {
self.t.next();
if self.t.peek() == Some(&CssToken::Delim(',')) {
self.t.next();
}
}
CssToken::Number(_) => {
self.t.next();
if self.t.peek() == Some(&CssToken::Delim(',')) {
self.t.next();
}
}
_ => {
console_warning(&self.browser, format!("unexpected token {:?}", token));
console_warning(
&self.browser,
format!(
"unexpected token in consume_list_of_declarations {:?}",
token
),
);
self.t.next();
}
}
Expand Down Expand Up @@ -478,7 +451,7 @@ mod tests {
rule.set_selector(Selector::TypeSelector("p".to_string()));
let mut declaration = Declaration::default();
declaration.set_property("color".to_string());
declaration.set_value(ComponentValue::Keyword("red".to_string()));
declaration.set_value(ComponentValue::Ident("red".to_string()));
rule.set_declarations(vec![declaration]);

let expected = [rule];
Expand All @@ -502,7 +475,7 @@ mod tests {
rule.set_selector(Selector::IdSelector("id".to_string()));
let mut declaration = Declaration::default();
declaration.set_property("color".to_string());
declaration.set_value(ComponentValue::Keyword("red".to_string()));
declaration.set_value(ComponentValue::Ident("red".to_string()));
rule.set_declarations(vec![declaration]);

let expected = [rule];
Expand All @@ -526,7 +499,7 @@ mod tests {
rule.set_selector(Selector::ClassSelector("class".to_string()));
let mut declaration = Declaration::default();
declaration.set_property("color".to_string());
declaration.set_value(ComponentValue::Keyword("red".to_string()));
declaration.set_value(ComponentValue::Ident("red".to_string()));
rule.set_declarations(vec![declaration]);

let expected = [rule];
Expand All @@ -550,9 +523,7 @@ mod tests {
rule1.set_selector(Selector::TypeSelector("p".to_string()));
let mut declaration1 = Declaration::default();
declaration1.set_property("content".to_string());
declaration1.set_value(ComponentValue::PreservedToken(CssToken::StringToken(
"Hey".to_string(),
)));
declaration1.set_value(ComponentValue::StringToken("Hey".to_string()));
rule1.set_declarations(vec![declaration1]);

let mut rule2 = QualifiedRule::default();
Expand All @@ -562,7 +533,7 @@ mod tests {
declaration2.set_value(ComponentValue::Number(40.0));
let mut declaration3 = Declaration::default();
declaration3.set_property("color".to_string());
declaration3.set_value(ComponentValue::Keyword("blue".to_string()));
declaration3.set_value(ComponentValue::Ident("blue".to_string()));
rule2.set_declarations(vec![declaration2, declaration3]);

let expected = [rule1, rule2];
Expand Down
1 change: 1 addition & 0 deletions core/src/renderer/css/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl Iterator for CssTokenizer {
// returned value, and return it.
if self.input[self.pos + 1].is_ascii_alphabetic()
&& self.input[self.pos + 2].is_alphanumeric()
&& self.input[self.pos + 3].is_alphanumeric()
{
// skip '@'
self.pos += 1;
Expand Down
3 changes: 2 additions & 1 deletion core/src/renderer/dom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use alloc::rc::{Rc, Weak};
use alloc::string::String;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::fmt::{Display, Formatter};
use core::fmt::Display;
use core::fmt::Formatter;
use core::str::FromStr;

#[derive(Debug, Clone)]
Expand Down
12 changes: 9 additions & 3 deletions core/src/renderer/html/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ impl HtmlParser {
match self.mode {
// https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode
InsertionMode::Initial => {
// Ignore <!doctype html>.
// <!doctype html> is generated as a Char token.
if let Some(HtmlToken::Char(_)) = token {
token = self.t.next();
continue;
}

self.mode = InsertionMode::BeforeHtml;
continue;
}
Expand Down Expand Up @@ -296,7 +303,6 @@ impl HtmlParser {
match token {
Some(HtmlToken::Char(c)) => {
if c == ' ' || c == '\n' {
self.insert_char(c);
token = self.t.next();
continue;
}
Expand Down Expand Up @@ -534,7 +540,7 @@ impl HtmlParser {
_ => {
console_warning(
&self.browser,
format!("unknown tag {:?}", tag),
format!("unsupported start tag in InBody {:?}", tag),
);
token = self.t.next();
}
Expand Down Expand Up @@ -616,7 +622,7 @@ impl HtmlParser {
_ => {
console_warning(
&self.browser,
format!("unknown tag {:?}", tag),
format!("unsupported end tag InBody {:?}", tag),
);
token = self.t.next();
}
Expand Down
15 changes: 5 additions & 10 deletions core/src/renderer/layout/layout_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::renderer::css::cssom::ComponentValue;
use crate::renderer::css::cssom::Declaration;
use crate::renderer::css::cssom::Selector;
use crate::renderer::css::cssom::StyleSheet;
use crate::renderer::css::token::CssToken;
use crate::renderer::dom::node::ElementKind;
use crate::renderer::dom::node::Node;
use crate::renderer::dom::node::NodeKind;
Expand Down Expand Up @@ -208,7 +207,7 @@ impl LayoutObject {
for declaration in declarations {
match declaration.property.as_str() {
"background-color" => {
if let ComponentValue::Keyword(value) = &declaration.value {
if let ComponentValue::Ident(value) = &declaration.value {
let color = match Color::from_name(value) {
Ok(color) => color,
Err(e) => {
Expand All @@ -220,9 +219,7 @@ impl LayoutObject {
continue;
}

if let ComponentValue::PreservedToken(CssToken::HashToken(color_code)) =
&declaration.value
{
if let ComponentValue::HashToken(color_code) = &declaration.value {
let color = match Color::from_code(color_code) {
Ok(color) => color,
Err(e) => {
Expand All @@ -235,7 +232,7 @@ impl LayoutObject {
}
}
"color" => {
if let ComponentValue::Keyword(value) = &declaration.value {
if let ComponentValue::Ident(value) = &declaration.value {
let color = match Color::from_name(value) {
Ok(color) => color,
Err(e) => {
Expand All @@ -246,9 +243,7 @@ impl LayoutObject {
self.style.set_color(color);
}

if let ComponentValue::PreservedToken(CssToken::HashToken(color_code)) =
&declaration.value
{
if let ComponentValue::HashToken(color_code) = &declaration.value {
let color = match Color::from_code(color_code) {
Ok(color) => color,
Err(e) => {
Expand All @@ -260,7 +255,7 @@ impl LayoutObject {
}
}
"display" => {
if let ComponentValue::Keyword(value) = declaration.value {
if let ComponentValue::Ident(value) = declaration.value {
let display_type = match DisplayType::from_str(&value) {
Ok(display_type) => display_type,
Err(e) => {
Expand Down
49 changes: 49 additions & 0 deletions net/wasabi/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,54 @@ use noli::net::TcpStream;
use saba_core::error::Error;
use saba_core::http::HttpResponse;

static FAKE_RESPONSE_BODY: &str = r#"<!doctype html>
<html>
<head>
<title>Example Domain title</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
"#;

/*
static FAKE_RESPONSE_BODY: &str = r#"<html>
<head>
<title>Example Domain Response</title>
Expand Down Expand Up @@ -60,6 +108,7 @@ static FAKE_RESPONSE_BODY: &str = r#"<html>
</body>
</html>
"#;
*/

pub struct HttpClient {}

Expand Down

0 comments on commit d6b0078

Please sign in to comment.