From 4b01ca94bbca324c1f75e8173a8f05a54d5e8b0c Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Fri, 17 Jul 2020 17:50:20 +0200 Subject: [PATCH] Fix dash --- README.md | 4 +-- dist/ffnm.min.js | 2 +- src/core.js | 82 +++++++++++++++++++++++++---------------------- test/i-hover.html | 2 +- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 3026e94..a70f2c9 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Simple utility to **avoid writing Javascript** when working with **classes**. It Add to your `` the following tag. ```html - + ``` ### Examples @@ -145,7 +145,7 @@ Add to your `` the following tag. AƱade a tu `` la siguiente etiqueta. ```html - + ``` ### Ejemplos de uso diff --git a/dist/ffnm.min.js b/dist/ffnm.min.js index 5d49c13..37acc18 100644 --- a/dist/ffnm.min.js +++ b/dist/ffnm.min.js @@ -1,2 +1,2 @@ -document.addEventListener("DOMContentLoaded",()=>{const e={class:["add","remove","toggle"]};let t=[],c=void 0;function s(e,t){let c=e.getAttribute(t),s=RegExp("^(\\w+):").exec(c)[1],l=RegExp(":(\\w+)\\(").exec(c)[1],a=RegExp("\\('(\\w+)',?").exec(c)[1],o=RegExp(", *'([#,.,a-zA-Z]\\w*)'\\)");return{functionParent:s,functionChild:l,value:a,target:null!==o.exec(c)?o.exec(c)[1]:void 0}}t=[],["i-click","i-scroll-up","i-scroll-down","i-hover"].map(c=>[...document.querySelectorAll(`[${c}]`)].map(s=>{let l=Object.keys(e).map(t=>e[t].map(e=>RegExp(`^${t}:${e}\\('.+'(, *'[.#i\\w][_-\\w0-9]+')?\\)$`).test(s.getAttribute(c))).some(e=>e)).every(e=>e);return l?t.push(s):(console.error("FFNM: Bad syntax"),console.error(s)),l}).every(e=>e)).every(e=>e),[...document.querySelectorAll("[i-click]")].forEach(e=>{let t=s(e,"i-click");switch(t.functionParent){case"class":[...document.querySelectorAll(t.target)].concat(e).forEach(c=>{e.addEventListener("click",()=>{switch(t.functionChild){case"add":c.classList.add(t.value);break;case"remove":c.classList.remove(t.value);break;case"toggle":c.classList.toggle(t.value)}})})}}),window.addEventListener("scroll",()=>{let e=window.pageYOffset||document.documentElement.scrollTop;[...document.querySelectorAll("[i-scroll-down]")].forEach(t=>{if(e>c){let e=s(t,"i-scroll-down");switch(e.functionParent){case"class":switch(e.functionChild){case"add":t.classList.add(e.value);break;case"remove":t.classList.remove(e.value)}}}}),[...document.querySelectorAll("[i-scroll-up]")].forEach(t=>{if(e<=c){let e=s(t,"i-scroll-up");switch(e.functionParent){case"class":switch(e.functionChild){case"add":t.classList.add(e.value);break;case"remove":t.classList.remove(e.value)}}}}),c=e<=0?0:e},!1),[...document.querySelectorAll("[i-hover]")].forEach(e=>{let t=s(e,"i-hover");switch(t.functionParent){case"class":[...document.querySelectorAll(t.target)].concat(e).forEach(c=>{switch(t.functionChild){case"add":e.addEventListener("mouseenter",()=>{c.classList.add(t.value)}),e.addEventListener("mouseout",()=>{c.classList.remove(t.value)});break;case"remove":e.addEventListener("mouseenter",()=>{c.classList.remove(t.value)}),e.addEventListener("mouseout",()=>{c.classList.add(t.value)});break;case"toggle":e.addEventListener("mouseenter",()=>{c.classList.toggle(t.value)}),e.addEventListener("mouseout",()=>{c.classList.toggle(t.value)})}})}})}); +document.addEventListener("DOMContentLoaded",()=>{const e={class:["add","remove","toggle"]};let t=[],c=void 0;function s(e,t){let c=e.getAttribute(t),s=RegExp("^(\\w+):").exec(c)[1],a=RegExp(":(\\w+)\\(").exec(c)[1],l=RegExp("\\('(\\w[_-\\w0-9]+)',?").exec(c)[1],o=RegExp(", ?'([.#i\\w][_-\\w0-9]+)'\\)");return{functionParent:s,functionChild:a,value:l,target:null!==o.exec(c)?o.exec(c)[1]:void 0}}t=[],["i-click","i-scroll-up","i-scroll-down","i-hover"].map(c=>[...document.querySelectorAll(`[${c}]`)].map(s=>{let a=Object.keys(e).map(t=>e[t].map(e=>RegExp(`^${t}:${e}\\('.+'(, ?'[.#i\\w][_-\\w0-9]+')?\\)$`).test(s.getAttribute(c))).some(e=>e)).every(e=>e);return a?t.push(s):(console.error("FFNM: Bad syntax"),console.error(s)),a}).every(e=>e)).every(e=>e),[...document.querySelectorAll("[i-click]")].forEach(e=>{let t=s(e,"i-click");switch(t.functionParent){case"class":[...document.querySelectorAll(t.target)].concat(void 0===t.target?e:void 0).forEach(c=>{void 0!==c&&e.addEventListener("click",()=>{switch(t.functionChild){case"add":c.classList.add(t.value);break;case"remove":c.classList.remove(t.value);break;case"toggle":c.classList.toggle(t.value)}})})}}),window.addEventListener("scroll",()=>{let e=window.pageYOffset||document.documentElement.scrollTop;[...document.querySelectorAll("[i-scroll-down]")].forEach(t=>{if(e>c){let e=s(t,"i-scroll-down");switch(e.functionParent){case"class":switch(e.functionChild){case"add":t.classList.add(e.value);break;case"remove":t.classList.remove(e.value)}}}}),[...document.querySelectorAll("[i-scroll-up]")].forEach(t=>{if(e<=c){let e=s(t,"i-scroll-up");switch(e.functionParent){case"class":switch(e.functionChild){case"add":t.classList.add(e.value);break;case"remove":t.classList.remove(e.value)}}}}),c=e<=0?0:e},!1),[...document.querySelectorAll("[i-hover]")].forEach(e=>{let t=s(e,"i-hover");switch(t.functionParent){case"class":[...document.querySelectorAll(t.target)].concat(void 0===t.target?e:void 0).forEach(c=>{if(void 0!==c)switch(t.functionChild){case"add":e.addEventListener("mouseenter",()=>{c.classList.add(t.value)}),e.addEventListener("mouseout",()=>{c.classList.remove(t.value)});break;case"remove":e.addEventListener("mouseenter",()=>{c.classList.remove(t.value)}),e.addEventListener("mouseout",()=>{c.classList.add(t.value)});break;case"toggle":e.addEventListener("mouseenter",()=>{c.classList.toggle(t.value)}),e.addEventListener("mouseout",()=>{c.classList.toggle(t.value)})}})}})}); //# sourceMappingURL=ffnm.min.js.map diff --git a/src/core.js b/src/core.js index f0244ac..cbf8bd7 100644 --- a/src/core.js +++ b/src/core.js @@ -28,7 +28,7 @@ document.addEventListener('DOMContentLoaded', () => { let checked = Object.keys(FUNCTION_TREE).map((key) => { // Check METHODS FUNCTION_TREE return FUNCTION_TREE[key].map((method) => { - return RegExp(`^${key}:${method}\\('.+'(, *'[.#i\\w][_-\\w0-9]+')?\\)$`).test(element.getAttribute(event)); + return RegExp(`^${key}:${method}\\('.+'(, ?'[.#i\\w][_-\\w0-9]+')?\\)$`).test(element.getAttribute(event)); }).some(method => method); }).every(key => key); if(checked) { @@ -52,10 +52,10 @@ document.addEventListener('DOMContentLoaded', () => { */ function splitParams(element, attribute) { let params = element.getAttribute(attribute); - let functionParent = RegExp(`^(\\w+):`).exec(params)[1]; - let functionChild = RegExp(`:(\\w+)\\(`).exec(params)[1]; - let value = RegExp(`\\(\'(\\w+)\',?`).exec(params)[1]; - let resultTarget = RegExp(`, *\'([#,.,a-zA-Z]\\w*)\'\\)`); + let functionParent = RegExp("^(\\w+):").exec(params)[1]; + let functionChild = RegExp(":(\\w+)\\(").exec(params)[1]; + let value = RegExp("\\(\'(\\w[_-\\w0-9]+)\',?").exec(params)[1]; + let resultTarget = RegExp(", ?\'([.#i\\w][_-\\w0-9]+)\'\\)"); let target = resultTarget.exec(params) !== null ? resultTarget.exec(params)[1] : undefined; return {'functionParent': functionParent, 'functionChild': functionChild, 'value': value, 'target': target}; } @@ -70,7 +70,8 @@ document.addEventListener('DOMContentLoaded', () => { let params = splitParams(element, eventClick); switch(params.functionParent) { case 'class': - [...document.querySelectorAll(params.target)].concat(element).forEach((item) => { + [...document.querySelectorAll(params.target)].concat(params.target === undefined ? element : undefined).forEach((item) => { + if (item !== undefined) { element.addEventListener('click', () => { switch(params.functionChild) { case 'add': @@ -84,6 +85,7 @@ document.addEventListener('DOMContentLoaded', () => { break; } }); + } }); break; } @@ -150,39 +152,41 @@ document.addEventListener('DOMContentLoaded', () => { let params = splitParams(element, eventHover); switch(params.functionParent) { case 'class': - [...document.querySelectorAll(params.target)].concat(element).forEach((item) => { - // Enter - switch(params.functionChild) { - case 'add': - // Enter - element.addEventListener('mouseenter', () => { - item.classList.add(params.value); - }); - // Out - element.addEventListener('mouseout', () => { - item.classList.remove(params.value); - }); - break; - case 'remove': - // Enter - element.addEventListener('mouseenter', () => { - item.classList.remove(params.value); - }); - // Out - element.addEventListener('mouseout', () => { - item.classList.add(params.value); - }); - break; - case 'toggle': - // Enter - element.addEventListener('mouseenter', () => { - item.classList.toggle(params.value); - }); - // Out - element.addEventListener('mouseout', () => { - item.classList.toggle(params.value); - }); - break; + [...document.querySelectorAll(params.target)].concat(params.target === undefined ? element : undefined).forEach((item) => { + if (item !== undefined) { + // Enter + switch(params.functionChild) { + case 'add': + // Enter + element.addEventListener('mouseenter', () => { + item.classList.add(params.value); + }); + // Out + element.addEventListener('mouseout', () => { + item.classList.remove(params.value); + }); + break; + case 'remove': + // Enter + element.addEventListener('mouseenter', () => { + item.classList.remove(params.value); + }); + // Out + element.addEventListener('mouseout', () => { + item.classList.add(params.value); + }); + break; + case 'toggle': + // Enter + element.addEventListener('mouseenter', () => { + item.classList.toggle(params.value); + }); + // Out + element.addEventListener('mouseout', () => { + item.classList.toggle(params.value); + }); + break; + } } }); break; diff --git a/test/i-hover.html b/test/i-hover.html index b1feec8..08129ab 100644 --- a/test/i-hover.html +++ b/test/i-hover.html @@ -11,7 +11,7 @@ -

Place the cursor over this text

+

Place the cursor over this text

Quisque id diam vel quam elementum pulvinar etiam non quam?