Skip to content

Commit

Permalink
css! macro resolving expressions in url
Browse files Browse the repository at this point in the history
  • Loading branch information
sfisol committed Apr 3, 2024
1 parent c885648 commit 35ad93c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Component embedding using non-local name (f. ex. `<my_module::MyComponent />`)
* Raw field name support in AutoJsJson derive macro
* `component!` macro copying attributes to struct (and doc-strings)
* `css!` macro resolving expressions in `url`
* vertigo-cli: Watch script now attached inside body tag

## 0.4.3 - 2024-02-28
Expand Down
4 changes: 2 additions & 2 deletions crates/vertigo-macro/src/css.pest
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ unknown_rule = {
}

rule_ident = { (ASCII_ALPHA_LOWER | "@" | "-" ) ~ (ASCII_ALPHA_LOWER | "-")+ }
value = _{ (simple_value)+ | url_value }
value = _{ url_value | (simple_value)+ }
simple_value = _{ expression | color_value | unquoted_value | quoted_value }

unquoted_value = { (!semicolon ~ ANY)+ }
Expand All @@ -290,7 +290,7 @@ hsla_value = _{ "hsl(" ~ ASCII_DIGIT ~ comma ~ (ASCII_DIGIT ~ percent ~ comma){2

transparency_value = _{ zero_to_one }

url_value = _{ "url(" ~ (expression | quoted_value) ~ ")" }
url_value = { "url(" ~ (expression | quoted_value) ~ ")" }

// { } and {$ $} expressions
expression = { brace_left ~ /*PUSH("$"*) ~*/ expression_value ~ /*POP ~*/ brace_right }
Expand Down
15 changes: 15 additions & 0 deletions crates/vertigo-macro/src/css_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ impl CssParser {
Rule::expression => self
.params
.insert(value.into_inner().next().unwrap().as_str().to_string()),
Rule::url_value => {
// url_value -> expression/quoted_value
let inner = value.clone().into_inner().next().unwrap();
match inner.as_rule() {
Rule::expression => {
// expression -> expression_value
let expr_value = inner.into_inner().next().unwrap().as_str().to_string();
format!("url('{}')", self.params.insert(expr_value))
},
_ => {
// quoted_value
value.as_str().to_string()
}
}
}
_ => {
emit_warning!(
self.call_site,
Expand Down
9 changes: 1 addition & 8 deletions crates/vertigo/src/computed/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<T> Drop for ValueInner<T> {
/// });
/// ```
///
#[derive(Clone)]
pub struct Value<T> {
inner: Rc<ValueInner<T>>,
}
Expand All @@ -56,14 +57,6 @@ impl<T: Clone + Default + 'static> Default for Value<T> {
}
}

impl<T> Clone for Value<T> {
fn clone(&self) -> Self {
Value {
inner: self.inner.clone(),
}
}
}

impl<T: PartialEq> PartialEq for Value<T> {
fn eq(&self, other: &Self) -> bool {
self.inner.id.eq(&other.inner.id)
Expand Down
31 changes: 31 additions & 0 deletions crates/vertigo/src/tests/css/expressions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::css;

use super::utils::*;

// Make crate available by its name for css macro
use crate as vertigo;

#[test]
fn test_background_url_plain() {
let value = css!("
background-image: url('foo');
");

assert_eq!(
get_s(&value),
"background-image: url('foo');"
)
}

#[test]
fn test_background_url_expression() {
let url = "bar";
let value = css!("
background-image: url({url});
");

assert_eq!(
get_d(&value),
"background-image: url('bar');"
)
}
1 change: 1 addition & 0 deletions crates/vertigo/src/tests/css/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod animation;
mod classes;
mod colors;
mod comments;
mod expressions;
mod media;
mod pseudoselectors;
mod refs;
Expand Down

0 comments on commit 35ad93c

Please sign in to comment.