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