1/* 2--- 3 4name: data-mootools 5 6description: An API for access to data-attributes in elements. 7 8authors: [Dimitar Christoff] 9 10license: MIT-style license. 11 12provides: Element.data 13 14requires: 15 - Core/Element 16 17... 18*/ 19/*jshint mootools:true */ 20;(function(){ 21 'use strict'; 22 23 var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/; 24 25 function formatDataProperty(prop){ 26 return prop.replace('data-', '').camelCase(); 27 } 28 29 var wrap = function(){ 30 31 [Document, Element].invoke('implement', { 32 33 data: function(property, force){ 34 var data = this.retrieve('dataCollection'), 35 ii = 0, 36 len, 37 hasData = false, 38 attribs; 39 40 if (!data || force === true) { 41 data = {}; 42 attribs = this.attributes || []; 43 for (len = attribs.length; ii < len; ++ii) { 44 if (attribs[ii].name.indexOf('data-') === 0) { 45 data[formatDataProperty(attribs[ii].name)] = attribs[ii].value.test(rbrace) 46 ? JSON.parse(attribs[ii].value) 47 : attribs[ii].value; 48 49 hasData = true; 50 } 51 } 52 53 if (!hasData) 54 data = null; 55 56 this.store('dataCollection', data); 57 } 58 else { 59 hasData = true; 60 } 61 62 return property ? hasData && data[formatDataProperty(property)] || null : data; 63 } 64 }); 65 }; 66 67 if (typeof define === 'function' && define.amd) { 68 // returns an empty module 69 define(wrap); 70 } 71 else { 72 wrap(); 73 } 74}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.