diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs new file mode 100644 index 0000000000..4d51eb94c8 --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs @@ -0,0 +1,132 @@ +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team +// +// 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 System.Linq; +using System.Text; +using System.Windows.Controls; +using System.Windows.Markup; +using NUnit.Framework; + +namespace ICSharpCode.WpfDesign.Tests.Designer +{ + [TestFixture] + public class MarkupExtensionModelTests : ModelTestHelper + { + private const string PathWithCommasAndSpaces = "C:\\Folder A\\Sub,Folder,A\\SubFolderB\\file,with,commas and spaces.txt"; + private const string Simple = "AbcDef"; + + [Test] + public void ElementMarkupExtensionWithSimpleString() + { + TestMarkupExtensionPrinter(Simple, true); + } + + [Test] + public void ShorthandMarkupExtensionWithSimpleString() + { + TestMarkupExtensionPrinter(Simple, false); + } + + [Test] + public void ElementMarkupExtensionWithFilePathString() + { + TestMarkupExtensionPrinter(PathWithCommasAndSpaces, true); + } + + [Test] + public void ShorthandMarkupExtensionWithFilePathString() + { + TestMarkupExtensionPrinter(PathWithCommasAndSpaces, false); + } + + private void TestMarkupExtensionPrinter(string s, bool useElementStyle) + { + var checkBoxItem = CreateCanvasContext("<CheckBox/>"); + var tagProp = checkBoxItem.Properties["Tag"]; + + tagProp.SetValue(new DataExtension()); + tagProp.Value.Properties["Data"].SetValue(s); + + string expectedXaml; + + if (useElementStyle) { + // Setting this should force element style + tagProp.Value.Properties["Object"].SetValue(new ExampleClass()); + + expectedXaml = @"<CheckBox> + <CheckBox.Tag> + <t:DataExtension Data=""" + s + @"""> + <t:DataExtension.Object> + <t:ExampleClass /> + </t:DataExtension.Object> + </t:DataExtension> + </CheckBox.Tag> +</CheckBox>"; + } else { + StringBuilder sb = new StringBuilder("<CheckBox Tag=\"{t:Data Data="); + + bool containsSpace = s.Contains(' '); + + if(containsSpace) { + sb.Append('\''); + } + + sb.Append(s.Replace("\\", "\\\\")); + + if(containsSpace) { + sb.Append('\''); + } + + sb.Append("}\" />"); + + expectedXaml = sb.ToString(); + } + + AssertCanvasDesignerOutput(expectedXaml, checkBoxItem.Context); + AssertLog(""); + + // The following tests that the official XamlReader is parsing the resulting xaml into the + // same string that we are testing, regardless if element or shorthand style is being used. + + string xaml = expectedXaml.Insert("<CheckBox".Length, " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " + + "xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " + + "xmlns:t=\"" + DesignerTestsNamespace + "\""); + + var checkBox = (CheckBox)System.Windows.Markup.XamlReader.Parse(xaml); + + Assert.AreEqual(s, (string)checkBox.Tag); + } + } + + public class DataExtension : MarkupExtension + { + public DataExtension() + { + } + + public override object ProvideValue(IServiceProvider serviceProvider) + { + return Data; + } + + public string Data { get; set; } + + public object Object { get; set; } + } +} diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj index 73295de0bd..9ed980b88b 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj @@ -62,6 +62,7 @@ <Compile Include="Designer\EditOperationTests.cs" /> <Compile Include="Designer\FocusNavigatorTests.cs" /> <Compile Include="Designer\MarginHandleTests.cs" /> + <Compile Include="Designer\MarkupExtensionModelTests.cs" /> <Compile Include="Designer\MockFocusNavigator.cs" /> <Compile Include="Designer\ModelTestHelper.cs" /> <Compile Include="Designer\ModelTests.cs" /> diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs index 966bf37b5d..1854242157 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs @@ -97,6 +97,19 @@ namespace ICSharpCode.WpfDesign.Tests.XamlDom "); } + [Test] + public void ExampleClassWithFilePathStringPropAttribute() + { + TestLoading(@" +<t:ExampleClass + xmlns=""http://schemas.microsoft.com/netfx/2007/xaml/presentation"" + xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @""" + xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" + StringProp=""C:\Folder A\Sub,Folder,A\SubFolderB\file,with,commas and spaces.txt""> +</t:ExampleClass> + "); + } + [Test] public void ExampleClassUseDefaultProperty() { diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs index a1a849ad2d..d7b1b1949e 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs @@ -93,8 +93,20 @@ namespace ICSharpCode.WpfDesign.XamlDom sb.Append("="); var value = property.PropertyValue; - if (value is XamlTextValue) { - sb.Append((value as XamlTextValue).Text); + var textValue = value as XamlTextValue; + if (textValue != null) { + string text = textValue.Text; + bool containsSpace = text.Contains(' '); + + if(containsSpace) { + sb.Append('\''); + } + + sb.Append(text.Replace("\\", "\\\\")); + + if(containsSpace) { + sb.Append('\''); + } } else if (value is XamlObject) { sb.Append(Print(value as XamlObject)); } diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs index 5af9e0188b..e8275a34e4 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs @@ -223,7 +223,7 @@ namespace ICSharpCode.WpfDesign.XamlDom foreach(XamlObject propXamlObject in holder.Properties.Where((prop) => prop.IsSet).Select((prop) => prop.PropertyValue).OfType<XamlObject>()) { XamlObject innerHolder; bool updateResult = propXamlObject.UpdateXmlAttribute(true, out innerHolder); - Debug.Assert(updateResult); + Debug.Assert(updateResult || innerHolder == null); if (propXamlObject == this) isThisUpdated = true;