[CSS] only cascade inheritable properties (#1767)

- fixes #1766
This commit is contained in:
Stephane Delcroix 2018-02-01 12:51:46 +01:00 коммит произвёл GitHub
Родитель 85334fd4a0
Коммит 1133771bdf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 93 добавлений и 19 удалений

Просмотреть файл

@ -20,7 +20,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
public void GetPropertyDefinedOnParent()
{
var label = new Label();
var bp = ((IStylable)label).GetProperty("background-color");
var bp = ((IStylable)label).GetProperty("background-color", false);
Assert.AreSame(VisualElement.BackgroundColorProperty, bp);
}
@ -28,7 +28,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
public void GetPropertyDefinedOnType()
{
var label = new Label();
var bp = ((IStylable)label).GetProperty("color");
var bp = ((IStylable)label).GetProperty("color", false);
Assert.AreSame(Label.TextColorProperty, bp);
}
@ -36,7 +36,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
public void GetPropertyDefinedOnType2()
{
var entry = new Entry();
var bp = ((IStylable)entry).GetProperty("color");
var bp = ((IStylable)entry).GetProperty("color", false);
Assert.AreSame(Entry.TextColorProperty, bp);
}
@ -44,7 +44,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
public void GetInvalidPropertyForType()
{
var grid = new Grid();
var bp = ((IStylable)grid).GetProperty("color");
var bp = ((IStylable)grid).GetProperty("color", false);
Assert.Null(bp);
}
@ -52,7 +52,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
public void GetPropertyDefinedOnPropertyOwnerType()
{
var frame = new Frame();
var bp = ((IStylable)frame).GetProperty("padding-left");
var bp = ((IStylable)frame).GetProperty("padding-left", false);
Assert.That(bp, Is.SameAs(PaddingElement.PaddingLeftProperty));
}
@ -60,7 +60,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
public void GetNonPublicProperty()
{
var label = new Label();
var bp = ((IStylable)label).GetProperty("margin-right");
var bp = ((IStylable)label).GetProperty("margin-right", false);
Assert.That(bp, Is.SameAs(View.MarginRightProperty));
}

Просмотреть файл

@ -54,6 +54,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
[Test]
public void StylesAreCascading()
{
//color should cascade, background-color should not
var styleString = @"background-color: #ff0000; color: #00ff00;";
var style = Style.Parse(new CssReader(new StringReader(styleString)), '}');
Assume.That(style, Is.Not.Null);
@ -71,7 +72,7 @@ namespace Xamarin.Forms.StyleSheets.UnitTests
style.Apply(layout);
Assert.That(layout.BackgroundColor, Is.EqualTo(Color.Red));
Assert.That(label.BackgroundColor, Is.EqualTo(Color.Red));
Assert.That(label.BackgroundColor, Is.EqualTo(Color.Default));
Assert.That(label.TextColor, Is.EqualTo(Color.Lime));
}

Просмотреть файл

@ -38,11 +38,11 @@ using Xamarin.Forms.StyleSheets;
[assembly: StyleProperty("background-image", typeof(Page), nameof(Page.BackgroundImageProperty))]
[assembly: StyleProperty("border-color", typeof(Button), nameof(BorderElement.BorderColorProperty))]
[assembly: StyleProperty("border-width", typeof(Button), nameof(Button.BorderWidthProperty))]
[assembly: StyleProperty("color", typeof(ITextElement), nameof(TextElement.TextColorProperty))]
[assembly: StyleProperty("direction", typeof(VisualElement), nameof(VisualElement.FlowDirectionProperty))]
[assembly: StyleProperty("font-family", typeof(IFontElement), nameof(FontElement.FontFamilyProperty))]
[assembly: StyleProperty("font-size", typeof(IFontElement), nameof(FontElement.FontSizeProperty))]
[assembly: StyleProperty("font-style", typeof(IFontElement), nameof(FontElement.FontAttributesProperty))]
[assembly: StyleProperty("color", typeof(ITextElement), nameof(TextElement.TextColorProperty), Inherited = true)]
[assembly: StyleProperty("direction", typeof(VisualElement), nameof(VisualElement.FlowDirectionProperty), Inherited = true)]
[assembly: StyleProperty("font-family", typeof(IFontElement), nameof(FontElement.FontFamilyProperty), Inherited = true)]
[assembly: StyleProperty("font-size", typeof(IFontElement), nameof(FontElement.FontSizeProperty), Inherited = true)]
[assembly: StyleProperty("font-style", typeof(IFontElement), nameof(FontElement.FontAttributesProperty), Inherited = true)]
[assembly: StyleProperty("height", typeof(VisualElement), nameof(VisualElement.HeightRequestProperty))]
[assembly: StyleProperty("margin", typeof(View), nameof(View.MarginProperty))]
[assembly: StyleProperty("margin-left", typeof(View), nameof(View.MarginLeftProperty))]
@ -57,6 +57,6 @@ using Xamarin.Forms.StyleSheets;
[assembly: StyleProperty("padding-top", typeof(IPaddingElement), nameof(PaddingElement.PaddingTopProperty), PropertyOwnerType = typeof(PaddingElement))]
[assembly: StyleProperty("padding-right", typeof(IPaddingElement), nameof(PaddingElement.PaddingRightProperty), PropertyOwnerType = typeof(PaddingElement))]
[assembly: StyleProperty("padding-bottom", typeof(IPaddingElement), nameof(PaddingElement.PaddingBottomProperty), PropertyOwnerType = typeof(PaddingElement))]
[assembly: StyleProperty("text-align", typeof(ITextAlignmentElement), nameof(TextAlignmentElement.HorizontalTextAlignmentProperty))]
[assembly: StyleProperty("visibility", typeof(VisualElement), nameof(VisualElement.IsVisibleProperty))]
[assembly: StyleProperty("text-align", typeof(ITextAlignmentElement), nameof(TextAlignmentElement.HorizontalTextAlignmentProperty), Inherited = true)]
[assembly: StyleProperty("visibility", typeof(VisualElement), nameof(VisualElement.IsVisibleProperty), Inherited = true)]
[assembly: StyleProperty("width", typeof(VisualElement), nameof(VisualElement.WidthRequestProperty))]

Просмотреть файл

@ -14,6 +14,6 @@ namespace Xamarin.Forms.StyleSheets
interface IStylable
{
BindableProperty GetProperty(string key);
BindableProperty GetProperty(string key, bool inheriting);
}
}

