Exemple #1
0
 public override string GetNamespace(string fileName, string definedSymbols)
 {
     using (IParser parser = ParserFactory.CreateParser(fileName))
     {
         char[]           separator = new char[] { ',' };
         HashSet <string> set       = new HashSet <string>(definedSymbols.Split(separator, StringSplitOptions.RemoveEmptyEntries));
         foreach (string str in set)
         {
             parser.Lexer.ConditionalCompilationSymbols.Add(str, string.Empty);
         }
         parser.Lexer.EvaluateConditionalCompilation = true;
         parser.Parse();
         try
         {
             NamespaceVisitor visitor = new NamespaceVisitor();
             VisitorData      data    = new VisitorData {
                 TargetClassName = Path.GetFileNameWithoutExtension(fileName)
             };
             parser.CompilationUnit.AcceptVisitor(visitor, data);
             return(!string.IsNullOrEmpty(data.DiscoveredNamespace) ? data.DiscoveredNamespace : string.Empty);
         }
         catch
         {
         }
     }
     return(string.Empty);
 }
        static void LoadTypes(string fileName)
        {
            using (var parser = ParserFactory.CreateParser(fileName))
            {
                // @TODO any standard preprocessor symbols we need?

                /*var uniqueSymbols = new HashSet<string>(definedSymbols.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                 * foreach (var symbol in uniqueSymbols)
                 * {
                 *  parser.Lexer.ConditionalCompilationSymbols.Add(symbol, string.Empty);
                 * }*/
                parser.Lexer.EvaluateConditionalCompilation = true;

                parser.Parse();
                try
                {
                    var visitor = new NamespaceVisitor();
                    var data    = new VisitorData {
                        typeName = s_TypeName
                    };
                    parser.CompilationUnit.AcceptVisitor(visitor, data);

                    if (data.generators.Count > 0)
                    {
                        s_SourceGenerators[fileName] = data.generators;
                    }
                }
                catch
                {
                    // does NRefactory throw anything we can handle here?
                    throw;
                }
            }
        }
 public override string GetNamespace(string fileName, string definedSymbols)
 {
     using (IParser parser = ParserFactory.CreateParser(fileName))
     {
         char[] separator = new char[] { ',' };
         HashSet<string> set = new HashSet<string>(definedSymbols.Split(separator, StringSplitOptions.RemoveEmptyEntries));
         foreach (string str in set)
         {
             parser.Lexer.ConditionalCompilationSymbols.Add(str, string.Empty);
         }
         parser.Lexer.EvaluateConditionalCompilation = true;
         parser.Parse();
         try
         {
             NamespaceVisitor visitor = new NamespaceVisitor();
             VisitorData data = new VisitorData {
                 TargetClassName = Path.GetFileNameWithoutExtension(fileName)
             };
             parser.CompilationUnit.AcceptVisitor(visitor, data);
             return (!string.IsNullOrEmpty(data.DiscoveredNamespace) ? data.DiscoveredNamespace : string.Empty);
         }
         catch
         {
         }
     }
     return string.Empty;
 }
