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 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A parsed URI, such as a URL. | 8 * A parsed URI, such as a URL. |
9 * | 9 * |
10 * **See also:** | 10 * **See also:** |
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 * new Uri.file(r"C:xxx\yyy", windows: true); | 698 * new Uri.file(r"C:xxx\yyy", windows: true); |
699 * | 699 * |
700 * // file://server/share/file | 700 * // file://server/share/file |
701 * new Uri.file(r"\\server\share\file", windows: true); | 701 * new Uri.file(r"\\server\share\file", windows: true); |
702 * ``` | 702 * ``` |
703 * | 703 * |
704 * If the path passed is not a legal file path [ArgumentError] is thrown. | 704 * If the path passed is not a legal file path [ArgumentError] is thrown. |
705 */ | 705 */ |
706 factory Uri.file(String path, {bool windows}) { | 706 factory Uri.file(String path, {bool windows}) { |
707 windows = (windows == null) ? Uri._isWindows : windows; | 707 windows = (windows == null) ? Uri._isWindows : windows; |
708 return windows ? _makeWindowsFileUrl(path) : _makeFileUri(path); | 708 return windows ? _makeWindowsFileUrl(path, false) |
| 709 : _makeFileUri(path, false); |
709 } | 710 } |
710 | 711 |
711 /** | 712 /** |
| 713 * Like [Uri.file] except that a non-empty URI path ends in a slash. |
| 714 * |
| 715 * If [path] is not empty, and it doesn't end in a directory separator, |
| 716 * then a slash is added to the returned URI's path. |
| 717 * In all other cases, the result is the same as returned by `Uri.file`. |
| 718 */ |
| 719 factory Uri.directory(String path, {bool windows}) { |
| 720 windows = (windows == null) ? Uri._isWindows : windows; |
| 721 return windows ? _makeWindowsFileUrl(path, true) |
| 722 : _makeFileUri(path, true); |
| 723 } |
| 724 |
| 725 /** |
712 * Returns the natural base URI for the current platform. | 726 * Returns the natural base URI for the current platform. |
713 * | 727 * |
714 * When running in a browser this is the current URL (from | 728 * When running in a browser this is the current URL (from |
715 * `window.location.href`). | 729 * `window.location.href`). |
716 * | 730 * |
717 * When not running in a browser this is the file URI referencing | 731 * When not running in a browser this is the file URI referencing |
718 * the current working directory. | 732 * the current working directory. |
719 */ | 733 */ |
720 external static Uri get base; | 734 external static Uri get base; |
721 | 735 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 } | 769 } |
756 if (argumentError) { | 770 if (argumentError) { |
757 throw new ArgumentError("Illegal drive letter " + | 771 throw new ArgumentError("Illegal drive letter " + |
758 new String.fromCharCode(charCode)); | 772 new String.fromCharCode(charCode)); |
759 } else { | 773 } else { |
760 throw new UnsupportedError("Illegal drive letter " + | 774 throw new UnsupportedError("Illegal drive letter " + |
761 new String.fromCharCode(charCode)); | 775 new String.fromCharCode(charCode)); |
762 } | 776 } |
763 } | 777 } |
764 | 778 |
765 static _makeFileUri(String path) { | 779 static _makeFileUri(String path, bool slashTerminated) { |
766 const String sep = "/"; | 780 const String sep = "/"; |
| 781 var segments = path.split(sep); |
| 782 if (slashTerminated && |
| 783 segments.isNotEmpty && |
| 784 segments.last.isNotEmpty) { |
| 785 segments.add(""); // Extra separator at end. |
| 786 } |
767 if (path.startsWith(sep)) { | 787 if (path.startsWith(sep)) { |
768 // Absolute file:// URI. | 788 // Absolute file:// URI. |
769 return new Uri(scheme: "file", pathSegments: path.split(sep)); | 789 return new Uri(scheme: "file", pathSegments: segments); |
770 } else { | 790 } else { |
771 // Relative URI. | 791 // Relative URI. |
772 return new Uri(pathSegments: path.split(sep)); | 792 return new Uri(pathSegments: segments); |
773 } | 793 } |
774 } | 794 } |
775 | 795 |
776 static _makeWindowsFileUrl(String path) { | 796 static _makeWindowsFileUrl(String path, bool slashTerminated) { |
777 if (path.startsWith(r"\\?\")) { | 797 if (path.startsWith(r"\\?\")) { |
778 if (path.startsWith(r"UNC\", 4)) { | 798 if (path.startsWith(r"UNC\", 4)) { |
779 path = path.replaceRange(0, 7, r'\'); | 799 path = path.replaceRange(0, 7, r'\'); |
780 } else { | 800 } else { |
781 path = path.substring(4); | 801 path = path.substring(4); |
782 if (path.length < 3 || | 802 if (path.length < 3 || |
783 path.codeUnitAt(1) != _COLON || | 803 path.codeUnitAt(1) != _COLON || |
784 path.codeUnitAt(2) != _BACKSLASH) { | 804 path.codeUnitAt(2) != _BACKSLASH) { |
785 throw new ArgumentError( | 805 throw new ArgumentError( |
786 r"Windows paths with \\?\ prefix must be absolute"); | 806 r"Windows paths with \\?\ prefix must be absolute"); |
787 } | 807 } |
788 } | 808 } |
789 } else { | 809 } else { |
790 path = path.replaceAll("/", r'\'); | 810 path = path.replaceAll("/", r'\'); |
791 } | 811 } |
792 const String sep = r'\'; | 812 const String sep = r'\'; |
793 if (path.length > 1 && path.codeUnitAt(1) == _COLON) { | 813 if (path.length > 1 && path.codeUnitAt(1) == _COLON) { |
794 _checkWindowsDriveLetter(path.codeUnitAt(0), true); | 814 _checkWindowsDriveLetter(path.codeUnitAt(0), true); |
795 if (path.length == 2 || path.codeUnitAt(2) != _BACKSLASH) { | 815 if (path.length == 2 || path.codeUnitAt(2) != _BACKSLASH) { |
796 throw new ArgumentError( | 816 throw new ArgumentError( |
797 "Windows paths with drive letter must be absolute"); | 817 "Windows paths with drive letter must be absolute"); |
798 } | 818 } |
799 // Absolute file://C:/ URI. | 819 // Absolute file://C:/ URI. |
800 var pathSegments = path.split(sep); | 820 var pathSegments = path.split(sep); |
| 821 if (slashTerminated && |
| 822 pathSegments.last.isNotEmpty) { |
| 823 pathSegments.add(""); // Extra separator at end. |
| 824 } |
801 _checkWindowsPathReservedCharacters(pathSegments, true, 1); | 825 _checkWindowsPathReservedCharacters(pathSegments, true, 1); |
802 return new Uri(scheme: "file", pathSegments: pathSegments); | 826 return new Uri(scheme: "file", pathSegments: pathSegments); |
803 } | 827 } |
804 | 828 |
805 if (path.startsWith(sep)) { | 829 if (path.startsWith(sep)) { |
806 if (path.startsWith(sep, 1)) { | 830 if (path.startsWith(sep, 1)) { |
807 // Absolute file:// URI with host. | 831 // Absolute file:// URI with host. |
808 int pathStart = path.indexOf(r'\', 2); | 832 int pathStart = path.indexOf(r'\', 2); |
809 String hostPart = | 833 String hostPart = |
810 (pathStart < 0) ? path.substring(2) : path.substring(2, pathStart); | 834 (pathStart < 0) ? path.substring(2) : path.substring(2, pathStart); |
811 String pathPart = | 835 String pathPart = |
812 (pathStart < 0) ? "" : path.substring(pathStart + 1); | 836 (pathStart < 0) ? "" : path.substring(pathStart + 1); |
813 var pathSegments = pathPart.split(sep); | 837 var pathSegments = pathPart.split(sep); |
814 _checkWindowsPathReservedCharacters(pathSegments, true); | 838 _checkWindowsPathReservedCharacters(pathSegments, true); |
| 839 if (slashTerminated && |
| 840 pathSegments.last.isNotEmpty) { |
| 841 pathSegments.add(""); // Extra separator at end. |
| 842 } |
815 return new Uri( | 843 return new Uri( |
816 scheme: "file", host: hostPart, pathSegments: pathSegments); | 844 scheme: "file", host: hostPart, pathSegments: pathSegments); |
817 } else { | 845 } else { |
818 // Absolute file:// URI. | 846 // Absolute file:// URI. |
819 var pathSegments = path.split(sep); | 847 var pathSegments = path.split(sep); |
| 848 if (slashTerminated && |
| 849 pathSegments.last.isNotEmpty) { |
| 850 pathSegments.add(""); // Extra separator at end. |
| 851 } |
820 _checkWindowsPathReservedCharacters(pathSegments, true); | 852 _checkWindowsPathReservedCharacters(pathSegments, true); |
821 return new Uri(scheme: "file", pathSegments: pathSegments); | 853 return new Uri(scheme: "file", pathSegments: pathSegments); |
822 } | 854 } |
823 } else { | 855 } else { |
824 // Relative URI. | 856 // Relative URI. |
825 var pathSegments = path.split(sep); | 857 var pathSegments = path.split(sep); |
826 _checkWindowsPathReservedCharacters(pathSegments, true); | 858 _checkWindowsPathReservedCharacters(pathSegments, true); |
| 859 if (slashTerminated && |
| 860 pathSegments.isNotEmpty && |
| 861 pathSegments.last.isNotEmpty) { |
| 862 pathSegments.add(""); // Extra separator at end. |
| 863 } |
827 return new Uri(pathSegments: pathSegments); | 864 return new Uri(pathSegments: pathSegments); |
828 } | 865 } |
829 } | 866 } |
830 | 867 |
831 /** | 868 /** |
832 * Returns a new `Uri` based on this one, but with some parts replaced. | 869 * Returns a new `Uri` based on this one, but with some parts replaced. |
833 * | 870 * |
834 * This method takes the same parameters as the [new Uri] constructor, | 871 * This method takes the same parameters as the [new Uri] constructor, |
835 * and they have the same meaning. | 872 * and they have the same meaning. |
836 * | 873 * |
(...skipping 1590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2427 0xafff, // 0x30 - 0x3f 1111111111110101 | 2464 0xafff, // 0x30 - 0x3f 1111111111110101 |
2428 // @ABCDEFGHIJKLMNO | 2465 // @ABCDEFGHIJKLMNO |
2429 0xffff, // 0x40 - 0x4f 1111111111111111 | 2466 0xffff, // 0x40 - 0x4f 1111111111111111 |
2430 // PQRSTUVWXYZ _ | 2467 // PQRSTUVWXYZ _ |
2431 0x87ff, // 0x50 - 0x5f 1111111111100001 | 2468 0x87ff, // 0x50 - 0x5f 1111111111100001 |
2432 // abcdefghijklmno | 2469 // abcdefghijklmno |
2433 0xfffe, // 0x60 - 0x6f 0111111111111111 | 2470 0xfffe, // 0x60 - 0x6f 0111111111111111 |
2434 // pqrstuvwxyz ~ | 2471 // pqrstuvwxyz ~ |
2435 0x47ff]; // 0x70 - 0x7f 1111111111100010 | 2472 0x47ff]; // 0x70 - 0x7f 1111111111100010 |
2436 } | 2473 } |
OLD | NEW |