OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** | |
6 * A parsed URI, inspired by: | |
7 * http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html | |
8 */ | |
9 class Uri extends uri.Uri { | |
10 /** | |
11 * Parses a URL query string into a map. Because you can have multiple values | |
12 * for the same parameter name, each parameter name maps to a list of | |
13 * values. For example, '?a=b&c=d&a=e' would be parsed as | |
14 * [{'a':['b','e'],'c':['d']}]. | |
15 */ | |
16 // TODO(jmesserly): consolidate with new Uri.fromString(...) | |
17 static Map<String, List<String>> parseQuery(String queryString) { | |
18 final queryParams = new Map<String, List<String>>(); | |
19 if (queryString.startsWith('?')) { | |
20 final params = queryString.substring(1, queryString.length).split('&'); | |
21 for (final param in params) { | |
22 List<String> parts = param.split('='); | |
23 if (parts.length == 2) { | |
24 // TODO(hiltonc) the name and value should be URL decoded. | |
25 String name = parts[0]; | |
26 String value = parts[1]; | |
27 | |
28 // Create a list of values for this name if not yet done. | |
29 List values = queryParams[name]; | |
30 if (values === null) { | |
31 values = new List(); | |
32 queryParams[name] = values; | |
33 } | |
34 | |
35 values.add(value); | |
36 } | |
37 } | |
38 } | |
39 return queryParams; | |
40 } | |
41 | |
42 /** | |
43 * Percent-encodes a string for use as a query parameter in a URI. | |
44 */ | |
45 // TODO(rnystrom): Get rid of this when the real encodeURIComponent() | |
46 // function is available within Dart. | |
47 static String encodeComponent(String component) { | |
48 if (component == null) return component; | |
49 | |
50 // TODO(terry): Added b/5096547 to track replace should by default behave | |
51 // like replaceAll to avoid a problematic usage pattern. | |
52 return component.replaceAll(':', '%3A') | |
53 .replaceAll('/', '%2F') | |
54 .replaceAll('?', '%3F') | |
55 .replaceAll('=', '%3D') | |
56 .replaceAll('&', '%26') | |
57 .replaceAll(' ', '%20'); | |
58 } | |
59 | |
60 /** | |
61 * Decodes a string used a query parameter by replacing percent-encoded | |
62 * sequences with their original characters. | |
63 */ | |
64 // TODO(jmesserly): replace this with a better implementation | |
65 static String decodeComponent(String component) { | |
66 if (component == null) return component; | |
67 | |
68 return component.replaceAll('%3A', ':') | |
69 .replaceAll('%2F', '/') | |
70 .replaceAll('%3F', '?') | |
71 .replaceAll('%3D', '=') | |
72 .replaceAll('%26', '&') | |
73 .replaceAll('%20', ' '); | |
74 } | |
75 | |
76 Uri.fromString(String uri) : super.fromString(uri); | |
77 } | |
OLD | NEW |