HtmlEscape() public static method

public static HtmlEscape ( string text ) : string
text string
return string
Exemplo n.º 1
0
        protected void GenerateRange(ClassifiedRange range, StringBuilder sb)
        {
            var text      = range.Text;
            var spanClass = GetSpanClass(range.Classification);

            if (spanClass != null)
            {
                sb.Append("<span class=\"");
                sb.Append(spanClass);
                sb.Append("\">");
            }

            try
            {
                text = ProcessRange(range, text);
            }
            catch (Exception)
            {
                text = Markup.HtmlEscape(range.Text);
            }

            sb.Append(text);
            if (spanClass != null)
            {
                sb.Append("</span>");
            }
        }
Exemplo n.º 2
0
        private void WriteType(DeclaredSymbolInfo typeDeclaration, StreamWriter sw, string className, string pathPrefix)
        {
            string typeUrl = typeDeclaration.GetUrl();

            sw.Write(string.Format("<div class=\"{3}\"><a class=\"tDN\" href=\"{0}\" target=\"s\"><img class=\"tDNI\" src=\"{4}content/icons/{2}.png\" />{1}</a></div>",
                                   typeUrl,
                                   Markup.HtmlEscape(typeDeclaration.Name),
                                   typeDeclaration.Glyph,
                                   className,
                                   pathPrefix));
        }
