Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Unified Diff: sdk/lib/_internal/js_runtime/lib/core_patch.dart

Issue 1416373007: Split _uriEncode into JS and VM versions. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/js_runtime/lib/core_patch.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/core_patch.dart b/sdk/lib/_internal/js_runtime/lib/core_patch.dart
index 7d6ad7c244533fc8af009361b01a016c19aae16b..cbdb010a67e7b308cf97bd3360325e01bb7781b1 100644
--- a/sdk/lib/_internal/js_runtime/lib/core_patch.dart
+++ b/sdk/lib/_internal/js_runtime/lib/core_patch.dart
@@ -527,6 +527,50 @@ class Uri {
if (uri != null) return Uri.parse(uri);
throw new UnsupportedError("'Uri.base' is not supported");
}
+
+
+ // Matches a String that _uriEncodes to itself regardless of the kind of
+ // component. This corresponds to [_unreservedTable], i.e. characters that
+ // are not encoded by any encoding table.
+ static final RegExp _needsNoEncoding = new RegExp(r'^[\-\.0-9A-Z_a-z~]*$');
+
+ /**
+ * This is the internal implementation of JavaScript's encodeURI function.
+ * It encodes all characters in the string [text] except for those
+ * that appear in [canonicalTable], and returns the escaped string.
+ */
+ @patch
+ static String _uriEncode(List<int> canonicalTable,
+ String text,
+ Encoding encoding,
+ bool spaceToPlus) {
+ if (identical(encoding, UTF8) &&
Lasse Reichstein Nielsen 2015/11/04 10:15:53 I added the UTF8 requirement to ensure that the en
+ _needsNoEncoding.hasMatch(text)) return text;
+
+ byteToHex(byte, buffer) {
+ const String hex = '0123456789ABCDEF';
+ buffer.writeCharCode(hex.codeUnitAt(byte >> 4));
+ buffer.writeCharCode(hex.codeUnitAt(byte & 0x0f));
+ }
Lasse Reichstein Nielsen 2015/11/04 10:15:53 Consider lining this function unless you know the
sra1 2015/11/04 18:38:33 Inline it, it is not inlined at the moment.
+
+ // Encode the string into bytes then generate an ASCII only string
+ // by percent encoding selected bytes.
+ StringBuffer result = new StringBuffer();
+ var bytes = encoding.encode(text);
+ for (int i = 0; i < bytes.length; i++) {
+ int byte = bytes[i];
+ if (byte < 128 &&
+ ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) {
+ result.writeCharCode(byte);
+ } else if (spaceToPlus && byte == _SPACE) {
+ result.writeCharCode(_PLUS);
+ } else {
+ result.writeCharCode(_PERCENT);
+ byteToHex(byte, result);
+ }
+ }
+ return result.toString();
+ }
}
@patch

Powered by Google App Engine
This is Rietveld 408576698