@duckafire/html.js 0.1.0 → 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 +27 -0
- package/html.js +52 -38
- 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
|
|
|
@@ -93,9 +95,14 @@ div( null,
|
|
|
93
95
|
"foo foo"
|
|
94
96
|
br();
|
|
95
97
|
"bar bar"
|
|
98
|
+
input( {type: "text", value: "foobar", readOnly: "~"}),
|
|
96
99
|
div);
|
|
97
100
|
```
|
|
98
101
|
|
|
102
|
+
> [!TIP]
|
|
103
|
+
> Defining a property as `"~"` makes it equal itself, in other words, `open: "~"` is
|
|
104
|
+
> equal `open: "open"`.
|
|
105
|
+
|
|
99
106
|
All these functions/methods have the same parameter structure, the only difference it
|
|
100
107
|
is their names/identifiers, because of this, I will not to list all them here, but I
|
|
101
108
|
will explain their structure.
|
|
@@ -110,6 +117,12 @@ will explain their structure.
|
|
|
110
117
|
example). See [this](#closing-the-element-creation) to learn how to
|
|
111
118
|
disable this optional parameter.
|
|
112
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
|
+
|
|
113
126
|
> [!IMPORTANT]
|
|
114
127
|
> Deprecated tags are not available.
|
|
115
128
|
|
|
@@ -156,12 +169,26 @@ SPAN({ eventListeners: { event: action } });
|
|
|
156
169
|
SPAN({ eventListeners: { event: [action0, actionN] } });
|
|
157
170
|
```
|
|
158
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
|
+
|
|
159
185
|
> [!NOTE]
|
|
160
186
|
> See more about these concepts bellow:
|
|
161
187
|
>
|
|
162
188
|
> * [data-][data-property]
|
|
163
189
|
> * [aria-][aria-property]
|
|
164
190
|
> * [Event Listeners][event-listeners]
|
|
191
|
+
> * [The `style` property][style-property]
|
|
165
192
|
|
|
166
193
|
### Other stuff
|
|
167
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
|
+
}
|
|
71
75
|
|
|
72
|
-
const
|
|
76
|
+
const __htmljs_css_rules__ = (elem, rule, value) =>
|
|
73
77
|
{
|
|
74
|
-
if(
|
|
75
|
-
|
|
78
|
+
if(rule.charAt(0) == "-") // it is a variable
|
|
79
|
+
elem.style.setProperty(rule, value);
|
|
80
|
+
else
|
|
81
|
+
elem.style[rule] = value;
|
|
82
|
+
}
|
|
76
83
|
|
|
77
|
-
|
|
84
|
+
const __htmljs_treat_special_properties__ = (elem, property, htmlProperties) =>
|
|
85
|
+
{
|
|
86
|
+
let behavior;
|
|
87
|
+
|
|
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;
|
|
95
|
+
}
|
|
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;
|
|
84
101
|
}
|
|
85
|
-
|
|
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,23 +127,15 @@ const __htmljs_core__ = (elementTag, htmlProperties, ...children) =>
|
|
|
105
127
|
}
|
|
106
128
|
|
|
107
129
|
for(const PROPERTY in htmlProperties){
|
|
108
|
-
|
|
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}.`));
|
|
130
|
+
if(__htmljs_treat_special_properties__(ELEM, PROPERTY, htmlProperties))
|
|
118
131
|
continue;
|
|
119
|
-
}
|
|
120
132
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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);
|
|
125
139
|
}
|
|
126
140
|
}
|
|
127
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.1
|
|
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"],
|