Skip to content

Commit 53e49c3

Browse files
committed
Fix tokenization of <unicode-range> tokens, per spec change.
1 parent e02f8ad commit 53e49c3

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

ast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ pub enum ComponentValue {
3737
Number(NumericValue),
3838
Percentage(NumericValue),
3939
Dimension(NumericValue, ~str),
40-
UnicodeRange(char, char), // UnicodeRange {start: char, end: char},
41-
EmptyUnicodeRange,
40+
UnicodeRange { start: u32, end: u32 },
4241
WhiteSpace,
4342
Colon, // :
4443
Semicolon, // ;

tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,8 @@ impl ToJson for ComponentValue {
325325
Dimension(ref value, ref unit)
326326
=> JList(~[JString(~"dimension")] + numeric(value) + ~[unit.to_json()]),
327327
328-
// TODO:
329-
UnicodeRange(_start, _end) => fail!(),
330-
EmptyUnicodeRange => fail!(),
328+
UnicodeRange { start: s, end: e }
329+
=> JList(~[JString(~"unicode-range"), s.to_json(), e.to_json()]),
331330
332331
WhiteSpace => JString(~" "),
333332
Colon => JString(~":"),

tokenizer.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,13 @@ fn consume_unicode_range(tokenizer: &mut Tokenizer) -> ComponentValue {
544544
question_marks += 1;
545545
tokenizer.position += 1
546546
}
547-
let start: char;
548-
let end: char;
547+
let start;
548+
let end;
549549
if question_marks > 0 {
550-
start = char_from_hex(hex + "0".repeat(question_marks));
551-
end = char_from_hex(hex + "F".repeat(question_marks));
550+
start = u32::from_str_radix(hex + "0".repeat(question_marks), 16).unwrap();
551+
end = u32::from_str_radix(hex + "F".repeat(question_marks), 16).unwrap();
552552
} else {
553-
start = char_from_hex(hex);
553+
start = u32::from_str_radix(hex, 16).unwrap();
554554
hex = ~"";
555555
if !tokenizer.is_eof() && tokenizer.current_char() == '-' {
556556
tokenizer.position += 1;
@@ -563,21 +563,12 @@ fn consume_unicode_range(tokenizer: &mut Tokenizer) -> ComponentValue {
563563
}
564564
}
565565
}
566-
end = if hex.len() > 0 { char_from_hex(hex) } else { start }
567-
}
568-
if start > MAX_UNICODE || end < start {
569-
EmptyUnicodeRange
570-
} else {
571-
let end = if end <= MAX_UNICODE { end } else { MAX_UNICODE };
572-
// UnicodeRange {start: start, end: end}
573-
UnicodeRange(start, end)
566+
end = if hex.len() > 0 { u32::from_str_radix(hex, 16).unwrap() } else { start }
574567
}
568+
UnicodeRange {start: start, end: end}
575569
}
576570

577571

578-
static MAX_UNICODE: char = '\U0010FFFF';
579-
580-
581572
// Assumes that the U+005C REVERSE SOLIDUS (\) has already been consumed
582573
// and that the next input character has already been verified
583574
// to not be a newline.
@@ -602,16 +593,11 @@ fn consume_escape(tokenizer: &mut Tokenizer) -> char {
602593
_ => ()
603594
}
604595
}
605-
let c = char_from_hex(hex);
596+
let c = u32::from_str_radix(hex, 16).unwrap() as char as char;
597+
static MAX_UNICODE: char = '\U0010FFFF';
606598
if '\x00' < c && c <= MAX_UNICODE { c }
607599
else { '\uFFFD' } // Replacement character
608600
},
609601
c => c
610602
}
611603
}
612-
613-
614-
#[inline]
615-
fn char_from_hex(hex: &str) -> char {
616-
u32::from_str_radix(hex, 16).unwrap() as char
617-
}

0 commit comments

Comments
 (0)