From a8a21d11cf511fffadefa24c810dc38218d894f5 Mon Sep 17 00:00:00 2001 From: yarinr Date: Mon, 30 Oct 2017 10:50:27 +0300 Subject: [PATCH 1/2] ofek-query library skeleton --- js/ofek-query.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 js/ofek-query.js diff --git a/js/ofek-query.js b/js/ofek-query.js new file mode 100644 index 0000000..d6fa2dd --- /dev/null +++ b/js/ofek-query.js @@ -0,0 +1,14 @@ +class OfekQuery { + + constructor(selector) {} + + addClass(className) {} + + removeClass(className) {} + + each(func) {} + + map(func) {} +} + +export var $ = selector => new OfekQuery(selector); \ No newline at end of file From a3c5bc75a3c5f07d45ddcd74438ac79a46d4f1de Mon Sep 17 00:00:00 2001 From: yarinr Date: Mon, 30 Oct 2017 15:00:54 +0300 Subject: [PATCH 2/2] implemented and tested ofek-query --- js/ofek-query-test.js | 3 ++ js/ofek-query.js | 66 +++++++++++++++++++++++++++++++++++++++---- ofek-query-test.html | 38 +++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 js/ofek-query-test.js create mode 100644 ofek-query-test.html diff --git a/js/ofek-query-test.js b/js/ofek-query-test.js new file mode 100644 index 0000000..a6b8733 --- /dev/null +++ b/js/ofek-query-test.js @@ -0,0 +1,3 @@ +import {$} from "./ofek-query.js"; +console.log('.a .b .c', $('.a .b .c').elements); +console.log('div div #shit .moshe', $('div div #shit .moshe').elements); diff --git a/js/ofek-query.js b/js/ofek-query.js index d6fa2dd..3562a4a 100644 --- a/js/ofek-query.js +++ b/js/ofek-query.js @@ -1,14 +1,70 @@ class OfekQuery { - constructor(selector) {} + constructor(selector) { + this.elements = []; + let currentlySelected = []; + const tokens = selector.split(" "); - addClass(className) {} + if (!tokens.length) { return; } - removeClass(className) {} + (function handleFirstToken() { + const firstToken = tokens[0]; + if (firstToken.startsWith("#")) { + currentlySelected.push(document.getElementsById(firstToken.substring(1))); + } else if (firstToken.startsWith(".")) { + currentlySelected = Array.from(document.getElementsByClassName(firstToken.substring(1))); + } else { + currentlySelected = Array.from(document.getElementsByTagName(firstToken)); + } + tokens.shift(); + })(); - each(func) {} + function getAllDescendants(element, selector) { + let descendants = []; - map(func) {} + Array.from(element.children).forEach(function getAllChildren(element) { + if (selector.startsWith("#")) { + if (element.id === selector.substring(1)) { descendants.push(element); } + } else if (selector.startsWith(".")) { + if (element.classList.contains(selector.substring(1))) { descendants.push(element); } + } else { + if (element.tagName.toLowerCase() === selector.toLowerCase()) { descendants.push(element); } + } + + Array.from(element.children).forEach(getAllChildren); + }); + + return descendants; + } + + tokens.forEach(token => { + let runOn = currentlySelected.slice(0); + currentlySelected = []; + runOn.forEach(e => currentlySelected.push(...getAllDescendants(e, token))); + }); + + this.elements = currentlySelected; + } + + addClass(className) { + this.each( e => e.classList.add(className) ); + console.log(this.elements); + } + + removeClass(className) { + this.each( e => e.classList.remove(className) ); + console.log(this.elements); + } + + each(func) { + this.elements.forEach(func); + console.log(this.elements); + } + + map(func) { + return this.elements.map(func); + console.log(this.elements); + } } export var $ = selector => new OfekQuery(selector); \ No newline at end of file diff --git a/ofek-query-test.html b/ofek-query-test.html new file mode 100644 index 0000000..9ae87a6 --- /dev/null +++ b/ofek-query-test.html @@ -0,0 +1,38 @@ + + + + + + + Sample DOM + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

Shalom

+
+
+
+ + + \ No newline at end of file