@duckafire/html.js 0.1.1 → 0.2.1
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 +22 -0
- package/html.js +54 -34
- package/html.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
[data-property]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/data-* "HTML attributes data-*"
|
|
4
4
|
[aria-property]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA "Accessible Rich Internet Applications"
|
|
5
5
|
[event-listeners]: https://www.w3schools.com/js/js_htmldom_eventlistener.asp "DOM Event Listeners"
|
|
6
|
+
[style-property]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style "The HTML `style` property"
|
|
7
|
+
[camel-case]: https://en.wikipedia.org/wiki/Camel_case "Understanding the camelCase"
|
|
6
8
|
|
|
7
9
|
# html.js
|
|
8
10
|
|
|
@@ -115,6 +117,12 @@ will explain their structure.
|
|
|
115
117
|
example). See [this](#closing-the-element-creation) to learn how to
|
|
116
118
|
disable this optional parameter.
|
|
117
119
|
|
|
120
|
+
> [!WARNING]
|
|
121
|
+
> Unlike HTML and CSS, the JavaScript syntax do not support the use of the hyphen (`-`)
|
|
122
|
+
> in identifiers name. So *compound properties* only can be declared:
|
|
123
|
+
> * Between single/double quotes: `"padding-top"`; `"background-color"`; `"font-size"`.
|
|
124
|
+
> * In [camelCase][camel-case]: `paddingTop`; `backgroundColor`; `fontSize`.
|
|
125
|
+
|
|
118
126
|
> [!IMPORTANT]
|
|
119
127
|
> Deprecated tags are not available.
|
|
120
128
|
|
|
@@ -161,12 +169,26 @@ SPAN({ eventListeners: { event: action } });
|
|
|
161
169
|
SPAN({ eventListeners: { event: [action0, actionN] } });
|
|
162
170
|
```
|
|
163
171
|
|
|
172
|
+
* `cssRules`: a list of CSS style rules and variables that will be added to the created
|
|
173
|
+
element.
|
|
174
|
+
|
|
175
|
+
``` js
|
|
176
|
+
SPAN({ cssRules: { rule: value } });
|
|
177
|
+
SPAN({ cssRules: { "--variable": value } });
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
> [!IMPORTANT]
|
|
181
|
+
> These *compound properties* follow the same writing rules of the ***common***
|
|
182
|
+
> *compound properties*. See [Create an element](#create-an-element) to more
|
|
183
|
+
> information about.
|
|
184
|
+
|
|
164
185
|
> [!NOTE]
|
|
165
186
|
> See more about these concepts bellow:
|
|
166
187
|
>
|
|
167
188
|
> * [data-][data-property]
|
|
168
189
|
> * [aria-][aria-property]
|
|
169
190
|
> * [Event Listeners][event-listeners]
|
|
191
|
+
> * [The `style` property][style-property]
|
|
170
192
|
|
|
171
193
|
### Other stuff
|
|
172
194
|
|
package/html.js
CHANGED
|
@@ -49,40 +49,62 @@ const __htmljs_element_tags__ = [
|
|
|
49
49
|
"tfoot", "th", "thead", "time", "title", "tr", "ul", "var", "video",
|
|
50
50
|
];
|
|
51
51
|
|
|
52
|
-
const
|
|
52
|
+
const __htmljs_set_prefixed_property__ = (prefix, elem, field, value) =>
|
|
53
53
|
{
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
__htmljs_debug_func__(new TypeError(`Invalid value attributed to \`${property}\`, expecting an object.`));
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
54
|
+
elem.setAttribute(prefix + "-" + field, value);
|
|
55
|
+
}
|
|
59
56
|
|
|
60
|
-
|
|
57
|
+
const __htmljs_data_sets__ = (elem, property, value) =>
|
|
58
|
+
{
|
|
59
|
+
__htmljs_set_prefixed_property__("data", elem, property, value);
|
|
61
60
|
}
|
|
62
61
|
|
|
63
|
-
const
|
|
62
|
+
const __htmljs_aria_attributes__ = (elem, property, value) =>
|
|
64
63
|
{
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
__htmljs_set_prefixed_property__("aria", elem, property, value);
|
|
65
|
+
}
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
const __htmljs_event_listeners__ = (elem, event, action) =>
|
|
68
|
+
{
|
|
69
|
+
if(Array.isArray(action))
|
|
70
|
+
for(const ACTION of action)
|
|
71
|
+
elem.addEventListener(event, ACTION);
|
|
72
|
+
else
|
|
73
|
+
elem.addEventListener(event, action);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const __htmljs_css_rules__ = (elem, rule, value) =>
|
|
77
|
+
{
|
|
78
|
+
if(rule.charAt(0) == "-") // it is a variable
|
|
79
|
+
elem.style.setProperty(rule, value);
|
|
80
|
+
else
|
|
81
|
+
elem.style[rule] = value;
|
|
82
|
+
}
|
|
71
83
|
|
|
72
|
-
const
|
|
84
|
+
const __htmljs_treat_special_properties__ = (elem, property, htmlProperties) =>
|
|
73
85
|
{
|
|
74
|
-
|
|
75
|
-
return;
|
|
86
|
+
let behavior;
|
|
76
87
|
|
|
77
|
-
|
|
88
|
+
switch(property)
|
|
78
89
|
{
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
90
|
+
case "dataSets": behavior = __htmljs_data_sets__; break;
|
|
91
|
+
case "ariaAttributes": behavior = __htmljs_aria_attributes__; break;
|
|
92
|
+
case "eventListeners": behavior = __htmljs_event_listeners__; break;
|
|
93
|
+
case "cssRules": behavior = __htmljs_css_rules__; break;
|
|
94
|
+
default: return false;
|
|
84
95
|
}
|
|
85
|
-
|
|
96
|
+
|
|
97
|
+
if(__htmljs_is_not_object__( htmlProperties[property] ))
|
|
98
|
+
{
|
|
99
|
+
__htmljs_debug_func__(new TypeError(`Invalid value attributed to \`${property}\`, expecting an object.`));
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
for(const PROPERTY in htmlProperties[property])
|
|
104
|
+
behavior(elem, PROPERTY, htmlProperties[property][PROPERTY]);
|
|
105
|
+
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
86
108
|
|
|
87
109
|
const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
|
|
88
110
|
{
|
|
@@ -105,17 +127,15 @@ const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
|
|
|
105
127
|
}
|
|
106
128
|
|
|
107
129
|
for(const PROPERTY in htmlProperties){
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
? PROPERTY
|
|
118
|
-
: htmlProperties[PROPERTY];
|
|
130
|
+
if(__htmljs_treat_special_properties__(ELEM, PROPERTY, htmlProperties))
|
|
131
|
+
continue;
|
|
132
|
+
|
|
133
|
+
const VALUE = (htmlProperties[PROPERTY] == "~" ? PROPERTY.toLowerCase() : htmlProperties[PROPERTY]);
|
|
134
|
+
|
|
135
|
+
if(ELEM[PROPERTY])
|
|
136
|
+
ELEM[PROPERTY] = VALUE;
|
|
137
|
+
else
|
|
138
|
+
ELEM.setAttribute(PROPERTY, VALUE);
|
|
119
139
|
}
|
|
120
140
|
}
|
|
121
141
|
|
package/html.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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"],
|
|
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_set_prefixed_property__=(e,t,_,r)=>{t.setAttribute(e+"-"+_,r)},__htmljs_data_sets__=(e,t,_)=>{__htmljs_set_prefixed_property__("data",e,t,_)},__htmljs_aria_attributes__=(e,t,_)=>{__htmljs_set_prefixed_property__("aria",e,t,_)},__htmljs_event_listeners__=(e,t,_)=>{if(Array.isArray(_))for(const r of _)e.addEventListener(t,r);else e.addEventListener(t,_)},__htmljs_css_rules__=(e,t,_)=>{"-"==t.charAt(0)?e.style.setProperty(t,_):e.style[t]=_},__htmljs_treat_special_properties__=(e,t,_)=>{let r;switch(t){case"dataSets":r=__htmljs_data_sets__;break;case"ariaAttributes":r=__htmljs_aria_attributes__;break;case"eventListeners":r=__htmljs_event_listeners__;break;case"cssRules":r=__htmljs_css_rules__;break;default:return!1}if(__htmljs_is_not_object__(_[t]))return __htmljs_debug_func__(new TypeError(`Invalid value attributed to \`${t}\`, expecting an object.`)),!1;for(const s in _[t])r(e,s,_[t][s]);return!0},__htmljs_core__=(e,t,..._)=>{const r=document.createElement(e);if(void 0!==t){let e="";const s=_.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){if(__htmljs_treat_special_properties__(r,e,t))continue;const _="~"==t[e]?e.toLowerCase():t[e];r[e]?r[e]=_:r.setAttribute(e,_)}}for(let t=0;t<s;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 s=_?{}:window,o=t||"";let a;return __htmljs_element_tags__.forEach((e,t)=>{a=o+e,s[r?a.toUpperCase():a]=t<12?t=>__htmljs_core__(e,t):(t,..._)=>__htmljs_core__(e,t,..._)}),s};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duckafire/html.js",
|
|
3
3
|
"author": "duckafire",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.1",
|
|
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"],
|