1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = createElement;
|
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 | function createElement() {
|
30 | var querySelector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'div';
|
31 |
|
32 | var nodeType = querySelector.match(/^[a-z]+/i);
|
33 | var id = querySelector.match(/#([a-z]+[a-z0-9-]*)/gi);
|
34 | var classes = querySelector.match(/\.([a-z]+[a-z0-9-]*)/gi);
|
35 | var attributes = querySelector.match(/\[([a-z][a-z-]+)(=['|"]?([^\]]*)['|"]?)?\]/gi);
|
36 | var node = nodeType ? nodeType[0] : 'div';
|
37 |
|
38 | if (id && id.length > 1) {
|
39 | throw CreateElementException('only 1 ID is allowed');
|
40 | }
|
41 |
|
42 | var elt = document.createElement(node);
|
43 |
|
44 | if (id) {
|
45 | elt.id = id[0].replace('#', '');
|
46 | }
|
47 |
|
48 | if (classes) {
|
49 | var attrClasses = classes.join(' ').replace(/\./g, '');
|
50 | elt.setAttribute('class', attrClasses);
|
51 | }
|
52 |
|
53 | if (attributes) {
|
54 | attributes.forEach(function (item) {
|
55 | item = item.slice(0, -1).slice(1);
|
56 |
|
57 | var _item$split = item.split('='),
|
58 | label = _item$split[0],
|
59 | value = _item$split[1];
|
60 |
|
61 | if (value) {
|
62 | value = value.replace(/^['"](.*)['"]$/, '$1');
|
63 | }
|
64 | elt.setAttribute(label, value || '');
|
65 | });
|
66 | }
|
67 |
|
68 | for (var _len = arguments.length, content = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
69 | content[_key - 1] = arguments[_key];
|
70 | }
|
71 |
|
72 | content.forEach(function (item) {
|
73 | if (typeof item === 'string' || typeof item === 'number') {
|
74 | elt.appendChild(document.createTextNode(item));
|
75 | } else if (item.nodeType === document.ELEMENT_NODE) {
|
76 | elt.appendChild(item);
|
77 | }
|
78 | });
|
79 |
|
80 | return elt;
|
81 | }
|
82 |
|
83 | function CreateElementException(message) {
|
84 | this.message = message;
|
85 | this.name = 'CreateElementException';
|
86 | }
|
87 | module.exports = exports['default']; |
\ | No newline at end of file |