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