Exemple #4
0
        public void ScopeOfXmlnsIsLimited()
        {
            var nodes   = ParseNodes("<x:pre/><x:foo xmlns:x='http://sparkviewengine.com/x'/><x:post/>");
            var visitor = new NamespaceVisitor(new VisitorContext());

            visitor.Accept(nodes);
            Assert.AreEqual("x:pre", ((ElementNode)visitor.Nodes[0]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[1]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
            Assert.AreEqual("x:post", ((ElementNode)visitor.Nodes[2]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[2]).Namespace);

            nodes   = ParseNodes("<x:pre/><x:foo xmlns:x='http://sparkviewengine.com/x'><x:bar/></x:foo><x:post/>");
            visitor = new NamespaceVisitor(new VisitorContext());
            visitor.Accept(nodes);
            Assert.AreEqual("x:pre", ((ElementNode)visitor.Nodes[0]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[1]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
            Assert.AreEqual("x:bar", ((ElementNode)visitor.Nodes[2]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[2]).Namespace);
            Assert.AreEqual("x:post", ((ElementNode)visitor.Nodes[4]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[4]).Namespace);
        }
        public string GetNamespaceOldRuntime(string filePath, string definedSymbols, string[] defines)
        {
            var definedSymbolSplit = definedSymbols.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var uniqueSymbols      = defines.Union(definedSymbolSplit).Distinct().ToArray();

            using (var parser = ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, ReadAndConverteNewLines(filePath)))
            {
                foreach (var symbol in uniqueSymbols)
                {
                    parser.Lexer.ConditionalCompilationSymbols.Add(symbol, string.Empty);
                }

                parser.Lexer.EvaluateConditionalCompilation = true;
                parser.Parse();
                try
                {
                    var visitor = new NamespaceVisitor();
                    var data    = new VisitorData {
                        TargetClassName = Path.GetFileNameWithoutExtension(filePath)
                    };
                    parser.CompilationUnit.AcceptVisitor(visitor, data);
                    return(string.IsNullOrEmpty(data.DiscoveredNamespace) ? string.Empty : data.DiscoveredNamespace);
                }
                catch
                {
                    // Don't care; all we want is the namespace
                }
            }
            return(string.Empty);
        }
 public void AssignNamespaceToElement()
 {
     var nodes = ParseNodes("<foo xmlns:x='http://sparkviewengine.com/x'><x:bar/></foo>");
     var visitor = new NamespaceVisitor(new VisitorContext());
     visitor.Accept(nodes);
     Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
     Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
 }
 public void AssignNamespaceWithDefaultPrefix()
 {
     var nodes = ParseNodes("<foo><quux:bar/></foo>");
     var visitor = new NamespaceVisitor(new VisitorContext { Prefix = "quux" });
     visitor.Accept(nodes);
     Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
     Assert.AreEqual("http://sparkviewengine.com/", ((ElementNode)visitor.Nodes[1]).Namespace);
 }
Exemple #8
0
        public void AssignNamespaceToElement()
        {
            var nodes   = ParseNodes("<foo xmlns:x='http://sparkviewengine.com/x'><x:bar/></foo>");
            var visitor = new NamespaceVisitor(new VisitorContext());

            visitor.Accept(nodes);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
        }
        public void ElementCanUseXmlnsOnSelf()
        {
            var nodes = ParseNodes("<x:foo y:bar='hello' xmlns:x='http://sparkviewengine.com/x' xmlns:y='http://sparkviewengine.com/y'><quux/></foo>");
            var visitor = new NamespaceVisitor(new VisitorContext());
            visitor.Accept(nodes);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[1]).Namespace);

            Assert.AreEqual("y:bar", ((ElementNode)visitor.Nodes[0]).Attributes[0].Name);
            Assert.AreEqual("http://sparkviewengine.com/y", ((ElementNode)visitor.Nodes[0]).Attributes[0].Namespace);
        }
Exemple #10
0
        public void AssignNamespaceWithDefaultPrefix()
        {
            var nodes   = ParseNodes("<foo><quux:bar/></foo>");
            var visitor = new NamespaceVisitor(new VisitorContext {
                Prefix = "quux"
            });

            visitor.Accept(nodes);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("http://sparkviewengine.com/", ((ElementNode)visitor.Nodes[1]).Namespace);
        }
Exemple #11
0
        public void ElementCanUseXmlnsOnSelf()
        {
            var nodes   = ParseNodes("<x:foo y:bar='hello' xmlns:x='http://sparkviewengine.com/x' xmlns:y='http://sparkviewengine.com/y'><quux/></foo>");
            var visitor = new NamespaceVisitor(new VisitorContext());

            visitor.Accept(nodes);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[1]).Namespace);

            Assert.AreEqual("y:bar", ((ElementNode)visitor.Nodes[0]).Attributes[0].Name);
            Assert.AreEqual("http://sparkviewengine.com/y", ((ElementNode)visitor.Nodes[0]).Attributes[0].Namespace);
        }
        public override IList <Node> IncludeFile(VisitorContext context, string path, string parse)
        {
            var existingPath = context.ViewPath;

            var directoryPath = Path.GetDirectoryName(context.ViewPath);

            var relativePath = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            while (relativePath.StartsWith(string.Format("..{0}", Path.DirectorySeparatorChar)))
            {
                directoryPath = Path.GetDirectoryName(directoryPath);
                relativePath  = relativePath.Substring(3);
            }
            context.ViewPath = Path.Combine(directoryPath, relativePath);

            var sourceContext = CreateSourceContext(context.ViewPath, context.ViewFolder);

            switch (parse)
            {
            case "text":
                var encoded = sourceContext.Content
                              .Replace("&", "&amp;")
                              .Replace("<", "&lt;")
                              .Replace(">", "&gt;");
                return(new[] { new TextNode(encoded) });

            case "html":
                return(new[] { new TextNode(sourceContext.Content) });
            }


            var position = new Position(sourceContext);
            var result   = _grammar.Nodes(position);

            if (result.Rest.PotentialLength() != 0)
            {
                ThrowParseException(context.ViewPath, position, result.Rest);
            }
            context.Paint = context.Paint.Union(result.Rest.GetPaint());

            var namespaceVisitor = new NamespaceVisitor(context);

            namespaceVisitor.Accept(result.Value);

            var includeVisitor = new IncludeVisitor(context);

            includeVisitor.Accept(namespaceVisitor.Nodes);

            context.ViewPath = existingPath;
            return(includeVisitor.Nodes);
        }
        public string GetNamespaceOldRuntime(string filePath, string definedSymbols)
        {
            var definedSymbolSplit = definedSymbols.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            string[] defines          = null;
            var      responseFilePath = Path.Combine("Assets", MonoCSharpCompiler.ReponseFilename);

            try
            {
                var responseFileData = ScriptCompilerBase.ParseResponseFileFromFile(responseFilePath);
                defines = new string[responseFileData.Defines.Length + definedSymbolSplit.Length];
                Array.Copy(definedSymbolSplit, defines, definedSymbolSplit.Length);
                Array.Copy(responseFileData.Defines, 0, defines, definedSymbolSplit.Length, responseFileData.Defines.Length);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            var uniqueSymbols = new HashSet <string>(defines ?? definedSymbolSplit);

            using (var parser = ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, ReadAndConverteNewLines(filePath)))
            {
                foreach (var symbol in uniqueSymbols)
                {
                    parser.Lexer.ConditionalCompilationSymbols.Add(symbol, string.Empty);
                }

                parser.Lexer.EvaluateConditionalCompilation = true;
                parser.Parse();
                try
                {
                    var visitor = new NamespaceVisitor();
                    var data    = new VisitorData {
                        TargetClassName = Path.GetFileNameWithoutExtension(filePath)
                    };
                    parser.CompilationUnit.AcceptVisitor(visitor, data);
                    return(string.IsNullOrEmpty(data.DiscoveredNamespace) ? string.Empty : data.DiscoveredNamespace);
                }
                catch
                {
                    // Don't care; all we want is the namespace
                }
            }
            return(string.Empty);
        }
            public static bool TryGetLastNamespaceDirective(
                RazorSyntaxTree syntaxTree,
                out string namespaceDirectiveContent,
                out SourceSpan namespaceDirectiveSpan)
            {
                var visitor = new NamespaceVisitor(syntaxTree.Source);

                visitor.Visit(syntaxTree.Root);
                if (string.IsNullOrEmpty(visitor.LastNamespaceContent))
                {
                    namespaceDirectiveContent = null;
                    namespaceDirectiveSpan    = SourceSpan.Undefined;
                    return(false);
                }

                namespaceDirectiveContent = visitor.LastNamespaceContent;
                namespaceDirectiveSpan    = visitor.LastNamespaceLocation;
                return(true);
            }
        public override IList <Node> IncludeFile(VisitorContext context, string path, string parse)
        {
            string viewPath      = context.ViewPath;
            string directoryName = Path.GetDirectoryName(context.ViewPath);
            string str3          = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            while (str3.StartsWith(string.Format("..{0}", Path.DirectorySeparatorChar)))
            {
                directoryName = Path.GetDirectoryName(directoryName);
                str3          = str3.Substring(3);
            }
            context.ViewPath = Path.Combine(directoryName, str3);
            SourceContext sourceContext = AbstractSyntaxProvider.CreateSourceContext(context.ViewPath, context.ViewFolder);

            switch (parse)
            {
            case "text":
            {
                string text = sourceContext.Content.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
                return(new TextNode[] { new TextNode(text) });
            }

            case "html":
                return(new TextNode[] { new TextNode(sourceContext.Content) });
            }
            Position position = new Position(sourceContext);
            ParseResult <IList <Node> > result = this._grammar.Nodes(position);

            if (result.Rest.PotentialLength() != 0)
            {
                base.ThrowParseException(context.ViewPath, position, result.Rest);
            }
            context.Paint = context.Paint.Union <Paint>(result.Rest.GetPaint());
            NamespaceVisitor visitor = new NamespaceVisitor(context);

            visitor.Accept(result.Value);
            IncludeVisitor visitor2 = new IncludeVisitor(context);

            visitor2.Accept(visitor.Nodes);
            context.ViewPath = viewPath;
            return(visitor2.Nodes);
        }
