|
| 1 | +import * as parser from "ui/styling/css-selector-parser"; |
| 2 | +import * as TKUnit from "../../TKUnit"; |
| 3 | + |
| 4 | +function test(css: string, expected: {}): void { |
| 5 | + let result = parser.parse(css); |
| 6 | + TKUnit.assertDeepEqual(result, expected); |
| 7 | +} |
| 8 | + |
| 9 | +export function test_fairly_complex_selector(): void { |
| 10 | + test(` listview#products.mark gridlayout:selected[row="2"] a> b > c >d>e *[src] `, [ |
| 11 | + { pos: 2, type: "", ident: "listview" }, |
| 12 | + { pos: 10, type: "#", ident: "products" }, |
| 13 | + { pos: 19, type: ".", ident: "mark", comb: " " }, |
| 14 | + { pos: 25, type: "", ident: "gridlayout" }, |
| 15 | + { pos: 35, type: ":", ident: "selected" }, |
| 16 | + { pos: 44, type: "[]", prop: "row", test: "=", value: "2", comb: " " }, |
| 17 | + { pos: 54, type: "", ident: "a", comb: ">" }, |
| 18 | + { pos: 57, type: "", ident: "b", comb: ">" }, |
| 19 | + { pos: 63, type: "", ident: "c", comb: ">" }, |
| 20 | + { pos: 66, type: "", ident: "d", comb: ">" }, |
| 21 | + { pos: 68, type: "", ident: "e", comb: " " }, |
| 22 | + { pos: 70, type: "*" }, |
| 23 | + { pos: 71, type: "[]", prop: "src" } |
| 24 | + ]); |
| 25 | +} |
| 26 | + |
| 27 | +export function test_typeguard_isUniversal(): void { |
| 28 | + let selector = parser.parse("*")[0]; |
| 29 | + TKUnit.assertTrue(parser.isUniversal(selector)); |
| 30 | + TKUnit.assertFalse(parser.isType(selector)); |
| 31 | + TKUnit.assertFalse(parser.isClass(selector)); |
| 32 | + TKUnit.assertFalse(parser.isId(selector)); |
| 33 | + TKUnit.assertFalse(parser.isPseudo(selector)); |
| 34 | + TKUnit.assertFalse(parser.isAttribute(selector)); |
| 35 | +} |
| 36 | +export function test_typeguard_isType(): void { |
| 37 | + let selector = parser.parse("button")[0]; |
| 38 | + TKUnit.assertFalse(parser.isUniversal(selector)); |
| 39 | + TKUnit.assertTrue(parser.isType(selector)); |
| 40 | + TKUnit.assertFalse(parser.isClass(selector)); |
| 41 | + TKUnit.assertFalse(parser.isId(selector)); |
| 42 | + TKUnit.assertFalse(parser.isPseudo(selector)); |
| 43 | + TKUnit.assertFalse(parser.isAttribute(selector)); |
| 44 | +} |
| 45 | +export function test_typeguard_isClass(): void { |
| 46 | + let selector = parser.parse(".login")[0]; |
| 47 | + TKUnit.assertFalse(parser.isUniversal(selector)); |
| 48 | + TKUnit.assertFalse(parser.isType(selector)); |
| 49 | + TKUnit.assertTrue(parser.isClass(selector)); |
| 50 | + TKUnit.assertFalse(parser.isId(selector)); |
| 51 | + TKUnit.assertFalse(parser.isPseudo(selector)); |
| 52 | + TKUnit.assertFalse(parser.isAttribute(selector)); |
| 53 | +} |
| 54 | +export function test_typeguard_isId(): void { |
| 55 | + let selector = parser.parse("#login")[0]; |
| 56 | + TKUnit.assertFalse(parser.isUniversal(selector)); |
| 57 | + TKUnit.assertFalse(parser.isType(selector)); |
| 58 | + TKUnit.assertFalse(parser.isClass(selector)); |
| 59 | + TKUnit.assertTrue(parser.isId(selector)); |
| 60 | + TKUnit.assertFalse(parser.isPseudo(selector)); |
| 61 | + TKUnit.assertFalse(parser.isAttribute(selector)); |
| 62 | +} |
| 63 | +export function test_typeguard_isPseudo(): void { |
| 64 | + let selector = parser.parse(":hover")[0]; |
| 65 | + TKUnit.assertFalse(parser.isUniversal(selector)); |
| 66 | + TKUnit.assertFalse(parser.isType(selector)); |
| 67 | + TKUnit.assertFalse(parser.isClass(selector)); |
| 68 | + TKUnit.assertFalse(parser.isId(selector)); |
| 69 | + TKUnit.assertTrue(parser.isPseudo(selector)); |
| 70 | + TKUnit.assertFalse(parser.isAttribute(selector)); |
| 71 | +} |
| 72 | +export function test_typeguard_isAttribute(): void { |
| 73 | + let selector = parser.parse("[src]")[0]; |
| 74 | + TKUnit.assertFalse(parser.isUniversal(selector)); |
| 75 | + TKUnit.assertFalse(parser.isType(selector)); |
| 76 | + TKUnit.assertFalse(parser.isClass(selector)); |
| 77 | + TKUnit.assertFalse(parser.isId(selector)); |
| 78 | + TKUnit.assertFalse(parser.isPseudo(selector)); |
| 79 | + TKUnit.assertTrue(parser.isAttribute(selector)); |
| 80 | +} |
| 81 | + |
| 82 | +export function test_universal_selector(): void { |
| 83 | + test(`*`, [{ pos: 0, type: "*" }]); |
| 84 | +} |
| 85 | +export function test_type_selector(): void { |
| 86 | + test(`button`, [{ pos: 0, type: "", ident: "button" }]); |
| 87 | +} |
| 88 | +export function test_class_selector(): void { |
| 89 | + test(`.red`, [{ pos: 0, type: ".", ident: "red" }]); |
| 90 | +} |
| 91 | +export function test_id_selector(): void { |
| 92 | + test(`#login`, [{ pos: 0, type: "#", ident: "login" }]); |
| 93 | +} |
| 94 | +export function test_pseudoClass(): void { |
| 95 | + test(`:hover`, [{ pos: 0, type: ":", ident: "hover" }]); |
| 96 | +} |
| 97 | +export function test_attribute_no_value(): void { |
| 98 | + test(`[src]`, [{ pos: 0, type: "[]", prop: "src" }]); |
| 99 | +} |
| 100 | +export function test_attribute_equal(): void { |
| 101 | + test(`[src = "res://"]`, [{ pos: 0, type: "[]", prop: "src", test: "=", value: `res://` }]); |
| 102 | +} |
| 103 | +export function test_attribute_all_tests(): void { |
| 104 | + ["=", "^=", "$=", "*=", "=", "~=", "|="].forEach(t => test(`[src ${t} "val"]`, [{ pos: 0, type: "[]", prop: "src", test: t, value: "val"}])); |
| 105 | +} |
| 106 | +export function test_direct_parent_comb(): void { |
| 107 | + test(`listview > .image`, [ |
| 108 | + { pos: 0, type: "", ident: "listview", comb: ">" }, |
| 109 | + { pos: 11, type: ".", ident: "image" } |
| 110 | + ]); |
| 111 | +} |
| 112 | +export function test_ancestor_comb(): void { |
| 113 | + test(`listview .image`, [ |
| 114 | + { pos: 0, type: "", ident: "listview", comb: " " }, |
| 115 | + { pos: 10, type: ".", ident: "image" } |
| 116 | + ]); |
| 117 | +} |
| 118 | +export function test_single_sequence(): void { |
| 119 | + test(`button:hover`, [ |
| 120 | + { pos: 0, type: "", ident: "button" }, |
| 121 | + { pos: 6, type: ":", ident: "hover" } |
| 122 | + ]); |
| 123 | +} |
| 124 | +export function test_multiple_sequences(): void { |
| 125 | + test(`listview>:selected image.product`, [ |
| 126 | + { pos: 0, type: "", ident: "listview", comb: ">" }, |
| 127 | + { pos: 9, type: ":", ident: "selected", comb: " " }, |
| 128 | + { pos: 19, type: "", ident: "image" }, |
| 129 | + { pos: 24, type: ".", ident: "product" } |
| 130 | + ]); |
| 131 | +} |
| 132 | +export function test_multiple_attribute_and_pseudo_classes(): void { |
| 133 | + test(`button#login[user][pass]:focused:hovered`, [ |
| 134 | + { pos: 0, type: "", ident: "button" }, |
| 135 | + { pos: 6, type: "#", ident: "login" }, |
| 136 | + { pos: 12, type: "[]", prop: "user" }, |
| 137 | + { pos: 18, type: "[]", prop: "pass" }, |
| 138 | + { pos: 24, type: ":", ident: "focused" }, |
| 139 | + { pos: 32, type: ":", ident: "hovered" } |
| 140 | + ]); |
| 141 | +} |
0 commit comments