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