Exemple #16
0
 public override string GetNamespace(string fileName)
 {
     using (IParser parser = ParserFactory.CreateParser(fileName))
     {
         parser.Parse();
         try
         {
             NamespaceVisitor visitor = new NamespaceVisitor();
             VisitorData      data    = new VisitorData {
                 TargetClassName = Path.GetFileNameWithoutExtension(fileName)
             };
             parser.CompilationUnit.AcceptVisitor(visitor, data);
             return(!string.IsNullOrEmpty(data.DiscoveredNamespace) ? data.DiscoveredNamespace : string.Empty);
         }
         catch
         {
         }
     }
     return(string.Empty);
 }
Exemple #17
0
 public override string GetNamespace(string fileName)
 {
     using (IParser parser = ParserFactory.CreateParser(fileName))
     {
         parser.Parse();
         try
         {
             NamespaceVisitor visitor = new NamespaceVisitor();
             VisitorData data = new VisitorData {
                 TargetClassName = Path.GetFileNameWithoutExtension(fileName)
             };
             parser.CompilationUnit.AcceptVisitor(visitor, data);
             return (!string.IsNullOrEmpty(data.DiscoveredNamespace) ? data.DiscoveredNamespace : string.Empty);
         }
         catch
         {
         }
     }
     return string.Empty;
 }