Exemplo n.º 3
0
        private string ProcessPropertyName(
            string lineText,
            int lineNumber,
            int startPositionOnLine,
            string text,
            bool isRootProject,
            bool isUsage)
        {
            var propertyName        = text.Trim();
            var leadingTriviaWidth  = text.IndexOf(propertyName, StringComparison.Ordinal);
            var trailingTriviaWidth = text.Length - propertyName.Length - leadingTriviaWidth;

            if (propertyName.IndexOfAny(complexCharsInProperties) != -1)
            {
                return(Markup.HtmlEscape(text));
            }

            if (propertyName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
            {
                Log.Exception(string.Format("Invalid property name {0} in project {1}", propertyName, this.sourceXmlFilePath));
                return(Markup.HtmlEscape(text));
            }

            var href =
                "/" +
                Constants.MSBuildPropertiesAssembly +
                "/" +
                Constants.ReferencesFileName +
                "/" +
                propertyName +
                ".html";

            text = string.Format(
                "{2}<a href=\"{1}\" target=\"n\" class=\"msbuildlink\">{0}</a>{3}",
                propertyName,
                href,
                text.Substring(0, leadingTriviaWidth),
                text.Substring(text.Length - trailingTriviaWidth));

            ProjectGenerator.AddReference(
                destinationHtmlFilePath,
                lineText,
                startPositionOnLine,
                propertyName.Length,
                lineNumber,
                isRootProject ? ProjectGenerator.AssemblyName : Constants.MSBuildFiles,
                Constants.MSBuildPropertiesAssembly,
                null,
                propertyName,
                isUsage ? ReferenceKind.MSBuildPropertyUsage : ReferenceKind.MSBuildPropertyAssignment);

            return(text);
        }
Exemplo n.º 4
0
        public static void WriteSymbol(DeclaredSymbolInfo symbol, StringBuilder sb)
        {
            var url = symbol.GetUrl();

            sb.AppendFormat("<a href=\"{0}\" target=\"s\"><div class=\"resultItem\" onClick=\"resultClick(this);\">", url);
            sb.Append("<div class=\"resultLine\">");
            sb.AppendFormat("<img src=\"/content/icons/{0}\" height=\"16\" width=\"16\" />", GetGlyph(symbol) + ".png");
            sb.AppendFormat("<div class=\"resultKind\">{0}</div>", symbol.Kind);
            sb.AppendFormat("<div class=\"resultName\">{0}</div>", Markup.HtmlEscape(symbol.Name));
            sb.AppendLine("</div>");
            sb.AppendFormat("<div class=\"resultDescription\">{0}</div>", Markup.HtmlEscape(symbol.Description));
            sb.AppendLine();
            sb.AppendLine("</div></a>");
        }
Exemplo n.º 5
0
        private string ProcessItemName(
            string lineText,
            int lineNumber,
            int startPositionOnLine,
            string text,
            bool isRootProject,
            bool isUsage)
        {
            var itemName = text.Trim();

            var leadingTriviaWidth  = text.IndexOf(itemName);
            var trailingTriviaWidth = text.Length - itemName.Length - leadingTriviaWidth;

            if (itemName.IndexOfAny(complexCharsInProperties) != -1)
            {
                return(Markup.HtmlEscape(text));
            }

            var href =
                "/" +
                Constants.MSBuildItemsAssembly +
                "/" +
                Constants.ReferencesFileName +
                "/" +
                itemName +
                ".html";

            text = string.Format(
                "{2}<a href=\"{1}\" target=\"n\" class=\"msbuildlink\">{0}</a>{3}",
                itemName,
                href,
                text.Substring(0, leadingTriviaWidth),
                text.Substring(text.Length - trailingTriviaWidth));

            projectGenerator.AddReference(
                destinationHtmlFilePath,
                lineText,
                startPositionOnLine,
                itemName.Length,
                lineNumber,
                isRootProject ? this.projectGenerator.AssemblyName : Constants.MSBuildFiles,
                Constants.MSBuildItemsAssembly,
                null,
                itemName,
                isUsage ? ReferenceKind.MSBuildItemUsage : ReferenceKind.MSBuildItemAssignment);

            return(text);
        }
Exemplo n.º 6
0
 protected virtual string ProcessRange(ClassifiedRange range, string text)
 {
     text = Markup.HtmlEscape(text);
     return(text);
 }
        private string GenerateRange(StreamWriter writer, Classification.Range range, int lineCount = 0)
        {
            var html = range.Text;

            html = Markup.HtmlEscape(html);
            bool            isLargeFile         = IsLargeFile(lineCount);
            string          classAttributeValue = GetClassAttribute(html, range);
            HtmlElementInfo hyperlinkInfo       = GenerateLinks(range, isLargeFile);

            if (hyperlinkInfo == null)
            {
                if (classAttributeValue == null || isLargeFile)
                {
                    return(html);
                }

                if (classAttributeValue == "k")
                {
                    return("<b>" + html + "</b>");
                }
            }

            var sb = new StringBuilder();

            var elementName = "span";

            if (hyperlinkInfo != null)
            {
                elementName = hyperlinkInfo.Name;
            }

            sb.Append("<" + elementName);
            bool overridingClassAttributeSpecified = false;

            if (hyperlinkInfo != null)
            {
                foreach (var attribute in hyperlinkInfo.Attributes)
                {
                    AddAttribute(sb, attribute.Key, attribute.Value);
                    if (attribute.Key == "class")
                    {
                        overridingClassAttributeSpecified = true;
                    }
                }
            }

            if (!overridingClassAttributeSpecified)
            {
                AddAttribute(sb, "class", classAttributeValue);
            }

            sb.Append('>');

            html = AddIdSpanForImplicitConstructorIfNecessary(hyperlinkInfo, html);

            sb.Append(html);
            sb.Append("</" + elementName + ">");

            html = sb.ToString();

            if (hyperlinkInfo != null && hyperlinkInfo.DeclaredSymbol != null)
            {
                writer.Flush();
                long streamPosition = writer.BaseStream.Length;

                streamPosition += html.IndexOf(hyperlinkInfo.Attributes["id"] + ".html");
                projectGenerator.AddDeclaredSymbol(
                    hyperlinkInfo.DeclaredSymbol,
                    hyperlinkInfo.DeclaredSymbolId,
                    documentRelativeFilePathWithoutHtmlExtension,
                    streamPosition);
            }

            return(html);
        }
        public void AddReference(
            string documentDestinationPath,
            string lineText,
            int referenceStartOnLine,
            int referenceLength,
            int lineNumber,
            string fromAssemblyName,
            string toAssemblyName,
            ISymbol symbol,
            string symbolId,
            ReferenceKind kind)
        {
            string localPath = Paths.MakeRelativeToFolder(
                documentDestinationPath,
                Path.Combine(SolutionGenerator.SolutionDestinationFolder, fromAssemblyName));

            localPath = Path.ChangeExtension(localPath, null);

            int referenceEndOnLine = referenceStartOnLine + referenceLength;

            lineText = Markup.HtmlEscape(lineText, ref referenceStartOnLine, ref referenceEndOnLine);

            string symbolName = GetSymbolName(symbol, symbolId);

            var reference = new Reference()
            {
                ToAssemblyId         = toAssemblyName,
                ToSymbolId           = symbolId,
                ToSymbolName         = symbolName,
                FromAssemblyId       = fromAssemblyName,
                FromLocalPath        = localPath,
                ReferenceLineText    = lineText,
                ReferenceLineNumber  = lineNumber,
                ReferenceColumnStart = referenceStartOnLine,
                ReferenceColumnEnd   = referenceEndOnLine,
                Kind = kind
            };

            if (referenceStartOnLine < 0 ||
                referenceStartOnLine >= referenceEndOnLine ||
                referenceEndOnLine > lineText.Length)
            {
                Log.Exception(
                    string.Format("AddReference: start = {0}, end = {1}, lineText = {2}, documentDestinationPath = {3}",
                                  referenceStartOnLine,
                                  referenceEndOnLine,
                                  lineText,
                                  documentDestinationPath));
            }

            string linkRelativePath = GetLinkRelativePath(reference);

            reference.Url = linkRelativePath;

            Dictionary <string, List <Reference> > referencesToAssembly = GetReferencesToAssembly(reference.ToAssemblyId);
            List <Reference> referencesToSymbol = GetReferencesToSymbol(reference, referencesToAssembly);

            lock (referencesToSymbol)
            {
                referencesToSymbol.Add(reference);
            }
        }
Exemplo n.º 9
0
        private HtmlElementInfo GenerateLinks(ClassifiedRange range, string destinationHtmlFilePath, Dictionary <string, int> localSymbolIdMap)
        {
            HtmlElementInfo result = null;

            var localRelativePath = destinationHtmlFilePath.Substring(
                Path.Combine(
                    Paths.SolutionDestinationFolder,
                    Constants.TypeScriptFiles).Length);

            if (!string.IsNullOrEmpty(range.definitionSymbolId))
            {
                var definitionSymbolId = SymbolIdService.GetId(range.definitionSymbolId);

                if (range.IsSymbolLocalOnly())
                {
                    var localId = GetLocalId(definitionSymbolId, localSymbolIdMap);
                    result = new HtmlElementInfo
                    {
                        Name       = "span",
                        Attributes =
                        {
                            { "id",    "r" + localId + " rd" },
                            { "class", "r" + localId + " r"  }
                        }
                    };
                    return(result);
                }

                result = new HtmlElementInfo
                {
                    Name       = "a",
                    Attributes =
                    {
                        { "id",     definitionSymbolId                                                                      },
                        { "href",   "/TypeScriptFiles/" + Constants.ReferencesFileName + "/" + definitionSymbolId + ".html" },
                        { "target", "n"                                                                                     }
                    }
                };

                var searchString = range.searchString;
                if (!string.IsNullOrEmpty(searchString) && searchString.Length > 2)
                {
                    lock (declarations)
                    {
                        searchString = searchString.StripQuotes();
                        if (IsWellFormed(searchString))
                        {
                            var declaration = string.Join(";",
                                                          searchString,                            // symbol name
                                                          definitionSymbolId,                      // 8-byte symbol ID
                                                          range.definitionKind,                    // kind (e.g. "class")
                                                          Markup.EscapeSemicolons(range.fullName), // symbol full name and signature
                                                          GetGlyph(range.definitionKind)           // glyph number
                                                          );
                            declarations.Add(declaration);
                        }
                    }
                }
            }

            if (range.hyperlinks == null || range.hyperlinks.Length == 0)
            {
                return(result);
            }

            var hyperlink = range.hyperlinks[0];
            var symbolId  = SymbolIdService.GetId(hyperlink.symbolId);

            if (range.IsSymbolLocalOnly() || localSymbolIdMap.ContainsKey(symbolId))
            {
                var localId = GetLocalId(symbolId, localSymbolIdMap);
                result = new HtmlElementInfo
                {
                    Name       = "span",
                    Attributes =
                    {
                        { "class", "r" + localId + " r" }
                    }
                };
                return(result);
            }

            var hyperlinkDestinationFile = Path.GetFullPath(hyperlink.sourceFile);

            hyperlinkDestinationFile = GetDestinationFilePath(hyperlinkDestinationFile);

            string href = "";

            if (!string.Equals(hyperlinkDestinationFile, destinationHtmlFilePath, StringComparison.OrdinalIgnoreCase))
            {
                href = Paths.MakeRelativeToFile(hyperlinkDestinationFile, destinationHtmlFilePath);
                href = href.Replace('\\', '/');
            }

            href = href + "#" + symbolId;

            if (result == null)
            {
                result = new HtmlElementInfo
                {
                    Name       = "a",
                    Attributes =
                    {
                        { "href",   href },
                        { "target", "s"  },
                    }
                };
            }
            else if (!result.Attributes.ContainsKey("href"))
            {
                result.Attributes.Add("href", href);
                result.Attributes.Add("target", "s");
            }

            lock (this.references)
            {
                var lineNumber      = range.lineNumber + 1;
                var linkToReference = ".." + localRelativePath + "#" + lineNumber.ToString();
                var start           = range.column;
                var end             = range.column + range.text.Length;
                var lineText        = Markup.HtmlEscape(range.lineText, ref start, ref end);
                var reference       = new Reference
                {
                    FromAssemblyId       = Constants.TypeScriptFiles,
                    ToAssemblyId         = Constants.TypeScriptFiles,
                    FromLocalPath        = localRelativePath.Substring(0, localRelativePath.Length - ".html".Length).Replace('\\', '/'),
                    Kind                 = ReferenceKind.Reference,
                    ToSymbolId           = symbolId,
                    ToSymbolName         = range.text,
                    ReferenceLineNumber  = lineNumber,
                    ReferenceLineText    = lineText,
                    ReferenceColumnStart = start,
                    ReferenceColumnEnd   = end,
                    Url = linkToReference.Replace('\\', '/')
                };

                if (!references.TryGetValue(symbolId, out List <Reference> bucket))
                {
                    bucket = new List <Reference>();
                    references.Add(symbolId, bucket);
                }

                bucket.Add(reference);
            }

            return(result);
        }
Exemplo n.º 10
0
        private void GenerateRange(
            StringBuilder sb,
            ClassifiedRange range,
            string destinationFilePath,
            Dictionary <string, int> localSymbolIdMap)
        {
            var html = range.text;

            html = Markup.HtmlEscape(html);

            var localRelativePath = destinationFilePath.Substring(
                Path.Combine(
                    Paths.SolutionDestinationFolder,
                    Constants.TypeScriptFiles).Length + 1);

            localRelativePath = localRelativePath.Substring(0, localRelativePath.Length - ".html".Length);

            string          classAttributeValue = GetSpanClass(range.classification);
            HtmlElementInfo hyperlinkInfo       = GenerateLinks(range, destinationFilePath, localSymbolIdMap);

            if (hyperlinkInfo == null)
            {
                if (classAttributeValue == null)
                {
                    sb.Append(html);
                    return;
                }

                if (classAttributeValue == "k")
                {
                    sb.Append("<b>").Append(html).Append("</b>");
                    return;
                }
            }

            var elementName = "span";

            if (hyperlinkInfo != null)
            {
                elementName = hyperlinkInfo.Name;
            }

            sb.Append("<").Append(elementName);
            bool overridingClassAttributeSpecified = false;

            if (hyperlinkInfo != null)
            {
                foreach (var attribute in hyperlinkInfo.Attributes)
                {
                    const string typeScriptFilesR = "/TypeScriptFiles/R/";
                    if (attribute.Key == "href" && attribute.Value.StartsWith(typeScriptFilesR, StringComparison.Ordinal))
                    {
                        var streamPosition = sb.Length + 7 + typeScriptFilesR.Length; // exact offset into <a href="HERE
                        ProjectGenerator.AddDeclaredSymbolToRedirectMap(
                            SymbolIDToListOfLocationsMap,
                            attribute.Value.Substring(typeScriptFilesR.Length, 16),
                            localRelativePath,
                            streamPosition);
                    }

                    AddAttribute(sb, attribute.Key, attribute.Value);
                    if (attribute.Key == "class")
                    {
                        overridingClassAttributeSpecified = true;
                    }
                }
            }

            if (!overridingClassAttributeSpecified)
            {
                AddAttribute(sb, "class", classAttributeValue);
            }

            sb.Append('>');

            sb.Append(html);
            sb.Append("</").Append(elementName).Append(">");
        }
Exemplo n.º 11
0
        private string ProcessExpressions(ClassifiedRange range, string text, bool isRootProject, Func <ClassifiedRange, string, bool, int, string> customStringProcessor = null)
        {
            var parts = MSBuildExpressionParser.SplitStringByPropertiesAndItems(text);

            if (parts.Count() == 1 && !text.StartsWith("$(", StringComparison.Ordinal) && !text.StartsWith("@(", StringComparison.Ordinal))
            {
                var processed = text;
                if (customStringProcessor != null)
                {
                    return(customStringProcessor(range, processed, isRootProject, range.Start));
                }
                else
                {
                    return(Markup.HtmlEscape(processed));
                }
            }

            var sb          = new StringBuilder();
            int lengthSoFar = 0;

            foreach (var part in parts)
            {
                if (part.StartsWith("$(", StringComparison.Ordinal) && part.EndsWith(")", StringComparison.Ordinal) && part.Length > 3)
                {
                    var    propertyName = part.Substring(2, part.Length - 3);
                    string suffix       = "";
                    int    dot          = propertyName.IndexOf('.');
                    if (dot > -1)
                    {
                        suffix       = propertyName.Substring(dot);
                        propertyName = propertyName.Substring(0, dot);
                    }

                    var currentPosition = range.Start + lengthSoFar;
                    var line            = TextUtilities.GetLineFromPosition(currentPosition, sourceText);
                    var lineNumber      = TextUtilities.GetLineNumber(currentPosition, this.lineLengths);
                    var lineText        = sourceText.Substring(line.Item1, line.Item2);
                    var url             = ProcessPropertyName(
                        lineText,
                        lineNumber + 1,
                        currentPosition - line.Item1 + 2,
                        propertyName,
                        isRootProject,
                        isUsage: true);

                    sb.Append("$(" + url + Markup.HtmlEscape(suffix) + ")");
                }
                else if (
                    part.StartsWith("@(", StringComparison.Ordinal) &&
                    (part.EndsWith(")", StringComparison.Ordinal) || part.EndsWith("-", StringComparison.Ordinal) || part.EndsWith(",", StringComparison.Ordinal)) &&
                    !part.Contains("%") &&
                    part.Length > 3)
                {
                    const int suffixLength = 1;
                    var       itemName     = part.Substring(2, part.Length - 2 - suffixLength);
                    string    suffix       = part.Substring(part.Length - suffixLength, suffixLength);

                    var currentPosition = range.Start + lengthSoFar;
                    var line            = TextUtilities.GetLineFromPosition(currentPosition, sourceText);
                    var lineNumber      = TextUtilities.GetLineNumber(currentPosition, this.lineLengths);
                    var lineText        = sourceText.Substring(line.Item1, line.Item2);
                    var url             = ProcessItemName(
                        lineText,
                        lineNumber + 1,
                        currentPosition - line.Item1 + 2,
                        itemName,
                        isRootProject,
                        isUsage: true);

                    sb.Append("@(").Append(url).Append(Markup.HtmlEscape(suffix));
                }
                else
                {
                    var processed = part;
                    if (customStringProcessor != null)
                    {
                        var currentPosition = range.Start + lengthSoFar;
                        processed = customStringProcessor(range, processed, isRootProject, currentPosition);
                    }
                    else
                    {
                        processed = Markup.HtmlEscape(processed);
                    }

                    sb.Append(processed);
                }

                lengthSoFar += part.Length;
            }

            return(sb.ToString());
        }
Exemplo n.º 12
0
        private string GetHtml(string sourceXmlFilePath, string destinationHtmlFilePath, string displayName)
        {
            var lines = File.ReadAllLines(sourceXmlFilePath);

            lineLengths = sourceText.GetLineLengths();
            var lineCount = lines.Length;

            var sb = new StringBuilder();

            var relativePathToRoot = Paths.CalculateRelativePathToRoot(destinationHtmlFilePath, ProjectGenerator.SolutionGenerator.SolutionDestinationFolder);

            var prefix = Markup.GetDocumentPrefix(Path.GetFileName(sourceXmlFilePath), relativePathToRoot, lineCount, "ix");

            sb.Append(prefix);

            var assemblyName = GetAssemblyName();

            var url = "/#" + assemblyName + "/" + displayName.Replace('\\', '/');

            Markup.WriteLinkPanel(
                s => sb.AppendLine(s),
                fileLink: (displayName, url),
                webAccessUrl: ProjectGenerator.GetWebAccessUrl(sourceXmlFilePath),
                projectLink: (ProjectGenerator.ProjectSourcePath, Url: "/#" + assemblyName, assemblyName));

            // pass a value larger than 0 to generate line numbers statically at HTML generation time
            var table = Markup.GetTablePrefix();

            sb.AppendLine(table);

            if (sourceText.Length > 1000000)
            {
                sb.AppendLine(Markup.HtmlEscape(sourceText));
            }
            else
            {
                var ranges = new List <ClassifiedRange>();

                var root = Parser.ParseText(sourceText);
                ClassifierVisitor.Visit(
                    root,
                    0,
                    sourceText.Length,
                    (start, length, node, classification) =>
                {
                    var line     = TextUtilities.GetLineFromPosition(start, sourceText);
                    var lineText = sourceText.Substring(line.Item1, line.Item2);

                    ranges.Add(
                        new ClassifiedRange
                    {
                        Classification = classification,
                        Node           = node,
                        Text           = sourceText.Substring(start, length),
                        LineText       = lineText,
                        LineStart      = line.Item1,
                        LineNumber     = TextUtilities.GetLineNumber(start, lineLengths),
                        Start          = start,
                        Length         = length
                    });
                });

                ranges = RangeUtilities.FillGaps(
                    sourceText,
                    ranges,
                    r => r.Start,
                    r => r.Length,
                    (s, l, t) => new ClassifiedRange
                {
                    Start  = s,
                    Length = l,
                    Text   = t.Substring(s, l)
                }).ToList();
                foreach (var range in ranges)
                {
                    GenerateRange(range, sb);
                }
            }

            var suffix = Markup.GetDocumentSuffix();

            sb.AppendLine(suffix);
            return(sb.ToString());
        }
Exemplo n.º 13
0
 private void WriteNamespace(string title, StreamWriter sw)
 {
     sw.Write(string.Format("<div class=\"folderTitle\">{0}</div>", Markup.HtmlEscape(title)));
 }