1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import util from '../ons/util.js';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const prefix = (function() {
|
27 | const styles = window.getComputedStyle(document.documentElement, '');
|
28 | const prefix = (Array.prototype.slice
|
29 | .call(styles)
|
30 | .join('')
|
31 | .match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o'])
|
32 | )[1];
|
33 |
|
34 | return function(name) {
|
35 | return '-' + prefix + '-' + util.hyphenate(name);
|
36 | };
|
37 | })();
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const styler = function(element, style) {
|
49 | Object.keys(style).forEach(function(key) {
|
50 | if (key in element.style) {
|
51 | element.style[key] = style[key];
|
52 | } else if (prefix(key) in element.style) {
|
53 | element.style[prefix(key)] = style[key];
|
54 | } else {
|
55 | util.warn('No such style property: ' + key);
|
56 | }
|
57 | });
|
58 | return element;
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | styler.clear = function(element, styles = '') {
|
66 | const clearlist = styles.split(/\s+/).reduce((r, s) => r.concat([util.hyphenate(s), prefix(s)]), []),
|
67 | keys = [];
|
68 |
|
69 | for (let i = element.style.length - 1; i >= 0; i--) {
|
70 | const key = element.style[i];
|
71 | if (clearlist.length === 0 || clearlist.some(s => key.indexOf(s) === 0)) {
|
72 | keys.push(key);
|
73 | }
|
74 | }
|
75 |
|
76 | keys.forEach(key => element.style[key] = '');
|
77 | element.getAttribute('style') === '' && element.removeAttribute('style');
|
78 | };
|
79 |
|
80 | export default styler;
|