Trait text_scanner::ext::CssScannerExt
source · pub trait CssScannerExt<'text>: Sealed {
// Required methods
fn scan_css_block_comment(&mut self) -> ScannerResult<'text, &'text str>;
fn scan_css_identifier(&mut self) -> ScannerResult<'text, &'text str>;
fn scan_css_at_keyword(&mut self) -> ScannerResult<'text, &'text str>;
fn scan_css_hash(&mut self) -> ScannerResult<'text, &'text str>;
fn scan_css_string(&mut self) -> ScannerResult<'text, &'text str>;
fn scan_css_number(&mut self) -> ScannerResult<'text, &'text str>;
}
Expand description
Scanner
extension for scanning CSS tokens.
See also ScssScannerExt
.
Required Methods§
sourcefn scan_css_block_comment(&mut self) -> ScannerResult<'text, &'text str>
fn scan_css_block_comment(&mut self) -> ScannerResult<'text, &'text str>
Scans a single CSS block comment.
Note: CSS block comments do not allow nested block comments.
Note: This has the same lifetime as the original text
,
so the scanner can continue to be used while this exists.
Example
use text_scanner::{ext::CssScannerExt, Scanner};
let text = r#"
/* Block Comment */
/* Multi
// Line
/* Block
Comment */
/* Unterminated Block Comment
"#;
let comments = [
(3..22, "/* Block Comment */"),
(26..71, "/* Multi\n // Line\n /* Block\n Comment */"),
(75..105, "/* Unterminated Block Comment\n"),
];
let mut scanner = Scanner::new(text);
for comment in comments {
scanner.skip_whitespace();
assert_eq!(scanner.scan_css_block_comment(), Ok(comment));
}
sourcefn scan_css_identifier(&mut self) -> ScannerResult<'text, &'text str>
fn scan_css_identifier(&mut self) -> ScannerResult<'text, &'text str>
Scans a single CSS identifier.
Note: This has the same lifetime as the original text
,
so the scanner can continue to be used while this exists.
Example
use text_scanner::{ext::CssScannerExt, Scanner};
let text = r#"
foo
foo_bar
foo-bar
--foo
"#;
let idents = [
(3..6, "foo"),
(9..16, "foo_bar"),
(19..26, "foo-bar"),
(29..34, "--foo"),
];
let mut scanner = Scanner::new(text);
for ident in idents {
scanner.skip_whitespace();
assert_eq!(scanner.scan_css_identifier(), Ok(ident));
}
fn scan_css_at_keyword(&mut self) -> ScannerResult<'text, &'text str>
fn scan_css_hash(&mut self) -> ScannerResult<'text, &'text str>
sourcefn scan_css_string(&mut self) -> ScannerResult<'text, &'text str>
fn scan_css_string(&mut self) -> ScannerResult<'text, &'text str>
Scans a single CSS string.
Note: This has the same lifetime as the original text
,
so the scanner can continue to be used while this exists.
Example
use text_scanner::{ext::CssScannerExt, Scanner};
let text = r#"
"Hello World"
'Hello World'
"Hello ' \" World"
'Hello \' " World'
"Unterminated String
"#;
let strings = [
(3..16, r#""Hello World""#),
(19..32, r#"'Hello World'"#),
(36..54, r#""Hello ' \" World""#),
(57..75, r#"'Hello \' " World'"#),
(79..100, "\"Unterminated String\n"),
];
let mut scanner = Scanner::new(text);
for string in strings {
scanner.skip_whitespace();
assert_eq!(scanner.scan_css_string(), Ok(string));
}
sourcefn scan_css_number(&mut self) -> ScannerResult<'text, &'text str>
fn scan_css_number(&mut self) -> ScannerResult<'text, &'text str>
Scans a single CSS number.
Note: CSS numbers allow a unary +
or -
before the number,
as opposed to other languages separating those into two different
tokens.
Note: This has the same lifetime as the original text
,
so the scanner can continue to be used while this exists.
Example
use text_scanner::{ext::CssScannerExt, Scanner};
let text = r#"
1
-2
+3
3.1415
+10.5E+100
"#;
let numbers = [
(3..4, "1"),
(7..9, "-2"),
(12..14, "+3"),
(17..23, "3.1415"),
(26..36, "+10.5E+100"),
];
let mut scanner = Scanner::new(text);
for num in numbers {
scanner.skip_whitespace();
assert_eq!(scanner.scan_css_number(), Ok(num));
}