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 '../internal_style.dart'; | 7 import '../internal_style.dart'; |
| 8 import '../utils.dart'; |
8 | 9 |
9 /// The style for URL paths. | 10 /// The style for URL paths. |
10 class UrlStyle extends InternalStyle { | 11 class UrlStyle extends InternalStyle { |
11 UrlStyle(); | 12 UrlStyle(); |
12 | 13 |
13 final name = 'url'; | 14 final name = 'url'; |
14 final separator = '/'; | 15 final separator = '/'; |
| 16 final separators = const ['/']; |
| 17 |
| 18 // Deprecated properties. |
| 19 |
15 final separatorPattern = new RegExp(r'/'); | 20 final separatorPattern = new RegExp(r'/'); |
16 final needsSeparatorPattern = new RegExp( | 21 final needsSeparatorPattern = new RegExp( |
17 r"(^[a-zA-Z][-+.a-zA-Z\d]*://|[^/])$"); | 22 r"(^[a-zA-Z][-+.a-zA-Z\d]*://|[^/])$"); |
18 final rootPattern = new RegExp(r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*"); | 23 final rootPattern = new RegExp(r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*"); |
19 final relativeRootPattern = new RegExp(r"^/"); | 24 final relativeRootPattern = new RegExp(r"^/"); |
20 | 25 |
| 26 bool needsSeparator(String path) { |
| 27 if (path.isEmpty) return false; |
| 28 |
| 29 // A URL that doesn't end in "/" always needs a separator. |
| 30 if (path.codeUnitAt(path.length - 1) != 0x2f) return true; |
| 31 |
| 32 // A URI that's just "scheme://" needs an extra separator, despite ending |
| 33 // with "/". |
| 34 var root = _getRoot(path); |
| 35 return root != null && root.endsWith('://'); |
| 36 } |
| 37 |
| 38 String getRoot(String path) { |
| 39 var root = _getRoot(path); |
| 40 return root == null ? getRelativeRoot(path) : root; |
| 41 } |
| 42 |
| 43 String getRelativeRoot(String path) { |
| 44 if (path.isEmpty) return null; |
| 45 return path.codeUnitAt(0) == 0x2f ? "/" : null; |
| 46 } |
| 47 |
21 String pathFromUri(Uri uri) => uri.toString(); | 48 String pathFromUri(Uri uri) => uri.toString(); |
22 | 49 |
23 Uri relativePathToUri(String path) => Uri.parse(path); | 50 Uri relativePathToUri(String path) => Uri.parse(path); |
24 Uri absolutePathToUri(String path) => Uri.parse(path); | 51 Uri absolutePathToUri(String path) => Uri.parse(path); |
| 52 |
| 53 // A helper method for [getRoot] that doesn't handle relative roots. |
| 54 String _getRoot(String path) { |
| 55 if (path.isEmpty) return null; |
| 56 |
| 57 // We aren't using a RegExp for this because they're slow (issue 19090). If |
| 58 // we could, we'd match against r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*". |
| 59 |
| 60 if (!isAlphabetic(path.codeUnitAt(0))) return null; |
| 61 var start = 1; |
| 62 for (; start < path.length; start++) { |
| 63 var char = path.codeUnitAt(start); |
| 64 if (isAlphabetic(char)) continue; |
| 65 if (isNumeric(char)) continue; |
| 66 // Schemes can contain "-", "+", or ".". |
| 67 if (char == 0x2d || char == 0x2b || char == 0x2e) continue; |
| 68 break; |
| 69 } |
| 70 |
| 71 if (start + 3 > path.length) return null; |
| 72 if (path.substring(start, start + 3) != '://') return null; |
| 73 start += 3; |
| 74 |
| 75 // A URL root can end with a non-"/" prefix. |
| 76 while (start < path.length && path.codeUnitAt(start) != 0x2f) { |
| 77 start++; |
| 78 } |
| 79 return path.substring(0, start); |
| 80 } |
25 } | 81 } |
OLD | NEW |