From 0b819f24b153bf3e562085faba1b8c6b6f6d7363 Mon Sep 17 00:00:00 2001 From: Lluis Sanchez Date: Thu, 14 Nov 2013 10:20:43 +0100 Subject: [PATCH] Fix anchor parsing issue in FormattedText When parsing an anchor element, allow relative uris. Added unit tests for FormattedText and LinkLabel. --- Testing/GtkTestRunner.csproj | 2 + Testing/Tests/FormattedTextTests.cs | 246 ++++++++++++++++++++++++++++ Testing/Tests/LinkLabelTests.cs | 39 +++++ Xwt/Xwt/FormattedText.cs | 29 +++- 4 files changed, 312 insertions(+), 4 deletions(-) create mode 100644 Testing/Tests/FormattedTextTests.cs create mode 100644 Testing/Tests/LinkLabelTests.cs diff --git a/Testing/GtkTestRunner.csproj b/Testing/GtkTestRunner.csproj index 8e060068..a7233e20 100644 --- a/Testing/GtkTestRunner.csproj +++ b/Testing/GtkTestRunner.csproj @@ -107,6 +107,8 @@ + + diff --git a/Testing/Tests/FormattedTextTests.cs b/Testing/Tests/FormattedTextTests.cs new file mode 100644 index 00000000..fb984140 --- /dev/null +++ b/Testing/Tests/FormattedTextTests.cs @@ -0,0 +1,246 @@ +// +// FormattedTextTests.cs +// +// Author: +// Lluis Sanchez +// +// Copyright (c) 2013 Xamarin Inc +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; +using Xwt.Drawing; + +namespace Xwt +{ + public class FormattedTextTests + { + [Test] + public void ParseEmptySpan () + { + var s = "012"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (0, ft.Attributes.Count); + } + + [Test] + public void ParseIncorrectlyNestedSpan () + { + var s = "012"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (1, ft.Attributes.Count); + Assert.IsAssignableFrom (ft.Attributes [0]); + } + + [Test] + public void UnclosedElements () + { + var s = "012"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (1, ft.Attributes.Count); + Assert.IsAssignableFrom (ft.Attributes [0]); + } + + [Test] + public void ParseBrokenMarkup () + { + var s = "012>"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (1, ft.Attributes.Count); + Assert.IsAssignableFrom (ft.Attributes [0]); + } + + [Test] + public void ParseFontWeight () + { + var s = "0123456"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (3, ft.Attributes.Count); + + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (FontWeightTextAttribute)ft.Attributes [0]; + Assert.AreEqual (1, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (FontWeight.Bold, at.Weight); + + Assert.IsAssignableFrom (ft.Attributes [1]); + at = (FontWeightTextAttribute)ft.Attributes [1]; + Assert.AreEqual (3, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (FontWeight.Ultrabold, at.Weight); + + Assert.IsAssignableFrom (ft.Attributes [2]); + at = (FontWeightTextAttribute)ft.Attributes [2]; + Assert.AreEqual (5, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (FontWeight.Light, at.Weight); + } + + [Test] + public void ParseItalic () + { + var s = "012345"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (2, ft.Attributes.Count); + + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (FontStyleTextAttribute)ft.Attributes [0]; + Assert.AreEqual (1, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (FontStyle.Italic, at.Style); + + Assert.IsAssignableFrom (ft.Attributes [1]); + at = (FontStyleTextAttribute)ft.Attributes [1]; + Assert.AreEqual (4, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (FontStyle.Oblique, at.Style); + } + + [Test] + public void ParseStrikethrough () + { + var s = "012345678"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (4, ft.Attributes.Count); + + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (StrikethroughTextAttribute)ft.Attributes [0]; + Assert.AreEqual (1, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.IsTrue (at.Strikethrough); + + Assert.IsAssignableFrom (ft.Attributes [1]); + at = (StrikethroughTextAttribute)ft.Attributes [1]; + Assert.AreEqual (4, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.IsTrue (at.Strikethrough); + + Assert.IsAssignableFrom (ft.Attributes [2]); + at = (StrikethroughTextAttribute)ft.Attributes [2]; + Assert.AreEqual (7, at.StartIndex); + Assert.AreEqual (1, at.Count); + Assert.IsFalse (at.Strikethrough); + + Assert.IsAssignableFrom (ft.Attributes [3]); + at = (StrikethroughTextAttribute)ft.Attributes [3]; + Assert.AreEqual (8, at.StartIndex); + Assert.AreEqual (1, at.Count); + Assert.IsFalse (at.Strikethrough); + } + + [Test] + public void ParseUnderline () + { + var s = "012345678"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (4, ft.Attributes.Count); + + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (UnderlineTextAttribute)ft.Attributes [0]; + Assert.AreEqual (1, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.IsTrue (at.Underline); + + Assert.IsAssignableFrom (ft.Attributes [1]); + at = (UnderlineTextAttribute)ft.Attributes [1]; + Assert.AreEqual (4, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.IsTrue (at.Underline); + + Assert.IsAssignableFrom (ft.Attributes [2]); + at = (UnderlineTextAttribute)ft.Attributes [2]; + Assert.AreEqual (7, at.StartIndex); + Assert.AreEqual (1, at.Count); + Assert.IsFalse (at.Underline); + + Assert.IsAssignableFrom (ft.Attributes [3]); + at = (UnderlineTextAttribute)ft.Attributes [3]; + Assert.AreEqual (8, at.StartIndex); + Assert.AreEqual (1, at.Count); + Assert.IsFalse (at.Underline); + } + + [Test] + public void ParseAnchor () + { + var s = "0123456789"; + var ft = FormattedText.FromMarkup (s); + + Assert.AreEqual (5, ft.Attributes.Count); + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (LinkTextAttribute)ft.Attributes [0]; + Assert.AreEqual (0, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (new Uri ("http://foo.bar", UriKind.RelativeOrAbsolute), at.Target); + + Assert.IsAssignableFrom (ft.Attributes [1]); + at = (LinkTextAttribute)ft.Attributes [1]; + Assert.AreEqual (2, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (new Uri ("#anchor", UriKind.RelativeOrAbsolute), at.Target); + + Assert.IsAssignableFrom (ft.Attributes [2]); + at = (LinkTextAttribute)ft.Attributes [2]; + Assert.AreEqual (4, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (new Uri ("relative", UriKind.RelativeOrAbsolute), at.Target); + + Assert.IsAssignableFrom (ft.Attributes [3]); + at = (LinkTextAttribute)ft.Attributes [3]; + Assert.AreEqual (6, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (new Uri ("custom:uri", UriKind.RelativeOrAbsolute), at.Target); + + Assert.IsAssignableFrom (ft.Attributes [4]); + at = (LinkTextAttribute)ft.Attributes [4]; + Assert.AreEqual (8, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (new Uri ("", UriKind.RelativeOrAbsolute), at.Target); + } + + [Test] + public void ParseColor () + { + var s = "012"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (1, ft.Attributes.Count); + + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (ColorTextAttribute)ft.Attributes [0]; + Assert.AreEqual (1, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (Colors.Red, at.Color); + } + + [Test] + public void ParseBackgroundColor () + { + var s = "012"; + var ft = FormattedText.FromMarkup (s); + Assert.AreEqual (1, ft.Attributes.Count); + + Assert.IsAssignableFrom (ft.Attributes [0]); + var at = (BackgroundTextAttribute)ft.Attributes [0]; + Assert.AreEqual (1, at.StartIndex); + Assert.AreEqual (2, at.Count); + Assert.AreEqual (Colors.Blue, at.Color); + } + } +} + diff --git a/Testing/Tests/LinkLabelTests.cs b/Testing/Tests/LinkLabelTests.cs new file mode 100644 index 00000000..71f40188 --- /dev/null +++ b/Testing/Tests/LinkLabelTests.cs @@ -0,0 +1,39 @@ +// +// LinkLabelTests.cs +// +// Author: +// Lluis Sanchez +// +// Copyright (c) 2013 Xamarin Inc +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; + +namespace Xwt +{ + public class LinkLabelTests: WidgetTests + { + public override Widget CreateWidget () + { + return new LinkLabel ("Hello World"); + } + } +} + diff --git a/Xwt/Xwt/FormattedText.cs b/Xwt/Xwt/FormattedText.cs index c308f434..74009af9 100644 --- a/Xwt/Xwt/FormattedText.cs +++ b/Xwt/Xwt/FormattedText.cs @@ -109,7 +109,7 @@ namespace Xwt Uri href = null; ReadXmlAttributes (markup, ref k, (name, val) => { if (name == "href") { - href = new Uri (val); + href = new Uri (val, UriKind.RelativeOrAbsolute); return true; } return false; @@ -218,6 +218,7 @@ namespace Xwt { switch (name) { case "font": + case "font-desc": case "font_desc": return new FontTextAttribute () { Font = Font.FromName (val) }; @@ -229,12 +230,20 @@ namespace Xwt return new FontSizeTextAttribute () { Size = s }; */ case "font_weight": + case "font-weight": case "weight": FontWeight w; if (!Enum.TryParse (val, true, out w)) return null; return new FontWeightTextAttribute () { Weight = w }; + case "font_style": + case "font-style": + FontStyle s; + if (!Enum.TryParse (val, true, out s)) + return null; + return new FontStyleTextAttribute () { Style = s }; + case "foreground": case "fgcolor": case "color": @@ -244,6 +253,7 @@ namespace Xwt return new ColorTextAttribute () { Color = c }; case "background": + case "background-color": case "bgcolor": Color bc; if (!Color.TryParse (val, out bc)) @@ -251,13 +261,24 @@ namespace Xwt return new BackgroundTextAttribute () { Color = bc }; case "underline": - return new UnderlineTextAttribute (); - + return new UnderlineTextAttribute () { + Underline = ParseBool (val, false) + }; + case "strikethrough": - return new StrikethroughTextAttribute (); + return new StrikethroughTextAttribute () { + Strikethrough = ParseBool (val, false) + }; } return null; } + + bool ParseBool (string s, bool defaultValue) + { + if (s.Length == 0) + return defaultValue; + return string.Equals (s, "true", StringComparison.OrdinalIgnoreCase); + } bool ReadId (string markup, ref int i, out string tag) {