diff --git a/lib/widgets.js b/lib/widgets.js index 062239d..a3d9183 100644 --- a/lib/widgets.js +++ b/lib/widgets.js @@ -76,7 +76,7 @@ var input = function (type) { id: f.id || true, classes: w.classes, value: w.formatValue(f.value) - }, userAttrs]); + }, userAttrs, w.attrs || {}]); }; w.getDataRegExp = function () { return dataRegExp; @@ -129,14 +129,14 @@ exports.checkbox = function (opt) { }; w.toHTML = function (name, f) { if (!f) { f = {}; } - return singleTag('input', { + return singleTag('input', [{ type: 'checkbox', name: name, id: f.id || true, classes: w.classes, checked: !!f.value, value: 'on' - }); + }, w.attrs || {}]); }; return w; }; @@ -155,11 +155,11 @@ exports.select = function (opt) { selected: !!(f.value && f.value === k) }, f.choices[k]); }, ''); - return tag('select', { + return tag('select', [{ name: name, id: f.id || true, classes: w.classes - }, optionsHTML); + }, w.attrs || {}], optionsHTML); }; return w; }; @@ -172,13 +172,13 @@ exports.textarea = function (opt) { }; w.toHTML = function (name, f) { if (!f) { f = {}; } - return tag('textarea', { + return tag('textarea', [{ name: name, id: f.id || true, classes: w.classes, rows: opt.rows || null, cols: opt.cols || null - }, f.value || ''); + }, w.attrs || {}], f.value || ''); }; return w; }; @@ -196,14 +196,14 @@ exports.multipleCheckbox = function (opt) { var id = f.id ? f.id + '_' + k : 'id_' + name + '_' + k, checked = Array.isArray(f.value) ? f.value.some(function (v) { return v === k; }) : f.value === k; - html += singleTag('input', { + html += singleTag('input', [{ type: 'checkbox', name: name, id: id, classes: w.classes, value: k, checked: !!checked - }); + }, w.attrs || {}]); // label element html += tag('label', {'for': id}, f.choices[k]); @@ -220,11 +220,10 @@ exports.label = function (opt) { classes: opt.classes || [] }; w.toHTML = function (forID, f) { - var labelAttrs = { + return tag('label', [{ for: forID, classes: w.classes - }; - return tag('label', labelAttrs, opt.content); + }, w.attrs || {}], opt.content); }; return w; }; @@ -242,14 +241,14 @@ exports.multipleRadio = function (opt) { var id = f.id ? f.id + '_' + k : 'id_' + name + '_' + k, checked = Array.isArray(f.value) ? f.value.some(function (v) { return v === k; }) : f.value === k; - html += singleTag('input', { + html += singleTag('input', [{ type: 'radio', name: name, id: id, classes: w.classes, value: k, checked: !!checked - }); + }, w.attrs || {}]); // label element html += tag('label', {for: id}, f.choices[k]); @@ -274,12 +273,12 @@ exports.multipleSelect = function (opt) { selected: !!selected }, f.choices[k]); }, ''); - return tag('select', { + return tag('select', [{ multiple: true, name: name, id: f.id || true, classes: w.classes - }, optionsHTML); + }, w.attrs || {}], optionsHTML); }; return w; }; diff --git a/test/test-widgets.js b/test/test-widgets.js index 2706858..de42bc9 100644 --- a/test/test-widgets.js +++ b/test/test-widgets.js @@ -306,3 +306,13 @@ exports.label = function (test) { test.done(); }; +exports['dynamic widget attributes'] = function(test) { + var re = /autocomplete="no"/; + Object.keys(forms.widgets).forEach(function(name) { + var w = forms.widgets[name](); + w.attrs = {autocomplete: 'no'}; + var html = w.toHTML('test', {choices: {foo: 'bar'}}); + test.equals(re.test(html), true); + }); + test.done(); +};