-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLToUI-js.js
More file actions
213 lines (192 loc) · 7.96 KB
/
HTMLToUI-js.js
File metadata and controls
213 lines (192 loc) · 7.96 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
/* HTMLToUI-js
*
* Immediately shows a modal that converts raw HTML into UI-js format.
* I'm sick of people using strings on showCustomModal and jQuery method calls :P
* Only side effects from back and forth translation is the loss of onclick attributes in favor of event listeners,
* which is a good thing.
*
* No, you can't localize this. No, I won't add support in a future version.
*
* https://dev.wikia.com/wiki/MediaWiki:UI-js/code.js
*/
$.Deferred(function(def) {
if (window.dev && dev.ui) {
def.resolve(dev.ui);
return;
}
importArticle({
type: 'script',
article: 'u:dev:MediaWiki:UI-js/code.js'
});
mw.hook('dev.ui').add(function(ui) {
def.resolve(ui);
});
}).then(function(ui) {
function convert(html, options) {
function node_to_obj(elem) {
var obj = {};
obj.type = elem.tagName.toLowerCase();
if (elem.hasAttributes()) {
var i = elem.attributes.length;
while (i--) {
var attribute = elem.attributes[i];
if (attribute.name.startsWith('data-')) {
obj.data = obj.data || {};
obj.data[attribute.name.slice(5)] = attribute.value;
} else if (attribute.name.startsWith('on')) {
obj.events = obj.events || {};
obj.events[attribute.name.slice(2)] = elem[attribute.name];
} else if (attribute.name == 'class') {
obj.classes = Array.from(elem.classList.values()); // Browser support? What is that?
} else if (attribute.name == 'style') {
obj.style = {};
var l = elem.style.length;
while (l--) {
var style_prop = elem.style[l];
obj.style[style_prop] = elem.style[style_prop];
}
} else {
obj.attr = obj.attr || {};
obj.attr[attribute.name] = attribute.value;
}
}
}
elem.childNodes.forEach(function(node) {
switch(node.nodeType) {
case 3: // #text
if (node.textContent.trim()) {
obj.text = obj.text || node.textContent.trim();
}
break;
case 1: // Element
obj.children = obj.children || [];
obj.children.push(node_to_obj(node));
break;
}
});
return obj;
}
var dummy = document.createElement('div'),
obj = {};
dummy.innerHTML = html;
if (dummy.children.length > 1) {
alert('Only one top-level element is supported! Consider wrapping your html in a container.');
return html;
} else if (!dummy.children[0]) {
try { // If there is already JSON in, and the user just wants to change the indent amount, why not?
var parsed = JSON.parse(html);
return pretty_javascript_json(parsed, options);
} catch(e) { // Oh, they didn't mean to do that? Then they're stupid.
alert('No children found; did you actually put HTML in?');
return html;
}
}
return pretty_javascript_json(node_to_obj(dummy.children[0]), options);
}
function pretty_javascript_json(obj, options) {
var json = JSON.stringify(obj, null, options.indent);
if (options.quotes) {
json = json.replace(/"((?:\\"|[^"])+)":/g, function(match, content) {
console.log(match, content, options);
var reserved_words = [ 'class', 'for', 'default' ]; // Reserved words sometimes used in HTML
if (content.includes('-') || reserved_words.includes(content)) return match;
return content + ':';
});
}
return json;
}
$.showCustomModal('Convert HTML to UI-js',
ui({
type: 'div',
classes: ['converter-container'],
children: [
{
type: 'div',
text: 'Paste your HTML in the textarea and press the "Convert" button',
classes: ['converter-legend'],
children: [
{
type: 'select',
classes: ['converter-indent'],
children: [
{
type: 'option',
attr: { value: ' ' },
text: 'Indent with one space',
},
{
type: 'option',
attr: { value: ' ' },
text: 'Indent with two spaces',
},
{
type: 'option',
attr: { value: ' ', selected: true },
text: 'Indent with four spaces',
},
{
type: 'option',
attr: { value: ' ' },
text: 'Indent with eight spaces',
},
{
type: 'option',
attr: { value: '\t' },
text: 'Indent with tabs',
},
]
},
{
type: 'span',
children: [
{
type: 'input',
attr: { type: 'checkbox', id: 'converter-checkbox', checked: true },
},
{
type: 'label',
attr: { 'for': 'converter-checkbox' },
text: 'Remove unnecessary quotes in code',
},
]
}
],
},
{
type: 'textarea',
classes: ['converter-textarea'],
style: {
width: '100%',
height: '300px',
resize: 'vertical',
'min-height': '100px', // Oi, Kocka, can you add camelCase CSS prop support in UI-js?
'margin-top': '10px',
}
},
],
}),
{
id: 'converter-modal',
width: 500,
buttons: [{
id: 'converter-close',
message: 'Close',
handler: function() {
$('#converter-modal').closeModal();
},
}, {
id: 'converter-convert',
message: 'Convert',
defaultButton: true,
handler: function() {
var textarea = document.querySelector('.converter-textarea'),
options = {
indent: document.querySelector('.converter-indent').value,
quotes: document.getElementById('converter-checkbox').checked
};
textarea.value = convert(textarea.value, options);
}
}]
}
);
});