OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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.internal_style; |
| 6 |
| 7 import 'context.dart'; |
| 8 import 'style.dart'; |
| 9 |
| 10 /// The internal interface for the [Style] type. |
| 11 /// |
| 12 /// Users should be able to pass around instances of [Style] like an enum, but |
| 13 /// the members that [Context] uses should be hidden from them. Those members |
| 14 /// are defined on this class instead. |
| 15 abstract class InternalStyle extends Style { |
| 16 /// The default path separator for this style. |
| 17 /// |
| 18 /// On POSIX, this is `/`. On Windows, it's `\`. |
| 19 String get separator; |
| 20 |
| 21 /// Returns whether [path] contains a separator. |
| 22 bool containsSeparator(String path); |
| 23 |
| 24 /// Returns whether [codeUnit] is the character code of a separator. |
| 25 bool isSeparator(int codeUnit); |
| 26 |
| 27 /// Returns whether this path component needs a separator after it. |
| 28 /// |
| 29 /// Windows and POSIX styles just need separators when the previous component |
| 30 /// doesn't already end in a separator, but the URL always needs to place a |
| 31 /// separator between the root and the first component, even if the root |
| 32 /// already ends in a separator character. For example, to join "file://" and |
| 33 /// "usr", an additional "/" is needed (making "file:///usr"). |
| 34 bool needsSeparator(String path); |
| 35 |
| 36 /// Returns the number of characters of the root part. |
| 37 /// |
| 38 /// Returns 0 if the path is relative. |
| 39 /// |
| 40 /// If the path is root-relative, the root length is 1. |
| 41 int rootLength(String path); |
| 42 |
| 43 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, |
| 44 /// returns `null`. |
| 45 String getRoot(String path) { |
| 46 var length = rootLength(path); |
| 47 if (length > 0) return path.substring(0, length); |
| 48 return isRootRelative(path) ? path[0] : null; |
| 49 } |
| 50 |
| 51 /// Returns whether [path] is root-relative. |
| 52 /// |
| 53 /// If [path] is relative or absolute and not root-relative, returns `false`. |
| 54 bool isRootRelative(String path); |
| 55 |
| 56 /// Returns the path represented by [uri] in this style. |
| 57 String pathFromUri(Uri uri); |
| 58 |
| 59 /// Returns the URI that represents the relative path made of [parts]. |
| 60 Uri relativePathToUri(String path) { |
| 61 var segments = context.split(path); |
| 62 |
| 63 // Ensure that a trailing slash in the path produces a trailing slash in the |
| 64 // URL. |
| 65 if (isSeparator(path.codeUnitAt(path.length - 1))) segments.add(''); |
| 66 return new Uri(pathSegments: segments); |
| 67 } |
| 68 |
| 69 /// Returns the URI that represents [path], which is assumed to be absolute. |
| 70 Uri absolutePathToUri(String path); |
| 71 } |
OLD | NEW |