Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library path.style.url; | 5 library path.style.url; |
| 6 | 6 |
| 7 import '../characters.dart' as chars; | 7 import '../characters.dart' as chars; |
| 8 import '../internal_style.dart'; | 8 import '../internal_style.dart'; |
| 9 import '../utils.dart'; | 9 import '../utils.dart'; |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 bool isSeparator(int codeUnit) => codeUnit == chars.SLASH; | 29 bool isSeparator(int codeUnit) => codeUnit == chars.SLASH; |
| 30 | 30 |
| 31 bool needsSeparator(String path) { | 31 bool needsSeparator(String path) { |
| 32 if (path.isEmpty) return false; | 32 if (path.isEmpty) return false; |
| 33 | 33 |
| 34 // A URL that doesn't end in "/" always needs a separator. | 34 // A URL that doesn't end in "/" always needs a separator. |
| 35 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true; | 35 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true; |
| 36 | 36 |
| 37 // A URI that's just "scheme://" needs an extra separator, despite ending | 37 // A URI that's just "scheme://" needs an extra separator, despite ending |
| 38 // with "/". | 38 // with "/". |
| 39 var root = _getRoot(path); | 39 return rootLength(path) > 0 && path.endsWith("://"); |
|
nweiz
2014/08/05 20:22:27
I believe this still breaks on "http://foo.com/bar
Anders Johnsen
2014/08/06 07:05:15
Better. Fixed. Also, swapped them as endsWith is f
| |
| 40 return root != null && root.endsWith('://'); | |
| 41 } | 40 } |
| 42 | 41 |
| 43 String getRoot(String path) { | 42 int rootLength(String path) { |
| 44 var root = _getRoot(path); | 43 if (path.isEmpty) return 0; |
| 45 return root == null ? getRelativeRoot(path) : root; | 44 if (isSeparator(path.codeUnitAt(0))) return 1; |
| 45 var index = path.indexOf("/"); | |
| 46 if (index > 0 && path.startsWith('://', index - 1)) { | |
| 47 // The root part is up until the next '/', or the full path. Skip | |
| 48 // '://' and search for '/' after that. | |
| 49 index = path.indexOf('/', index + 2); | |
| 50 if (index > 0) return index; | |
| 51 return path.length; | |
| 52 } | |
| 53 return 0; | |
| 46 } | 54 } |
| 47 | 55 |
| 48 String getRelativeRoot(String path) { | 56 bool isRootRelative(String path) => |
| 49 if (path.isEmpty) return null; | 57 path.isNotEmpty && isSeparator(path.codeUnitAt(0)); |
| 50 return isSeparator(path.codeUnitAt(0)) ? "/" : null; | 58 |
| 51 } | 59 String getRelativeRoot(String path) => isRootRelative(path) ? '/' : null; |
| 52 | 60 |
| 53 String pathFromUri(Uri uri) => uri.toString(); | 61 String pathFromUri(Uri uri) => uri.toString(); |
| 54 | 62 |
| 55 Uri relativePathToUri(String path) => Uri.parse(path); | 63 Uri relativePathToUri(String path) => Uri.parse(path); |
| 56 Uri absolutePathToUri(String path) => Uri.parse(path); | 64 Uri absolutePathToUri(String path) => Uri.parse(path); |
| 57 | |
| 58 // A helper method for [getRoot] that doesn't handle relative roots. | |
| 59 String _getRoot(String path) { | |
| 60 if (path.isEmpty) return null; | |
| 61 | |
| 62 // We aren't using a RegExp for this because they're slow (issue 19090). If | |
| 63 // we could, we'd match against r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*". | |
| 64 | |
| 65 if (!isAlphabetic(path.codeUnitAt(0))) return null; | |
| 66 var start = 1; | |
| 67 for (; start < path.length; start++) { | |
| 68 var char = path.codeUnitAt(start); | |
| 69 if (isAlphabetic(char)) continue; | |
| 70 if (isNumeric(char)) continue; | |
| 71 if (char == chars.MINUS || char == chars.PLUS || char == chars.PERIOD) { | |
| 72 continue; | |
| 73 } | |
| 74 | |
| 75 break; | |
| 76 } | |
| 77 | |
| 78 if (start + 3 > path.length) return null; | |
| 79 if (path.substring(start, start + 3) != '://') return null; | |
| 80 start += 3; | |
| 81 | |
| 82 // A URL root can end with a non-"/" prefix. | |
| 83 while (start < path.length && !isSeparator(path.codeUnitAt(start))) { | |
| 84 start++; | |
| 85 } | |
| 86 return path.substring(0, start); | |
| 87 } | |
| 88 } | 65 } |
| OLD | NEW |