Skip to content

Commit 60abae4

Browse files
committed
image
1 parent 06eb5be commit 60abae4

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern crate selectors;
99
mod parser;
1010
mod media_query;
1111
mod properties;
12+
mod values;
1213

1314
use napi::{CallContext, JsObject, JsUndefined};
1415
use serde::{Deserialize, Serialize};

src/properties/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ mod custom;
22

33
use cssparser::*;
44
use custom::*;
5+
use crate::values::{image::*};
56

67
#[derive(Debug)]
78
pub enum Property {
89
BackgroundColor(Color),
10+
BackgroundImage(Vec<Image>),
911
Color(Color),
1012
Custom(CustomProperty)
1113
}
@@ -18,11 +20,17 @@ impl Property {
1820
return Ok(Property::$property(c))
1921
}
2022
};
21-
}
23+
($property: ident, $type: ident, $multi: expr) => {
24+
if let Ok(c) = input.parse_comma_separated(|input| $type::parse(input)) {
25+
return Ok(Property::$property(c))
26+
}
27+
}
28+
}
2229

2330
let state = input.state();
2431
match name.as_ref() {
2532
"background-color" => property!(BackgroundColor, Color),
33+
"background-image" => property!(BackgroundImage, Image, true),
2634
"color" => property!(Color, Color),
2735
_ => {}
2836
}

src/values/image.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use cssparser::*;
2+
3+
#[derive(Debug)]
4+
pub enum Image {
5+
None,
6+
Url(String)
7+
}
8+
9+
impl Image {
10+
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
11+
if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
12+
return Ok(Image::None)
13+
}
14+
15+
if let Ok(url) = input.try_parse(|input| input.expect_url()) {
16+
return Ok(Image::Url(url.as_ref().into()))
17+
}
18+
19+
Err(input.new_error_for_next_token())
20+
}
21+
}

src/values/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod image;

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ css.transform({
88
99
.foo + .bar:not(.baz) {
1010
background-color: blue;
11-
// background: url(img.png), url('test.jpg');
11+
background-image: url(img.png), url('test.jpg');
1212
color: blue;
1313
}
1414

0 commit comments

Comments
 (0)