From 90c4b5d63f2b50cbc3a228ea30ac2762781fa55d Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Fri, 16 Apr 2021 15:58:51 +0100 Subject: [PATCH] Switch to using HTML entities for escaping --- extractor/extractor.go | 6 +++--- extractor/trap/labels.go | 4 ++-- extractor/util/util.go | 13 ++++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/extractor/extractor.go b/extractor/extractor.go index dd2346728b9..6413ef6a1b6 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -139,7 +139,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error { scope := extractPackageScope(tw, pkg) tw.ForEachObject(extractObjectType) - lbl := tw.Labeler.GlobalID(util.EscapeCurlyBraces(pkg.PkgPath) + ";pkg") + lbl := tw.Labeler.GlobalID(util.EscapeTrapSpecialChars(pkg.PkgPath) + ";pkg") dbscheme.PackagesTable.Emit(tw, lbl, pkg.Name, pkg.PkgPath, scope) if len(pkg.Errors) != 0 { @@ -624,7 +624,7 @@ func (extraction *Extraction) extractFileInfo(tw *trap.Writer, file string) { extraction.Lock.Unlock() break } - lbl := tw.Labeler.GlobalID(util.EscapeCurlyBraces(path) + ";folder") + lbl := tw.Labeler.GlobalID(util.EscapeTrapSpecialChars(path) + ";folder") dbscheme.FoldersTable.Emit(tw, lbl, path, component) if i > 0 { dbscheme.ContainerParentTable.Emit(tw, parentLbl, lbl) @@ -1496,7 +1496,7 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) { if field.Embedded() { name = "" } - fmt.Fprintf(&b, "%s,{%s},%s", name, fieldTypeLbl, util.EscapeCurlyBraces(tp.Tag(i))) + fmt.Fprintf(&b, "%s,{%s},%s", name, fieldTypeLbl, util.EscapeTrapSpecialChars(tp.Tag(i))) } lbl = tw.Labeler.GlobalID(fmt.Sprintf("%s;structtype", b.String())) case *types.Pointer: diff --git a/extractor/trap/labels.go b/extractor/trap/labels.go index c6fbfa1d160..2176cdf929c 100644 --- a/extractor/trap/labels.go +++ b/extractor/trap/labels.go @@ -74,7 +74,7 @@ func (l *Labeler) FileLabel() Label { // FileLabelFor returns the label for the file for which the trap writer `tw` is associated func (l *Labeler) FileLabelFor(path string) Label { - return l.GlobalID(util.EscapeCurlyBraces(path) + ";sourcefile") + return l.GlobalID(util.EscapeTrapSpecialChars(path) + ";sourcefile") } // LocalID associates a label with the given AST node `nd` and returns it @@ -103,7 +103,7 @@ func (l *Labeler) ScopeID(scope *types.Scope, pkg *types.Package) Label { } else { if pkg != nil && pkg.Scope() == scope { // if this scope is the package scope - pkgLabel := l.GlobalID(util.EscapeCurlyBraces(pkg.Path()) + ";package") + pkgLabel := l.GlobalID(util.EscapeTrapSpecialChars(pkg.Path()) + ";package") label = l.GlobalID("{" + pkgLabel.String() + "};scope") } else { label = l.FreshID() diff --git a/extractor/util/util.go b/extractor/util/util.go index 54aee83b8b0..5725c03d5b6 100644 --- a/extractor/util/util.go +++ b/extractor/util/util.go @@ -198,10 +198,13 @@ func GetExtractorPath() (string, error) { return extractorPath, nil } -func EscapeCurlyBraces(s string) string { - // Replace carets with ^caret, then curly braces with ^lcbrace and ^rcbrace. - s = strings.ReplaceAll(s, "^", "^caret") - s = strings.ReplaceAll(s, "{", "^lcbrace") - s = strings.ReplaceAll(s, "}", "^rcbrace") +func EscapeTrapSpecialChars(s string) string { + // Replace TRAP special characters with their HTML entities, as well as '&' to avoid ambiguity. + s = strings.ReplaceAll(s, "&", "&") + s = strings.ReplaceAll(s, "{", "{") + s = strings.ReplaceAll(s, "}", "}") + s = strings.ReplaceAll(s, "\"", """) + s = strings.ReplaceAll(s, "@", "@") + s = strings.ReplaceAll(s, "#", "#") return s }