| 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:math'; |
| 8 | 8 |
| 9 String relativize(Uri base, Uri uri, bool isWindows) { | 9 String relativize(Uri base, Uri uri, bool isWindows) { |
| 10 if (!base.path.startsWith('/')) { | |
| 11 // Also throw an exception if [base] or base.path is null. | |
| 12 throw new ArgumentError('Expected absolute path: ${base.path}'); | |
| 13 } | |
| 14 if (!uri.path.startsWith('/')) { | |
| 15 // Also throw an exception if [uri] or uri.path is null. | |
| 16 throw new ArgumentError('Expected absolute path: ${uri.path}'); | |
| 17 } | |
| 18 bool equalsNCS(String a, String b) { | 10 bool equalsNCS(String a, String b) { |
| 19 return a.toLowerCase() == b.toLowerCase(); | 11 return a.toLowerCase() == b.toLowerCase(); |
| 20 } | 12 } |
| 21 | 13 |
| 14 if (!equalsNCS(base.scheme, uri.scheme) || |
| 15 equalsNCS(base.scheme, 'dart') || |
| 16 equalsNCS(base.scheme, 'package')) { |
| 17 return uri.toString(); |
| 18 } |
| 19 |
| 20 if (!equalsNCS(base.scheme, 'file')) { |
| 21 isWindows = false; |
| 22 } |
| 23 |
| 22 String normalize(String path) { | 24 String normalize(String path) { |
| 23 if (isWindows) { | 25 if (isWindows) { |
| 24 return path.toLowerCase(); | 26 return path.toLowerCase(); |
| 25 } else { | 27 } else { |
| 26 return path; | 28 return path; |
| 27 } | 29 } |
| 28 } | 30 } |
| 29 | 31 |
| 30 if (equalsNCS(base.scheme, 'file') && | 32 if (base.userInfo == uri.userInfo && |
| 31 equalsNCS(base.scheme, uri.scheme) && | |
| 32 base.userInfo == uri.userInfo && | |
| 33 equalsNCS(base.host, uri.host) && | 33 equalsNCS(base.host, uri.host) && |
| 34 base.port == uri.port && | 34 base.port == uri.port && |
| 35 uri.query == "" && uri.fragment == "") { | 35 uri.query == "" && uri.fragment == "") { |
| 36 if (normalize(uri.path).startsWith(normalize(base.path))) { | 36 if (normalize(uri.path).startsWith(normalize(base.path))) { |
| 37 return uri.path.substring(base.path.lastIndexOf('/') + 1); | 37 return uri.path.substring(base.path.lastIndexOf('/') + 1); |
| 38 } | 38 } |
| 39 | 39 |
| 40 if (!base.path.startsWith('/') || |
| 41 !uri.path.startsWith('/')) { |
| 42 return uri.toString(); |
| 43 } |
| 44 |
| 40 List<String> uriParts = uri.path.split('/'); | 45 List<String> uriParts = uri.path.split('/'); |
| 41 List<String> baseParts = base.path.split('/'); | 46 List<String> baseParts = base.path.split('/'); |
| 42 int common = 0; | 47 int common = 0; |
| 43 int length = min(uriParts.length, baseParts.length); | 48 int length = min(uriParts.length, baseParts.length); |
| 44 while (common < length && | 49 while (common < length && |
| 45 normalize(uriParts[common]) == normalize(baseParts[common])) { | 50 normalize(uriParts[common]) == normalize(baseParts[common])) { |
| 46 common++; | 51 common++; |
| 47 } | 52 } |
| 48 if (common == 1 || (isWindows && common == 2)) { | 53 if (common == 1 || (isWindows && common == 2)) { |
| 49 // The first part will always be an empty string because the | 54 // The first part will always be an empty string because the |
| 50 // paths are absolute. On Windows, we must also consider drive | 55 // paths are absolute. On Windows, we must also consider drive |
| 51 // letters or hostnames. | 56 // letters or hostnames. |
| 52 if (baseParts.length > common + 1) { | 57 if (baseParts.length > common + 1) { |
| 53 // Avoid using '..' to go to the root, unless we are already there. | 58 // Avoid using '..' to go to the root, unless we are already there. |
| 54 return uri.path; | 59 return uri.path; |
| 55 } | 60 } |
| 56 } | 61 } |
| 57 StringBuffer sb = new StringBuffer(); | 62 StringBuffer sb = new StringBuffer(); |
| 58 for (int i = common + 1; i < baseParts.length; i++) { | 63 for (int i = common + 1; i < baseParts.length; i++) { |
| 59 sb.write('../'); | 64 sb.write('../'); |
| 60 } | 65 } |
| 61 for (int i = common; i < uriParts.length - 1; i++) { | 66 for (int i = common; i < uriParts.length - 1; i++) { |
| 62 sb.write('${uriParts[i]}/'); | 67 sb.write('${uriParts[i]}/'); |
| 63 } | 68 } |
| 64 sb.write('${uriParts.last}'); | 69 sb.write('${uriParts.last}'); |
| 65 return sb.toString(); | 70 return sb.toString(); |
| 66 } | 71 } |
| 67 return uri.toString(); | 72 return uri.toString(); |
| 68 } | 73 } |
| OLD | NEW |