Fix anchor parsing issue in FormattedText
When parsing an anchor element, allow relative uris. Added unit tests for FormattedText and LinkLabel.
This commit is contained in:
Родитель
ef54d065c7
Коммит
0b819f24b1
|
@ -107,6 +107,8 @@
|
||||||
<Compile Include="Tests\WindowTests.cs" />
|
<Compile Include="Tests\WindowTests.cs" />
|
||||||
<Compile Include="Tests\XwtTest.cs" />
|
<Compile Include="Tests\XwtTest.cs" />
|
||||||
<Compile Include="Tests\InternalChildrenTests.cs" />
|
<Compile Include="Tests\InternalChildrenTests.cs" />
|
||||||
|
<Compile Include="Tests\LinkLabelTests.cs" />
|
||||||
|
<Compile Include="Tests\FormattedTextTests.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -0,0 +1,246 @@
|
||||||
|
//
|
||||||
|
// FormattedTextTests.cs
|
||||||
|
//
|
||||||
|
// Author:
|
||||||
|
// Lluis Sanchez <lluis@xamarin.com>
|
||||||
|
//
|
||||||
|
// 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 = "0<span>12</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (0, ft.Attributes.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ParseIncorrectlyNestedSpan ()
|
||||||
|
{
|
||||||
|
var s = "0<b><u>12</b></u>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (1, ft.Attributes.Count);
|
||||||
|
Assert.IsAssignableFrom<UnderlineTextAttribute> (ft.Attributes [0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UnclosedElements ()
|
||||||
|
{
|
||||||
|
var s = "0<b><u>12</u>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (1, ft.Attributes.Count);
|
||||||
|
Assert.IsAssignableFrom<UnderlineTextAttribute> (ft.Attributes [0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ParseBrokenMarkup ()
|
||||||
|
{
|
||||||
|
var s = "0<b<u>12</u>>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (1, ft.Attributes.Count);
|
||||||
|
Assert.IsAssignableFrom<UnderlineTextAttribute> (ft.Attributes [0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ParseFontWeight ()
|
||||||
|
{
|
||||||
|
var s = "0<b>12</b><span weight='ultrabold'>34</span><span font-weight='Light'>56</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (3, ft.Attributes.Count);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<FontWeightTextAttribute> (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<FontWeightTextAttribute> (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<FontWeightTextAttribute> (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 = "0<i>12</i>3<span font-style='oBlique'>45</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (2, ft.Attributes.Count);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<FontStyleTextAttribute> (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<FontStyleTextAttribute> (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 = "0<s>12</s>3<span strikethrough='true'>45</span>6<span strikethrough='f'>7</span><span strikethrough=''>8</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (4, ft.Attributes.Count);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<StrikethroughTextAttribute> (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<StrikethroughTextAttribute> (ft.Attributes [1]);
|
||||||
|
at = (StrikethroughTextAttribute)ft.Attributes [1];
|
||||||
|
Assert.AreEqual (4, at.StartIndex);
|
||||||
|
Assert.AreEqual (2, at.Count);
|
||||||
|
Assert.IsTrue (at.Strikethrough);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<StrikethroughTextAttribute> (ft.Attributes [2]);
|
||||||
|
at = (StrikethroughTextAttribute)ft.Attributes [2];
|
||||||
|
Assert.AreEqual (7, at.StartIndex);
|
||||||
|
Assert.AreEqual (1, at.Count);
|
||||||
|
Assert.IsFalse (at.Strikethrough);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<StrikethroughTextAttribute> (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 = "0<u>12</u>3<span underline='true'>45</span>6<span underline='f'>7</span><span underline=''>8</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (4, ft.Attributes.Count);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<UnderlineTextAttribute> (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<UnderlineTextAttribute> (ft.Attributes [1]);
|
||||||
|
at = (UnderlineTextAttribute)ft.Attributes [1];
|
||||||
|
Assert.AreEqual (4, at.StartIndex);
|
||||||
|
Assert.AreEqual (2, at.Count);
|
||||||
|
Assert.IsTrue (at.Underline);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<UnderlineTextAttribute> (ft.Attributes [2]);
|
||||||
|
at = (UnderlineTextAttribute)ft.Attributes [2];
|
||||||
|
Assert.AreEqual (7, at.StartIndex);
|
||||||
|
Assert.AreEqual (1, at.Count);
|
||||||
|
Assert.IsFalse (at.Underline);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<UnderlineTextAttribute> (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 = "<a href='http://foo.bar'>01</a><a href='#anchor'>23</a><a href='relative'>45</a><a href='custom:uri'>67</a><a href=''>89</a>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
|
||||||
|
Assert.AreEqual (5, ft.Attributes.Count);
|
||||||
|
Assert.IsAssignableFrom<LinkTextAttribute> (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<LinkTextAttribute> (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<LinkTextAttribute> (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<LinkTextAttribute> (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<LinkTextAttribute> (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 = "0<span color='#ff0000'>12</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (1, ft.Attributes.Count);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<ColorTextAttribute> (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 = "0<span background-color='#0000ff'>12</span>";
|
||||||
|
var ft = FormattedText.FromMarkup (s);
|
||||||
|
Assert.AreEqual (1, ft.Attributes.Count);
|
||||||
|
|
||||||
|
Assert.IsAssignableFrom<BackgroundTextAttribute> (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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
//
|
||||||
|
// LinkLabelTests.cs
|
||||||
|
//
|
||||||
|
// Author:
|
||||||
|
// Lluis Sanchez <lluis@xamarin.com>
|
||||||
|
//
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -109,7 +109,7 @@ namespace Xwt
|
||||||
Uri href = null;
|
Uri href = null;
|
||||||
ReadXmlAttributes (markup, ref k, (name, val) => {
|
ReadXmlAttributes (markup, ref k, (name, val) => {
|
||||||
if (name == "href") {
|
if (name == "href") {
|
||||||
href = new Uri (val);
|
href = new Uri (val, UriKind.RelativeOrAbsolute);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -218,6 +218,7 @@ namespace Xwt
|
||||||
{
|
{
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "font":
|
case "font":
|
||||||
|
case "font-desc":
|
||||||
case "font_desc":
|
case "font_desc":
|
||||||
return new FontTextAttribute () { Font = Font.FromName (val) };
|
return new FontTextAttribute () { Font = Font.FromName (val) };
|
||||||
|
|
||||||
|
@ -229,12 +230,20 @@ namespace Xwt
|
||||||
return new FontSizeTextAttribute () { Size = s };
|
return new FontSizeTextAttribute () { Size = s };
|
||||||
*/
|
*/
|
||||||
case "font_weight":
|
case "font_weight":
|
||||||
|
case "font-weight":
|
||||||
case "weight":
|
case "weight":
|
||||||
FontWeight w;
|
FontWeight w;
|
||||||
if (!Enum.TryParse<FontWeight> (val, true, out w))
|
if (!Enum.TryParse<FontWeight> (val, true, out w))
|
||||||
return null;
|
return null;
|
||||||
return new FontWeightTextAttribute () { Weight = w };
|
return new FontWeightTextAttribute () { Weight = w };
|
||||||
|
|
||||||
|
case "font_style":
|
||||||
|
case "font-style":
|
||||||
|
FontStyle s;
|
||||||
|
if (!Enum.TryParse<FontStyle> (val, true, out s))
|
||||||
|
return null;
|
||||||
|
return new FontStyleTextAttribute () { Style = s };
|
||||||
|
|
||||||
case "foreground":
|
case "foreground":
|
||||||
case "fgcolor":
|
case "fgcolor":
|
||||||
case "color":
|
case "color":
|
||||||
|
@ -244,6 +253,7 @@ namespace Xwt
|
||||||
return new ColorTextAttribute () { Color = c };
|
return new ColorTextAttribute () { Color = c };
|
||||||
|
|
||||||
case "background":
|
case "background":
|
||||||
|
case "background-color":
|
||||||
case "bgcolor":
|
case "bgcolor":
|
||||||
Color bc;
|
Color bc;
|
||||||
if (!Color.TryParse (val, out bc))
|
if (!Color.TryParse (val, out bc))
|
||||||
|
@ -251,13 +261,24 @@ namespace Xwt
|
||||||
return new BackgroundTextAttribute () { Color = bc };
|
return new BackgroundTextAttribute () { Color = bc };
|
||||||
|
|
||||||
case "underline":
|
case "underline":
|
||||||
return new UnderlineTextAttribute ();
|
return new UnderlineTextAttribute () {
|
||||||
|
Underline = ParseBool (val, false)
|
||||||
|
};
|
||||||
|
|
||||||
case "strikethrough":
|
case "strikethrough":
|
||||||
return new StrikethroughTextAttribute ();
|
return new StrikethroughTextAttribute () {
|
||||||
|
Strikethrough = ParseBool (val, false)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return null;
|
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)
|
bool ReadId (string markup, ref int i, out string tag)
|
||||||
{
|
{
|
||||||
|
|
Загрузка…
Ссылка в новой задаче