simplecss/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/*!
A simple [CSS 2.1](https://www.w3.org/TR/CSS21/) parser and selector.
This is not a browser-grade CSS parser. If you need one,
use [cssparser](https://crates.io/crates/cssparser) +
[selectors](https://crates.io/crates/selectors).
Since it's very simple we will start with limitations:
## Limitations
- [At-rules](https://www.w3.org/TR/CSS21/syndata.html#at-rules) are not supported.
They will be skipped during parsing.
- Property values are not parsed.
In CSS like `* { width: 5px }` you will get a `width` property with a `5px` value as a string.
- CDO/CDC comments are not supported.
- Parser is case sensitive. All keywords must be lowercase.
- Unicode escape, like `\26`, is not supported.
## Features
- Selector matching support.
- The rules are sorted by specificity.
- `!import` parsing support.
- Has a high-level parsers and low-level, zero-allocation tokenizers.
- No unsafe.
*/
#![doc(html_root_url = "https://docs.rs/simplecss/0.2.1")]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
use std::fmt;
use log::warn;
mod selector;
mod stream;
pub use selector::*;
use stream::Stream;
/// A list of possible errors.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Error {
/// The steam ended earlier than we expected.
///
/// Should only appear on invalid input data.
UnexpectedEndOfStream,
/// An invalid ident.
InvalidIdent(TextPos),
/// An unclosed comment.
InvalidComment(TextPos),
/// An invalid declaration value.
InvalidValue(TextPos),
/// An invalid byte.
#[allow(missing_docs)]
InvalidByte { expected: u8, actual: u8, pos: TextPos },
/// A missing selector.
SelectorMissing,
/// An unexpected selector.
UnexpectedSelector,
/// An unexpected combinator.
UnexpectedCombinator,
/// An invalid or unsupported attribute selector.
InvalidAttributeSelector,
/// An invalid language pseudo-class.
InvalidLanguagePseudoClass,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::UnexpectedEndOfStream => {
write!(f, "unexpected end of stream")
}
Error::InvalidIdent(pos) => {
write!(f, "invalid ident at {}", pos)
}
Error::InvalidComment(pos) => {
write!(f, "invalid comment at {}", pos)
}
Error::InvalidValue(pos) => {
write!(f, "invalid value at {}", pos)
}
Error::InvalidByte { expected, actual, pos } => {
write!(f, "expected '{}' not '{}' at {}",
expected as char, actual as char, pos)
}
Error::SelectorMissing => {
write!(f, "selector missing")
}
Error::UnexpectedSelector => {
write!(f, "unexpected selector")
}
Error::UnexpectedCombinator => {
write!(f, "unexpected combinator")
}
Error::InvalidAttributeSelector => {
write!(f, "invalid or unsupported attribute selector")
}
Error::InvalidLanguagePseudoClass => {
write!(f, "invalid language pseudo-class")
}
}
}
}
impl std::error::Error for Error {}
/// A position in text.
///
/// Position indicates a row/line and a column in the original text. Starting from 1:1.
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub struct TextPos {
pub row: u32,
pub col: u32,
}
impl TextPos {
/// Constructs a new `TextPos`.
///
/// Should not be invoked manually, but rather via `Stream::gen_text_pos`.
pub fn new(row: u32, col: u32) -> TextPos {
TextPos { row, col }
}
}
impl fmt::Display for TextPos {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.row, self.col)
}
}
/// A declaration.
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub struct Declaration<'a> {
pub name: &'a str,
pub value: &'a str,
pub important: bool,
}
/// A rule.
#[derive(Clone, Debug)]
pub struct Rule<'a> {
/// A rule selector.
pub selector: Selector<'a>,
/// A rule declarations.
pub declarations: Vec<Declaration<'a>>,
}
/// A style sheet.
#[derive(Clone, Debug)]
pub struct StyleSheet<'a> {
/// A list of rules.
pub rules: Vec<Rule<'a>>,
}
impl<'a> StyleSheet<'a> {
/// Creates an empty style sheet.
pub fn new() -> Self {
StyleSheet { rules: Vec::new() }
}
/// Parses a style sheet from text.
///
/// At-rules are not supported and will be skipped.
///
/// # Errors
///
/// Doesn't produce any errors. In worst case scenario will return an empty stylesheet.
///
/// All warnings will be logged.
pub fn parse(text: &'a str) -> Self {
let mut sheet = StyleSheet::new();
sheet.parse_more(text);
sheet
}
/// Parses a style sheet from a text to the current style sheet.
pub fn parse_more(&mut self, text: &'a str) {
let mut s = Stream::from(text);
if s.skip_spaces_and_comments().is_err() {
return;
}
while !s.at_end() {
if s.skip_spaces_and_comments().is_err() {
break;
}
let _ = consume_statement(&mut s, &mut self.rules);
}
if !s.at_end() {
warn!("{} bytes were left.", s.slice_tail().len());
}
// Remove empty rules.
self.rules.retain(|rule| !rule.declarations.is_empty());
// Sort the rules by specificity.
self.rules.sort_by_cached_key(|rule| rule.selector.specificity());
}
}
impl fmt::Display for StyleSheet<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, rule) in self.rules.iter().enumerate() {
write!(f, "{} {{ ", rule.selector)?;
for dec in &rule.declarations {
write!(f, "{}:{}", dec.name, dec.value)?;
if dec.important {
write!(f, " !important")?;
}
write!(f, ";")?;
}
write!(f, " }}")?;
if i != self.rules.len() - 1 {
writeln!(f)?;
}
}
Ok(())
}
}
fn consume_statement<'a>(s: &mut Stream<'a>, rules: &mut Vec<Rule<'a>>) -> Result<(), Error> {
if s.curr_byte() == Ok(b'@') {
s.advance(1);
consume_at_rule(s)
} else {
consume_rule_set(s, rules)
}
}
fn consume_at_rule(s: &mut Stream) -> Result<(), Error> {
let ident = s.consume_ident()?;
warn!("The @{} rule is not supported. Skipped.", ident);
s.skip_bytes(|c| c != b';' && c != b'{');
match s.curr_byte()? {
b';' => s.advance(1),
b'{' => consume_block(s),
_ => {}
}
Ok(())
}
fn consume_rule_set<'a>(s: &mut Stream<'a>, rules: &mut Vec<Rule<'a>>) -> Result<(), Error> {
let start_rule_idx = rules.len();
while s.curr_byte()? == b',' || start_rule_idx == rules.len() {
if s.curr_byte()? == b',' {
s.advance(1);
}
let (selector, offset) = crate::selector::parse(s.slice_tail());
s.advance(offset);
s.skip_spaces();
if let Some(selector) = selector {
rules.push(Rule { selector, declarations: Vec::new() });
}
match s.curr_byte()? {
b'{' => break,
b',' => {}
_ => {
s.skip_bytes(|c| c != b'{');
break;
}
}
}
s.try_consume_byte(b'{');
let declarations = consume_declarations(s)?;
for i in start_rule_idx..rules.len() {
rules[i].declarations = declarations.clone();
}
s.try_consume_byte(b'}');
Ok(())
}
fn consume_block(s: &mut Stream) {
s.try_consume_byte(b'{');
consume_until_block_end(s);
}
fn consume_until_block_end(s: &mut Stream) {
// Block can have nested blocks, so we have to check for matching braces.
// We simply counting the number of opening braces, which is incorrect,
// since `{` can be inside a string, but it's fine for majority of the cases.
let mut braces = 0;
while !s.at_end() {
match s.curr_byte_unchecked() {
b'{' => {
braces += 1;
}
b'}' => {
if braces == 0 {
break;
} else {
braces -= 1;
}
}
_ => {}
}
s.advance(1);
}
s.try_consume_byte(b'}');
}
fn consume_declarations<'a>(s: &mut Stream<'a>) -> Result<Vec<Declaration<'a>>, Error> {
let mut declarations = Vec::new();
while !s.at_end() && s.curr_byte() != Ok(b'}') {
match consume_declaration(s) {
Ok(declaration) => declarations.push(declaration),
Err(_) => {
consume_until_block_end(s);
break;
}
}
}
Ok(declarations)
}
/// A declaration tokenizer.
///
/// Tokenizer will stop at the first invalid token.
///
/// # Example
///
/// ```
/// use simplecss::{DeclarationTokenizer, Declaration};
///
/// let mut t = DeclarationTokenizer::from("background: url(\"img.png\"); color:red !important");
/// assert_eq!(t.next().unwrap(), Declaration { name: "background", value: "url(\"img.png\")", important: false });
/// assert_eq!(t.next().unwrap(), Declaration { name: "color", value: "red", important: true });
/// ```
pub struct DeclarationTokenizer<'a> {
stream: Stream<'a>,
}
impl<'a> From<&'a str> for DeclarationTokenizer<'a> {
fn from(text: &'a str) -> Self {
DeclarationTokenizer {
stream: Stream::from(text),
}
}
}
impl<'a> Iterator for DeclarationTokenizer<'a> {
type Item = Declaration<'a>;
fn next(&mut self) -> Option<Self::Item> {
let _ = self.stream.skip_spaces_and_comments();
if self.stream.at_end() {
return None;
}
match consume_declaration(&mut self.stream) {
Ok(v) => Some(v),
Err(_) => {
self.stream.jump_to_end();
None
}
}
}
}
fn consume_declaration<'a>(s: &mut Stream<'a>) -> Result<Declaration<'a>, Error> {
s.skip_spaces_and_comments()?;
// Parse name.
// https://snook.ca/archives/html_and_css/targetting_ie7
if s.curr_byte() == Ok(b'*') {
s.advance(1);
}
let name = s.consume_ident()?;
s.skip_spaces_and_comments()?;
s.consume_byte(b':')?;
s.skip_spaces_and_comments()?;
// Parse value.
let start = s.pos();
let mut end = s.pos();
while consume_term(s).is_ok() {
end = s.pos();
s.skip_spaces_and_comments()?;
}
let value = s.slice_range(start, end).trim();
s.skip_spaces_and_comments()?;
// Check for `important`.
let mut important = false;
if s.curr_byte() == Ok(b'!') {
s.advance(1);
s.skip_spaces_and_comments()?;
if s.slice_tail().starts_with("important") {
s.advance(9);
important = true;
}
}
s.skip_spaces_and_comments()?;
while s.curr_byte() == Ok(b';') {
s.advance(1);
s.skip_spaces_and_comments()?;
}
s.skip_spaces_and_comments()?;
if value.is_empty() {
return Err(Error::InvalidValue(s.gen_text_pos_from(start)));
}
Ok(Declaration { name, value, important })
}
fn consume_term(s: &mut Stream) -> Result<(), Error> {
fn consume_digits(s: &mut Stream) {
while let Ok(c) = s.curr_byte() {
match c {
b'0'..=b'9' => s.advance(1),
_ => break,
}
}
}
match s.curr_byte()? {
b'#' => {
s.advance(1);
match s.consume_ident() {
Ok(_) => {}
Err(_) => {
// Try consume as a hex color.
while let Ok(c) = s.curr_byte() {
match c {
b'0'..=b'9' | b'a'..=b'f' | b'A'..=b'F' => s.advance(1),
_ => break,
}
}
}
}
}
b'+' | b'-' | b'0'..=b'9' | b'.' => {
// Consume number.
s.advance(1);
consume_digits(s);
if s.curr_byte() == Ok(b'.') {
s.advance(1);
consume_digits(s);
}
if s.curr_byte() == Ok(b'%') {
s.advance(1);
} else {
// Consume suffix if any.
let _ = s.consume_ident();
}
}
b'\'' | b'"' => {
s.consume_string()?;
}
b',' => {
s.advance(1);
}
_ => {
let _ = s.consume_ident()?;
// Consume function.
if s.curr_byte() == Ok(b'(') {
s.skip_bytes(|c| c != b')');
s.consume_byte(b')')?;
}
}
}
Ok(())
}