Advertisement
Guest User

HTML5 ol polyfill

a guest
Feb 21st, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (!('reversed' in document.createElement('ol'))) {
  2.     (function () {
  3.         'use strict';
  4.         // Run the code on each ordered list with a "reversed" attribute.
  5.         var lists = document.getElementsByTagName('ol'),    // All ol elements
  6.             length = lists.length,  // Number of ol elements
  7.             i,                      // Counter var
  8.             j,                      // Counter var
  9.             child,                  // ol element
  10.             currChildren,           // li elements for a given ol element
  11.             childrenLength,         // Number of li elements
  12.             start,                  // Value of ol start attribute
  13.             currCount,              // Value to be used in li value attribute
  14.             val;                    // Value from li value attribute
  15.         for (i = 0; i < length; i++) {
  16.             child = lists[i];
  17.             if (child.getAttribute('reversed') !== null) {
  18.                 currChildren = child.getElementsByTagName('li');
  19.                 childrenLength = currChildren.length;
  20.                 start = child.getAttribute('start');
  21.                 // Check the existence of the start attribute and if it's
  22.                 // a number.
  23.                 if (start !== null && !isNaN(start)) {
  24.                     currCount = start;
  25.                 } else {
  26.                     // Do this if the start attribute is not present. The first
  27.                     // number is derived from the number of list items.
  28.                     // (Ugh; Do we need double loops to get the correct count?)
  29.                     currCount = 0;
  30.                     for (j = 0; j < childrenLength; j++) {
  31.                         if (currChildren[j].parentNode === child) {
  32.                             currCount++;
  33.                         }
  34.                     }
  35.                 }
  36.                 // Go through each list item, adding the 'value' attribute
  37.                 // plus currCount number then subtract one from currCount
  38.                 // so we're ready for the next one.
  39.                 for (j = 0; j < childrenLength; j++) {
  40.                     if (currChildren[j].parentNode === child) {
  41.                         // Per spec, if set, the value attribute should be used
  42.                         // and renumbering started from there.
  43.                         val = currChildren[j].getAttribute('value');
  44.                         if (val !== null && !isNaN(val)) {
  45.                             currCount = val;
  46.                         }
  47.                         currChildren[j].setAttribute('value', currCount);
  48.                         currCount -= 1;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }());
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement