-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.js
More file actions
285 lines (221 loc) · 6.7 KB
/
html.js
File metadata and controls
285 lines (221 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/javascript/blob/master/LICENSE
*/
// html
// script containing event listeners functions for target elements
// import
import { Arr, Bool, Pojo, Str, Vari } from '../index.js';
// export
export default {
// valueAttr
// retourne l'attribut à utiliser pour la valeur d'un tag selflcosing
valueAttr: {
br: 'data-value',
hr: 'data-value',
img: 'src',
meta: 'content',
link: 'href',
input: 'value'
},
// htmlEscapes
// convertiseur pour les caractères html à escape
htmlEscapes: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
},
// isSelfClosing
// retourne vrai si la balise se ferme dans l'ouverture
isSelfClosing: function(tag)
{
return Arr.in(tag,['br','hr','img','meta','link','input'])
},
// escape
// permet de faire un escape des caractères html dangereux sur une string
// transforme < > ' " &
escape: function(value)
{
let r = null;
const $inst = this;
Str.typecheck(value);
return value.replace(/[&<>"']/g,function(value) {
return $inst.htmlEscapes[value];
});
},
// start
// ouvre une tag html
start: function(tag,value,attr)
{
let r = '';
Str.typecheck(tag,true);
const isSelfClosing = this.isSelfClosing(tag);
const attrStr = this.attr(attr,tag,value);
r += "<";
r += tag;
if(Str.isNotEmpty(attrStr))
{
r += " ";
r += attrStr;
}
if(isSelfClosing === true)
r += "/>";
else
{
r += ">";
r += this.value(value);
}
return r;
},
// end
// ferme une tag html
end: function(tag)
{
let r = '';
Str.typecheck(tag,true);
const isSelfClosing = this.isSelfClosing(tag);
if(isSelfClosing === false)
{
r += "</";
r += tag;
r += ">";
}
return r;
},
// value
// permet de préparer la valeur du input
value: function(value,isAttr)
{
if(value === true)
value = (isAttr === true)? 1:' ';
else if(value === false)
value = (isAttr === true)? 0:'';
if(isAttr !== true)
{
if(Pojo.is(value))
value = Pojo.values(value);
if(Arr.is(value))
value = value.join(', ');
}
return Str.cast(value,true);
},
// attr
// génère la string d'attribut pour la balise
// possible de fournir une tag et une valeur
attr: function(attr,tag,value)
{
let r = '';
const isSelfClosing = this.isSelfClosing(tag);
attr = this.attrToPojo(attr);
if(isSelfClosing === true && Pojo.keyExists(tag,this.valueAttr))
{
const valueAttr = Pojo.get(tag,this.valueAttr);
if(valueAttr != null)
{
const current = Pojo.get(valueAttr,attr);
if(current == null || value != null)
attr[valueAttr] = this.value(value,true);
}
}
attr = this.prepareAttr(attr);
r = Pojo.str(attr,"="," ",true);
return r;
},
// attrToPojo
// permet de transformer un argument attr en pojo
attrToPojo: function(attr)
{
let r = {};
if(Str.isNotEmpty(attr))
r = {class: attr};
if(Pojo.is(attr))
r = attr;
return r;
},
// prepareAttr
// utilisé pour préparer un objet attr
// remplace les clés camelcase
// gestion de la clé data contenant un objet
// gestion des classes en tableau
prepareAttr: function(attr)
{
let r = {};
Pojo.typecheck(attr);
const defaultKeyValue = function(key,value) {
return {
key: Str.fromCamelCase('-',key),
value: (Bool.is(value))? Bool.toInt(value):value
}
};
Pojo.each(attr,function(value,key) {
const keyValue = defaultKeyValue(key,value);
key = keyValue.key;
value = keyValue.value;
if(key === 'class' && Arr.is(value))
value = value.join(' ');
else if(key === 'data' && Pojo.is(value))
{
Pojo.each(value,function(value2,key2) {
const keyValue2 = defaultKeyValue(key2,value2);
const newKey = 'data-'+keyValue2.key;
r[newKey] = keyValue2.value;
});
value = null;
}
if(value != null)
r[key] = value;
});
return r;
},
// tag
// ouvre et ferme une tag avec contenu et attribut
tag: function(tag,value,attr)
{
return this.start(tag,value,attr)+this.end(tag);
},
// tagCond
// ouvre et ferme une tag avec contenu et attribut seulement si la valeur n'est pas vide
tagCond: function(tag,value,attr)
{
return (Vari.isNotEmpty(this.value(value)))? this.tag(tag,value,attr):'';
},
// div
// ouvre et ferme une tag div avec contenu et attribut
div: function(value,attr)
{
return this.tag('div',value,attr);
},
// span
// ouvre et ferme une tag span avec contenu et attribut
span: function(value,attr)
{
return this.tag('span',value,attr);
},
// ul
// ouvre et ferme une tag ul avec contenu et attribut
ul: function(value,attr)
{
return this.tag('ul',value,attr);
},
// li
// ouvre et ferme une tag li avec contenu et attribut
li: function(value,attr)
{
return this.tag('li',value,attr);
},
// input
// ouvre et ferme une tag input avec valeur et attribut
input: function(value,attr)
{
return this.tag('input',value,Pojo.replace({type: 'text'}, this.attrToPojo(attr)));
},
// button
// ouvre et ferme une tag button avec contenu et attribut
button: function(value,attr)
{
return this.tag('button',value,Pojo.replace({type: 'button'}, this.attrToPojo(attr)));
}
}