Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ rust-version = "1.85"
exclude = ["src/css-parsing-tests/**", "src/big-data-url.css"]

[dependencies]
dtoa-short = "0.3"
itoa = "1.0"
smallvec = "1.0"

# Optional dependencies
cssparser-macros = { path = "./macros", version = "0.7.0", optional = true }
dtoa-short = { version = "0.3", optional = true }
itoa = { version = "1.0", optional = true }
malloc_size_of = { version = "0.1", default-features = false, optional = true }
phf = { version = "0.14.0", features = ["macros"], optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
dtoa-short = "0.3"
itoa = "1.0"
serde_json = "1.0.25"
difference = "2.0"
encoding_rs = "0.8"
Expand All @@ -35,10 +37,11 @@ inherits = "release"
debug = true

[features]
default = ["fast_match_byte", "fast_match_color"]
default = ["fast_match_byte", "fast_match_color", "serialize"]
bench = []
fast_match_byte = ["dep:cssparser-macros"]
fast_match_color = ["dep:phf"]
serialize = ["dep:dtoa-short", "dep:itoa"]
# Useful for skipping tests when execution is slow, e.g., under miri
skip_long_tests = []

Expand Down
2 changes: 1 addition & 1 deletion color/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.85"
path = "lib.rs"

[dependencies]
cssparser = { path = "..", version = "0.38", default-features = false }
cssparser = { path = "..", version = "0.38", default-features = false, features = ["serialize"] }
serde = { version = "1.0", features = ["derive"], optional = true }

[features]
Expand Down
7 changes: 6 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
/// The opaque alpha value of 1.0.
pub const OPAQUE: f32 = 1.0;

use crate::{BasicParseError, Parser, ToCss};
#[cfg(any(feature = "serialize", test))]
use crate::ToCss;
use crate::{BasicParseError, Parser};
#[cfg(any(feature = "serialize", test))]
use std::fmt;

/// Clamp a 0..1 number to a 0..255 range to u8.
Expand Down Expand Up @@ -46,6 +49,7 @@ pub fn clamp_floor_256_f32(val: f32) -> u8 {

/// Serialize the alpha copmonent of a color according to the specification.
/// <https://drafts.csswg.org/css-color-4/#serializing-alpha-values>
#[cfg(any(feature = "serialize", test))]
#[inline]
pub fn serialize_color_alpha(
dest: &mut impl fmt::Write,
Expand Down Expand Up @@ -118,6 +122,7 @@ impl PredefinedColorSpace {
}
}

#[cfg(any(feature = "serialize", test))]
impl ToCss for PredefinedColorSpace {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ pub use crate::rules_and_declarations::{AtRuleParser, QualifiedRuleParser};
pub use crate::rules_and_declarations::{DeclarationParser, RuleBodyItemParser, RuleBodyParser};
pub use crate::rules_and_declarations::{StyleSheetParser, parse_one_rule};
pub use crate::rules_and_declarations::{parse_important, parse_one_declaration};
#[cfg(any(feature = "serialize", test))]
pub use crate::serializer::{CssStringWriter, ToCss, TokenSerializationType};
#[cfg(any(feature = "serialize", test))]
pub use crate::serializer::{serialize_identifier, serialize_name, serialize_string};
pub use crate::tokenizer::{SourceLocation, SourcePosition, Token};
pub use crate::unicode_range::UnicodeRange;
Expand Down Expand Up @@ -131,6 +133,7 @@ mod cow_rc_str;
mod from_bytes;
mod nth;
mod parser;
#[cfg(any(feature = "serialize", test))]
mod serializer;
mod unicode_range;

Expand Down
23 changes: 16 additions & 7 deletions src/unicode_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

//! https://drafts.csswg.org/css-syntax/#urange

#[cfg(any(feature = "serialize", test))]
use crate::ToCss;
use crate::tokenizer::Token;
use crate::{BasicParseError, Parser, ToCss};
use crate::{BasicParseError, Parser};
use std::char;
use std::fmt;

Expand Down Expand Up @@ -157,21 +159,28 @@ fn consume_question_marks(text: &mut &[u8]) -> usize {
question_marks
}

impl UnicodeRange {
fn write<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
write!(dest, "U+{:X}", self.start)?;
if self.end != self.start {
write!(dest, "-{:X}", self.end)?;
}
Ok(())
}
}

impl fmt::Debug for UnicodeRange {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.to_css(formatter)
self.write(formatter)
}
}

#[cfg(any(feature = "serialize", test))]
impl ToCss for UnicodeRange {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
{
write!(dest, "U+{:X}", self.start)?;
if self.end != self.start {
write!(dest, "-{:X}", self.end)?;
}
Ok(())
self.write(dest)
}
}
Loading