-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtao.js
151 lines (130 loc) · 5.08 KB
/
tao.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Atto.tao: lightweight HTML templating using Zen Coding syntax
// (see http://code.google.com/p/zen-coding/)
//
// author: Ryan Corradini
// version: 1.0
// date: 29 Oct 2012
// license: MIT
//
/*
Example, from the Zen Coding homepage:
var dom_fragment = tao.expand("div#page>div.logo+ul#navigation>li*5>a");
dom_fragment.toString():
<div id="page">
<div class="logo"></div>
<ul id="navigation">
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>
</div>
*/
define(
["atto/classList"],
function(ClassList) {
"use strict";
window.cl = ClassList;
var _tagRex = /^[a-z]+[1-6]?/i,
_posRex = /[>|<|\+]+/g,
_idRex = /#[_a-z]+[_a-z0-9-]*/i,
_classRex = /\.-?[_a-z]+[_a-z0-9-]*/gi,
_textRex = /{[^}]+}/,
_countRex = /\*[0-9]+/,
_attrsRex = /\[[^\]]*\]/;
function _buildElement(spec) {
var tag = _tagRex.exec(spec),
i, matches, val,
el = tag ? document.createElement(tag[0]) : null,
myClassList = el ? ClassList(el) : null;
if (el) {
// add element attributes
if (_attrsRex.test(spec)) {
// only one; if there are multiples, they're ignored
matches = _attrsRex.exec(spec);
// strip out the attrs clause from the spec so its contents don't get mistaken for a class or id
spec = spec.slice(0,matches.index) + spec.slice(matches.index+matches[0].length);
matches = matches[0].slice(1, -1).split(',');
for (i=0; i<matches.length; i++) {
val = matches[i].split('=');
if (val.length == 2) {
el.setAttribute(val[0], val[1]);
}
}
}
// add element id
if (_idRex.test(spec)) {
matches = _idRex.exec(spec);
el.id = matches[0].slice(1);
}
// add element class(es)
matches = _classRex.exec(spec);
while (matches && matches.length) {
for (i=0; i< matches.length; i++) {
myClassList.add(matches[i].slice(1));
}
matches = _classRex.exec(spec);
}
// add element text node(s)
if (_textRex.test(spec)) {
matches = _textRex.exec(spec);
el.appendChild(document.createTextNode( matches[0].slice(1, -1) ));
}
if (_countRex.test(spec)) {
// TODO: return a list instead of a single node
}
} else {
// no tag; maybe it's a bare text node?
if (_textRex.test(spec)) {
matches = _textRex.exec(spec);
el = document.createTextNode( matches[0].slice(1, -1) );
}
}
return el;
}
// here are the to-be-exposed functions
function _parseTree(expression, serialize) {
var _frag = (serialize) ? document.createElement('div') : document.createDocumentFragment(),
_cur = _frag,
child = null,
i, posCode, tags = expression.split(_posRex);
for (i=0; i<tags.length; i++) {
child = _buildElement(tags[i]);
if (child) {
_cur.appendChild(child);
posCode = _posRex.exec(expression);
if (posCode && posCode.length) {
switch (posCode[0]) {
case '<':
// jump back up to the previous insertion level
_cur = _cur.parentNode;
break;
case '>':
// insert next element into this new child
_cur = child;
break;
case '+':
// no change in insert level
break;
default:
// unrecognized insert level operation
console.log('next posCode:', posCode);
}
}
}
}
return (serialize) ? _frag.innerHTML : _frag;
}
function _createElement(expression) {
var tags = expression.split(_posRex);
return _buildElement(tags[0]);
}
// return available generator functions
return {
expand : _parseTree,
create : _createElement
};
}
);