| 1 | // Copyright 2014 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/ |
| 16 | |
| 17 | import ( |
| 18 | "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/" |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 20 | |
| 21 | "github.com/google/pprof/third_party/svgpanscip-go gomod github.com/google/pprof . `github.com/google/pprof/third_party/svgpan`/" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | viewBoxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/viewBox. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`<svg\s*width="[^"]+"\s*height="[^"]+"\s*viewBox="[^"]+"`) |
| 26 | graphIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/graphID. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`<g id="graph\d"`) |
| 27 | svgClosescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/svgClose. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`</svg>`) |
| 28 | ) |
| 29 | |
| 30 | // massageSVG enhances the SVG output from DOT to provide better |
| 31 | // panning inside a web browser. It uses the svgpan library, which is |
| 32 | // embedded into the svgpan.JSSource variable. |
| 33 | func massageSVGscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/massageSVG().(svg string) string { |
| 34 | // Work around for dot bug which misses quoting some ampersands, |
| 35 | // resulting on unparsable SVG. |
| 36 | svg = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.24.0 strings/ReplaceAll().(svg, "&;", "&;") |
| 37 | |
| 38 | // Dot's SVG output is |
| 39 | // |
| 40 | // <svg width="___" height="___" |
| 41 | // viewBox="___" xmlns=...> |
| 42 | // <g id="graph0" transform="..."> |
| 43 | // ... |
| 44 | // </g> |
| 45 | // </svg> |
| 46 | // |
| 47 | // Change it to |
| 48 | // |
| 49 | // <svg width="100%" height="100%" |
| 50 | // xmlns=...> |
| 51 | |
| 52 | // <script type="text/ecmascript"><![CDATA[` ..$(svgpan.JSSource)... `]]></script>` |
| 53 | // <g id="viewport" transform="translate(0,0)"> |
| 54 | // <g id="graph0" transform="..."> |
| 55 | // ... |
| 56 | // </g> |
| 57 | // </g> |
| 58 | // </svg> |
| 59 | |
| 60 | if loc := viewBoxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/viewBox..FindStringIndexscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringIndex().(svg); loc != nil { |
| 61 | svg = svg[:loc[0]] + |
| 62 | `<svg width="100%" height="100%"` + |
| 63 | svg[loc[1]:] |
| 64 | } |
| 65 | |
| 66 | if loc := graphIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/graphID..FindStringIndexscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringIndex().(svg); loc != nil { |
| 67 | svg = svg[:loc[0]] + |
| 68 | `<script type="text/ecmascript"><![CDATA[` + svgpanscip-go gomod github.com/google/pprof . `github.com/google/pprof/third_party/svgpan`/.JSSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/third_party/svgpan`/JSSource. + `]]></script>` + |
| 69 | `<g id="viewport" transform="scale(0.5,0.5) translate(0,0)">` + |
| 70 | svg[loc[0]:] |
| 71 | } |
| 72 | |
| 73 | if loc := svgClosescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/svgClose..FindStringIndexscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringIndex().(svg); loc != nil { |
| 74 | svg = svg[:loc[0]] + |
| 75 | `</g>` + |
| 76 | svg[loc[0]:] |
| 77 | } |
| 78 | |
| 79 | return svg |
| 80 | } |