| OLD | NEW |
| (Empty) | |
| 1 part of angular.core.dom; |
| 2 |
| 3 /** |
| 4 * This class provides low-level acces to the browser's cookies. |
| 5 * It is not meant to be used directly by applications. Instead |
| 6 * use the Cookies service. |
| 7 * |
| 8 */ |
| 9 @NgInjectableService() |
| 10 class BrowserCookies { |
| 11 dom.Document _document; |
| 12 |
| 13 var lastCookies = {}; |
| 14 var lastCookieString = ''; |
| 15 var cookiePath; |
| 16 var baseElement; |
| 17 |
| 18 |
| 19 BrowserCookies() { |
| 20 // Injecting document produces the error 'Caught Compile-time error during m
irrored execution: |
| 21 // <'file:///mnt/data/b/build/slave/dartium-lucid32-full-trunk/build/src/out
/Release/gen/blink/ |
| 22 // bindings/dart/dart/html/Document.dart': Error: line 7 pos 3: expression m
ust be a compile-time constant |
| 23 // @ DocsEditable ' |
| 24 // I have not had time to debug it yet. |
| 25 _document = dom.document; |
| 26 |
| 27 var baseElementList = _document.getElementsByName('base'); |
| 28 if (baseElementList.isEmpty) return; |
| 29 baseElement = _document.getElementsByName('base').first; |
| 30 cookiePath = _baseHref(); |
| 31 } |
| 32 |
| 33 var URL_PROTOCOL = new RegExp(r'^https?\:\/\/[^\/]*'); |
| 34 _baseHref() { |
| 35 var href = baseElement != null ? baseElement.attr('href') : null; |
| 36 return href != null ? href.replace(URL_PROTOCOL, '') : ''; |
| 37 } |
| 38 |
| 39 // NOTE(deboer): This is sub-optimal, see dartbug.com/14281 |
| 40 _unescape(s) => Uri.decodeFull(s); |
| 41 _escape(s) => |
| 42 Uri.encodeFull(s) |
| 43 .replaceAll('=', '%3D') |
| 44 .replaceAll(';', '%3B'); |
| 45 |
| 46 |
| 47 _updateLastCookies() { |
| 48 var cookieLength, cookieArray, cookie, i, index; |
| 49 |
| 50 if (_document.cookie != lastCookieString) { |
| 51 lastCookieString = _document.cookie; |
| 52 cookieArray = lastCookieString.split("; "); |
| 53 lastCookies = {}; |
| 54 |
| 55 for (i = 0; i < cookieArray.length; i++) { |
| 56 cookie = cookieArray[i]; |
| 57 index = cookie.indexOf('='); |
| 58 if (index > 0) { //ignore nameless cookies |
| 59 var name = _unescape(cookie.substring(0, index)); |
| 60 // the first value that is seen for a cookie is the most |
| 61 // specific one. values for the same cookie name that |
| 62 // follow are for less specific paths. |
| 63 if (!lastCookies.containsKey(name)) { |
| 64 lastCookies[name] = _unescape(cookie.substring(index + 1)); |
| 65 } |
| 66 } |
| 67 } |
| 68 } |
| 69 return lastCookies; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Returns a cookie. |
| 74 */ |
| 75 operator[](key) => _updateLastCookies()[key]; |
| 76 |
| 77 /** |
| 78 * Sets a cookie. Setting a cookie to [null] deletes the cookie. |
| 79 */ |
| 80 operator[]=(name, value) { |
| 81 var cookieLength, cookieArray, cookie, i, index; |
| 82 |
| 83 if (identical(value, null)) { |
| 84 _document.cookie = "${_escape(name)}=;path=$cookiePath;expires=Thu, 01 Jan
1970 00:00:00 GMT"; |
| 85 } else { |
| 86 if (value is String) { |
| 87 cookieLength = (_document.cookie = "${_escape(name)}=${_escape(value)};p
ath=$cookiePath").length + 1; |
| 88 |
| 89 // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum
: |
| 90 // - 300 cookies |
| 91 // - 20 cookies per unique domain |
| 92 // - 4096 bytes per cookie |
| 93 if (cookieLength > 4096) { |
| 94 print("Cookie '$name' possibly not set or overflowed because it was "
+ |
| 95 "too large ($cookieLength > 4096 bytes)!"); |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 } |
| 101 |
| 102 get all => _updateLastCookies(); |
| 103 } |
| 104 |
| 105 /** |
| 106 * Cookies service |
| 107 */ |
| 108 class Cookies { |
| 109 |
| 110 BrowserCookies _browserCookies; |
| 111 |
| 112 Cookies(BrowserCookies this._browserCookies); |
| 113 |
| 114 /** |
| 115 * Returns the value of given cookie key |
| 116 */ |
| 117 operator[](name) => this._browserCookies[name]; |
| 118 |
| 119 /** |
| 120 * Sets a value for given cookie key |
| 121 */ |
| 122 operator[]=(name, value) { |
| 123 this._browserCookies[name] = value; |
| 124 } |
| 125 |
| 126 /** |
| 127 * Remove given cookie |
| 128 */ |
| 129 remove(name) { |
| 130 this._browserCookies[name] = null; |
| 131 } |
| 132 } |
| 133 |
| OLD | NEW |