<!DOCTYPE html>
<html>
<head>
    <title>CSSOM - Extensions to the Document Interface: StyleSheetList length reflects dynamically loaded and unloaded sheets</title>
    <link rel="author" title="Jesse Bounds" href="mailto:jesse@codeforamerica.org">
    <link rel="help" href="http://www.w3.org/TR/cssom-1/#extensions-to-the-document-interface">
    <link rel="help" href="http://www.w3.org/TR/cssom-1/#the-stylesheetlist-interface">
    <link rel="help" href="http://www.w3.org/TR/cssom-1/#css-style-sheet-collections">
    <link rel="stylesheet" href="stylesheet.css" type="text/css">
    <meta name="flags" content="dom">
    <meta name="assert" content="The styleSheets length attribute must reflect the number of sheets at page load and after dynamically">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <div id="log"></div>
    <script>

      // Get the Document's styleSheets attribute
      var styleSheets = document.styleSheets;

      // Verify that the styleSheets list length is 1 due to "stylesheet.css" loaded in head section
      test(function() {
        assert_equals(styleSheets.length, 1, "styleSheets.length is incorrect:");
      }, "stylesheet.css should be loaded and styleSheets.length === 1");

      // Verify that the styleSheets list length is 0 after removing the loaded sheet from the DOM
      test(function() {

        // get the one and only sheet loaded
        var sheet = styleSheets.item(0);

        // remove the sheet from the DOM
        sheet.ownerNode.parentNode.removeChild(sheet.ownerNode)

        // assert that there are 0 styleSheets in the styleSheets property
        assert_equals(styleSheets.length, 0, "styleSheets.length is incorrect:");

      }, "stylesheet.css should be unloaded and styleSheets.length === 0");

      // Verify that the styleSheets list length is back to 1 after loading a new sheet
      async_test(function(t) {

        // create a css file reference
        var fileReference = document.createElement("link");
        fileReference.setAttribute("rel", "stylesheet");
        fileReference.setAttribute("type", "text/css");
        fileReference.setAttribute("href", "stylesheet-1.css");
        fileReference.onerror = t.step_func_done(function() {
          // assert that there is 1 styleSheet in the styleSheets property
          assert_equals(styleSheets.length, 1, "styleSheets.length is incorrect:");
        });

        // load the css file reference into the head section
        var head = document.getElementsByTagName("HEAD")[0];
        head.appendChild(fileReference);
      }, "stylesheet-1.css should be loaded and styleSheets.length === 1");

    </script>
</body>
</html>
