2020-01-26 16:21:20 +01:00
|
|
|
/**
|
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
|
* @description This is the form utilities module.
|
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
import tpl_field from "../templates/field.html";
|
|
|
|
import u from "./core";
|
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
/**
|
|
|
|
* Takes an HTML DOM and turns it into an XForm field.
|
|
|
|
* @private
|
|
|
|
* @method u#webForm2xForm
|
|
|
|
* @param { DOMElement } field - the field to convert
|
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
u.webForm2xForm = function (field) {
|
2020-09-25 18:43:37 +02:00
|
|
|
const name = field.getAttribute('name');
|
|
|
|
if (!name) {
|
|
|
|
return null; // See #1924
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
let value;
|
|
|
|
if (field.getAttribute('type') === 'checkbox') {
|
|
|
|
value = field.checked && 1 || 0;
|
|
|
|
} else if (field.tagName == "TEXTAREA") {
|
2020-03-06 15:19:48 +01:00
|
|
|
value = field.value.split('\n').filter(s => s.trim());
|
2018-10-23 03:41:38 +02:00
|
|
|
} else if (field.tagName == "SELECT") {
|
|
|
|
value = u.getSelectValues(field);
|
|
|
|
} else {
|
|
|
|
value = field.value;
|
|
|
|
}
|
2020-09-25 18:43:37 +02:00
|
|
|
return u.stringToNode(tpl_field({ name, value }));
|
2018-10-23 03:41:38 +02:00
|
|
|
};
|
|
|
|
export default u;
|