@duckafire/html.js 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
- [html-js]: ./html.js
2
- [example-html]: ./example.html
1
+ [html-js]: ./html.js "html.js source file"
2
+ [example-html]: ./example.html "html.js example"
3
+ [data-property]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/data-* "HTML attributes data-*"
4
+ [aria-property]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA "Accessible Rich Internet Applications"
5
+ [event-listeners]: https://www.w3schools.com/js/js_htmldom_eventlistener.asp "DOM Event Listeners"
3
6
 
4
7
  # html.js
5
8
 
@@ -12,6 +15,16 @@ This is a web component library, thought to be:
12
15
  * Similar to HTML (mainly in relation to the structure)
13
16
  * Independent of preprocessor (or similar)
14
17
 
18
+ ### Topics
19
+
20
+ * [Installing](#installing)
21
+ * [How to use](#how-to-use)
22
+ * [Start library](#start-library)
23
+ * [Create an element](#create-an-element)
24
+ * [Closing the element creation](#closing-the-element-creation)
25
+ * [Special properties](#special-properties)
26
+ * [Other stuff](#other-stuff)
27
+
15
28
  ## Installing
16
29
 
17
30
  Just copy and paste this code chunk in your HTML file:
@@ -43,6 +56,7 @@ As mentioned earlier on, it is necessary call the functions bellow to
43
56
  start the library:
44
57
 
45
58
  * `{} declare_htmljs( [prefix: string = ""], [createObject: boolean = false], [useUpperCase: boolean = false] )`
59
+ * `{} declare_htmljs( [ args: object = {[prefix: string = ""], [createObject: boolean = false], [useUpperCase: boolean = false]} ] )`
46
60
  * `prefix`: prefixs the name of the functions/methods that are
47
61
  used to create the elements.
48
62
  * `createObject`: specifics that the methods have to be declared
@@ -55,6 +69,9 @@ start the library:
55
69
  > If `createObject != true`, the methods will be declared in `window`, that
56
70
  > it also will be returned.
57
71
 
72
+ > [!TIP]
73
+ > Try `declare_htmljs({useUpperCase: true})` instead `declare_html(null, false, true)`.
74
+
58
75
  Call any function, from this library, without call this function (probably) will
59
76
  generate an error.
60
77
 
@@ -115,6 +132,37 @@ functions below:
115
132
  > [!TIP]
116
133
  > Run [example.html][example-html] (in your browser) to see how all these work.
117
134
 
135
+ ### Special properties
136
+
137
+ In addition of the *common element properties*, the `htmlProperties` support the
138
+ properties bellow:
139
+
140
+ * `dataSets`: a list of customized properties that will be prefixed by `data-`.
141
+
142
+ ``` js
143
+ SPAN({ dataSets: { property: "value" } });
144
+ ```
145
+
146
+ * `ariaAttributes`: a list of properties that will be prefixed by `aria-`.
147
+
148
+ ``` js
149
+ SPAN({ ariaProperties: { property: "value" } });
150
+ ```
151
+
152
+ * `eventListeners`: a list of events that will be added to the created element.
153
+
154
+ ``` js
155
+ SPAN({ eventListeners: { event: action } });
156
+ SPAN({ eventListeners: { event: [action0, actionN] } });
157
+ ```
158
+
159
+ > [!NOTE]
160
+ > See more about these concepts bellow:
161
+ >
162
+ > * [data-][data-property]
163
+ > * [aria-][aria-property]
164
+ > * [Event Listeners][event-listeners]
165
+
118
166
  ### Other stuff
119
167
 
120
168
  In addition to the things present earlier, this library declare some other stuff, there
package/html.js CHANGED
@@ -21,11 +21,12 @@
21
21
  // 3. This notice may not be removed or altered from any source distribution.
22
22
 
23
23
  let __htmljs_ignore_last__ = 1;
24
+ let __htmljs_debug_func__ = console.warn;
24
25
 
25
26
  const __htmljs_is_not_object__ = (thing) =>
26
27
  {
27
28
  return typeof thing != "object" || Array.isArray(thing);
28
- }
29
+ };
29
30
 
30
31
  const __htmljs_element_tags__ = [
31
32
  // NO-containers
@@ -48,6 +49,41 @@ const __htmljs_element_tags__ = [
48
49
  "tfoot", "th", "thead", "time", "title", "tr", "ul", "var", "video",
49
50
  ];
50
51
 
52
+ const __htmljs_check_group_type__ = (property, htmlProperties) =>
53
+ {
54
+ if(__htmljs_is_not_object__( htmlProperties[property] ))
55
+ {
56
+ __htmljs_debug_func__(new TypeError(`Invalid value attributed to \`${property}\`, expecting an object.`));
57
+ return false;
58
+ }
59
+
60
+ return true;
61
+ }
62
+
63
+ const __htmljs_set_group_of_attributes__ = (prefix, elem, property, htmlProperties) =>
64
+ {
65
+ if(!__htmljs_check_group_type__(property, htmlProperties))
66
+ return;
67
+
68
+ for(const FIELD in htmlProperties[property])
69
+ elem.setAttribute(prefix + "-" + FIELD, htmlProperties[property][FIELD]);
70
+ };
71
+
72
+ const __htmljs_set_event_listeners__ = (elem, property, htmlProperties) =>
73
+ {
74
+ if(!__htmljs_check_group_type__(property, htmlProperties))
75
+ return;
76
+
77
+ for(const EVENT in htmlProperties[property])
78
+ {
79
+ if(Array.isArray( htmlProperties[property][EVENT] ))
80
+ for(const LAMBDA of htmlProperties[property][EVENT])
81
+ elem.addEventListener(EVENT, LAMBDA);
82
+ else
83
+ elem.addEventListener(EVENT, htmlProperties[property][EVENT]);
84
+ }
85
+ };
86
+
51
87
  const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
52
88
  {
53
89
  const ELEM = document.createElement(elementTag);
@@ -60,10 +96,33 @@ const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
60
96
  if(htmlProperties !== null)
61
97
  {
62
98
  if(__htmljs_is_not_object__( htmlProperties ))
63
- throw new TypeError(`Excepting Object to \`htmlProperties\`, instead "${typeof htmlProperties}".`);
99
+ {
100
+ __htmljs_debug_func__(new TypeError(`Excepting Object to \`htmlProperties\`, instead "${typeof htmlProperties}".`));
101
+
102
+ // this allows to jump the for-loop below,
103
+ // if the Debug Mode is != 2
104
+ htmlProperties = {};
105
+ }
64
106
 
65
- for(const property in htmlProperties)
66
- ELEM[property] = htmlProperties[property]
107
+ for(const PROPERTY in htmlProperties){
108
+ switch(PROPERTY)
109
+ {
110
+ case "dataSets": __htmljs_set_group_of_attributes__("data", ELEM, PROPERTY, htmlProperties); continue;
111
+ case "ariaAttributes": __htmljs_set_group_of_attributes__("aria", ELEM, PROPERTY, htmlProperties); continue;
112
+ case "eventListeners": __htmljs_set_event_listeners__( ELEM, PROPERTY, htmlProperties); continue;
113
+ }
114
+
115
+ if(ELEM[PROPERTY] === undefined)
116
+ {
117
+ __htmljs_debug_func__(new TypeError(`Invalid property (${PROPERTY}) to ${ELEM.constructor.name}.`));
118
+ continue;
119
+ }
120
+
121
+ ELEM[PROPERTY] =
122
+ htmlProperties[PROPERTY] == "~"
123
+ ? PROPERTY
124
+ : htmlProperties[PROPERTY];
125
+ }
67
126
  }
68
127
 
69
128
  for(let i = 0; i < ARGS_MAX; i++)
@@ -74,8 +133,11 @@ const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
74
133
  continue;
75
134
  }
76
135
 
77
- if(__htmljs_is_not_object__( children[i] ))
78
- throw new TypeError(`Excepting Object or String to \`children[${i}]\`, instead "${typeof children[i]}".`);
136
+ if(!(children[i] instanceof HTMLElement))
137
+ {
138
+ __htmljs_debug_func__(new TypeError(`Excepting Object or String to \`children[${i}]\`, instead "${typeof children[i]}".`));
139
+ continue;
140
+ }
79
141
 
80
142
  if(textnode != "")
81
143
  {
@@ -89,12 +151,12 @@ const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
89
151
  }
90
152
  catch(ex)
91
153
  {
92
- console.error(ex)
154
+ __htmljs_debug_func__(ex);
93
155
  break;
94
156
  }
95
157
  }
96
158
 
97
- if(textnode !== null)
159
+ if(textnode != "")
98
160
  ELEM.appendChild(document.createTextNode( textnode ));
99
161
  }
100
162
 
@@ -104,10 +166,34 @@ const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
104
166
  const htmljs_set_ignore_last = (ignore) =>
105
167
  {
106
168
  __htmljs_ignore_last__ = ignore ? 1 : 0;
107
- }
169
+ };
108
170
 
109
- const declare_htmljs = (prefix, createObject, useUpperCase) =>
171
+ const htmljs_set_debug_mode = (mode) =>
110
172
  {
173
+ const TYPE = typeof mode;
174
+
175
+ if(TYPE != "number")
176
+ throw new TypeError(`Invalid mode type: "${TYPE}". Use only integer numbers.`);
177
+
178
+ if(mode > 2)
179
+ throw new RangeError(`Invalid mode value: "${mode}". Use only 0, 1, or 2.`);
180
+
181
+ __htmljs_debug_func__ = [
182
+ () => {},
183
+ console.warn,
184
+ (exception) => { throw exception; }
185
+ ][ Math.floor(mode) ];
186
+ };
187
+
188
+ const declare_htmljs = (...args) =>
189
+ {
190
+ let prefix, createObject, useUpperCase;
191
+
192
+ if(args[0] !== null && typeof args[0] == "object")
193
+ ({prefix, createObject, useUpperCase} = args[0]);
194
+ else
195
+ [prefix, createObject, useUpperCase] = args;
196
+
111
197
  const DEST = createObject ? {} : window;
112
198
  const PREF = prefix || "";
113
199
  let tag;
package/html.min.js CHANGED
@@ -1 +1 @@
1
- "use strict";let __htmljs_ignore_last__=1;const __htmljs_is_not_object__=t=>"object"!=typeof t||Array.isArray(t),__htmljs_element_tags__=["area","base","br","col","hr","img","input","link","meta","source","track","wbr","a","abbr","address","article","audio","b","bdi","bdo","blockquote","body","button","canvas","caption","cite","code","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","html","i","iframe","ins","kbd","label","legend","li","main","map","mark","meter","nav","noscript","object","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","samp","script","section","select","small","span","strong","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","ul","var","video"],__htmljs_core__=(t,e,...o)=>{const r=document.createElement(t);if(void 0!==e){let t="";const _=o.length-__htmljs_ignore_last__;if(null!==e){if(__htmljs_is_not_object__(e))throw new TypeError(`Excepting Object to \`htmlProperties\`, instead "${typeof e}".`);for(const t in e)r[t]=e[t]}for(let e=0;e<_;e++)if("string"!=typeof o[e]){if(__htmljs_is_not_object__(o[e]))throw new TypeError(`Excepting Object or String to \`children[${e}]\`, instead "${typeof o[e]}".`);""!=t&&(r.appendChild(document.createTextNode(t)),t="");try{r.appendChild(o[e])}catch(t){console.error(t);break}}else t+=(""==t?"":" ")+o[e];null!==t&&r.appendChild(document.createTextNode(t))}return r},htmljs_set_ignore_last=t=>{__htmljs_ignore_last__=t?1:0},declare_htmljs=(t,e,o)=>{const r=e?{}:window,_=t||"";let l;return __htmljs_element_tags__.forEach((t,e)=>{l=_+t,r[o?l.toUpperCase():l]=e<12?e=>__htmljs_core__(t,e):(e,...o)=>__htmljs_core__(t,e,...o)}),r};
1
+ "use strict";let __htmljs_ignore_last__=1,__htmljs_debug_func__=console.warn;const __htmljs_is_not_object__=e=>"object"!=typeof e||Array.isArray(e),__htmljs_element_tags__=["area","base","br","col","hr","img","input","link","meta","source","track","wbr","a","abbr","address","article","audio","b","bdi","bdo","blockquote","body","button","canvas","caption","cite","code","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","html","i","iframe","ins","kbd","label","legend","li","main","map","mark","meter","nav","noscript","object","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","samp","script","section","select","small","span","strong","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","ul","var","video"],__htmljs_check_group_type__=(e,t)=>!__htmljs_is_not_object__(t[e])||(__htmljs_debug_func__(new TypeError(`Invalid value attributed to \`${e}\`, expecting an object.`)),!1),__htmljs_set_group_of_attributes__=(e,t,_,r)=>{if(__htmljs_check_group_type__(_,r))for(const o in r[_])t.setAttribute(e+"-"+o,r[_][o])},__htmljs_set_event_listeners__=(e,t,_)=>{if(__htmljs_check_group_type__(t,_))for(const r in _[t])if(Array.isArray(_[t][r]))for(const o of _[t][r])e.addEventListener(r,o);else e.addEventListener(r,_[t][r])},__htmljs_core__=(e,t,..._)=>{const r=document.createElement(e);if(void 0!==t){let e="";const o=_.length-__htmljs_ignore_last__;if(null!==t){__htmljs_is_not_object__(t)&&(__htmljs_debug_func__(new TypeError(`Excepting Object to \`htmlProperties\`, instead "${typeof t}".`)),t={});for(const e in t){switch(e){case"dataSets":__htmljs_set_group_of_attributes__("data",r,e,t);continue;case"ariaAttributes":__htmljs_set_group_of_attributes__("aria",r,e,t);continue;case"eventListeners":__htmljs_set_event_listeners__(r,e,t);continue}void 0!==r[e]?r[e]="~"==t[e]?e:t[e]:__htmljs_debug_func__(new TypeError(`Invalid property (${e}) to ${r.constructor.name}.`))}}for(let t=0;t<o;t++)if("string"!=typeof _[t])if(_[t]instanceof HTMLElement){""!=e&&(r.appendChild(document.createTextNode(e)),e="");try{r.appendChild(_[t])}catch(e){__htmljs_debug_func__(e);break}}else __htmljs_debug_func__(new TypeError(`Excepting Object or String to \`children[${t}]\`, instead "${typeof _[t]}".`));else e+=(""==e?"":" ")+_[t];""!=e&&r.appendChild(document.createTextNode(e))}return r},htmljs_set_ignore_last=e=>{__htmljs_ignore_last__=e?1:0},htmljs_set_debug_mode=e=>{const t=typeof e;if("number"!=t)throw new TypeError(`Invalid mode type: "${t}". Use only integer numbers.`);if(e>2)throw new RangeError(`Invalid mode value: "${e}". Use only 0, 1, or 2.`);__htmljs_debug_func__=[()=>{},console.warn,e=>{throw e}][Math.floor(e)]},declare_htmljs=(...e)=>{let t,_,r;null!==e[0]&&"object"==typeof e[0]?({prefix:t,createObject:_,useUpperCase:r}=e[0]):[t,_,r]=e;const o=_?{}:window,s=t||"";let n;return __htmljs_element_tags__.forEach((e,t)=>{n=s+e,o[r?n.toUpperCase():n]=t<12?t=>__htmljs_core__(e,t):(t,..._)=>__htmljs_core__(e,t,..._)}),o};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@duckafire/html.js",
3
3
  "author": "duckafire",
4
- "version": "0.0.3",
4
+ "version": "0.1.0",
5
5
  "license": "Zlib",
6
6
  "description": "Just a very simple components library.",
7
7
  "keywords": ["front-end", "client", "html", "dom", "light", "components", "web", "simple", "minimalist", "easy"],