CSSStyleSheet-replace.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <!DOCTYPE html>
  2. <style>
  3. .test {
  4. font-size: 12px;
  5. }
  6. </style>
  7. <script src="../include.js"></script>
  8. <script>
  9. asyncTest(async done => {
  10. const newStyle = `
  11. .test {
  12. font-size: 14px;
  13. }
  14. .test2 {
  15. font-size: 16px;
  16. }`;
  17. const newStyle2 = `.test {
  18. padding: 100px;
  19. }`;
  20. const nonConstructedSheet = document.styleSheets[0];
  21. try {
  22. await nonConstructedSheet.replace(newStyle);
  23. println("FAIL");
  24. } catch (e) {
  25. println(`Exception thrown when calling replace() on non-constructed stylesheet: ${e.name}`);
  26. }
  27. const sheet = new CSSStyleSheet();
  28. const cssRules = sheet.cssRules;
  29. await sheet.replace(newStyle);
  30. println(`Number of CSS rules after replace(): ${sheet.cssRules.length}`);
  31. for (const rule of sheet.cssRules) {
  32. println(`Rule: ${rule.cssText}`);
  33. }
  34. const cssRulesAfterReplace = sheet.cssRules;
  35. println(`cssRules returns the same object before and after replace(): ${cssRules === cssRulesAfterReplace}`);
  36. const importRule = `@import url("test.css");`;
  37. await sheet.replace(`${newStyle2} ${importRule}`);
  38. println(`@import rule should be not appear below:`);
  39. for (const rule of sheet.cssRules) {
  40. println(`Rule: ${rule.cssText}`);
  41. }
  42. done();
  43. });
  44. </script>