| 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; | |
| 6 | |
| 7 import '../characters.dart' as chars; | 5 import '../characters.dart' as chars; |
| 8 import '../internal_style.dart'; | 6 import '../internal_style.dart'; |
| 7 import '../utils.dart'; |
| 9 | 8 |
| 10 /// The style for URL paths. | 9 /// The style for URL paths. |
| 11 class UrlStyle extends InternalStyle { | 10 class UrlStyle extends InternalStyle { |
| 12 UrlStyle(); | 11 UrlStyle(); |
| 13 | 12 |
| 14 final name = 'url'; | 13 final name = 'url'; |
| 15 final separator = '/'; | 14 final separator = '/'; |
| 16 final separators = const ['/']; | 15 final separators = const ['/']; |
| 17 | 16 |
| 18 // Deprecated properties. | 17 // Deprecated properties. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 31 if (path.isEmpty) return false; | 30 if (path.isEmpty) return false; |
| 32 | 31 |
| 33 // A URL that doesn't end in "/" always needs a separator. | 32 // A URL that doesn't end in "/" always needs a separator. |
| 34 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true; | 33 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true; |
| 35 | 34 |
| 36 // A URI that's just "scheme://" needs an extra separator, despite ending | 35 // A URI that's just "scheme://" needs an extra separator, despite ending |
| 37 // with "/". | 36 // with "/". |
| 38 return path.endsWith("://") && rootLength(path) == path.length; | 37 return path.endsWith("://") && rootLength(path) == path.length; |
| 39 } | 38 } |
| 40 | 39 |
| 41 int rootLength(String path) { | 40 int rootLength(String path, {bool withDrive: false}) { |
| 42 if (path.isEmpty) return 0; | 41 if (path.isEmpty) return 0; |
| 43 if (isSeparator(path.codeUnitAt(0))) return 1; | 42 if (isSeparator(path.codeUnitAt(0))) return 1; |
| 43 |
| 44 for (var i = 0; i < path.length; i++) { |
| 45 var codeUnit = path.codeUnitAt(i); |
| 46 if (isSeparator(codeUnit)) return 0; |
| 47 if (codeUnit == chars.COLON) { |
| 48 if (i == 0) return 0; |
| 49 |
| 50 // The root part is up until the next '/', or the full path. Skip ':' |
| 51 // (and '//' if it exists) and search for '/' after that. |
| 52 if (path.startsWith('//', i + 1)) i += 3; |
| 53 var index = path.indexOf('/', i); |
| 54 if (index <= 0) return path.length; |
| 55 |
| 56 // file: URLs sometimes consider Windows drive letters part of the root. |
| 57 // See https://url.spec.whatwg.org/#file-slash-state. |
| 58 if (!withDrive || path.length < index + 3) return index; |
| 59 if (!path.startsWith('file://')) return index; |
| 60 if (!isDriveLetter(path, index + 1)) return index; |
| 61 return path.length == index + 3 ? index + 3 : index + 4; |
| 62 } |
| 63 } |
| 64 |
| 44 var index = path.indexOf("/"); | 65 var index = path.indexOf("/"); |
| 45 if (index > 0 && path.startsWith('://', index - 1)) { | 66 if (index > 0 && path.startsWith('://', index - 1)) { |
| 46 // The root part is up until the next '/', or the full path. Skip | |
| 47 // '://' and search for '/' after that. | |
| 48 index = path.indexOf('/', index + 2); | |
| 49 if (index > 0) return index; | |
| 50 return path.length; | |
| 51 } | 67 } |
| 52 return 0; | 68 return 0; |
| 53 } | 69 } |
| 54 | 70 |
| 55 bool isRootRelative(String path) => | 71 bool isRootRelative(String path) => |
| 56 path.isNotEmpty && isSeparator(path.codeUnitAt(0)); | 72 path.isNotEmpty && isSeparator(path.codeUnitAt(0)); |
| 57 | 73 |
| 58 String getRelativeRoot(String path) => isRootRelative(path) ? '/' : null; | 74 String getRelativeRoot(String path) => isRootRelative(path) ? '/' : null; |
| 59 | 75 |
| 60 String pathFromUri(Uri uri) => uri.toString(); | 76 String pathFromUri(Uri uri) => uri.toString(); |
| 61 | 77 |
| 62 Uri relativePathToUri(String path) => Uri.parse(path); | 78 Uri relativePathToUri(String path) => Uri.parse(path); |
| 63 Uri absolutePathToUri(String path) => Uri.parse(path); | 79 Uri absolutePathToUri(String path) => Uri.parse(path); |
| 64 } | 80 } |
| OLD | NEW |