public
Created

ol reverse attribute polyfill

  • Download Gist
a.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//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
 
//uses these awesomeness:
// Array.prototype.forEach
// Element.prototype.children
//if you want support for older browsers *cough*IE8-*cough*, then use the other
// file provided
(function () {
"use strict";
 
if ( 'reversed' in document.createElement('ol') ) {
return;
}
 
[].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--;
});
}
}());
b.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//use this if you want support for browsers which don't have ES5 goodies
(function () {
if ( 'reversed' in document.createElement('ol') ) {
return;
}
 
var lists = document.getElementsByTagName( 'ol' );
 
for ( var i = 0, len = lists.length; i < len; i++ ) {
if ( lists[i].getAttribute( 'reversed' ) !== null ) {
reverseList( lists[i] );
}
}
 
function reverseList ( list ) {
var children = [], childNodes = list.childNodes,
count = list.getAttribute('start'), i, len;
 
for ( i = 0, len = childNodes.length; i < len; i++ ) {
//Node.ELEMENT_NODE === 1
if ( childNodes[i].nodeType === 1 ) {
children.push( childNodes[i] );
}
}
 
//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 won’t work in browsers that don’t support Array#forEach, which rules out IE < 9. :'(

@mathiasbynens If you want older browsers support, there's the second file in this gist

Ah, duh.

Perhaps give these files more obvious file names than a.js and b.js? :D And perhaps make it so that the b.js is the first file to appear. Or you could add a README.md to the gist to prevent confusing n00bs like me. How about that?

I actually named them a.js and b.js so that the ES5 one will appear first. After all, the ES5 version is clearly the more awesome one.
The README file idea is sound, I'll get right to it.

Edit: Actually, a file isn't necessary. Editing the comment at the top should suffice.

I added support for li value attributes: https://gist.github.com/1896721

What's the license for these code snippets?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.