|
1 | 1 | /*! |
2 | | - * ShortQuery.js v0.3.1-chrome MIT License |
| 2 | + * ShortQuery.js v1.0.0-chrome MIT License |
3 | 3 | * (C) 2015 S <https://github.com/S--Minecraft> |
4 | 4 | */ |
5 | | -/* |
6 | | - DOM Extention |
7 | | - */ |
8 | | - |
9 | 5 | (function() { |
| 6 | + /* |
| 7 | + DOM Extention |
| 8 | + */ |
10 | 9 | var d; |
11 | 10 |
|
12 | 11 | d = Document; |
13 | 12 |
|
14 | 13 | d.prototype.id = d.prototype.getElementById; |
15 | 14 |
|
16 | | - d.prototype["class"] = d.prototype.getElementsByClassName; |
| 15 | + d.prototype.class = d.prototype.getElementsByClassName; |
17 | 16 |
|
18 | 17 | d.prototype.tag = d.prototype.getElementsByTagName; |
19 | 18 |
|
|
23 | 22 |
|
24 | 23 | d.prototype.I = d.prototype.id; |
25 | 24 |
|
26 | | - d.prototype.C = d.prototype["class"]; |
| 25 | + d.prototype.C = d.prototype.class; |
27 | 26 |
|
28 | 27 | d.prototype.T = d.prototype.tag; |
29 | 28 |
|
|
41 | 40 |
|
42 | 41 | }).call(this); |
43 | 42 |
|
44 | | - |
45 | | -/* |
46 | | - DOM Extention |
47 | | - */ |
48 | | - |
49 | 43 | (function() { |
| 44 | + /* |
| 45 | + DOM Extention |
| 46 | + */ |
50 | 47 | var d; |
51 | 48 |
|
52 | 49 | d = DocumentFragment; |
53 | 50 |
|
| 51 | + // child node search |
54 | 52 | d.prototype.id = d.prototype.getElementById; |
55 | 53 |
|
56 | 54 | d.prototype.query = d.prototype.querySelector; |
|
63 | 61 |
|
64 | 62 | d.prototype.$$ = d.prototype.queryAll; |
65 | 63 |
|
66 | | - d.prototype.addLast = d.prototype.appendChild; |
| 64 | + // add/remove node |
| 65 | + d.prototype.addLast = d.prototype.append; |
67 | 66 |
|
68 | | - d.prototype.addFirst = function(a) { |
69 | | - return this.insertBefore(a, this.firstChild); |
70 | | - }; |
| 67 | + d.prototype.addFirst = d.prototype.prepend; |
71 | 68 |
|
72 | 69 | d.prototype.removeChildren = function() { |
73 | 70 | this.textContent = null; |
74 | 71 | return this; |
75 | 72 | }; |
76 | 73 |
|
| 74 | + // get/set family |
77 | 75 | d.prototype.child = function() { |
78 | 76 | return this.children; |
79 | 77 | }; |
80 | 78 |
|
81 | | -}).call(this); |
| 79 | + d.prototype.first = function() { |
| 80 | + return this.firstElementChild; |
| 81 | + }; |
82 | 82 |
|
| 83 | + d.prototype.last = function() { |
| 84 | + return this.lastElementChild; |
| 85 | + }; |
83 | 86 |
|
84 | | -/* |
85 | | - DOM Extention |
86 | | - */ |
| 87 | +}).call(this); |
87 | 88 |
|
88 | 89 | (function() { |
| 90 | + /* |
| 91 | + DOM Extention |
| 92 | + */ |
89 | 93 | var ele; |
90 | 94 |
|
91 | 95 | ele = Element; |
92 | 96 |
|
| 97 | + // child node search |
93 | 98 | ele.prototype.childClass = ele.prototype.getElementsByClassName; |
94 | 99 |
|
95 | 100 | ele.prototype.childTag = ele.prototype.getElementsByTagName; |
|
106 | 111 |
|
107 | 112 | ele.prototype.$$ = ele.prototype.queryAll; |
108 | 113 |
|
109 | | - ele.prototype.addLast = ele.prototype.appendChild; |
| 114 | + // add/remove node |
| 115 | + ele.prototype.addLast = ele.prototype.append; |
110 | 116 |
|
111 | | - ele.prototype.addFirst = function(a) { |
112 | | - return this.insertBefore(a, this.firstChild); |
113 | | - }; |
| 117 | + ele.prototype.addFirst = ele.prototype.prepend; |
114 | 118 |
|
115 | | - ele.prototype.addBefore = function(a) { |
116 | | - return this.parentNode.insertBefore(a, this); |
117 | | - }; |
| 119 | + ele.prototype.addBefore = ele.prototype.before; |
118 | 120 |
|
119 | | - ele.prototype.addAfter = function(a) { |
120 | | - return this.parentNode.insertBefore(a, this.nextSibling); |
121 | | - }; |
| 121 | + ele.prototype.addAfter = ele.prototype.after; |
122 | 122 |
|
123 | 123 | ele.prototype.removeChildren = function() { |
124 | 124 | this.textContent = null; |
125 | 125 | return this; |
126 | 126 | }; |
127 | 127 |
|
| 128 | + // get/set family |
128 | 129 | ele.prototype.parent = function() { |
129 | 130 | return this.parentElement; |
130 | 131 | }; |
|
141 | 142 | return this.nextElementSibling; |
142 | 143 | }; |
143 | 144 |
|
| 145 | + ele.prototype.first = function() { |
| 146 | + return this.firstElementChild; |
| 147 | + }; |
| 148 | + |
| 149 | + ele.prototype.last = function() { |
| 150 | + return this.lastElementChild; |
| 151 | + }; |
| 152 | + |
| 153 | + // get/set node |
144 | 154 | ele.prototype.getAttr = ele.prototype.getAttribute; |
145 | 155 |
|
146 | 156 | ele.prototype.setAttr = ele.prototype.setAttribute; |
|
149 | 159 |
|
150 | 160 | ele.prototype.rmvAttr = ele.prototype.removeAttr; |
151 | 161 |
|
| 162 | + ele.prototype.hasAttr = ele.prototype.hasAttribute; |
| 163 | + |
152 | 164 | ele.prototype.attr = function(a, b) { |
153 | 165 | if (b != null) { |
154 | 166 | return this.setAttr(a, b); |
|
161 | 173 | return this.classList; |
162 | 174 | }; |
163 | 175 |
|
164 | | - ele.prototype.setClass = function(a) { |
165 | | - if (Array.isArray(a)) { |
166 | | - this.className = a.join(" "); |
167 | | - return this.classList; |
168 | | - } else if (a != null) { |
169 | | - this.className = a; |
170 | | - return this.classList; |
171 | | - } |
| 176 | + ele.prototype.setClass = function(...a) { |
| 177 | + this.className = a.join(" "); |
| 178 | + return this; |
172 | 179 | }; |
173 | 180 |
|
174 | | - ele.prototype["class"] = function(a) { |
| 181 | + ele.prototype.class = function(a) { |
175 | 182 | if (a != null) { |
176 | 183 | return this.setClass(a); |
177 | 184 | } else { |
178 | 185 | return this.getClass(); |
179 | 186 | } |
180 | 187 | }; |
181 | 188 |
|
182 | | - ele.prototype.addClass = function(a) { |
183 | | - this.classList.add(a); |
184 | | - return this.classList; |
| 189 | + // extention |
| 190 | + ele.prototype.addClass = function(...a) { |
| 191 | + this.classList.add(...a); |
| 192 | + return this; |
185 | 193 | }; |
186 | 194 |
|
187 | | - ele.prototype.removeClass = function(a) { |
188 | | - this.classList.remove(a); |
189 | | - return this.classList; |
| 195 | + ele.prototype.removeClass = function(...a) { |
| 196 | + this.classList.remove(...a); |
| 197 | + return this; |
190 | 198 | }; |
191 | 199 |
|
| 200 | + if (DOMTokenList.prototype.replace != null) { |
| 201 | + ele.prototype.replaceClass = function(a, b) { |
| 202 | + this.classList.replace(a, b); |
| 203 | + return this; |
| 204 | + }; |
| 205 | + } else { |
| 206 | + ele.prototype.replaceClass = function(a, b) { |
| 207 | + if (this.classList.has(a)) { |
| 208 | + this.classList.remove(a); |
| 209 | + this.classList.add(b); |
| 210 | + } |
| 211 | + return this; |
| 212 | + }; |
| 213 | + } |
| 214 | + |
192 | 215 | ele.prototype.toggleClass = function(a) { |
193 | 216 | this.classList.toggle(a); |
194 | | - return this.classList; |
| 217 | + return this; |
195 | 218 | }; |
196 | 219 |
|
197 | 220 | ele.prototype.hasClass = function(a) { |
|
200 | 223 |
|
201 | 224 | }).call(this); |
202 | 225 |
|
203 | | - |
204 | | -/* |
205 | | - Event |
206 | | - */ |
207 | | - |
208 | 226 | (function() { |
| 227 | + /* |
| 228 | + Event |
| 229 | + */ |
209 | 230 | var e; |
210 | 231 |
|
211 | 232 | e = EventTarget; |
|
221 | 242 |
|
222 | 243 | exports = this; |
223 | 244 |
|
| 245 | + // shorthands |
224 | 246 | d = document; |
225 | 247 |
|
226 | | - |
227 | | - /* |
228 | | - shortQuery |
229 | | - */ |
230 | | - |
231 | 248 | exports.shortQuery = (function() { |
232 | 249 | var ctor; |
233 | 250 |
|
234 | | - function shortQuery() { |
235 | | - return ctor.apply(this, arguments); |
236 | | - } |
| 251 | + /* |
| 252 | + shortQuery |
| 253 | + */ |
| 254 | + class shortQuery { |
| 255 | + constructor() { |
| 256 | + return ctor.apply(this, arguments); |
| 257 | + } |
| 258 | + |
| 259 | + }; |
237 | 260 |
|
238 | 261 | ctor = d.querySelectorAll.bind(d); |
239 | 262 |
|
240 | 263 | shortQuery.id = d.getElementById.bind(d); |
241 | 264 |
|
242 | | - shortQuery["class"] = d.getElementsByClassName.bind(d); |
| 265 | + shortQuery.class = d.getElementsByClassName.bind(d); |
243 | 266 |
|
244 | 267 | shortQuery.tag = d.getElementsByTagName.bind(d); |
245 | 268 |
|
|
249 | 272 |
|
250 | 273 | shortQuery.I = shortQuery.id; |
251 | 274 |
|
252 | | - shortQuery.C = shortQuery["class"]; |
| 275 | + shortQuery.C = shortQuery.class; |
253 | 276 |
|
254 | 277 | shortQuery.T = shortQuery.tag; |
255 | 278 |
|
256 | 279 | shortQuery.$ = shortQuery.query; |
257 | 280 |
|
258 | 281 | shortQuery.$$ = shortQuery.queryAll; |
259 | 282 |
|
| 283 | + // create node |
260 | 284 | shortQuery.create = d.createElement.bind(d); |
261 | 285 |
|
262 | 286 | shortQuery.createFragment = d.createDocumentFragment.bind(d); |
263 | 287 |
|
264 | 288 | return shortQuery; |
265 | 289 |
|
266 | | - })(); |
| 290 | + }).call(this); |
267 | 291 |
|
268 | 292 | exports.$$ = shortQuery; |
269 | 293 |
|
|
0 commit comments