Skip to content

Commit d5ce717

Browse files
committed
fix: Ignore @media queries
Ref: #7
1 parent 37f8c19 commit d5ce717

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Ignore `@media` queries, since they can not be inlined. [#7](https://github.com/Stranger6667/css-inline/issues/7)
8+
9+
## 0.1.0 - 2020-06-22
10+
11+
- Initial public release
12+
13+
[Unreleased]: https://github.com/Stranger6667/jsonschema-rs/compare/v0.1.0...HEAD

src/lib.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,28 @@ pub fn inline(html: &str) -> Result<String, InlineError> {
135135
let mut parse_input = cssparser::ParserInput::new(css.as_str());
136136
let mut parser = parse::CSSParser::new(&mut parse_input);
137137
for parsed in parser.parse() {
138-
let (selector, declarations) = parsed?;
139-
let rule = Rule::new(&selector, declarations)
140-
.map_err(|_| error::InlineError::ParseError)?;
141-
let matching_elements = document
142-
.inclusive_descendants()
143-
.filter_map(|node| node.into_element_ref())
144-
.filter(|element| rule.selectors.matches(element));
145-
for matching_element in matching_elements {
146-
let mut attributes = matching_element.attributes.borrow_mut();
147-
let style = if let Some(existing_style) = attributes.get("style") {
148-
merge_styles(existing_style, &rule.declarations)?
149-
} else {
150-
rule.declarations
151-
.iter()
152-
.map(|&(ref key, ref value)| format!("{}:{};", key, value))
153-
.collect()
154-
};
155-
attributes.insert("style", style);
138+
if let Ok((selector, declarations)) = parsed {
139+
let rule = Rule::new(&selector, declarations)
140+
.map_err(|_| error::InlineError::ParseError)?;
141+
let matching_elements = document
142+
.inclusive_descendants()
143+
.filter_map(|node| node.into_element_ref())
144+
.filter(|element| rule.selectors.matches(element));
145+
for matching_element in matching_elements {
146+
let mut attributes = matching_element.attributes.borrow_mut();
147+
let style = if let Some(existing_style) = attributes.get("style") {
148+
merge_styles(existing_style, &rule.declarations)?
149+
} else {
150+
rule.declarations
151+
.iter()
152+
.map(|&(ref key, ref value)| format!("{}:{};", key, value))
153+
.collect()
154+
};
155+
attributes.insert("style", style);
156+
}
156157
}
158+
// Ignore not parsable entries. E.g. there is no parser for @media queries
159+
// Which means that they will fall into this category and will be ignored
157160
}
158161
}
159162
}

tests/test_inlining.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ fn empty_style() {
7575
expected = r#"<h1>Hello world!</h1>"#
7676
)
7777
}
78+
79+
#[test]
80+
fn media_query_ignore() {
81+
// When the style value includes @media query
82+
assert_inlined!(
83+
style = r#"@media screen and (max-width: 992px) {
84+
body {
85+
background-color: blue;
86+
}
87+
}"#,
88+
body = "<h1>Hello world!</h1>",
89+
expected = "<h1>Hello world!</h1>"
90+
)
91+
}

0 commit comments

Comments
 (0)