public AstTransformationContext(TransformerConfiguration configuration,
                                 Dictionary <string, string> namespaceAliases, bool strictMode = true)
 {
     Configuration    = configuration;
     NamespaceAliases = namespaceAliases;
     StrictMode       = strictMode;
 }
        /// <summary>
        /// Applies the whitespace normalization process and content model transformation.
        /// </summary>
        public static void Apply(List <IXamlAstValueNode> contentNodes,
                                 TransformerConfiguration config)
        {
            bool ShouldTrimWhitespaceAround(int index) => contentNodes[index] is XamlAstObjectNode objectNode &&
            config.IsTrimSurroundingWhitespaceElement(
                objectNode.Type.GetClrType());

            for (var i = contentNodes.Count - 1; i >= 0; i--)
            {
                var node = contentNodes[i];

                if (node is XamlAstTextNode textNode)
                {
                    // XAML whitespace normalization can be disabled via xml:space="preserve" on an element or
                    // any of its ancestors.
                    if (!textNode.PreserveWhitespace)
                    {
                        // Trim spaces immediately following the start tag or following a tag that wants surrounding
                        // whitespace trimmed
                        var trimStart = i <= 0 || ShouldTrimWhitespaceAround(i - 1);

                        // Trim spaces immediately preceding the end tag or preceding a tag that wants surrounding
                        // whitespace trimmed
                        var trimEnd = i >= contentNodes.Count - 1 || ShouldTrimWhitespaceAround(i + 1);

                        textNode.Text = NormalizeWhitespace(textNode.Text, trimStart, trimEnd);
                        if (textNode.Text.Length == 0)
                        {
                            // Remove text nodes that have been trimmed in their entirety
                            contentNodes.RemoveAt(i);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public static IXamlType TryGetTypeConverterFromCustomAttribute(TransformerConfiguration cfg,
                                                                       IXamlCustomAttribute attribute)
        {
            if (attribute != null)
            {
                var arg = attribute.Parameters.FirstOrDefault();
                return((arg as IXamlType) ??
                       (arg is String sarg ? cfg.TypeSystem.FindType(sarg) : null));
            }

            return(null);
        }
Esempio n. 4
0
        public static List <NamespaceResolveResult> TryResolve(TransformerConfiguration config, string xmlns)
        {
            if (config.XmlnsMappings.Namespaces.TryGetValue(xmlns, out var lst))
            {
                return(lst.Select(p => new NamespaceResolveResult
                {
                    ClrNamespace = p.ns,
                    Assembly = p.asm
                }).ToList());
            }

            var prefixStringComparison = StringComparison.Ordinal;

            const string clrNamespace       = "clr-namespace:";
            const string assemblyNamePrefix = ";assembly=";

            if (xmlns.StartsWith(clrNamespace, prefixStringComparison))
            {
                var ns = xmlns.Substring(clrNamespace.Length);

                var    indexOfAssemblyPrefix = ns.IndexOf(assemblyNamePrefix, prefixStringComparison);
                string asm = config.DefaultAssembly?.Name;
                if (indexOfAssemblyPrefix != -1)
                {
                    asm = ns.Substring(indexOfAssemblyPrefix + assemblyNamePrefix.Length).Trim();
                    ns  = ns.Substring(0, indexOfAssemblyPrefix);
                }
                return(new List <NamespaceResolveResult>
                {
                    new NamespaceResolveResult
                    {
                        ClrNamespace = ns,
                        AssemblyName = asm
                    }
                });
            }

            const string usingPrefix = "using:";

            if (xmlns.StartsWith(usingPrefix, prefixStringComparison))
            {
                var ns = xmlns.Substring(usingPrefix.Length);
                return(new List <NamespaceResolveResult>
                {
                    new NamespaceResolveResult
                    {
                        ClrNamespace = ns
                    }
                });
            }

            return(null);
        }