-
Notifications
You must be signed in to change notification settings - Fork 42
/
html-populators.xsl
208 lines (181 loc) · 8.9 KB
/
html-populators.xsl
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
<?xml version="1.0"?>
<xsl:stylesheet
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template name="add-html-populators">
<xsl:element name="script">
<xsl:attribute name="type">text/javascript</xsl:attribute>
<xsl:text disable-output-escaping="yes">
/* HTML POPULATORS */
var addHiddenFields = function() {
document.querySelectorAll("[data-xsd2html2xml-min], [data-xsd2html2xml-max]").forEach(function(o) {
//add hidden element
var newNode = o.previousElementSibling.cloneNode(true);
newNode.setAttribute("hidden", "");
newNode.querySelectorAll("input, textarea, select").forEach(function(p) {
p.setAttribute("disabled", "");
});
o.parentElement.insertBefore(
newNode, o
);
});
};
var ensureMinimum = function() {
document.querySelectorAll("[data-xsd2html2xml-min], [data-xsd2html2xml-max]").forEach(function(o) {
//add minimum number of elements
if (o.hasAttribute("data-xsd2html2xml-min")) {
//if no minimum, remove element
if (o.getAttribute("data-xsd2html2xml-min") === "0"
//check for input elements existing to handle empty elements
&& o.previousElementSibling.previousElementSibling.querySelector("input, textarea, select")
//check if element has been populated with data from an xml document
&& !o.previousElementSibling.previousElementSibling.querySelector("input, textarea, select").hasAttribute("data-xsd2html2xml-filled")) {
clickRemoveButton(
o.parentElement.children[0].querySelector("legend > button.remove, span > button.remove")
);
//if there is only one allowed element that has been filled, disable the button
} else if (o.getAttribute("data-xsd2html2xml-max") === "1"
//check for input elements existing to handle empty elements
&& o.previousElementSibling.previousElementSibling.querySelector("input, textarea, select")
//check if element has been populated with data from an xml document
&& o.previousElementSibling.previousElementSibling.querySelector("input, textarea, select").hasAttribute("data-xsd2html2xml-filled")) {
o.setAttribute("disabled", "disabled");
//else, add up to minimum number of elements
} else {
var remainder = o.getAttribute("data-xsd2html2xml-min") - (o.parentNode.children.length - 2);
for (i=0; i<remainder; i++) {
clickAddButton(o);
};
};
};
});
};
var xmlToHTML = function(root) {
var xmlDocument;
//check if form was generated from an XML document
if (document.querySelector("meta[name='generator'][content='XSD2HTML2XML v3: https://github.com/MichielCM/xsd2html2xml']").getAttribute("data-xsd2html2xml-source")) {
//parse xml document from attribute
xmlDocument = new DOMParser().parseFromString(
document.querySelector("meta[name='generator'][content='XSD2HTML2XML v3: https://github.com/MichielCM/xsd2html2xml']").getAttribute("data-xsd2html2xml-source"),
"application/xml"
);
//start parsing nodes, providing the root node and the corresponding document element
parseNode(
xmlDocument.childNodes[0],
document.querySelector("[data-xsd2html2xml-xpath = '/".concat(xmlDocument.childNodes[0].nodeName).concat("']"))
);
};
};
var setValue = function(element, value) {
element.querySelector("input, textarea, select").setAttribute("data-xsd2html2xml-filled", "true");
if (element.querySelector("input") !== null) {
if (element.querySelector("input").getAttribute("data-xsd2html2xml-primitive") === "boolean") {
if (value === "true") {
element.querySelector("input").setAttribute("checked", "checked");
};
} else {
element.querySelector("input").setAttribute("value", value);
};
if (element.querySelector("input").getAttribute("type") === "file") {
element.querySelector("input").removeAttribute("required");
element.querySelector("input").setAttribute("data-xsd2html2xml-required", "true");
};
};
if (element.querySelector("textarea") !== null) {
element.querySelector("textarea").textContent = value;
};
if (element.querySelector("select") !== null) {
if (element.querySelector("select").getAttribute("data-xsd2html2xml-primitive") === "idref"
|| element.querySelector("select").getAttribute("data-xsd2html2xml-primitive") === "idrefs") {
globalValuesMap.push({
object: element.querySelector("select"),
values: value.split(/\s+/)
});
/*var values = value.split(/\\s+/);
for (var i=0; i<values.length; i++) {
element.querySelector("select option[value = '".concat(values[i]).concat("']")).setAttribute("selected", "selected");
}*/
} else {
element.querySelector("select option[value = '".concat(value).concat("']")).setAttribute("selected", "selected");
}
};
};
var parseNode = function(node, element) {
//iterate through the node's attributes and fill them out
for (var i=0; i<node.attributes.length; i++) {
var attribute = element.querySelector(
"[data-xsd2html2xml-xpath = '".concat(
element.getAttribute("data-xsd2html2xml-xpath").concat(
"/@".concat(node.attributes[i].nodeName)
//"/@*[name() = \"".concat(node.attributes[i].nodeName).concat("\"]")
)
).concat("']")
);
if (attribute !== null) {
setValue(attribute, node.attributes[i].nodeValue);
};
};
//if there is only one (non-element) node, it must contain the value; note: this will not work for potential mixed="true" support
if (node.childNodes.length === 1 && node.childNodes[0].nodeType === Node.TEXT_NODE) {
//in the case of complexTypes with simpleContents, select the sub-element that actually contains the input element
if (element.querySelectorAll("[data-xsd2html2xml-xpath='".concat(element.getAttribute("data-xsd2html2xml-xpath")).concat("']")).length > 0) {
setValue(element.querySelector("[data-xsd2html2xml-xpath='".concat(element.getAttribute("data-xsd2html2xml-xpath")).concat("']")), node.childNodes[0].nodeValue);
} else {
setValue(element, node.childNodes[0].nodeValue);
};
//else, iterate through the children
} else {
var previousChildName;
for (var i=0; i<node.childNodes.length; i++) {
var childNode = node.childNodes[i];
if (childNode.nodeType === Node.ELEMENT_NODE) {
//find the corresponding element
var childElement = element.querySelector(
"[data-xsd2html2xml-xpath = '".concat(
element.getAttribute("data-xsd2html2xml-xpath").concat(
"/".concat(childNode.nodeName)
//"/*[name() = \"".concat(childNode.nodeName).concat("\"]")
)
).concat("']")
);
//if there is an add-button (and it is not the first child node being parsed), add an element
var button;
if (childElement.parentElement.lastElementChild.nodeName.toLowerCase() === "button") {
button = childElement.parentElement.lastElementChild;
} else if (childElement.parentElement.parentElement.parentElement.lastElementChild.nodeName.toLowerCase() === "button"
&& !childElement.parentElement.parentElement.parentElement.lastElementChild.hasAttribute("data-xsd2html2xml-element")) {
button = childElement.parentElement.parentElement.parentElement.lastElementChild;
};
if (button !== null && childNode.nodeName === previousChildName) {
clickAddButton(button);
parseNode(
childNode,
button.previousElementSibling.previousElementSibling
//childElement.parentElement.lastElementChild.previousElementSibling.previousElementSibling
);
//else, use the already generated element
} else {
parseNode(
childNode,
childElement
);
};
previousChildName = childNode.nodeName;
}
};
}
};
var setDynamicValues = function() {
for (var i=0; i<globalValuesMap.length; i++) {
for (var j=0; j<globalValuesMap[i].values.length; j++) {
globalValuesMap[i].object.querySelector(
"select option[value = '".concat(globalValuesMap[i].values[j]).concat("']")
).setAttribute("selected", "selected");
}
}
};
</xsl:text>
</xsl:element>
</xsl:template>
</xsl:stylesheet>