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