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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSSOM - Serialization of CSSMediaRule</title>
<link rel="help" href="https://drafts.csswg.org/cssom/#serialize-a-css-rule">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
function makeSheet(t) {
var style = document.createElement('style');
document.body.appendChild(style);
t.add_cleanup(function() {
document.body.removeChild(style);
});
return style.sheet;
}
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media {}', 0);
assert_equals(sheet.cssRules.length, 1);
assert_equals(sheet.cssRules[0].cssText, '@media {\n}');
}, 'empty media query list');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media all {}');
sheet.insertRule('@media print {}');
sheet.insertRule('@media screen {}');
sheet.insertRule('@media speech {}');
assert_equals(sheet.cssRules.length, 4);
assert_equals(sheet.cssRules[0].cssText, '@media speech {\n}');
assert_equals(sheet.cssRules[1].cssText, '@media screen {\n}');
assert_equals(sheet.cssRules[2].cssText, '@media print {\n}');
assert_equals(sheet.cssRules[3].cssText, '@media all {\n}');
}, 'type - no features');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media not all {}');
sheet.insertRule('@media not print {}');
sheet.insertRule('@media not screen {}');
sheet.insertRule('@media not speech {}');
assert_equals(sheet.cssRules.length, 4);
assert_equals(sheet.cssRules[0].cssText, '@media not speech {\n}');
assert_equals(sheet.cssRules[1].cssText, '@media not screen {\n}');
assert_equals(sheet.cssRules[2].cssText, '@media not print {\n}');
assert_equals(sheet.cssRules[3].cssText, '@media not all {\n}');
}, 'type - no features - negation');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media aLL {}');
sheet.insertRule('@media pRiNt {}');
sheet.insertRule('@media screEN {}');
sheet.insertRule('@media spEech {}');
assert_equals(sheet.cssRules.length, 4);
assert_equals(sheet.cssRules[0].cssText, '@media speech {\n}');
assert_equals(sheet.cssRules[1].cssText, '@media screen {\n}');
assert_equals(sheet.cssRules[2].cssText, '@media print {\n}');
assert_equals(sheet.cssRules[3].cssText, '@media all {\n}');
}, 'type - no features - character case normalization');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media all and (color) {}');
assert_equals(sheet.cssRules.length, 1);
assert_equals(sheet.cssRules[0].cssText, '@media (color) {\n}');
}, 'type - omission of all');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media not all and (color) {}');
assert_equals(sheet.cssRules.length, 1);
assert_equals(sheet.cssRules[0].cssText, '@media not all and (color) {\n}');
}, 'type - inclusion of negated all');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media screen and (Color) {}');
sheet.insertRule('@media screen and (cOLor) {}');
assert_equals(sheet.cssRules.length, 2);
assert_equals(sheet.cssRules[0].cssText, '@media screen and (color) {\n}');
assert_equals(sheet.cssRules[1].cssText, '@media screen and (color) {\n}');
}, 'features - character case normalization');
/**
* The following test is disabled pending clarification of the intended
* behavior: https://github.com/w3c/csswg-drafts/issues/533
*/
//test(function(t) {
// var sheet = makeSheet(t);
// sheet.insertRule('@media screen and (color) and (color) {}');
//
// assert_equals(sheet.cssRules.length, 1);
// assert_equals(
// sheet.cssRules[0].cssText,
// '@media screen and (color) {\n}'
// );
//}, 'features - de-duplication');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media print and (max-width: 23px) and (max-width: 45px) {}');
assert_equals(sheet.cssRules.length, 1);
assert_equals(
sheet.cssRules[0].cssText,
'@media print and (max-width: 23px) and (max-width: 45px) {\n}'
);
}, 'features - preservation of overspecified features');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media screen and (max-width: 0px) and (color) {}');
sheet.insertRule('@media screen and (color) and (max-width: 0px) {}');
assert_equals(sheet.cssRules.length, 2);
assert_equals(
sheet.cssRules[0].cssText,
'@media screen and (color) and (max-width: 0px) {\n}'
);
assert_equals(
sheet.cssRules[1].cssText,
'@media screen and (max-width: 0px) and (color) {\n}'
);
}, 'features - no lexicographical sorting');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media screen and (max-width: 0px), screen and (color) {}');
assert_equals(sheet.cssRules.length, 1);
assert_equals(
sheet.cssRules[0].cssText,
'@media screen and (max-width: 0px), screen and (color) {\n}'
);
}, 'media query list');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media print {}');
assert_equals(sheet.cssRules.length, 1);
sheet.cssRules[0].insertRule('#foo { z-index: 23; float: left; }', 0);
assert_equals(
sheet.cssRules[0].cssText,
[
'@media print {',
' #foo { z-index: 23; float: left; }',
'}'
].join('\n')
);
}, 'one rule');
test(function(t) {
var sheet = makeSheet(t);
sheet.insertRule('@media print {}');
assert_equals(sheet.cssRules.length, 1);
sheet.cssRules[0].insertRule('#foo { z-index: 23; float: left; }', 0);
sheet.cssRules[0].insertRule('#bar { float: none; z-index: 45; }', 0);
assert_equals(
sheet.cssRules[0].cssText,
[
'@media print {',
' #bar { float: none; z-index: 45; }',
' #foo { z-index: 23; float: left; }',
'}'
].join('\n')
);
}, 'many rules');
</script>
</body>
</html>
|