This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
prefixer.js
105 lines (84 loc) · 2.64 KB
/
prefixer.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
angular
.module('material.core')
.config(function($provide) {
$provide.decorator('$mdUtil', ['$delegate', function ($delegate) {
// Inject the prefixer into our original $mdUtil service.
$delegate.prefixer = MdPrefixer;
return $delegate;
}]);
});
/**
* @param {string|string[]} initialAttributes
* @param {boolean} buildSelector
* @return {string|string[]|{buildSelector: (function(string|string[]): string),
* buildList: (function(string|string[]): string[]),
* hasAttribute: (function(JQLite|Element, string): HTMLElement),
* removeAttribute: (function(JQLite|Element, string): void)}}
* @constructor
*/
function MdPrefixer(initialAttributes, buildSelector) {
var PREFIXES = ['data', 'x'];
if (initialAttributes) {
// The prefixer also accepts attributes as a parameter, and immediately builds a list or selector for
// the specified attributes.
return buildSelector ? _buildSelector(initialAttributes) : _buildList(initialAttributes);
}
return {
buildList: _buildList,
buildSelector: _buildSelector,
hasAttribute: _hasAttribute,
removeAttribute: _removeAttribute
};
function _buildList(attributes) {
attributes = angular.isArray(attributes) ? attributes : [attributes];
attributes.forEach(function(item) {
PREFIXES.forEach(function(prefix) {
attributes.push(prefix + '-' + item);
});
});
return attributes;
}
function _buildSelector(attributes) {
attributes = angular.isArray(attributes) ? attributes : [attributes];
return _buildList(attributes)
.map(function(item) {
return '[' + item + ']';
})
.join(',');
}
function _hasAttribute(element, attribute) {
element = _getNativeElement(element);
if (!element) {
return false;
}
var prefixedAttrs = _buildList(attribute);
for (var i = 0; i < prefixedAttrs.length; i++) {
if (element.hasAttribute(prefixedAttrs[i])) {
return true;
}
}
return false;
}
function _removeAttribute(element, attribute) {
element = _getNativeElement(element);
if (!element) {
return;
}
_buildList(attribute).forEach(function(prefixedAttribute) {
element.removeAttribute(prefixedAttribute);
});
}
/**
* Transforms a jqLite or DOM element into a HTML element.
* This is useful when supporting jqLite elements and DOM elements at
* same time.
* @param element {JQLite|Element} Element to be parsed
* @returns {HTMLElement} Parsed HTMLElement
*/
function _getNativeElement(element) {
element = element[0] || element;
if (element.nodeType) {
return element;
}
}
}