├── _config.yml ├── .gitignore ├── coverage ├── lcov-report │ ├── sort-arrow-sprite.png │ ├── prettify.css │ ├── index.html │ ├── cjs │ │ ├── index.html │ │ └── index.js.html │ ├── sorter.js │ ├── base.css │ └── prettify.js ├── lcov.info └── coverage.json ├── .travis.yml ├── .npmignore ├── test ├── ubl.js ├── browser.js ├── index.js └── index.html ├── rollup.config.js ├── LICENSE ├── package.json ├── base.html ├── min.js ├── README.md ├── esm └── index.js ├── cjs └── index.js └── index.js /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | package-lock.json -------------------------------------------------------------------------------- /coverage/lcov-report/sort-arrow-sprite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WebReflection/query-result/HEAD/coverage/lcov-report/sort-arrow-sprite.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | git: 5 | depth: 1 6 | branches: 7 | only: 8 | - master 9 | after_success: 10 | - "npm run coveralls" -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | coverage/* 2 | node_modules/* 3 | test/* 4 | _config.yml 5 | .DS_Store 6 | .gitignore 7 | .npmignore 8 | .travis.yml 9 | base.html 10 | package-lock.json 11 | rollup.config.js 12 | -------------------------------------------------------------------------------- /test/ubl.js: -------------------------------------------------------------------------------- 1 | // https://github.com/WebReflection/ubl 2 | (function(g,d,s){if(!g.ESM){ 3 | s=d.documentElement;s=s.insertBefore(d.createElement('script'),s.lastChild); 4 | s.defer=!0;s.type='text/javascript';s.src='../min.js'; 5 | }}(window,document)); 6 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel'; 2 | 3 | export default { 4 | input: 'esm/index.js', 5 | output: { 6 | file: 'index.js', 7 | format: 'iife', 8 | name: '$' 9 | }, 10 | plugins: [ 11 | babel({ 12 | babelrc: false, 13 | presets: ['@babel/preset-env'] 14 | }) 15 | ] 16 | }; -------------------------------------------------------------------------------- /test/browser.js: -------------------------------------------------------------------------------- 1 | if (!console.assert) 2 | console.assert = console.log; 3 | $(function (event) { 4 | console.assert(!!event, 'ready triggered'); 5 | $(function () { 6 | var children = $('p'); 7 | console.assert(children.length === 2, 'qSA works'); 8 | console.assert($(children).length === 2, 'qr works'); 9 | console.assert($('p:first,a:first').length === 1, 'qS works'); 10 | console.assert(children instanceof $, 'instanceof works'); 11 | console.assert(children.map(Object) instanceof $, 'map works'); 12 | $('html')[0].style.background = '#55FF99'; 13 | $('body')[0].textContent = 'OK'; 14 | }); 15 | }); -------------------------------------------------------------------------------- /coverage/lcov-report/prettify.css: -------------------------------------------------------------------------------- 1 | .pln{color:#000}@media screen{.str{color:#080}.kwd{color:#008}.com{color:#800}.typ{color:#606}.lit{color:#066}.pun,.opn,.clo{color:#660}.tag{color:#008}.atn{color:#606}.atv{color:#080}.dec,.var{color:#606}.fun{color:red}}@media print,projection{.str{color:#060}.kwd{color:#006;font-weight:bold}.com{color:#600;font-style:italic}.typ{color:#404;font-weight:bold}.lit{color:#044}.pun,.opn,.clo{color:#440}.tag{color:#006;font-weight:bold}.atn{color:#404}.atv{color:#060}}pre.prettyprint{padding:2px;border:1px solid #888}ol.linenums{margin-top:0;margin-bottom:0}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}li.L1,li.L3,li.L5,li.L7,li.L9{background:#eee} 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2018, Andrea Giammarchi, @WebReflection 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 | OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const tressa = require('tressa'); 2 | const {CustomEvent, Document} = require('basicHTML'); 3 | 4 | global.CustomEvent = CustomEvent; 5 | global.document = new Document; 6 | 7 | global.postMessage = function () {}; 8 | global.addEventListener = global.postMessage; 9 | global.removeEventListener = global.postMessage; 10 | 11 | const $ = require('../cjs/index.js').default; 12 | 13 | document.body.innerHTML = '
1
2
'; 14 | document.readyState = 'loading'; 15 | 16 | tressa.title('query-result'); 17 | 18 | $(event => { 19 | tressa.assert(!!event, 'ready triggered'); 20 | document.readyState = 'whatever'; 21 | document.documentElement.doScroll = false; 22 | $(() => { 23 | const children = $('p'); 24 | tressa.assert(children.length === 2, 'qSA works'); 25 | tressa.assert($(children).length === 2, 'qr works'); 26 | tressa.assert($('p:first,a:first').length === 1, 'qS works'); 27 | tressa.assert(children instanceof $, 'instanceof works'); 28 | tressa.assert(children.map(Object) instanceof $, 'map works'); 29 | }); 30 | }); 31 | 32 | $(document).dispatch('DOMContentLoaded'); 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.5", 3 | "license": "ISC", 4 | "name": "query-result", 5 | "description": "Rethinking the $", 6 | "homepage": "https://github.com/WebReflection/query-result", 7 | "keywords": [ 8 | "$", 9 | "jQuery", 10 | "Zepto", 11 | "like", 12 | "light", 13 | "modern", 14 | "utility", 15 | "DOM" 16 | ], 17 | "author": { 18 | "name": "Andrea Giammarchi", 19 | "web": "http://webreflection.blogspot.com/" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git://github.com/WebReflection/query-result.git" 24 | }, 25 | "main": "cjs/index.js", 26 | "module": "esm/index.js", 27 | "unpkg": "min.js", 28 | "scripts": { 29 | "build": "npm run cjs && npm run max && npm run min && npm run size && npm run test", 30 | "cjs": "ascjs esm cjs", 31 | "coveralls": "cat ./coverage/lcov.info | coveralls", 32 | "max": "rollup -c", 33 | "min": "echo '/*! (c) Andrea Giammarchi - ISC */'>tmp.js && cat index.js >> tmp.js && uglifyjs tmp.js --support-ie8 --comments=/^!/ -c -m -o min.js && rm tmp.js", 34 | "size": "cat min.js | wc -c;gzip -c9 min.js | wc -c;cat min.js | brotli | wc -c && rm -f min.js.br", 35 | "test": "istanbul cover test/index.js" 36 | }, 37 | "devDependencies": { 38 | "@babel/core": "^7.1.0", 39 | "@babel/preset-env": "^7.1.0", 40 | "ascjs": "^2.5.1", 41 | "basichtml": "^0.19.0", 42 | "istanbul": "^0.4.5", 43 | "rollup": "^0.66.2", 44 | "rollup-plugin-babel": "^4.0.3", 45 | "tressa": "^0.3.1", 46 | "uglify-js": "^2.8.29" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |1
2
42 | 43 | -------------------------------------------------------------------------------- /coverage/lcov.info: -------------------------------------------------------------------------------- 1 | TN: 2 | SF:/Users/agiammarchi/git/query-result/cjs/index.js 3 | FN:33,(anonymous_1) 4 | FN:76,$ 5 | FN:88,(anonymous_3) 6 | FN:105,dispatch 7 | FN:112,off 8 | FN:118,on 9 | FNF:6 10 | FNH:6 11 | FNDA:0,(anonymous_1) 12 | FNDA:10,$ 13 | FNDA:2,(anonymous_3) 14 | FNDA:2,dispatch 15 | FNDA:4,off 16 | FNDA:4,on 17 | DA:20,1 18 | DA:21,1 19 | DA:22,1 20 | DA:23,1 21 | DA:24,1 22 | DA:25,1 23 | DA:28,1 24 | DA:29,1 25 | DA:30,1 26 | DA:31,1 27 | DA:32,1 28 | DA:33,1 29 | DA:34,1 30 | DA:35,1 31 | DA:38,1 32 | DA:41,1 33 | DA:42,15 34 | DA:45,1 35 | DA:46,1 36 | DA:47,1 37 | DA:49,1 38 | DA:50,1 39 | DA:51,2 40 | DA:52,2 41 | DA:53,2 42 | DA:54,3 43 | DA:55,3 44 | DA:56,2 45 | DA:57,2 46 | DA:59,1 47 | DA:61,2 48 | DA:63,1 49 | DA:68,1 50 | DA:69,1 51 | DA:70,1 52 | DA:71,1 53 | DA:72,1 54 | DA:76,1 55 | DA:77,10 56 | DA:78,2 57 | DA:81,6 58 | DA:82,6 59 | DA:83,6 60 | DA:84,6 61 | DA:86,2 62 | DA:87,2 63 | DA:88,2 64 | DA:89,2 65 | DA:90,2 66 | DA:91,2 67 | DA:93,2 68 | DA:94,2 69 | DA:95,2 70 | DA:96,2 71 | DA:97,1 72 | DA:98,2 73 | DA:101,1 74 | DA:102,1 75 | DA:103,3 76 | DA:105,1 77 | DA:106,2 78 | DA:107,2 79 | DA:108,2 80 | DA:109,2 81 | DA:110,2 82 | DA:113,4 83 | DA:114,4 84 | DA:115,4 85 | DA:116,4 86 | DA:119,4 87 | DA:120,4 88 | DA:121,4 89 | DA:122,4 90 | DA:125,1 91 | LF:74 92 | LH:74 93 | BRDA:28,1,0,0 94 | BRDA:28,1,1,1 95 | BRDA:31,2,0,0 96 | BRDA:31,2,1,0 97 | BRDA:35,3,0,0 98 | BRDA:35,3,1,0 99 | BRDA:41,4,0,1 100 | BRDA:41,4,1,0 101 | BRDA:55,5,0,2 102 | BRDA:55,5,1,1 103 | BRDA:57,6,0,1 104 | BRDA:57,6,1,1 105 | BRDA:63,7,0,1 106 | BRDA:63,7,1,0 107 | BRDA:77,8,0,2 108 | BRDA:77,8,1,6 109 | BRDA:77,8,2,2 110 | BRDA:82,9,0,5 111 | BRDA:82,9,1,1 112 | BRDA:82,10,0,6 113 | BRDA:82,10,1,3 114 | BRDA:96,11,0,1 115 | BRDA:96,11,1,1 116 | BRDA:96,12,0,2 117 | BRDA:96,12,1,2 118 | BRDA:96,12,2,1 119 | BRF:26 120 | BRH:26 121 | end_of_record 122 | -------------------------------------------------------------------------------- /base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 || File | 54 |55 | | Statements | 56 |57 | | Branches | 58 |59 | | Functions | 60 |61 | | Lines | 62 |63 | |
|---|---|---|---|---|---|---|---|---|---|
| cjs/ | 67 |100% | 69 |76/76 | 70 |100% | 71 |26/26 | 72 |100% | 73 |6/6 | 74 |100% | 75 |74/74 | 76 |
| File | 54 |55 | | Statements | 56 |57 | | Branches | 58 |59 | | Functions | 60 |61 | | Lines | 62 |63 | |
|---|---|---|---|---|---|---|---|---|---|
| index.js | 67 |100% | 69 |76/76 | 70 |100% | 71 |26/26 | 72 |100% | 73 |6/6 | 74 |100% | 75 |74/74 | 76 |
| 1 51 | 2 52 | 3 53 | 4 54 | 5 55 | 6 56 | 7 57 | 8 58 | 9 59 | 10 60 | 11 61 | 12 62 | 13 63 | 14 64 | 15 65 | 16 66 | 17 67 | 18 68 | 19 69 | 20 70 | 21 71 | 22 72 | 23 73 | 24 74 | 25 75 | 26 76 | 27 77 | 28 78 | 29 79 | 30 80 | 31 81 | 32 82 | 33 83 | 34 84 | 35 85 | 36 86 | 37 87 | 38 88 | 39 89 | 40 90 | 41 91 | 42 92 | 43 93 | 44 94 | 45 95 | 46 96 | 47 97 | 48 98 | 49 99 | 50 100 | 51 101 | 52 102 | 53 103 | 54 104 | 55 105 | 56 106 | 57 107 | 58 108 | 59 109 | 60 110 | 61 111 | 62 112 | 63 113 | 64 114 | 65 115 | 66 116 | 67 117 | 68 118 | 69 119 | 70 120 | 71 121 | 72 122 | 73 123 | 74 124 | 75 125 | 76 126 | 77 127 | 78 128 | 79 129 | 80 130 | 81 131 | 82 132 | 83 133 | 84 134 | 85 135 | 86 136 | 87 137 | 88 138 | 89 139 | 90 140 | 91 141 | 92 142 | 93 143 | 94 144 | 95 145 | 96 146 | 97 147 | 98 148 | 99 149 | 100 150 | 101 151 | 102 152 | 103 153 | 104 154 | 105 155 | 106 156 | 107 157 | 108 158 | 109 159 | 110 160 | 111 161 | 112 162 | 113 163 | 114 164 | 115 165 | 116 166 | 117 167 | 118 168 | 119 169 | 120 170 | 121 171 | 122 172 | 123 173 | 124 174 | 125 175 | 126 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 1× 195 | 1× 196 | 1× 197 | 1× 198 | 1× 199 | 1× 200 | 201 | 202 | 1× 203 | 1× 204 | 1× 205 | 1× 206 | 1× 207 | 1× 208 | 1× 209 | 1× 210 | 211 | 212 | 1× 213 | 214 | 215 | 1× 216 | 15× 217 | 218 | 219 | 1× 220 | 1× 221 | 1× 222 | 223 | 1× 224 | 1× 225 | 2× 226 | 2× 227 | 2× 228 | 3× 229 | 3× 230 | 2× 231 | 2× 232 | 233 | 1× 234 | 235 | 2× 236 | 237 | 1× 238 | 239 | 240 | 241 | 242 | 1× 243 | 1× 244 | 1× 245 | 1× 246 | 1× 247 | 248 | 249 | 250 | 1× 251 | 10× 252 | 2× 253 | 254 | 255 | 6× 256 | 6× 257 | 6× 258 | 6× 259 | 260 | 2× 261 | 2× 262 | 2× 263 | 2× 264 | 2× 265 | 2× 266 | 267 | 2× 268 | 2× 269 | 2× 270 | 2× 271 | 1× 272 | 2× 273 | 274 | 275 | 1× 276 | 1× 277 | 3× 278 | 279 | 1× 280 | 2× 281 | 2× 282 | 2× 283 | 2× 284 | 2× 285 | 286 | 287 | 4× 288 | 4× 289 | 4× 290 | 4× 291 | 292 | 293 | 4× 294 | 4× 295 | 4× 296 | 4× 297 | 298 | 299 | 1× 300 | | 'use strict';
301 | /**
302 | * ISC License
303 | *
304 | * Copyright (c) 2018, Andrea Giammarchi, @WebReflection
305 | *
306 | * Permission to use, copy, modify, and/or distribute this software for any
307 | * purpose with or without fee is hereby granted, provided that the above
308 | * copyright notice and this permission notice appear in all copies.
309 | *
310 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
311 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
312 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
313 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
314 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
315 | * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
316 | * PERFORMANCE OF THIS SOFTWARE.
317 | */
318 | class QueryResult extends Array {}
319 | const {create, defineProperty} = Object;
320 | const AP = Array.prototype;
321 | const DOM_CONTENT_LOADED = 'DOMContentLoaded';
322 | const LOAD = 'load';
323 | const NO_TRANSPILER_ISSUES = (new QueryResult) instanceof QueryResult;
324 | const QRP = QueryResult.prototype;
325 | // fixes methods returning non QueryResult
326 | /* istanbul ignore if */
327 | Iif (!NO_TRANSPILER_ISSUES)
328 | Object.getOwnPropertyNames(AP).forEach(name => {
329 | const desc = Object.getOwnPropertyDescriptor(AP, name);
330 | if (typeof desc.value === 'function') {
331 | const fn = desc.value;
332 | desc.value = function () {
333 | const result = fn.apply(this, arguments);
334 | return result instanceof Array ? patch(result) : result;
335 | };
336 | }
337 | defineProperty(QRP, name, desc);
338 | });
339 | // fixes badly transpiled classes
340 | const patch = NO_TRANSPILER_ISSUES ?
341 | qr => qr :
342 | /* istanbul ignore next */
343 | qr => {
344 | const nqr = create(QRP);
345 | push.apply(nqr, slice(qr));
346 | return nqr;
347 | };
348 | const push = AP.push;
349 | const search = (list, el) => {
350 | const nodes = [];
351 | const length = list.length;
352 | for (let i = 0; i < length; i++) {
353 | const css = list[i].trim();
354 | if (css.slice(-6) === ':first') {
355 | const node = el.querySelector(css.slice(0, -6));
356 | if (node) push.call(nodes, node);
357 | } else
358 | push.apply(nodes, slice(el.querySelectorAll(css)));
359 | }
360 | return new QueryResult(...nodes);
361 | };
362 | const slice = NO_TRANSPILER_ISSUES ?
363 | patch :
364 | /* istanbul ignore next */
365 | all => {
366 | // do not use slice.call(...) due old IE gotcha
367 | const nodes = [];
368 | const length = all.length;
369 | for (let i = 0; i < length; i++)
370 | nodes[i] = all[i];
371 | return nodes;
372 | }
373 | // use function to avoid usage of Symbol.hasInstance
374 | // (broken in older browsers anyway)
375 | const $ = function $(CSS, parent = document) {
376 | switch (typeof CSS) {
377 | case 'string': return patch(search(CSS.split(','), parent));
378 | case 'object':
379 | // needed to avoid iterator dance (breaks in older IEs)
380 | const nodes = [];
381 | const all = ('nodeType' in CSS || 'postMessage' in CSS) ? [CSS] : CSS;
382 | push.apply(nodes, slice(all));
383 | return patch(new QueryResult(...nodes));
384 | case 'function':
385 | const $parent = $(parent);
386 | const $window = $(parent.defaultView);
387 | const handler = {handleEvent(event) {
388 | $parent.off(DOM_CONTENT_LOADED, handler);
389 | $window.off(LOAD, handler);
390 | CSS(event);
391 | }};
392 | $parent.on(DOM_CONTENT_LOADED, handler);
393 | $window.on(LOAD, handler);
394 | const rs = parent.readyState;
395 | if (rs == 'complete' || (rs != 'loading' && !parent.documentElement.doScroll))
396 | setTimeout(() => $parent.dispatch(DOM_CONTENT_LOADED));
397 | return $;
398 | }
399 | };
400 | $.prototype = QRP;
401 | $.extend = (key, value) =>
402 | (defineProperty(QRP, key, {configurable: true, value}), $);
403 | // dropped usage of for-of to avoid broken iteration dance in older IEs
404 | $.extend('dispatch', function dispatch(type, init = {}) {
405 | const event = new CustomEvent(type, init);
406 | const length = this.length;
407 | for (let i = 0; i < length; i++)
408 | this[i].dispatchEvent(event);
409 | return this;
410 | })
411 | .extend('off', function off(type, handler, options = false) {
412 | const length = this.length;
413 | for (let i = 0; i < length; i++)
414 | this[i].removeEventListener(type, handler, options);
415 | return this;
416 | })
417 | .extend('on', function on(type, handler, options = false) {
418 | const length = this.length;
419 | for (let i = 0; i < length; i++)
420 | this[i].addEventListener(type, handler, options);
421 | return this;
422 | });
423 |
424 | Object.defineProperty(exports, '__esModule', {value: true}).default = $;
425 | |