test_style_cssText.html (1888B)
1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 <title>Test for Bug 1391169</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 7 <style id="style"></style> 8 </head> 9 <body> 10 <pre id="log"> 11 Log is: 12 </pre> 13 <script> 14 let styleElement = document.getElementById("style"); 15 let logElement = document.getElementById("log"); 16 console.log("logElement is " + logElement); 17 18 function log(text) 19 { 20 logElement.innerHTML += text + "\n"; 21 } 22 23 function textContentToCssText(text) 24 { 25 // Pass input in via textContent. 26 styleElement.textContent = text; 27 28 // Read output from concatenated cssText of all rules. 29 let s = ""; 30 let rules = document.styleSheets[1].cssRules; 31 for (let i = 0; i < rules.length; ++i) { 32 s += rules.item(i).cssText; 33 } 34 return s; 35 } 36 37 function noWhitespace(text) 38 { 39 return text.replace(/\s/g, ""); 40 } 41 42 function testData(input) 43 { 44 let text; 45 let pass1Goal; 46 if (typeof(input) == "string") { 47 // Only text data, assume characters should be the same. 48 text = input; 49 pass1Goal = input; 50 } else { 51 [text, pass1Goal] = input; 52 } 53 54 let pass1Text = textContentToCssText(text); 55 is(noWhitespace(pass1Text), noWhitespace(pass1Goal), "textContent --> cssText correct characters emitted with input \"" + text + "\""); 56 57 let pass2Text = textContentToCssText(pass1Text); 58 is(pass2Text, pass1Text, "textContent --> cssText roundtrip with input \"" + text + "\""); 59 60 log(text + " --> " + pass1Text + " --> " + pass2Text); 61 } 62 63 let data = [ 64 "*{}", 65 "* *{}", 66 "* > *{}", 67 "*>*{}", 68 "* * *{}", 69 "* > * > *{}", 70 "* + *{}", 71 "* ~ *{}", 72 ["*|*{}", "*{}"], 73 ["*|* > *{}", "* > *{}"], 74 "#tag{}", 75 "tag{}", 76 "@namespace tag url(\"fakeURL\"); tag|*{}", 77 "@namespace tag url(\"fakeURL\"); tag|* + *{}", 78 ]; 79 80 for (let i = 0; i < data.length; i++) { 81 testData(data[i]); 82 } 83 </script> 84 </body> 85 </html>