-
-
Save Raynos/1671740 to your computer and use it in GitHub Desktop.
ol reverse attribute polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//use this if you want support for browsers which don't have ES5 goodies | |
(function () { | |
"use strict"; | |
var lists = document.getElementsByTagName( 'ol' ), list; | |
for ( var i = 0, len = lists.length; i < len; i++ ) { | |
// bug 1 not local variable | |
list = lists[i]; | |
if ( list.getAttribute( 'reversed' ) !== null ) { | |
reverseList( list ); | |
} | |
} | |
function reverseList ( list ) { | |
var children = [], childNodes = list.childNodes, | |
count = list.getAttribute('start'), i, len, node; | |
for ( i = 0, len = childNodes.length; i < len; i++ ) { | |
// Bug 2 not caching local variable | |
node = childNodes[i]; | |
// Bug 3 Node.ELEMENT_NODE not cross browser | |
if ( node.nodeType === 1 /* Node.ELEMENT_NODE */ ) { | |
children.push( node ); | |
} | |
} | |
//check to see if a start attribute is provided | |
if ( count !== null ) { | |
count = Number( count ); | |
if ( isNaN(count) ) { | |
count = null; | |
} | |
} | |
//no, this isn't duplication - start will be set to null | |
// in the previous if statement if an invalid start attribute | |
// is provided | |
if ( count === null ) { | |
count = children.length; | |
} | |
for ( i = 0, len = children.length; i < len; i++ ) { | |
children[ i ].value = count--; | |
} | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a polyfill for the ordered-list reversed attribute | |
// http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#the-ol-element | |
// http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#dom-li-value | |
(function () { | |
"use strict"; | |
[].forEach.call( document.getElementsByTagName('ol'), function ( list ) { | |
if ( list.getAttribute( 'reversed' ) !== null ) { | |
reverseList( list ); | |
} | |
}); | |
function reverseList ( list ) { | |
var children = list.children, count = list.getAttribute('start'); | |
//check to see if a start attribute is provided | |
if ( count !== null ) { | |
count = Number( count ); | |
if ( isNaN(count) ) { | |
count = null; | |
} | |
} | |
//no, this isn't duplication - start will be set to null | |
// in the previous if statement if an invalid start attribute | |
// is provided | |
if ( count === null ) { | |
count = children.length; | |
} | |
[].forEach.call( children, function ( child ) { | |
child.value = count--; | |
}); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment