Correctly materialize and toHTML textareas

This commit is contained in:
David Greenspan
2013-12-07 10:35:36 -08:00
parent 0ec049008b
commit f3715306e1
2 changed files with 61 additions and 4 deletions

View File

@@ -301,9 +301,19 @@ var materialize = function (node, parent, before, parentComponent) {
updateAttributes(elem, attrs);
}
}
_.each(node, function (child) {
materialize(child, elem, null, parentComponent);
});
if (node.tagName === 'TEXTAREA') {
var value = '';
_.each(node, function (child) {
// XXX put the functionality of attributeValueToString in
// something like UI.toText(node)
value += attributeValueToString(child);
});
elem.value = value;
} else {
_.each(node, function (child) {
materialize(child, elem, null, parentComponent);
});
}
insert(elem, parent, before);
} else if (type === 'array') {
_.each(node, function (child) {
@@ -507,9 +517,17 @@ var toHTML = function (node, parentComponent) {
});
}
result += '>';
var contents = '';
_.each(node, function (child) {
result += toHTML(child, parentComponent);
contents += toHTML(child, parentComponent);
});
if (node.tagName === 'TEXTAREA' &&
contents.slice(0, 1) === '\n') {
// TEXTAREA will absorb a newline, so if we see one, add
// another one.
result += '\n';
}
result += contents;
if (node.length || ! HTML.isVoidElement(node.tagName)) {
// "Void" elements like BR are the only ones that don't get a close
// tag in HTML5. They shouldn't have contents, either, so we could

View File

@@ -12,6 +12,7 @@ var UL = HTML.Tag.UL;
var LI = HTML.Tag.LI;
var SPAN = HTML.Tag.SPAN;
var HR = HTML.Tag.HR;
var TEXTAREA = HTML.Tag.TEXTAREA;
Tinytest.add("ui - render - basic", function (test) {
var run = function (input, expectedInnerHTML, expectedHTML, expectedCode) {
@@ -70,6 +71,44 @@ Tinytest.add("ui - render - basic", function (test) {
'<div class="foo"><ul><li><p><a href="#one">One</a></p></li><li><p>Two<br>Three</p></li></ul></div>',
'<div class="foo"><ul><li><p><a href="#one">One</a></p></li><li><p>Two<br>Three</p></li></ul></div>',
'HTML.Tag.DIV({"class": "foo"}, HTML.Tag.UL(HTML.Tag.LI(HTML.Tag.P(HTML.Tag.A({href: "#one"}, "One"))), HTML.Tag.LI(HTML.Tag.P("Two", HTML.Tag.BR(), "Three"))))');
});
Tinytest.add("ui - render - textarea", function (test) {
var run = function (optNode, text, html, code) {
if (typeof optNode === 'string') {
// called with args (text, html, code)
code = html;
html = text;
text = optNode;
optNode = null;
}
var div = document.createElement("DIV");
var node = TEXTAREA(optNode || text);
materialize(node, div);
test.equal(div.querySelector('textarea').value, text);
test.equal(UI.toHTML(node), html);
test.equal(UI.toCode(node), code);
};
run('Hello',
'<textarea>Hello</textarea>',
'HTML.Tag.TEXTAREA("Hello")');
run('\nHello',
'<textarea>\n\nHello</textarea>',
'HTML.Tag.TEXTAREA("\\nHello")');
run('</textarea>',
'<textarea>&lt;/textarea></textarea>',
'HTML.Tag.TEXTAREA("</textarea>")');
run(CharRef({html: '&amp;', str: '&'}),
'&',
'<textarea>&amp;</textarea>',
'HTML.Tag.TEXTAREA(HTML.CharRef({html: "&amp;", str: "&"}))');
});
Tinytest.add("ui - render - closures", function (test) {