OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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('uri_extras'); | 5 #library('uri_extras'); |
6 | 6 |
| 7 #import('dart:math'); |
7 #import('dart:uri'); | 8 #import('dart:uri'); |
8 | 9 |
9 String relativize(Uri base, Uri uri) { | 10 String relativize(Uri base, Uri uri) { |
10 if (base.scheme == 'file' && | 11 if (base.scheme == 'file' && |
11 base.scheme == uri.scheme && | 12 base.scheme == uri.scheme && |
12 base.userInfo == uri.userInfo && | 13 base.userInfo == uri.userInfo && |
13 base.domain == uri.domain && | 14 base.domain == uri.domain && |
14 base.port == uri.port && | 15 base.port == uri.port && |
15 uri.query == "" && uri.fragment == "") { | 16 uri.query == "" && uri.fragment == "") { |
16 if (uri.path.startsWith(base.path)) { | 17 if (uri.path.startsWith(base.path)) { |
17 return uri.path.substring(base.path.length); | 18 return uri.path.substring(base.path.length); |
18 } | 19 } |
19 List<String> uriParts = uri.path.split('/'); | 20 List<String> uriParts = uri.path.split('/'); |
20 List<String> baseParts = base.path.split('/'); | 21 List<String> baseParts = base.path.split('/'); |
21 int common = 0; | 22 int common = 0; |
22 int length = Math.min(uriParts.length, baseParts.length); | 23 int length = min(uriParts.length, baseParts.length); |
23 while (common < length && uriParts[common] == baseParts[common]) { | 24 while (common < length && uriParts[common] == baseParts[common]) { |
24 common++; | 25 common++; |
25 } | 26 } |
26 StringBuffer sb = new StringBuffer(); | 27 StringBuffer sb = new StringBuffer(); |
27 for (int i = common + 1; i < baseParts.length; i++) { | 28 for (int i = common + 1; i < baseParts.length; i++) { |
28 sb.add('../'); | 29 sb.add('../'); |
29 } | 30 } |
30 for (int i = common; i < uriParts.length - 1; i++) { | 31 for (int i = common; i < uriParts.length - 1; i++) { |
31 sb.add('${uriParts[i]}/'); | 32 sb.add('${uriParts[i]}/'); |
32 } | 33 } |
33 sb.add('${uriParts.last()}'); | 34 sb.add('${uriParts.last()}'); |
34 return sb.toString(); | 35 return sb.toString(); |
35 } | 36 } |
36 return uri.toString(); | 37 return uri.toString(); |
37 } | 38 } |
OLD | NEW |