| 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('/')) { | 10 if (!base.path.startsWith('/')) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 if (equalsNCS(base.scheme, 'file') && | 30 if (equalsNCS(base.scheme, 'file') && |
| 31 equalsNCS(base.scheme, uri.scheme) && | 31 equalsNCS(base.scheme, uri.scheme) && |
| 32 base.userInfo == uri.userInfo && | 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.length); | 37 return uri.path.substring(base.path.lastIndexOf('/') + 1); |
| 38 } | 38 } |
| 39 |
| 39 List<String> uriParts = uri.path.split('/'); | 40 List<String> uriParts = uri.path.split('/'); |
| 40 List<String> baseParts = base.path.split('/'); | 41 List<String> baseParts = base.path.split('/'); |
| 41 int common = 0; | 42 int common = 0; |
| 42 int length = min(uriParts.length, baseParts.length); | 43 int length = min(uriParts.length, baseParts.length); |
| 43 while (common < length && | 44 while (common < length && |
| 44 normalize(uriParts[common]) == normalize(baseParts[common])) { | 45 normalize(uriParts[common]) == normalize(baseParts[common])) { |
| 45 common++; | 46 common++; |
| 46 } | 47 } |
| 47 if (common == 1 || (isWindows && common == 2)) { | 48 if (common == 1 || (isWindows && common == 2)) { |
| 48 // The first part will always be an empty string because the | 49 // The first part will always be an empty string because the |
| 49 // paths are absolute. On Windows, we must also consider drive | 50 // paths are absolute. On Windows, we must also consider drive |
| 50 // letters or hostnames. | 51 // letters or hostnames. |
| 51 return uri.path; | 52 if (baseParts.length > common + 1) { |
| 53 // Avoid using '..' to go to the root, unless we are already there. |
| 54 return uri.path; |
| 55 } |
| 52 } | 56 } |
| 53 StringBuffer sb = new StringBuffer(); | 57 StringBuffer sb = new StringBuffer(); |
| 54 for (int i = common + 1; i < baseParts.length; i++) { | 58 for (int i = common + 1; i < baseParts.length; i++) { |
| 55 sb.write('../'); | 59 sb.write('../'); |
| 56 } | 60 } |
| 57 for (int i = common; i < uriParts.length - 1; i++) { | 61 for (int i = common; i < uriParts.length - 1; i++) { |
| 58 sb.write('${uriParts[i]}/'); | 62 sb.write('${uriParts[i]}/'); |
| 59 } | 63 } |
| 60 sb.write('${uriParts.last}'); | 64 sb.write('${uriParts.last}'); |
| 61 return sb.toString(); | 65 return sb.toString(); |
| 62 } | 66 } |
| 63 return uri.toString(); | 67 return uri.toString(); |
| 64 } | 68 } |
| OLD | NEW |