Exemple #1
0
        public void Parser_Should_Handle_Ignorable_Content(bool map)
        {
            var root = XDocumentXamlParser.Parse(@"
<Root xmlns='rootns' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d d2' xmlns:d='test' xmlns:d2='test2'
    d:DataContext='123' d2:Lalala='321'>
    <d:DesignWidth>test</d:DesignWidth>
</Root>
 ", map ? new Dictionary <string, string>()
            {
                ["test"] = "mapped"
            } : null);
            var ni       = new NullLineInfo();
            var rootType = new XamlAstXmlTypeReference(ni, "rootns", "Root");

            if (map)
            {
                Helpers.StructDiff(root.Root, new XamlAstObjectNode(ni, rootType)
                {
                    Children =
                    {
                        new XamlAstXmlDirective(ni,     "mapped",                       "DataContext", new[] { new XamlAstTextNode(ni, "123", true), }),
                        new XamlAstTextNode(ni,         "\n    "),
                        new XamlAstObjectNode(ni,       new XamlAstXmlTypeReference(ni, "mapped",      "DesignWidth"))
                        {
                            Children =
                            {
                                new XamlAstTextNode(ni, "test")
                            }
                        },
                        new XamlAstTextNode(ni,         "\n")
                    }
                });
            }
            else
            {
                Helpers.StructDiff(root.Root, new XamlAstObjectNode(ni, rootType)
                {
                    Children =
                    {
                        new XamlAstTextNode(ni, "\n    ")
                    }
                });
            }
        }
            XamlAstObjectNode ParseNewInstance(XElement el, bool root)
            {
                if (el.Name.LocalName.Contains("."))
                {
                    throw ParseError(el, "Dots aren't allowed in type names");
                }
                var type = GetTypeReference(el);
                var i    = new XamlAstObjectNode(el.AsLi(), type);

                foreach (var attr in el.Attributes())
                {
                    if (attr.Name.NamespaceName == "http://www.w3.org/2000/xmlns/" ||
                        (attr.Name.NamespaceName == "" && attr.Name.LocalName == "xmlns"))
                    {
                        if (!root)
                        {
                            throw ParseError(attr,
                                             "xmlns declarations are only allowed on the root element to preserve memory");
                        }
                    }
                    else if (attr.Name.NamespaceName.StartsWith("http://www.w3.org"))
                    {
                        // Silently ignore all xml-parser related attributes
                    }
                    // Parse type arguments
                    else if (attr.Name.NamespaceName == XamlNamespaces.Xaml2006 &&
                             attr.Name.LocalName == "TypeArguments")
                    {
                        type.GenericArguments = ParseTypeArguments(attr.Value, el, attr.AsLi());
                    }
                    // Parse as a directive
                    else if (attr.Name.NamespaceName != "" && !attr.Name.LocalName.Contains("."))
                    {
                        i.Children.Add(new XamlAstXmlDirective(el.AsLi(),
                                                               attr.Name.NamespaceName, attr.Name.LocalName, new[]
                        {
                            ParseTextValueOrMarkupExtension(attr.Value, el, attr.AsLi())
                        }
                                                               ));
                    }
                    // Parse as a property
                    else
                    {
                        var pname = attr.Name.LocalName;
                        var ptype = i.Type;

                        if (pname.Contains("."))
                        {
                            var parts = pname.Split(new[] { '.' }, 2);
                            pname = parts[1];
                            var ns = attr.Name.Namespace == "" ? el.GetDefaultNamespace().NamespaceName : attr.Name.NamespaceName;
                            ptype = new XamlAstXmlTypeReference(el.AsLi(), ns, parts[0]);
                        }

                        i.Children.Add(new XamlAstXamlPropertyValueNode(el.AsLi(),
                                                                        new XamlAstNamePropertyReference(el.AsLi(), ptype, pname, type),
                                                                        ParseTextValueOrMarkupExtension(attr.Value, el, attr.AsLi())));
                    }
                }


                foreach (var node in el.Nodes())
                {
                    if (node is XElement elementNode && elementNode.Name.LocalName.Contains("."))
                    {
                        if (elementNode.HasAttributes)
                        {
                            throw ParseError(node, "Attributes aren't allowed on element properties");
                        }
                        var pair = elementNode.Name.LocalName.Split(new[] { '.' }, 2);
                        i.Children.Add(new XamlAstXamlPropertyValueNode(el.AsLi(), new XamlAstNamePropertyReference
                                                                        (
                                                                            el.AsLi(),
                                                                            new XamlAstXmlTypeReference(el.AsLi(), elementNode.Name.NamespaceName,
                                                                                                        pair[0]), pair[1], type
                                                                        ),
                                                                        ParseValueNodeChildren(elementNode)
                                                                        ));
                    }
Exemple #3
0
        public void Parser_Should_Be_Able_To_Parse_A_Simple_Tree()
        {
            var root = XDocumentXamlParser.Parse(
                @"
<Root xmlns='rootns' xmlns:t='testns' xmlns:d='directive' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <Child Ext='{Extension 123, 321, Prop=test, Prop2=test2, Prop3={Extension}, Prop4=test3}'
        Other.Prop='{}Not extension'
        Prop1='123' 
        Root.AttachedProp='AttachedValue'
        t:Namespaced.AttachedProp='AttachedValue'
        d:Directive='DirectiveValue'
        d:DirectiveExt='{Extension 123}'>
        <t:SubChild Prop='321' Root.AttachedProp='AttachedValue'/>
        <Child.DottedProp>DottedValue</Child.DottedProp>
        <Root.AttachedDottedProp>AttachedValue</Root.AttachedDottedProp>
        <Child.NodeListProp>
            <SubChild/>
            <SubChild/>
        </Child.NodeListProp>
    </Child>
    <GenericType x:TypeArguments='Child,t:NamespacedGeneric(Child,GenericType ( Child, t:Namespaced) )'/>

</Root>");
            var ni             = new NullLineInfo();
            var rootType       = new XamlAstXmlTypeReference(ni, "rootns", "Root");
            var childType      = new XamlAstXmlTypeReference(ni, "rootns", "Child");
            var subChildType   = new XamlAstXmlTypeReference(ni, "rootns", "SubChild");
            var nsSubChildType = new XamlAstXmlTypeReference(ni, "testns", "SubChild");
            var namespacedType = new XamlAstXmlTypeReference(ni, "testns", "Namespaced");
            var extensionType  = new XamlAstXmlTypeReference(ni, "rootns", "Extension")
            {
                IsMarkupExtension = true
            };

            var other = new XamlDocument
            {
                NamespaceAliases = new Dictionary <string, string>
                {
                    [""]  = "rootns",
                    ["t"] = "testns",
                    ["d"] = "directive",
                    ["x"] = "http://schemas.microsoft.com/winfx/2006/xaml"
                },
                Root = new XamlAstObjectNode(ni, rootType)
                {
                    Children =
                    {
                        // <Child
                        new XamlAstObjectNode(ni,                                                                                                                             childType)
                        {
                            Children =
                            {
                                // Ext='{Extension 123, 321, Prop=test, Prop2=test2}'
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             childType,                                                                                                                                            "Ext",                                                                                                                                       childType),
                                                                 new XamlAstObjectNode(ni,                                                                                                                                                                  extensionType)
                                {
                                    Arguments =
                                    {
                                        new XamlAstTextNode(ni, "123"),
                                        new XamlAstTextNode(ni, "321"),
                                    },
                                    Children =
                                    {
                                        new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                                                                                                                                                                             new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                                                                                                                                                                                          extensionType,                                                                                                                                                                                                                                                                                             "Prop",                                                                                                                                      extensionType),
                                                                         new XamlAstTextNode(ni,                                                                                                                                                                                                                                                                                                                         "test")),
                                        new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                                                                                                                                                                             new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                                                                                                                                                                                          extensionType,                                                                                                                                                                                                                                                                                             "Prop2",                                                                                                                                     extensionType),
                                                                         new XamlAstTextNode(ni,                                                                                                                                                                                                                                                                                                                         "test2")),
                                        new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                                                                                                                                                                             new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                                                                                                                                                                                          extensionType,                                                                                                                                                                                                                                                                                             "Prop3",                                                                                                                                     extensionType),
                                                                         new XamlAstObjectNode(ni,                                                                                                                                                                                                                                                                                                                       extensionType)),
                                        new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                                                                                                                                                                             new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                                                                                                                                                                                          extensionType,                                                                                                                                                                                                                                                                                             "Prop4",                                                                                                                                     extensionType),
                                                                         new XamlAstTextNode(ni,                                                                                                                                                                                                                                                                                                                         "test3")),
                                    }
                                }),
                                //Other.Prop='{}Not extension'
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             new XamlAstXmlTypeReference(ni,                                                                                                                       "rootns",                                                                                                                                    "Other"),                               "Prop", childType),
                                                                 new XamlAstTextNode(ni,                                                                                                                                                                    "Not extension")),
                                //  Prop1='123'
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             childType,                                                                                                                                            "Prop1",                                                                                                                                     childType),
                                                                 new XamlAstTextNode(ni,                                                                                                                                                                    "123")),
                                // Root.AttachedProp='AttachedValue'
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             rootType,                                                                                                                                             "AttachedProp",                                                                                                                              childType),
                                                                 new XamlAstTextNode(ni,                                                                                                                                                                    "AttachedValue")),
                                //t:Namespaced.AttachedProp='AttachedValue'
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             namespacedType,                                                                                                                                       "AttachedProp",                                                                                                                              childType),
                                                                 new XamlAstTextNode(ni,                                                                                                                                                                    "AttachedValue")),
                                //d:Directive='DirectiveValue'>
                                new XamlAstXmlDirective(ni,                                                                                                                                                                                                 "directive",                                                                                                                                 "Directive",                            new[]
                                {
                                    new XamlAstTextNode(ni,                                                                                                                                                                                                 "DirectiveValue")
                                }),
                                //d:DirectiveExt='{Extension 123}'>
                                new XamlAstXmlDirective(ni,                                                                                                                                                                                                 "directive",                                                                                                                                 "DirectiveExt",                         new[]
                                {
                                    new XamlAstObjectNode(ni,                                                                                                                                                                                               extensionType)
                                    {
                                        Arguments =
                                        {
                                            new XamlAstTextNode(ni, "123"),
                                        }
                                    }
                                }),
                                // <t:SubChild Prop='321' Root.AttachedProp='AttachedValue'/>
                                new XamlAstObjectNode(ni,                                                                                                                                                                                                   nsSubChildType)
                                {
                                    Children =
                                    {
                                        new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                                                                                                                                                                              new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                                                                                                                                                                                           nsSubChildType,                                                                                                                                                                                                                                                                                             "Prop",                                                                                                                                      nsSubChildType),
                                                                         new XamlAstTextNode(ni,                                                                                                                                                                                                                                                                                                                          "321")),
                                        // Root.AttachedProp='AttachedValue'
                                        new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                                                                                                                                                                              new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                                                                                                                                                                                           rootType,                                                                                                                                                                                                                                                                                                   "AttachedProp",                                                                                                                              nsSubChildType),
                                                                         new XamlAstTextNode(ni,                                                                                                                                                                                                                                                                                                                          "AttachedValue")),
                                    }
                                },
                                //<Child.DottedProp>DottedValue</Child.DottedProp>
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             childType,                                                                                                                                            "DottedProp",                                                                                                                                childType),
                                                                 new[]
                                {
                                    new XamlAstTextNode(ni,                                                                                                                                                                                                 "DottedValue")
                                }),
                                // <Root.AttachedDottedProp>AttachedValue</Root.AttachedDottedProp>
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             rootType,                                                                                                                                             "AttachedDottedProp",                                                                                                                        childType),
                                                                 new[]
                                {
                                    new XamlAstTextNode(ni,                                                                                                                                                                                                 "AttachedValue")
                                }),
                                //<Child.NodeListProp>
                                new XamlAstXamlPropertyValueNode(ni,                                                                                                                                                                                        new XamlAstNamePropertyReference(ni,
                                                                                                                                                                                                                                                                                             childType,                                                                                                                                            "NodeListProp",                                                                                                                              childType),
                                                                 new[]
                                {
                                    // <SubChild/>
                                    new XamlAstObjectNode(ni,                                                                                                                                                                                               subChildType),
                                    // <SubChild/>
                                    new XamlAstObjectNode(ni,                                                                                                                                                                                               subChildType),
                                })
                            }
                        },
                        //<GenericType x:TypeArguments='Child,t:NamespacedGeneric(Child,GenericType(Child, t:Namespaced))'/>
                        new XamlAstObjectNode(ni,                                                                                                                             new XamlAstXmlTypeReference(ni,     "rootns",             "GenericType",
                                                                                                                                                                                                          new[]
                        {
                            childType,
                            new XamlAstXmlTypeReference(ni,                                                                                                                   "testns",                           "NamespacedGeneric",  new[]
                            {
                                childType,
                                new XamlAstXmlTypeReference(ni,                                                                                                               "rootns",                           "GenericType",        new[]
                                {
                                    childType,
                                    namespacedType
                                }),
                            }),
                        }))
                    }
                }
            };

            Helpers.StructDiff(root, other);
        }
 public static XamlAstClrTypeReference ResolveType(AstTransformationContext context,
                                                   XamlAstXmlTypeReference xmlref, bool strict)
 {
     return(ResolveType(context, xmlref.XmlNamespace, xmlref.Name, xmlref.IsMarkupExtension,
                        xmlref.GenericArguments, xmlref, strict));
 }
 public MeScannerTypeName(XamlAstXmlTypeReference typeReference)
 {
     TypeReference = typeReference;
 }