Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit c8a714b

Browse files
Merge branch 'master' of github.com:icsharpcode/SharpDevelop
2 parents 12fb6ae + 51d8765 commit c8a714b

5 files changed

Lines changed: 161 additions & 3 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using System.Linq;
21+
using System.Text;
22+
using System.Windows.Controls;
23+
using System.Windows.Markup;
24+
using NUnit.Framework;
25+
26+
namespace ICSharpCode.WpfDesign.Tests.Designer
27+
{
28+
[TestFixture]
29+
public class MarkupExtensionModelTests : ModelTestHelper
30+
{
31+
private const string PathWithCommasAndSpaces = "C:\\Folder A\\Sub,Folder,A\\SubFolderB\\file,with,commas and spaces.txt";
32+
private const string Simple = "AbcDef";
33+
34+
[Test]
35+
public void ElementMarkupExtensionWithSimpleString()
36+
{
37+
TestMarkupExtensionPrinter(Simple, true);
38+
}
39+
40+
[Test]
41+
public void ShorthandMarkupExtensionWithSimpleString()
42+
{
43+
TestMarkupExtensionPrinter(Simple, false);
44+
}
45+
46+
[Test]
47+
public void ElementMarkupExtensionWithFilePathString()
48+
{
49+
TestMarkupExtensionPrinter(PathWithCommasAndSpaces, true);
50+
}
51+
52+
[Test]
53+
public void ShorthandMarkupExtensionWithFilePathString()
54+
{
55+
TestMarkupExtensionPrinter(PathWithCommasAndSpaces, false);
56+
}
57+
58+
private void TestMarkupExtensionPrinter(string s, bool useElementStyle)
59+
{
60+
var checkBoxItem = CreateCanvasContext("<CheckBox/>");
61+
var tagProp = checkBoxItem.Properties["Tag"];
62+
63+
tagProp.SetValue(new DataExtension());
64+
tagProp.Value.Properties["Data"].SetValue(s);
65+
66+
string expectedXaml;
67+
68+
if (useElementStyle) {
69+
// Setting this should force element style
70+
tagProp.Value.Properties["Object"].SetValue(new ExampleClass());
71+
72+
expectedXaml = @"<CheckBox>
73+
<CheckBox.Tag>
74+
<t:DataExtension Data=""" + s + @""">
75+
<t:DataExtension.Object>
76+
<t:ExampleClass />
77+
</t:DataExtension.Object>
78+
</t:DataExtension>
79+
</CheckBox.Tag>
80+
</CheckBox>";
81+
} else {
82+
StringBuilder sb = new StringBuilder("<CheckBox Tag=\"{t:Data Data=");
83+
84+
bool containsSpace = s.Contains(' ');
85+
86+
if(containsSpace) {
87+
sb.Append('\'');
88+
}
89+
90+
sb.Append(s.Replace("\\", "\\\\"));
91+
92+
if(containsSpace) {
93+
sb.Append('\'');
94+
}
95+
96+
sb.Append("}\" />");
97+
98+
expectedXaml = sb.ToString();
99+
}
100+
101+
AssertCanvasDesignerOutput(expectedXaml, checkBoxItem.Context);
102+
AssertLog("");
103+
104+
// The following tests that the official XamlReader is parsing the resulting xaml into the
105+
// same string that we are testing, regardless if element or shorthand style is being used.
106+
107+
string xaml = expectedXaml.Insert("<CheckBox".Length, " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
108+
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
109+
"xmlns:t=\"" + DesignerTestsNamespace + "\"");
110+
111+
var checkBox = (CheckBox)System.Windows.Markup.XamlReader.Parse(xaml);
112+
113+
Assert.AreEqual(s, (string)checkBox.Tag);
114+
}
115+
}
116+
117+
public class DataExtension : MarkupExtension
118+
{
119+
public DataExtension()
120+
{
121+
}
122+
123+
public override object ProvideValue(IServiceProvider serviceProvider)
124+
{
125+
return Data;
126+
}
127+
128+
public string Data { get; set; }
129+
130+
public object Object { get; set; }
131+
}
132+
}

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="Designer\EditOperationTests.cs" />
6363
<Compile Include="Designer\FocusNavigatorTests.cs" />
6464
<Compile Include="Designer\MarginHandleTests.cs" />
65+
<Compile Include="Designer\MarkupExtensionModelTests.cs" />
6566
<Compile Include="Designer\MockFocusNavigator.cs" />
6667
<Compile Include="Designer\ModelTestHelper.cs" />
6768
<Compile Include="Designer\ModelTests.cs" />

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public void ExampleClassWithEscapedBraceStringPropAttribute()
9797
");
9898
}
9999

100+
[Test]
101+
public void ExampleClassWithFilePathStringPropAttribute()
102+
{
103+
TestLoading(@"
104+
<t:ExampleClass
105+
xmlns=""http://schemas.microsoft.com/netfx/2007/xaml/presentation""
106+
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
107+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
108+
StringProp=""C:\Folder A\Sub,Folder,A\SubFolderB\file,with,commas and spaces.txt"">
109+
</t:ExampleClass>
110+
");
111+
}
112+
100113
[Test]
101114
public void ExampleClassUseDefaultProperty()
102115
{

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,20 @@ public static string Print(XamlObject obj)
9393
sb.Append("=");
9494

9595
var value = property.PropertyValue;
96-
if (value is XamlTextValue) {
97-
sb.Append((value as XamlTextValue).Text);
96+
var textValue = value as XamlTextValue;
97+
if (textValue != null) {
98+
string text = textValue.Text;
99+
bool containsSpace = text.Contains(' ');
100+
101+
if(containsSpace) {
102+
sb.Append('\'');
103+
}
104+
105+
sb.Append(text.Replace("\\", "\\\\"));
106+
107+
if(containsSpace) {
108+
sb.Append('\'');
109+
}
98110
} else if (value is XamlObject) {
99111
sb.Append(Print(value as XamlObject));
100112
}

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ internal void OnPropertyChanged(XamlProperty property)
223223
foreach(XamlObject propXamlObject in holder.Properties.Where((prop) => prop.IsSet).Select((prop) => prop.PropertyValue).OfType<XamlObject>()) {
224224
XamlObject innerHolder;
225225
bool updateResult = propXamlObject.UpdateXmlAttribute(true, out innerHolder);
226-
Debug.Assert(updateResult);
226+
Debug.Assert(updateResult || innerHolder == null);
227227

228228
if (propXamlObject == this)
229229
isThisUpdated = true;

0 commit comments

Comments
 (0)