From d74cfc590d9a2b0e6632eba66eb0a189d10bd724 Mon Sep 17 00:00:00 2001 From: Chase Sterling Date: Sun, 24 Sep 2023 22:50:57 -0400 Subject: [PATCH 1/3] Experiment with tree traversal and new style 'me' --- surreal.js | 89 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/surreal.js b/surreal.js index b3e926b..d088d91 100644 --- a/surreal.js +++ b/surreal.js @@ -36,37 +36,65 @@ var $ = { // You can use a different name than "$", but you must change the refe e.attributes = e.attribute e.attr = e.attribute + // Tree Traversal. + e.el = (selector, start = e, warning = true) => $.el(selector, start, warning) + e.any = (selector, start = e, warning = true) => $.any(selector, start, warning) + Object.defineProperty(e, 'parent', { + get: () => $.sugar(e.parentElement), + configurable: true + }) + + // Add all plugin sugar. $._e = e // Plugin access to "e" for chaining. - for (const [key, value] of Object.entries(sugars)) { + for (const [key, value] of Object.entries($.sugars)) { e[key] = value.bind($) //e[key] = value } return e }, - // Return single element. Selector not needed if used with inline - // - me(selector=null, start=document, warning=true) { - if (selector == null) return $.sugar(start.currentScript.parentElement) // Just local me() in + // + get me() { + if (document.currentScript) return $.sugar(document.currentScript.parentElement) // Just local me in - // + // Return current element. Works inside + // get me() { if (document.currentScript) return $.sugar(document.currentScript.parentElement) // Just local me in