1 |
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | module.exports = function () {
|
6 | if (!document.documentElement.dataset &&
|
7 | (
|
8 | !Object.getOwnPropertyDescriptor(Element.prototype, 'dataset') ||
|
9 | !Object.getOwnPropertyDescriptor(Element.prototype, 'dataset').get
|
10 | )
|
11 | ) {
|
12 | const descriptor = {}
|
13 |
|
14 | descriptor.enumerable = true
|
15 |
|
16 | descriptor.get = function () {
|
17 | const element = this
|
18 | const map = {}
|
19 | const attributes = this.attributes
|
20 |
|
21 | function toUpperCase (n0) {
|
22 | return n0.charAt(1).toUpperCase()
|
23 | }
|
24 |
|
25 | function getter () {
|
26 | return this.value
|
27 | }
|
28 |
|
29 | function setter (name, value) {
|
30 | if (typeof value !== 'undefined') {
|
31 | this.setAttribute(name, value)
|
32 | }
|
33 | else {
|
34 | this.removeAttribute(name)
|
35 | }
|
36 | }
|
37 |
|
38 | for (let index = 0; index < attributes.length; index++) {
|
39 | const attribute = attributes[index]
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | if (attribute && attribute.name && (/^data-\w[\w\-]*$/).test(attribute.name)) {
|
45 | const name = attribute.name
|
46 | const value = attribute.value
|
47 |
|
48 |
|
49 |
|
50 | const propName = name.substr(5).replace(/-./g, toUpperCase)
|
51 |
|
52 | Object.defineProperty(map, propName, {
|
53 | enumerable: this.enumerable,
|
54 | get: getter.bind({value: value || ''}),
|
55 | set: setter.bind(element, name)
|
56 | })
|
57 | }
|
58 | }
|
59 | return map
|
60 | }
|
61 |
|
62 | Object.defineProperty(Element.prototype, 'dataset', descriptor)
|
63 | }
|
64 | }
|