forked from evil-icons/evil-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evil-icons.js
76 lines (59 loc) · 1.8 KB
/
evil-icons.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
var fs = require('fs');
var spritePath = __dirname + '/assets/sprite.svg';
var sprite = fs.readFileSync(spritePath).toString();
function icon(name, options) {
var options = options || {};
var size = options.size ? ' icon--' + options.size : '';
var classes = 'icon icon--' + name + size + ' ' + (options.class || '');
classes = classes.trim();
var icon = '<svg class="icon__cnt">' +
'<use xlink:href="#' + name + '-icon" />' +
'</svg>';
var html = '<div class="' + classes + '">' +
wrapSpinner(icon, classes) +
'</div>';
return html;
}
function wrapSpinner(html, klass) {
if (klass.indexOf('spinner') > -1) {
return '<div class="icon__spinner">' + html + '</div>';
} else {
return html;
}
}
function buildParamsFromString(string) {
var match, attr, value;
var params = {};
var attrsRegexp = /(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/gi;
while (match = attrsRegexp.exec(string)) {
attr = match[1];
value = match[2].replace(/'|"/, '');
params[attr] = value;
}
return params;
}
function replaceIconTags(src) {
var match, tag, params, name;
var html = src.toString();
var iconRegexp = /<icon\s+([-=\w\d'"\s]+)\s*\/?>(<\/icon>)?/gi;
while (match = iconRegexp.exec(html)) {
tag = match[0];
params = buildParamsFromString(match[1]);
name = params.name;
delete params.name;
html = html.replace(tag, icon(name, params));
}
return html;
}
function iconizeHtml(src) {
var html = src.toString();
if (html.indexOf(sprite) == -1) {
html = html.replace(/<body.*?>/, function(match) { return match + sprite });
}
return replaceIconTags(html);
}
module.exports = {
iconizeHtml: iconizeHtml,
sprite: sprite,
icon: icon
};