kupriyanenko / jbone

JavaScript Library for Events and DOM manipulation. Replaces jQuery for Backbone (2.5kb gzipped)
http://jbone.js.org
MIT License
279 stars 35 forks source link

Update attributes.js #36

Closed Archiolidius closed 9 years ago

Archiolidius commented 9 years ago

IE 10 does not support elem.dataset It is fix for <IE 11

kupriyanenko commented 9 years ago

Hi @Archiolidius,

If you want to fix it, need to replace using of .dataset in all cases (get and set data). But thanks, I will think about it.

Like another way you can use shim for .dataset:

if (!document.documentElement.dataset &&
         // FF is empty while IE gives empty object
        (!Object.getOwnPropertyDescriptor(Element.prototype, 'dataset')      ||
         !Object.getOwnPropertyDescriptor(Element.prototype, 'dataset').get)
    ) {
    var propDescriptor = {
        get: function () {
            'use strict';
            var i,
                HTML5_DOMStringMap = {},
                attrVal, attrName, propName,
                attribute,
                attributes = this.attributes,
                attsLength = attributes.length,
                toUpperCase = function (n0) {
                    return n0.charAt(1).toUpperCase();
                },
                getter = function () {
                    return this;
                },
                setter = function (attrName, value) {
                    return (typeof value !== 'undefined') ?
                        this.setAttribute(attrName, value) :
                        this.removeAttribute(attrName);
                };
            for (i = 0; i < attsLength; i++) {
                attribute = attributes[i];
                // Fix: This test really should allow any XML Name without
                //         colons (and non-uppercase for XHTML)
                if (attribute && attribute.name &&
                    (/^data-\w[\w\-]*$/).test(attribute.name)) {
                    attrVal = attribute.value;
                    attrName = attribute.name;
                    // Change to CamelCase
                    propName = attrName.substr(5).replace(/-./g, toUpperCase);
                    HTML5_DOMStringMap[propName] = attrVal;
                }
            }
            return HTML5_DOMStringMap;
        }
    };

    Object.defineProperty(Element.prototype, 'dataset', propDescriptor);
}
henryruhs commented 9 years ago

I suggest to merge or close the pull request.