|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
4 | 4 |
|
5 |
| -use ast; |
6 | 5 | use ast::*;
|
7 | 6 |
|
8 | 7 |
|
9 |
| -impl ast::ComponentValue { |
10 |
| - pub fn to_css(&mut self) -> String { |
11 |
| - let mut css = String::new(); |
12 |
| - self.to_css_push(&mut css); |
13 |
| - css |
14 |
| - } |
15 |
| - |
16 |
| - pub fn to_css_push(&self, css: &mut String) { |
17 |
| - match *self { |
18 |
| - Ident(ref value) => serialize_identifier(value.as_slice(), css), |
19 |
| - AtKeyword(ref value) => { |
20 |
| - css.push_char('@'); |
21 |
| - serialize_identifier(value.as_slice(), css); |
22 |
| - }, |
23 |
| - Hash(ref value) => { |
24 |
| - css.push_char('#'); |
25 |
| - for c in value.as_slice().chars() { |
| 8 | +pub fn to_css_push(component_value: &ComponentValue, css: &mut String) { |
| 9 | + match *component_value { |
| 10 | + Ident(ref value) => serialize_identifier(value.as_slice(), css), |
| 11 | + AtKeyword(ref value) => { |
| 12 | + css.push_char('@'); |
| 13 | + serialize_identifier(value.as_slice(), css); |
| 14 | + }, |
| 15 | + Hash(ref value) => { |
| 16 | + css.push_char('#'); |
| 17 | + for c in value.as_slice().chars() { |
| 18 | + serialize_char(c, css, /* is_identifier_start = */ false); |
| 19 | + } |
| 20 | + }, |
| 21 | + IDHash(ref value) => { |
| 22 | + css.push_char('#'); |
| 23 | + serialize_identifier(value.as_slice(), css); |
| 24 | + } |
| 25 | + String(ref value) => serialize_string(value.as_slice(), css), |
| 26 | + URL(ref value) => { |
| 27 | + css.push_str("url("); |
| 28 | + serialize_string(value.as_slice(), css); |
| 29 | + css.push_char(')'); |
| 30 | + }, |
| 31 | + Delim(value) => css.push_char(value), |
| 32 | + |
| 33 | + Number(ref value) => css.push_str(value.representation.as_slice()), |
| 34 | + Percentage(ref value) => { |
| 35 | + css.push_str(value.representation.as_slice()); |
| 36 | + css.push_char('%'); |
| 37 | + }, |
| 38 | + Dimension(ref value, ref unit) => { |
| 39 | + css.push_str(value.representation.as_slice()); |
| 40 | + // Disambiguate with scientific notation. |
| 41 | + let unit = unit.as_slice(); |
| 42 | + if unit == "e" || unit == "E" || unit.starts_with("e-") || unit.starts_with("E-") { |
| 43 | + css.push_str("\\65 "); |
| 44 | + for c in unit.slice_from(1).chars() { |
26 | 45 | serialize_char(c, css, /* is_identifier_start = */ false);
|
27 | 46 | }
|
28 |
| - }, |
29 |
| - IDHash(ref value) => { |
30 |
| - css.push_char('#'); |
31 |
| - serialize_identifier(value.as_slice(), css); |
| 47 | + } else { |
| 48 | + serialize_identifier(unit, css) |
32 | 49 | }
|
33 |
| - String(ref value) => serialize_string(value.as_slice(), css), |
34 |
| - URL(ref value) => { |
35 |
| - css.push_str("url("); |
36 |
| - serialize_string(value.as_slice(), css); |
37 |
| - css.push_char(')'); |
38 |
| - }, |
39 |
| - Delim(value) => css.push_char(value), |
40 |
| - |
41 |
| - Number(ref value) => css.push_str(value.representation.as_slice()), |
42 |
| - Percentage(ref value) => { |
43 |
| - css.push_str(value.representation.as_slice()); |
44 |
| - css.push_char('%'); |
45 |
| - }, |
46 |
| - Dimension(ref value, ref unit) => { |
47 |
| - css.push_str(value.representation.as_slice()); |
48 |
| - // Disambiguate with scientific notation. |
49 |
| - let unit = unit.as_slice(); |
50 |
| - if unit == "e" || unit == "E" || unit.starts_with("e-") || unit.starts_with("E-") { |
51 |
| - css.push_str("\\65 "); |
52 |
| - for c in unit.slice_from(1).chars() { |
53 |
| - serialize_char(c, css, /* is_identifier_start = */ false); |
54 |
| - } |
55 |
| - } else { |
56 |
| - serialize_identifier(unit, css) |
57 |
| - } |
58 |
| - }, |
| 50 | + }, |
59 | 51 |
|
60 |
| - UnicodeRange(start, end) => { |
61 |
| - css.push_str(format!("U+{:X}", start).as_slice()); |
62 |
| - if end != start { |
63 |
| - css.push_str(format!("-{:X}", end).as_slice()); |
64 |
| - } |
| 52 | + UnicodeRange(start, end) => { |
| 53 | + css.push_str(format!("U+{:X}", start).as_slice()); |
| 54 | + if end != start { |
| 55 | + css.push_str(format!("-{:X}", end).as_slice()); |
65 | 56 | }
|
66 |
| - |
67 |
| - WhiteSpace => css.push_char(' '), |
68 |
| - Colon => css.push_char(':'), |
69 |
| - Semicolon => css.push_char(';'), |
70 |
| - Comma => css.push_char(','), |
71 |
| - IncludeMatch => css.push_str("~="), |
72 |
| - DashMatch => css.push_str("|="), |
73 |
| - PrefixMatch => css.push_str("^="), |
74 |
| - SuffixMatch => css.push_str("$="), |
75 |
| - SubstringMatch => css.push_str("*="), |
76 |
| - Column => css.push_str("||"), |
77 |
| - CDO => css.push_str("<!--"), |
78 |
| - CDC => css.push_str("-->"), |
79 |
| - |
80 |
| - Function(ref name, ref arguments) => { |
81 |
| - serialize_identifier(name.as_slice(), css); |
82 |
| - css.push_char('('); |
83 |
| - arguments.iter().to_css_push(css); |
84 |
| - css.push_char(')'); |
85 |
| - }, |
86 |
| - ParenthesisBlock(ref content) => { |
87 |
| - css.push_char('('); |
88 |
| - content.iter().to_css_push(css); |
89 |
| - css.push_char(')'); |
90 |
| - }, |
91 |
| - SquareBracketBlock(ref content) => { |
92 |
| - css.push_char('['); |
93 |
| - content.iter().to_css_push(css); |
94 |
| - css.push_char(']'); |
95 |
| - }, |
96 |
| - CurlyBracketBlock(ref content) => { |
97 |
| - css.push_char('{'); |
98 |
| - content.iter().map(|t| match *t { (ref c, _) => c }).to_css_push(css); |
99 |
| - css.push_char('}'); |
100 |
| - }, |
101 |
| - |
102 |
| - BadURL => css.push_str("url(<bad url>)"), |
103 |
| - BadString => css.push_str("\"<bad string>\n"), |
104 |
| - CloseParenthesis => css.push_char(')'), |
105 |
| - CloseSquareBracket => css.push_char(']'), |
106 |
| - CloseCurlyBracket => css.push_char('}'), |
107 | 57 | }
|
| 58 | + |
| 59 | + WhiteSpace => css.push_char(' '), |
| 60 | + Colon => css.push_char(':'), |
| 61 | + Semicolon => css.push_char(';'), |
| 62 | + Comma => css.push_char(','), |
| 63 | + IncludeMatch => css.push_str("~="), |
| 64 | + DashMatch => css.push_str("|="), |
| 65 | + PrefixMatch => css.push_str("^="), |
| 66 | + SuffixMatch => css.push_str("$="), |
| 67 | + SubstringMatch => css.push_str("*="), |
| 68 | + Column => css.push_str("||"), |
| 69 | + CDO => css.push_str("<!--"), |
| 70 | + CDC => css.push_str("-->"), |
| 71 | + |
| 72 | + Function(ref name, ref arguments) => { |
| 73 | + serialize_identifier(name.as_slice(), css); |
| 74 | + css.push_char('('); |
| 75 | + arguments.iter().to_css_push(css); |
| 76 | + css.push_char(')'); |
| 77 | + }, |
| 78 | + ParenthesisBlock(ref content) => { |
| 79 | + css.push_char('('); |
| 80 | + content.iter().to_css_push(css); |
| 81 | + css.push_char(')'); |
| 82 | + }, |
| 83 | + SquareBracketBlock(ref content) => { |
| 84 | + css.push_char('['); |
| 85 | + content.iter().to_css_push(css); |
| 86 | + css.push_char(']'); |
| 87 | + }, |
| 88 | + CurlyBracketBlock(ref content) => { |
| 89 | + css.push_char('{'); |
| 90 | + content.iter().map(|t| match *t { (ref c, _) => c }).to_css_push(css); |
| 91 | + css.push_char('}'); |
| 92 | + }, |
| 93 | + |
| 94 | + BadURL => css.push_str("url(<bad url>)"), |
| 95 | + BadString => css.push_str("\"<bad string>\n"), |
| 96 | + CloseParenthesis => css.push_char(')'), |
| 97 | + CloseSquareBracket => css.push_char(']'), |
| 98 | + CloseCurlyBracket => css.push_char('}'), |
108 | 99 | }
|
109 | 100 | }
|
110 | 101 |
|
|
0 commit comments