File: declaration_tokenizer.rs

package info (click to toggle)
rust-simplecss 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220 kB
  • sloc: makefile: 2
file content (165 lines) | stat: -rw-r--r-- 3,900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2019 the SimpleCSS Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Declaration Tokenizer

use simplecss::*;

macro_rules! tokenize {
    ($name:ident, $text:expr, $( $token:expr ),*) => (
        #[test]
        fn $name() {
            let mut t = DeclarationTokenizer::from($text);
            $(
                assert_eq!(t.next().unwrap(), $token);
            )*

            assert!(t.next().is_none());
        }
    )
}

fn declare<'a>(name: &'a str, value: &'a str) -> Declaration<'a> {
    Declaration {
        name,
        value,
        important: false,
    }
}

fn declare_important<'a>(name: &'a str, value: &'a str) -> Declaration<'a> {
    Declaration {
        name,
        value,
        important: true,
    }
}

tokenize!(tokenize_01, "",);

tokenize!(tokenize_02, " ",);

tokenize!(tokenize_03, "/**/",);

tokenize!(tokenize_04, "color:red", declare("color", "red"));

tokenize!(tokenize_05, "color:red;", declare("color", "red"));

tokenize!(tokenize_06, "color:red ", declare("color", "red"));

tokenize!(tokenize_07, " color: red; ", declare("color", "red"));

tokenize!(tokenize_08, "  color  :  red  ; ", declare("color", "red"));

tokenize!(
    tokenize_09,
    "  color:red;;;;color:red; ",
    declare("color", "red"),
    declare("color", "red")
);

tokenize!(
    tokenize_10,
    "background: url(\"img.png\");",
    declare("background", "url(\"img.png\")")
);

tokenize!(
    tokenize_11,
    "background: url(\"{}\");",
    declare("background", "url(\"{}\")")
);

tokenize!(
    tokenize_12,
    "color: red ! important",
    declare_important("color", "red")
);

tokenize!(
    tokenize_13,
    "color: red !important",
    declare_important("color", "red")
);

tokenize!(
    tokenize_14,
    "color: red!important",
    declare_important("color", "red")
);

tokenize!(
    tokenize_15,
    "color: red !/**/important",
    declare_important("color", "red")
);

tokenize!(
    tokenize_16,
    "border: 1em solid blue",
    declare("border", "1em solid blue")
);

tokenize!(
    tokenize_17,
    "background: navy url(support/diamond.png) -2em -2em no-repeat",
    declare(
        "background",
        "navy url(support/diamond.png) -2em -2em no-repeat"
    )
);

tokenize!(tokenize_18, "/**/color:red", declare("color", "red"));

tokenize!(tokenize_19, "/* *\\/*/color: red;", declare("color", "red"));

tokenize!(
    tokenize_20,
    "/**/color/**/:/**/red/**/;/**/",
    declare("color", "red")
);

tokenize!(tokenize_21, "\ncolor\n:\nred\n;\n", declare("color", "red"));

tokenize!(tokenize_22, "{color:red}",);

tokenize!(tokenize_23, "(color:red)",);

tokenize!(tokenize_24, "[color:red]",);

tokenize!(tokenize_25, "color:",);

tokenize!(tokenize_26, "value:\"text\"", declare("value", "\"text\""));

tokenize!(tokenize_27, "value:'text'", declare("value", "'text'"));

tokenize!(tokenize_28, "color:#fff", declare("color", "#fff"));
tokenize!(tokenize_29, "color:0.5", declare("color", "0.5"));

tokenize!(tokenize_30, "color:.5", declare("color", ".5"));

tokenize!(tokenize_31, "color:#FFF", declare("color", "#FFF"));

tokenize!(
    tokenize_32,
    "content: counter(chapno, upper-roman) \". \"",
    declare("content", "counter(chapno, upper-roman) \". \"")
);

tokenize!(
    tokenize_33,
    "font-family:'Noto Serif','DejaVu Serif',serif",
    declare("font-family", "'Noto Serif','DejaVu Serif',serif")
);

tokenize!(tokenize_34, "*zoom:1;", declare("zoom", "1"));

//tokenize!(tokenize_, "@unsupported { splines: reticulating } color: green",
//    declare("color", "green")
//);

//tokenize!(tokenize_, "/*\\*/*/color: red;", declare("color", "red"));

//tokenize!(tokenize_, "\"this is a string]}\"\"[{\\\"'\";  /*should be parsed as a string but be ignored*/
//    {{}}[]'';                     /*should be parsed as nested blocks and a string but be ignored*/
//    color: red;", declare("color", "red"));