Exemple #18
0
        public void NestedElementsDontWreckScope()
        {
            var nodes   = ParseNodes("<x:pre/><x:foo xmlns:x='http://sparkviewengine.com/x'><x:foo><x:foo/></x:foo></x:foo><x:post/>");
            var visitor = new NamespaceVisitor(new VisitorContext());

            visitor.Accept(nodes);
            Assert.AreEqual("x:pre", ((ElementNode)visitor.Nodes[0]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[1]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[2]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[2]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[3]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[3]).Namespace);
            Assert.AreEqual("x:foo", ((EndElementNode)visitor.Nodes[4]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((EndElementNode)visitor.Nodes[4]).Namespace);
            Assert.AreEqual("x:foo", ((EndElementNode)visitor.Nodes[5]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((EndElementNode)visitor.Nodes[5]).Namespace);
            Assert.AreEqual("x:post", ((ElementNode)visitor.Nodes[6]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[6]).Namespace);
        }
Exemple #19
0
        public override IList<Node> IncludeFile(VisitorContext context, string path, string parse)
        {
            var existingPath = context.ViewPath;

            var directoryPath = Path.GetDirectoryName(context.ViewPath);

            var relativePath = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            while (relativePath.StartsWith(string.Format("..{0}", Path.DirectorySeparatorChar)))
            {
                directoryPath = Path.GetDirectoryName(directoryPath);
                relativePath = relativePath.Substring(3);
            }
            context.ViewPath = Path.Combine(directoryPath, relativePath);

            var sourceContext = CreateSourceContext(context.ViewPath, context.ViewFolder);

            switch (parse)
            {
                case "text":
                    var encoded = sourceContext.Content
                        .Replace("&", "&amp;")
                        .Replace("<", "&lt;")
                        .Replace(">", "&gt;");
                    return new[] {new TextNode(encoded)};
                case "html":
                    return new[] {new TextNode(sourceContext.Content)};
            }

            var position = new Position(sourceContext);
            var result = _grammar.Nodes(position);
            if (result.Rest.PotentialLength() != 0)
            {
                ThrowParseException(context.ViewPath, position, result.Rest);
            }
            context.Paint = context.Paint.Union(result.Rest.GetPaint());

            var namespaceVisitor = new NamespaceVisitor(context);
            namespaceVisitor.Accept(result.Value);

            var includeVisitor = new IncludeVisitor(context);
            includeVisitor.Accept(namespaceVisitor.Nodes);

            context.ViewPath = existingPath;
            return includeVisitor.Nodes;
        }
        // In general documents will have a relative path (relative to the project root).
        // We can only really compute a nice namespace when we know a relative path.
        //
        // However all kinds of thing are possible in tools. We shouldn't barf here if the document isn't
        // set up correctly.
        public static bool TryComputeNamespace(this RazorCodeDocument document, bool fallbackToRootNamespace, out string @namespace)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var filePath = document.Source.FilePath;

            if (filePath == null || document.Source.RelativePath == null || filePath.Length < document.Source.RelativePath.Length)
            {
                @namespace = null;
                return(false);
            }

            // If the document or it's imports contains a @namespace directive, we want to use that over the root namespace.
            var baseNamespace         = string.Empty;
            var appendSuffix          = true;
            var lastNamespaceContent  = string.Empty;
            var lastNamespaceLocation = SourceSpan.Undefined;
            var importSyntaxTrees     = document.GetImportSyntaxTrees();

            if (importSyntaxTrees != null)
            {
                // ImportSyntaxTrees is usually set. Just being defensive.
                foreach (var importSyntaxTree in importSyntaxTrees)
                {
                    if (importSyntaxTree != null && NamespaceVisitor.TryGetLastNamespaceDirective(importSyntaxTree, out var importNamespaceContent, out var importNamespaceLocation))
                    {
                        lastNamespaceContent  = importNamespaceContent;
                        lastNamespaceLocation = importNamespaceLocation;
                    }
                }
            }

            var syntaxTree = document.GetSyntaxTree();

            if (syntaxTree != null && NamespaceVisitor.TryGetLastNamespaceDirective(syntaxTree, out var namespaceContent, out var namespaceLocation))
            {
                lastNamespaceContent  = namespaceContent;
                lastNamespaceLocation = namespaceLocation;
            }

            StringSegment relativePath = document.Source.RelativePath;

            // If there are multiple @namespace directives in the heirarchy,
            // we want to pick the closest one to the current document.
            if (!string.IsNullOrEmpty(lastNamespaceContent))
            {
                baseNamespace = lastNamespaceContent;
                var directiveLocationDirectory = NormalizeDirectory(lastNamespaceLocation.FilePath);

                var sourceFilePath = new StringSegment(document.Source.FilePath);
                // We're specifically using OrdinalIgnoreCase here because Razor treats all paths as case-insensitive.
                if (!sourceFilePath.StartsWith(directiveLocationDirectory, StringComparison.OrdinalIgnoreCase) ||
                    sourceFilePath.Length <= directiveLocationDirectory.Length)
                {
                    // The most relevant directive is not from the directory hierarchy, can't compute a suffix.
                    appendSuffix = false;
                }
                else
                {
                    // We know that the document containing the namespace directive is in the current document's heirarchy.
                    // Let's compute the actual relative path that we'll use to compute the namespace suffix.
                    relativePath = sourceFilePath.Subsegment(directiveLocationDirectory.Length);
                }
            }
            else if (fallbackToRootNamespace)
            {
                var options = document.GetCodeGenerationOptions() ?? document.GetDocumentIntermediateNode()?.Options;
                baseNamespace = options?.RootNamespace;
                appendSuffix  = true;
            }

            if (string.IsNullOrEmpty(baseNamespace))
            {
                // There was no valid @namespace directive and we couldn't compute the RootNamespace.
                @namespace = null;
                return(false);
            }

            var builder = new StringBuilder();

            // Sanitize the base namespace, but leave the dots.
            var segments = new StringTokenizer(baseNamespace, NamespaceSeparators);
            var first    = true;

            foreach (var token in segments)
            {
                if (token.IsEmpty)
                {
                    continue;
                }

                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.Append('.');
                }

                CSharpIdentifier.AppendSanitized(builder, token);
            }

            if (appendSuffix)
            {
                // If we get here, we already have a base namespace and the relative path that should be used as the namespace suffix.
                segments = new StringTokenizer(relativePath, PathSeparators);
                var previousLength = builder.Length;
                foreach (var token in segments)
                {
                    if (token.IsEmpty)
                    {
                        continue;
                    }

                    previousLength = builder.Length;

                    builder.Append('.');
                    CSharpIdentifier.AppendSanitized(builder, token);
                }

                // Trim the last segment because it's the FileName.
                builder.Length = previousLength;
            }

            @namespace = builder.ToString();

            return(true);

            // We want to normalize the path of the file containing the '@namespace' directive to just the containing
            // directory with a trailing separator.
            //
            // Not using Path.GetDirectoryName here because it doesn't meet these requirements, and we want to handle
            // both 'view engine' style paths and absolute paths.
            //
            // We also don't normalize the separators here. We expect that all documents are using a consistent style of path.
            //
            // If we can't normalize the path, we just return null so it will be ignored.
            StringSegment NormalizeDirectory(string path)
            {
                if (string.IsNullOrEmpty(path))
                {
                    return(default);
Exemple #21
0
 public void NestedElementsDontWreckScope()
 {
     var nodes = ParseNodes("<x:pre/><x:foo xmlns:x='http://sparkviewengine.com/x'><x:foo><x:foo/></x:foo></x:foo><x:post/>");
     var visitor = new NamespaceVisitor(new VisitorContext());
     visitor.Accept(nodes);
     Assert.AreEqual("x:pre", ((ElementNode)visitor.Nodes[0]).Name);
     Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
     Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[1]).Name);
     Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
     Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[2]).Name);
     Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[2]).Namespace);
     Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[3]).Name);
     Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[3]).Namespace);
     Assert.AreEqual("x:foo", ((EndElementNode)visitor.Nodes[4]).Name);
     Assert.AreEqual("http://sparkviewengine.com/x", ((EndElementNode)visitor.Nodes[4]).Namespace);
     Assert.AreEqual("x:foo", ((EndElementNode)visitor.Nodes[5]).Name);
     Assert.AreEqual("http://sparkviewengine.com/x", ((EndElementNode)visitor.Nodes[5]).Namespace);
     Assert.AreEqual("x:post", ((ElementNode)visitor.Nodes[6]).Name);
     Assert.AreEqual("", ((ElementNode)visitor.Nodes[6]).Namespace);
 }
