Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit adcdb8b

Browse files
smowtonSauyon Lee
authored andcommitted
Switch to using HTML entities for escaping
1 parent 3e450ff commit adcdb8b

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

extractor/extractor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
139139

140140
scope := extractPackageScope(tw, pkg)
141141
tw.ForEachObject(extractObjectType)
142-
lbl := tw.Labeler.GlobalID(util.EscapeCurlyBraces(pkg.PkgPath) + ";pkg")
142+
lbl := tw.Labeler.GlobalID(util.EscapeTrapSpecialChars(pkg.PkgPath) + ";pkg")
143143
dbscheme.PackagesTable.Emit(tw, lbl, pkg.Name, pkg.PkgPath, scope)
144144

145145
if len(pkg.Errors) != 0 {
@@ -624,7 +624,7 @@ func (extraction *Extraction) extractFileInfo(tw *trap.Writer, file string) {
624624
extraction.Lock.Unlock()
625625
break
626626
}
627-
lbl := tw.Labeler.GlobalID(util.EscapeCurlyBraces(path) + ";folder")
627+
lbl := tw.Labeler.GlobalID(util.EscapeTrapSpecialChars(path) + ";folder")
628628
dbscheme.FoldersTable.Emit(tw, lbl, path, component)
629629
if i > 0 {
630630
dbscheme.ContainerParentTable.Emit(tw, parentLbl, lbl)
@@ -1496,7 +1496,7 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
14961496
if field.Embedded() {
14971497
name = ""
14981498
}
1499-
fmt.Fprintf(&b, "%s,{%s},%s", name, fieldTypeLbl, util.EscapeCurlyBraces(tp.Tag(i)))
1499+
fmt.Fprintf(&b, "%s,{%s},%s", name, fieldTypeLbl, util.EscapeTrapSpecialChars(tp.Tag(i)))
15001500
}
15011501
lbl = tw.Labeler.GlobalID(fmt.Sprintf("%s;structtype", b.String()))
15021502
case *types.Pointer:

extractor/trap/labels.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (l *Labeler) FileLabel() Label {
7474

7575
// FileLabelFor returns the label for the file for which the trap writer `tw` is associated
7676
func (l *Labeler) FileLabelFor(path string) Label {
77-
return l.GlobalID(util.EscapeCurlyBraces(path) + ";sourcefile")
77+
return l.GlobalID(util.EscapeTrapSpecialChars(path) + ";sourcefile")
7878
}
7979

8080
// 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 {
103103
} else {
104104
if pkg != nil && pkg.Scope() == scope {
105105
// if this scope is the package scope
106-
pkgLabel := l.GlobalID(util.EscapeCurlyBraces(pkg.Path()) + ";package")
106+
pkgLabel := l.GlobalID(util.EscapeTrapSpecialChars(pkg.Path()) + ";package")
107107
label = l.GlobalID("{" + pkgLabel.String() + "};scope")
108108
} else {
109109
label = l.FreshID()

extractor/util/util.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,13 @@ func GetExtractorPath() (string, error) {
198198
return extractorPath, nil
199199
}
200200

201-
func EscapeCurlyBraces(s string) string {
202-
// Replace carets with ^caret, then curly braces with ^lcbrace and ^rcbrace.
203-
s = strings.ReplaceAll(s, "^", "^caret")
204-
s = strings.ReplaceAll(s, "{", "^lcbrace")
205-
s = strings.ReplaceAll(s, "}", "^rcbrace")
201+
func EscapeTrapSpecialChars(s string) string {
202+
// Replace TRAP special characters with their HTML entities, as well as '&' to avoid ambiguity.
203+
s = strings.ReplaceAll(s, "&", "&")
204+
s = strings.ReplaceAll(s, "{", "{")
205+
s = strings.ReplaceAll(s, "}", "}")
206+
s = strings.ReplaceAll(s, "\"", """)
207+
s = strings.ReplaceAll(s, "@", "@")
208+
s = strings.ReplaceAll(s, "#", "#")
206209
return s
207210
}

0 commit comments

Comments
 (0)