Merge pull request #155 from mrange/pr/mrange/text_optimization

Node.Text optimization
This commit is contained in:
Frank A. Krueger 2018-05-13 11:08:22 -07:00 committed by GitHub
commit 2c5efd7cf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -35,6 +35,14 @@ namespace Ooui
return sb.ToString (); return sb.ToString ();
} }
set { set {
if (Children.Count == 1) {
var textNode = Children[0] as TextNode;
if (textNode != null) {
textNode.Text = value;
return;
}
}
ReplaceAll (new TextNode (value ?? "")); ReplaceAll (new TextNode (value ?? ""));
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Text;
#if NUNIT #if NUNIT
using NUnit.Framework; using NUnit.Framework;
@ -68,5 +69,48 @@ namespace Tests
p.Receive (Message.Event (b.Id, "change", "please work")); p.Receive (Message.Event (b.Id, "change", "please work"));
Assert.AreEqual ("please work", b.Value); Assert.AreEqual ("please work", b.Value);
} }
[TestMethod]
public void Text ()
{
var div = new Div ();
// Log tracks all messages sent due to updates
var log = new StringBuilder ();
log.AppendLine ();
void Parent_MessageSent (Message obj)
{
log.AppendLine (obj.ToString ());
}
div.MessageSent += Parent_MessageSent;
var divId = div.Id;
log.AppendLine ("#1 - Inserts new text node");
div.Text = "Hello";
Assert.AreEqual (1, div.Children.Count);
var text = div.Children[0];
var textId = text.Id;
Assert.AreEqual ("Hello", div.Text);
Assert.AreEqual ("Hello", text.Text);
log.AppendLine ("#2 - Sets text node text to There");
div.Text = "There";
Assert.AreEqual ("There", div.Text);
Assert.AreEqual ("There", text.Text);
var expected = string.Format(@"
#1 - Inserts new text node
{{""m"":""call"",""id"":""{0}"",""k"":""insertBefore"",""v"":[""{1}"",null]}}
#2 - Sets text node text to There
{{""m"":""set"",""id"":""{1}"",""k"":""data"",""v"":""There""}}
", divId, textId).Replace ("\r\n", "\n");
var actual = log.ToString ().Replace ("\r\n", "\n");
Assert.AreEqual (expected, actual);
}
} }
} }