Exemple #22
0
        public void ScopeOfXmlnsIsLimited()
        {
            var nodes = ParseNodes("<x:pre/><x:foo xmlns:x='http://sparkviewengine.com/x'/><x:post/>");
            var visitor = new NamespaceVisitor(new VisitorContext());
            visitor.Accept(nodes);
            Assert.AreEqual("x:pre", ((ElementNode)visitor.Nodes[0]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[1]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
            Assert.AreEqual("x:post", ((ElementNode)visitor.Nodes[2]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[2]).Namespace);

            nodes = ParseNodes("<x:pre/><x:foo xmlns:x='http://sparkviewengine.com/x'><x:bar/></x:foo><x:post/>");
            visitor = new NamespaceVisitor(new VisitorContext());
            visitor.Accept(nodes);
            Assert.AreEqual("x:pre", ((ElementNode)visitor.Nodes[0]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[0]).Namespace);
            Assert.AreEqual("x:foo", ((ElementNode)visitor.Nodes[1]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[1]).Namespace);
            Assert.AreEqual("x:bar", ((ElementNode)visitor.Nodes[2]).Name);
            Assert.AreEqual("http://sparkviewengine.com/x", ((ElementNode)visitor.Nodes[2]).Namespace);
            Assert.AreEqual("x:post", ((ElementNode)visitor.Nodes[4]).Name);
            Assert.AreEqual("", ((ElementNode)visitor.Nodes[4]).Namespace);
        }
Exemple #23
0
 private string GetNamespaceForSyntaxTree(SyntaxTree syntaxTree)
 {
     var namespaceVisitor = new NamespaceVisitor();
     namespaceVisitor.Visit(syntaxTree.GetRoot());
     return namespaceVisitor.Namespace;
 }