| 8 | * http://tools.ietf.org/html/rfc3986#section-5.2.4 |
| 9 | */ |
| 10 | export function normalizePath(path: string): string { |
| 11 | if (path.indexOf('..') === -1 && path.indexOf('.') === -1) { |
| 12 | return path; |
| 13 | } |
| 14 | |
| 15 | // Remove leading slash (will be added back below) so we |
| 16 | // can handle all (including empty) segments consistently. |
| 17 | const segments = (path[0] === '/' ? path.substring(1) : path).split('/'); |
| 18 | const normalizedSegments = []; |
| 19 | for (const segment of segments) { |
| 20 | if (segment === '.') { |
| 21 | continue; |
| 22 | } else if (segment === '..') { |
| 23 | normalizedSegments.pop(); |
| 24 | } else { |
| 25 | normalizedSegments.push(segment); |
| 26 | } |
| 27 | } |
| 28 | let normalizedPath = normalizedSegments.join('/'); |
| 29 | if (path[0] === '/' && normalizedPath) { |
| 30 | normalizedPath = '/' + normalizedPath; |
| 31 | } |
| 32 | if (normalizedPath[normalizedPath.length - 1] !== '/' && |
| 33 | ((path[path.length - 1] === '/') || (segments[segments.length - 1] === '.') || |
| 34 | (segments[segments.length - 1] === '..'))) { |
| 35 | normalizedPath = normalizedPath + '/'; |
| 36 | } |
| 37 | |
| 38 | return normalizedPath; |
| 39 | } |
| 40 | |
| 41 | export function schemeIs(url: Platform.DevToolsPath.UrlString|URL, scheme: string): boolean { |
| 42 | try { |