| 1 | // Copyright 2019 the SimpleCSS Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | |
| 4 | //! Declaration Tokenizer |
| 5 | |
| 6 | use simplecss::*; |
| 7 | |
| 8 | macro_rules! tokenize { |
| 9 | ($name:ident, $text:expr, $( $token:expr ),*) => ( |
| 10 | #[test] |
| 11 | fn $name() { |
| 12 | let mut t = DeclarationTokenizer::from($text); |
| 13 | $( |
| 14 | assert_eq!(t.next().unwrap(), $token); |
| 15 | )* |
| 16 | |
| 17 | assert!(t.next().is_none()); |
| 18 | } |
| 19 | ) |
| 20 | } |
| 21 | |
| 22 | fn declare<'a>(name: &'a str, value: &'a str) -> Declaration<'a> { |
| 23 | Declaration { |
| 24 | name, |
| 25 | value, |
| 26 | important: false, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn declare_important<'a>(name: &'a str, value: &'a str) -> Declaration<'a> { |
| 31 | Declaration { |
| 32 | name, |
| 33 | value, |
| 34 | important: true, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | tokenize!(tokenize_01, "",); |
| 39 | |
| 40 | tokenize!(tokenize_02, " ",); |
| 41 | |
| 42 | tokenize!(tokenize_03, "/**/",); |
| 43 | |
| 44 | tokenize!(tokenize_04, "color:red", declare("color", "red")); |
| 45 | |
| 46 | tokenize!(tokenize_05, "color:red;", declare("color", "red")); |
| 47 | |
| 48 | tokenize!(tokenize_06, "color:red ", declare("color", "red")); |
| 49 | |
| 50 | tokenize!(tokenize_07, " color: red; ", declare("color", "red")); |
| 51 | |
| 52 | tokenize!(tokenize_08, " color : red ; ", declare("color", "red")); |
| 53 | |
| 54 | tokenize!( |
| 55 | tokenize_09, |
| 56 | " color:red;;;;color:red; ", |
| 57 | declare("color", "red"), |
| 58 | declare("color", "red") |
| 59 | ); |
| 60 | |
| 61 | tokenize!( |
| 62 | tokenize_10, |
| 63 | "background: url(\"img.png\");", |
| 64 | declare("background", "url(\"img.png\")") |
| 65 | ); |
| 66 | |
| 67 | tokenize!( |
| 68 | tokenize_11, |
| 69 | "background: url(\"{}\");", |
| 70 | declare("background", "url(\"{}\")") |
| 71 | ); |
| 72 | |
| 73 | tokenize!( |
| 74 | tokenize_12, |
| 75 | "color: red ! important", |
| 76 | declare_important("color", "red") |
| 77 | ); |
| 78 | |
| 79 | tokenize!( |
| 80 | tokenize_13, |
| 81 | "color: red !important", |
| 82 | declare_important("color", "red") |
| 83 | ); |
| 84 | |
| 85 | tokenize!( |
| 86 | tokenize_14, |
| 87 | "color: red!important", |
| 88 | declare_important("color", "red") |
| 89 | ); |
| 90 | |
| 91 | tokenize!( |
| 92 | tokenize_15, |
| 93 | "color: red !/**/important", |
| 94 | declare_important("color", "red") |
| 95 | ); |
| 96 | |
| 97 | tokenize!( |
| 98 | tokenize_16, |
| 99 | "border: 1em solid blue", |
| 100 | declare("border", "1em solid blue") |
| 101 | ); |
| 102 | |
| 103 | tokenize!( |
| 104 | tokenize_17, |
| 105 | "background: navy url(support/diamond.png) -2em -2em no-repeat", |
| 106 | declare( |
| 107 | "background", |
| 108 | "navy url(support/diamond.png) -2em -2em no-repeat" |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | tokenize!(tokenize_18, "/**/color:red", declare("color", "red")); |
| 113 | |
| 114 | tokenize!(tokenize_19, "/* *\\/*/color: red;", declare("color", "red")); |
| 115 | |
| 116 | tokenize!( |
| 117 | tokenize_20, |
| 118 | "/**/color/**/:/**/red/**/;/**/", |
| 119 | declare("color", "red") |
| 120 | ); |
| 121 | |
| 122 | tokenize!(tokenize_21, "\ncolor\n:\nred\n;\n", declare("color", "red")); |
| 123 | |
| 124 | tokenize!(tokenize_22, "{color:red}",); |
| 125 | |
| 126 | tokenize!(tokenize_23, "(color:red)",); |
| 127 | |
| 128 | tokenize!(tokenize_24, "[color:red]",); |
| 129 | |
| 130 | tokenize!(tokenize_25, "color:",); |
| 131 | |
| 132 | tokenize!(tokenize_26, "value:\"text\"", declare("value", "\"text\"")); |
| 133 | |
| 134 | tokenize!(tokenize_27, "value:'text'", declare("value", "'text'")); |
| 135 | |
| 136 | tokenize!(tokenize_28, "color:#fff", declare("color", "#fff")); |
| 137 | tokenize!(tokenize_29, "color:0.5", declare("color", "0.5")); |
| 138 | |
| 139 | tokenize!(tokenize_30, "color:.5", declare("color", ".5")); |
| 140 | |
| 141 | tokenize!(tokenize_31, "color:#FFF", declare("color", "#FFF")); |
| 142 | |
| 143 | tokenize!( |
| 144 | tokenize_32, |
| 145 | "content: counter(chapno, upper-roman) \". \"", |
| 146 | declare("content", "counter(chapno, upper-roman) \". \"") |
| 147 | ); |
| 148 | |
| 149 | tokenize!( |
| 150 | tokenize_33, |
| 151 | "font-family:'Noto Serif','DejaVu Serif',serif", |
| 152 | declare("font-family", "'Noto Serif','DejaVu Serif',serif") |
| 153 | ); |
| 154 | |
| 155 | tokenize!(tokenize_34, "*zoom:1;", declare("zoom", "1")); |
| 156 | |
| 157 | //tokenize!(tokenize_, "@unsupported { splines: reticulating } color: green", |
| 158 | // declare("color", "green") |
| 159 | //); |
| 160 | |
| 161 | //tokenize!(tokenize_, "/*\\*/*/color: red;", declare("color", "red")); |
| 162 | |
| 163 | //tokenize!(tokenize_, "\"this is a string]}\"\"[{\\\"'\"; /*should be parsed as a string but be ignored*/ |
| 164 | // {{}}[]''; /*should be parsed as nested blocks and a string but be ignored*/ |
| 165 | // color: red;", declare("color", "red")); |