content/xml/tests/books/books.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     2 // An inefficient, but effective bubble sort
     3 function sort(collection, key)
     4 {
     5   var i, j;
     6   var count = collection.length;
     7   var parent, child;
     9   for (i = count-1; i >= 0; i--) {
    10     for (j = 1; j <= i; j++) {
    11       if (collection[j-1][key] > collection[j][key]) {
    12          // Move the item both in the local array and
    13          // in the tree
    14          child = collection[j];
    15          parent = child.parentNode;
    17          collection[j] = collection[j-1];
    18          collection[j-1] = child;
    20          parent.removeChild(child);       
    21          parent.insertBefore(child, collection[j]);
    22       }
    23     }
    24   }
    25 }
    27 // Set user properties on the nodes in the collection
    28 // based on information found in its children. For example,
    29 // make a property "Author" based on the content of the
    30 // "Author" element found in the childNode list of the node.
    31 // This makes later sorting more efficient
    32 function collectInfo(nodes, propNames)
    33 {
    34   var i, j, k;
    35   var ncount = nodes.length; 
    36   var pcount = propNames.length;
    38   for (i = 0; i < ncount; i++) {
    39     var node = nodes[i];
    40     var childNodes = node.childNodes;
    41     var ccount = childNodes.length;
    43     for (j = 0; j < ccount; j++) {
    44       var child = childNodes[j];
    46       if (child.nodeType == Node.ELEMENT_NODE) {
    47         var tagName = child.tagName;
    49         for (k = 0; k < pcount; k++) {
    50           var prop = propNames[k];
    51           if (prop == tagName) {
    52             node[prop] = child.firstChild.data;
    53           }  
    54         }
    55       }    
    56     }
    57   }
    58 }
    60 var enabled = true;
    61 function toggleStyleSheet()
    62 {
    63   if (enabled) {
    64     document.styleSheets[2].disabled = true;
    65   }
    66   else {
    67     document.styleSheets[2].disabled = false;
    68   }
    70   enabled = !enabled;
    71 }
    73 // XXX This is a workaround for a bug where
    74 // changing the disabled state of a stylesheet can't
    75 // be done in an event handler. For now, we do it
    76 // in a zero-delay timeout.
    77 function initiateToggle()
    78 {
    79   setTimeout(toggleStyleSheet, 0);
    80 }
    82 var sortableProps = new Array("Author", "Title", "ISBN");
    83 var books = new Array();
    85 // We uppercase the tagName as a workaround for a bug
    86 // that loses the original case of the tag.
    87 var bookset = document.getElementsByTagName("Book");
    89 // We need to create a "non-live" array to operate on. Since
    90 // we'll be moving things around in this array, we can't use
    91 // the read-only, live one returned by getElementsByTagName.
    92 for (var i=0; i < bookset.length; i++) {
    93   books[i] = bookset[i];
    94 }
    96 collectInfo(books, sortableProps);

mercurial