public void Or_Selector_Matches_Control_Of_Correct_Type()
        {
            var target = Selectors.Or(
                default(Selector).OfType <Control1>(),
                default(Selector).OfType <Control2>().Class("bar"));
            var control = new Control1();

            Assert.Equal(SelectorMatchResult.AlwaysThisType, target.Match(control).Result);
        }
Beispiel #2
0
        public void Or_Selector_Doesnt_Match_Control_With_Incorrect_Name()
        {
            var target = Selectors.Or(
                default(Selector).OfType <Control1>().Name("foo"),
                default(Selector).OfType <Control2>().Name("foo"));
            var control = new Control1 {
                Name = "bar"
            };

            Assert.Equal(SelectorMatchResult.NeverThisInstance, target.Match(control).Result);
        }
Beispiel #3
0
        public void Nested_Or_Style_Can_Be_Added()
        {
            var parent = new Style(x => x.OfType <Class1>());
            var nested = new Style(x => Selectors.Or(
                                       x.Nesting().Class("foo"),
                                       x.Nesting().Class("bar")));

            parent.Children.Add(nested);

            Assert.Same(parent, nested.Parent);
        }
Beispiel #4
0
        public void TestCombo()
        {
            var value1 = Selectors.And(Selectors.Css("title"), Selectors.Regex("aa(bb)cc")).Select(_html2);

            Assert.AreEqual(value1, "bb");

            var or = Selectors.Or(Selectors.Css("div h1 a", "innerHtml"), Selectors.XPath("//title"));

            Assert.AreEqual(or.Select(_html), "aabbcc");
            Assert.AreEqual(or.Select(_html2), "aabbcc");
        }
        public void Or_Nesting_Class_Doesnt_Match_Parent_OfType_Selector()
        {
            var   control = new Control2();
            Style nested;
            var   parent = new Style(x => x.OfType <Control1>())
            {
                Children =
                {
                    (nested = new Style(x => Selectors.Or(
                                            x.Nesting().Class("foo"),
                                            x.Nesting().Class("bar")))),
                }
            };

            var match = nested.Selector.Match(control, parent);

            Assert.Equal(SelectorMatchResult.NeverThisType, match.Result);
        }
        public void Or_Nesting_Child_OfType_Doesnt_Match_Parent_OfType_Selector()
        {
            var control = new Control1();
            var panel   = new DockPanel {
                Children = { control }
            };
            Style nested;
            var   parent = new Style(x => x.OfType <Panel>())
            {
                Children =
                {
                    (nested = new Style(x => Selectors.Or(
                                            x.Nesting().Child().OfType <Control1>(),
                                            x.Nesting().Child().OfType <Control1>()))),
                }
            };

            var match = nested.Selector.Match(control, parent);

            Assert.Equal(SelectorMatchResult.NeverThisInstance, match.Result);
        }
        public void Or_Nesting_Child_OfType_Matches()
        {
            var control = new Control1 {
                Classes = { "foo" }
            };
            var panel = new Panel {
                Children = { control }
            };
            Style nested;
            var   parent = new Style(x => x.OfType <Panel>())
            {
                Children =
                {
                    (nested = new Style(x => Selectors.Or(
                                            x.Nesting().Child().OfType <Control1>(),
                                            x.Nesting().Child().OfType <Control1>()))),
                }
            };

            var match = nested.Selector.Match(control, parent);

            Assert.Equal(SelectorMatchResult.AlwaysThisInstance, match.Result);
        }
Beispiel #8
0
        private Selector?Create(IEnumerable <SelectorGrammar.ISyntax> syntax)
        {
            var result  = default(Selector);
            var results = default(List <Selector>);

            foreach (var i in syntax)
            {
                switch (i)
                {
                case SelectorGrammar.OfTypeSyntax ofType:
                    result = result.OfType(Resolve(ofType.Xmlns, ofType.TypeName));
                    break;

                case SelectorGrammar.IsSyntax @is:
                    result = result.Is(Resolve(@is.Xmlns, @is.TypeName));
                    break;

                case SelectorGrammar.ClassSyntax @class:
                    result = result.Class(@class.Class);
                    break;

                case SelectorGrammar.NameSyntax name:
                    result = result.Name(name.Name);
                    break;

                case SelectorGrammar.PropertySyntax property:
                {
                    var type = result?.TargetType;

                    if (type == null)
                    {
                        throw new InvalidOperationException("Property selectors must be applied to a type.");
                    }

                    var targetProperty = AvaloniaPropertyRegistry.Instance.FindRegistered(type, property.Property);

                    if (targetProperty == null)
                    {
                        throw new InvalidOperationException($"Cannot find '{property.Property}' on '{type}");
                    }

                    object typedValue;

                    if (TypeUtilities.TryConvert(
                            targetProperty.PropertyType,
                            property.Value,
                            CultureInfo.InvariantCulture,
                            out typedValue))
                    {
                        result = result.PropertyEquals(targetProperty, typedValue);
                    }
                    else
                    {
                        throw new InvalidOperationException(
                                  $"Could not convert '{property.Value}' to '{targetProperty.PropertyType}");
                    }
                    break;
                }

                case SelectorGrammar.ChildSyntax child:
                    result = result.Child();
                    break;

                case SelectorGrammar.DescendantSyntax descendant:
                    result = result.Descendant();
                    break;

                case SelectorGrammar.TemplateSyntax template:
                    result = result.Template();
                    break;

                case SelectorGrammar.NotSyntax not:
                    result = result.Not(x => Create(not.Argument));
                    break;

                case SelectorGrammar.NthChildSyntax nth:
                    result = result.NthChild(nth.Step, nth.Offset);
                    break;

                case SelectorGrammar.NthLastChildSyntax nth:
                    result = result.NthLastChild(nth.Step, nth.Offset);
                    break;

                case SelectorGrammar.CommaSyntax comma:
                    if (results == null)
                    {
                        results = new List <Selector>();
                    }

                    results.Add(result ?? throw new NotSupportedException("Invalid selector!"));
                    result = null;
                    break;

                default:
                    throw new NotSupportedException($"Unsupported selector grammar '{i.GetType()}'.");
                }
            }

            if (results != null)
            {
                if (result != null)
                {
                    results.Add(result);
                }

                result = results.Count > 1 ? Selectors.Or(results) : results[0];
            }

            return(result);
        }