Просмотреть файл

@ -55,13 +55,13 @@ namespace Xamarin.Forms.StyleSheets
return style;
}
public void Apply(VisualElement styleable)
public void Apply(VisualElement styleable, bool inheriting = false)
{
if (styleable == null)
throw new ArgumentNullException(nameof(styleable));
foreach (var decl in Declarations) {
var property = ((IStylable)styleable).GetProperty(decl.Key);
var property = ((IStylable)styleable).GetProperty(decl.Key, inheriting);
if (property == null)
continue;
if (string.Equals(decl.Value, "initial", StringComparison.OrdinalIgnoreCase))
@ -78,7 +78,7 @@ namespace Xamarin.Forms.StyleSheets
var ve = child as VisualElement;
if (ve == null)
continue;
Apply(ve);
Apply(ve, inheriting: true);
}
}

Просмотреть файл

@ -10,6 +10,7 @@ namespace Xamarin.Forms.StyleSheets
public Type TargetType { get; }
public Type PropertyOwnerType { get; set; }
public BindableProperty BindableProperty { get; set; }
public bool Inherited { get; set; } = false;
public StylePropertyAttribute(string cssPropertyName, Type targetType, string bindablePropertyName)

Просмотреть файл

@ -11,7 +11,7 @@ namespace Xamarin.Forms
IList<string> IStyleSelectable.Classes
=> StyleClass;
BindableProperty IStylable.GetProperty(string key)
BindableProperty IStylable.GetProperty(string key, bool inheriting)
{
StylePropertyAttribute styleAttribute;
if (!Internals.Registrar.StyleProperties.TryGetValue(key, out styleAttribute))
@ -20,6 +20,10 @@ namespace Xamarin.Forms
if (!styleAttribute.TargetType.GetTypeInfo().IsAssignableFrom(GetType().GetTypeInfo()))
return null;
//do not inherit non-inherited properties
if (inheriting && !styleAttribute.Inherited)
return null;
if (styleAttribute.BindableProperty != null)
return styleAttribute.BindableProperty;

Просмотреть файл

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Xaml.UnitTests.Gh1766">
<ContentPage.Resources>
<StyleSheet>
<![CDATA[
.body {
background-color: pink;
}
]]>
</StyleSheet>
</ContentPage.Resources>
<StackLayout StyleClass="body" x:Name="stack">
<Entry x:Name="entry" />
</StackLayout>
</ContentPage>

Просмотреть файл

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class Gh1766 : ContentPage
{
public Gh1766()
{
InitializeComponent();
}
public Gh1766(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
[TestFixture]
class Tests
{
[SetUp]
public void Setup()
{
Device.PlatformServices = new MockPlatformServices();
Xamarin.Forms.Internals.Registrar.RegisterAll(new Type[0]);
}
[TearDown]
public void TearDown()
{
Device.PlatformServices = null;
}
[TestCase(true), TestCase(false)]
public void CSSPropertiesNotInerited(bool useCompiledXaml)
{
var layout = new Gh1766(useCompiledXaml);
Assert.That(layout.stack.BackgroundColor, Is.EqualTo(Color.Pink));
Assert.That(layout.entry.BackgroundColor, Is.EqualTo(VisualElement.BackgroundColorProperty.DefaultValue));
}
}
}
}

Просмотреть файл

@ -573,6 +573,9 @@
<Compile Include="Issues\Gh1566.xaml.cs">
<DependentUpon>Gh1566.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Gh1766.xaml.cs">
<DependentUpon>Gh1766.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
@ -1022,6 +1025,10 @@
<EmbeddedResource Include="Issues\Gh1566.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Gh1766.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />