Ejemplo n.º 1
0
        public void Parser_Should_Handle_Ignorable_Content(bool map)
        {
            var root = XDocumentXamlIlParser.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 XamlIlAstXmlTypeReference(ni, "rootns", "Root");

            if (map)
            {
                Helpers.StructDiff(root.Root, new XamlIlAstObjectNode(ni, rootType)
                {
                    Children =
                    {
                        new XamlIlAstXmlDirective(ni,     "mapped",                         "DataContext", new[] { new XamlIlAstTextNode(ni, "123"), }),
                        new XamlIlAstObjectNode(ni,       new XamlIlAstXmlTypeReference(ni, "mapped",      "DesignWidth"))
                        {
                            Children =
                            {
                                new XamlIlAstTextNode(ni, "test")
                            }
                        }
                    }
                });
            }
            else
            {
                Helpers.StructDiff(root.Root, new XamlIlAstObjectNode(ni, rootType)
                {
                });
            }
        }
Ejemplo n.º 2
0
        public void Parser_Should_Be_Able_To_Parse_A_Simple_Tree()
        {
            var root = XDocumentXamlIlParser.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 XamlIlAstXmlTypeReference(ni, "rootns", "Root");
            var childType      = new XamlIlAstXmlTypeReference(ni, "rootns", "Child");
            var subChildType   = new XamlIlAstXmlTypeReference(ni, "rootns", "SubChild");
            var nsSubChildType = new XamlIlAstXmlTypeReference(ni, "testns", "SubChild");
            var namespacedType = new XamlIlAstXmlTypeReference(ni, "testns", "Namespaced");
            var extensionType  = new XamlIlAstXmlTypeReference(ni, "rootns", "Extension")
            {
                IsMarkupExtension = true
            };

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

            Helpers.StructDiff(root, other);
        }
Ejemplo n.º 3
0
 public static IXamlIlType ResolveType(XamlIlAstTransformationContext context,
                                       XamlIlAstXmlTypeReference xmlref, bool strict)
 {
     return(ResolveType(context, xmlref.XmlNamespace, xmlref.Name, xmlref.GenericArguments, xmlref, strict));
 }
Ejemplo n.º 4
0
            XamlIlAstObjectNode 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 XamlIlAstObjectNode(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 XamlIlAstXmlDirective(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 XamlIlAstXmlTypeReference(el.AsLi(), ns, parts[0]);
                        }

                        i.Children.Add(new XamlIlAstXamlPropertyValueNode(el.AsLi(),
                                                                          new XamlIlAstNamePropertyReference(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 XamlIlAstXamlPropertyValueNode(el.AsLi(), new XamlIlAstNamePropertyReference
                                                                          (
                                                                              el.AsLi(),
                                                                              new XamlIlAstXmlTypeReference(el.AsLi(), elementNode.Name.NamespaceName,
                                                                                                            pair[0]), pair[1], type
                                                                          ),
                                                                          ParseValueNodeChildren(elementNode)
                                                                          ));
                    }
Ejemplo n.º 5
0
 public MeScannerTypeName(XamlIlAstXmlTypeReference typeReference)
 {
     TypeReference = typeReference;
 }