OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library path.style.url; | |
6 | |
7 import '../characters.dart' as chars; | |
8 import '../internal_style.dart'; | |
9 | |
10 /// The style for URL paths. | |
11 class UrlStyle extends InternalStyle { | |
12 UrlStyle(); | |
13 | |
14 final name = 'url'; | |
15 final separator = '/'; | |
16 final separators = const ['/']; | |
17 | |
18 // Deprecated properties. | |
19 | |
20 final separatorPattern = new RegExp(r'/'); | |
21 final needsSeparatorPattern = | |
22 new RegExp(r"(^[a-zA-Z][-+.a-zA-Z\d]*://|[^/])$"); | |
23 final rootPattern = new RegExp(r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*"); | |
24 final relativeRootPattern = new RegExp(r"^/"); | |
25 | |
26 bool containsSeparator(String path) => path.contains('/'); | |
27 | |
28 bool isSeparator(int codeUnit) => codeUnit == chars.SLASH; | |
29 | |
30 bool needsSeparator(String path) { | |
31 if (path.isEmpty) return false; | |
32 | |
33 // A URL that doesn't end in "/" always needs a separator. | |
34 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true; | |
35 | |
36 // A URI that's just "scheme://" needs an extra separator, despite ending | |
37 // with "/". | |
38 return path.endsWith("://") && rootLength(path) == path.length; | |
39 } | |
40 | |
41 int rootLength(String path) { | |
42 if (path.isEmpty) return 0; | |
43 if (isSeparator(path.codeUnitAt(0))) return 1; | |
44 var index = path.indexOf("/"); | |
45 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 } | |
52 return 0; | |
53 } | |
54 | |
55 bool isRootRelative(String path) => | |
56 path.isNotEmpty && isSeparator(path.codeUnitAt(0)); | |
57 | |
58 String getRelativeRoot(String path) => isRootRelative(path) ? '/' : null; | |
59 | |
60 String pathFromUri(Uri uri) => uri.toString(); | |
61 | |
62 Uri relativePathToUri(String path) => Uri.parse(path); | |
63 Uri absolutePathToUri(String path) => Uri.parse(path); | |
64 } | |
OLD | NEW |