@duckafire/html.js 0.1.1 → 0.2.2
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 +28 -4
- 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
|
|
|
@@ -89,10 +91,12 @@ below:
|
|
|
89
91
|
``` js
|
|
90
92
|
// create a <div> that it contains two
|
|
91
93
|
// "text nodes" and a <br> element
|
|
92
|
-
div(
|
|
93
|
-
"
|
|
94
|
-
br()
|
|
95
|
-
|
|
94
|
+
div( {className: "foo"},
|
|
95
|
+
"Lorem ipsum",
|
|
96
|
+
br(),
|
|
97
|
+
span( null,
|
|
98
|
+
"Lorem ipsum",
|
|
99
|
+
span),
|
|
96
100
|
input( {type: "text", value: "foobar", readOnly: "~"}),
|
|
97
101
|
div);
|
|
98
102
|
```
|
|
@@ -115,6 +119,12 @@ will explain their structure.
|
|
|
115
119
|
example). See [this](#closing-the-element-creation) to learn how to
|
|
116
120
|
disable this optional parameter.
|
|
117
121
|
|
|
122
|
+
> [!WARNING]
|
|
123
|
+
> Unlike HTML and CSS, the JavaScript syntax do not support the use of the hyphen (`-`)
|
|
124
|
+
> in identifiers name. So *compound properties* only can be declared:
|
|
125
|
+
> * Between single/double quotes: `"padding-top"`; `"background-color"`; `"font-size"`.
|
|
126
|
+
> * In [camelCase][camel-case]: `paddingTop`; `backgroundColor`; `fontSize`.
|
|
127
|
+
|
|
118
128
|
> [!IMPORTANT]
|
|
119
129
|
> Deprecated tags are not available.
|
|
120
130
|
|
|
@@ -161,12 +171,26 @@ SPAN({ eventListeners: { event: action } });
|
|
|
161
171
|
SPAN({ eventListeners: { event: [action0, actionN] } });
|
|
162
172
|
```
|
|
163
173
|
|
|
174
|
+
* `cssRules`: a list of CSS style rules and variables that will be added to the created
|
|
175
|
+
element.
|
|
176
|
+
|
|
177
|
+
``` js
|
|
178
|
+
SPAN({ cssRules: { rule: value } });
|
|
179
|
+
SPAN({ cssRules: { "--variable": value } });
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
> [!IMPORTANT]
|
|
183
|
+
> These *compound properties* follow the same writing rules of the ***common***
|
|
184
|
+
> *compound properties*. See [Create an element](#create-an-element) to more
|
|
185
|
+
> information about.
|
|
186
|
+
|
|
164
187
|
> [!NOTE]
|
|
165
188
|
> See more about these concepts bellow:
|
|
166
189
|
>
|
|
167
190
|
> * [data-][data-property]
|
|
168
191
|
> * [aria-][aria-property]
|
|
169
192
|
> * [Event Listeners][event-listeners]
|
|
193
|
+
> * [The `style` property][style-property]
|
|
170
194
|
|
|
171
195
|
### Other stuff
|
|
172
196
|
|
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] !== undefined || PROPERTY == "className")
|
|
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];void 0!==r[e]||"className"==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.2",
|
|
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"],
|