OLD | NEW |
1 part of html_common; | 1 part of html_common; |
2 | 2 |
3 convertDartToNative_PrepareForStructuredClone(value) => | 3 convertDartToNative_PrepareForStructuredClone(value) => |
4 new _StructuredCloneDartium().convertDartToNative_PrepareForStructuredClone(
value); | 4 new _StructuredCloneDartium().convertDartToNative_PrepareForStructuredClone(
value); |
5 | 5 |
6 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) => | 6 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) => |
7 new _AcceptStructuredCloneDartium().convertNativeToDart_AcceptStructuredClon
e(object, mustCopy: mustCopy); | 7 new _AcceptStructuredCloneDartium().convertNativeToDart_AcceptStructuredClon
e(object, mustCopy: mustCopy); |
8 | 8 |
9 class _StructuredCloneDartium extends _StructuredClone { | 9 class _StructuredCloneDartium extends _StructuredClone { |
10 newJsMap() => new js.JsObject(js.context["Object"]); | 10 newJsMap() => new js.JsObject(js.context["Object"]); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 return jsObject; | 119 return jsObject; |
120 } | 120 } |
121 | 121 |
122 var wrapper = js.getDartHtmlWrapperFor(jsObject); | 122 var wrapper = js.getDartHtmlWrapperFor(jsObject); |
123 // if we have a wrapper return the Dart instance. | 123 // if we have a wrapper return the Dart instance. |
124 if (wrapper != null) { | 124 if (wrapper != null) { |
125 return wrapper; | 125 return wrapper; |
126 } | 126 } |
127 | 127 |
128 if (jsObject is js.JsArray) { | 128 if (jsObject is js.JsArray) { |
129 var wrappingList = new DartHtmlWrappingList(jsObject); | 129 wrapper = new js.JSArray.create(jsObject); |
130 js.setDartHtmlWrapperFor(jsObject, wrappingList); | 130 js.setDartHtmlWrapperFor(jsObject, wrapper); |
131 return wrappingList; | 131 return wrapper; |
| 132 } |
| 133 if (jsObject is js.JsFunction) { |
| 134 wrapper = new js.JSFunction.create(jsObject); |
| 135 js.setDartHtmlWrapperFor(jsObject, wrapper); |
| 136 return wrapper; |
132 } | 137 } |
133 | 138 |
134 // Try the most general type conversions on it. | 139 // Try the most general type conversions on it. |
135 // TODO(alanknight): We may be able to do better. This maintains identity, | 140 // TODO(alanknight): We may be able to do better. This maintains identity, |
136 // which is useful, but expensive. And if we nest something that only | 141 // which is useful, but expensive. And if we nest something that only |
137 // this conversion handles, how does that work? e.g. a list of maps of eleme
nts. | 142 // this conversion handles, how does that work? e.g. a list of maps of eleme
nts. |
138 var converted = convertNativeToDart_SerializedScriptValue(jsObject); | 143 var converted = convertNativeToDart_SerializedScriptValue(jsObject); |
139 if (!identical(converted, jsObject)) { | 144 if (!identical(converted, jsObject)) { |
140 return converted; | 145 return converted; |
141 } | 146 } |
142 | 147 |
143 var constructor = js.JsNative.getProperty(jsObject, 'constructor'); | 148 var constructor = js.JsNative.getProperty(jsObject, 'constructor'); |
144 if (constructor == null) { | 149 if (constructor == null) { |
145 // Perfectly valid case for JavaScript objects where __proto__ has | 150 // Perfectly valid case for JavaScript objects where __proto__ has |
146 // intentionally been set to null. | 151 // intentionally been set to null. |
147 js.setDartHtmlWrapperFor(jsObject, jsObject); | 152 js.setDartHtmlWrapperFor(jsObject, new js.JSObject.create(jsObject)); |
148 return jsObject; | 153 return jsObject; |
149 } | 154 } |
150 var jsTypeName = js.JsNative.getProperty(constructor, 'name'); | 155 var jsTypeName = js.JsNative.getProperty(constructor, 'name'); |
151 if (jsTypeName is! String || jsTypeName.length == 0) { | 156 if (jsTypeName is! String || jsTypeName.length == 0) { |
152 // Not an html type. | 157 // Not an html type. |
153 js.setDartHtmlWrapperFor(jsObject, jsObject); | 158 wrapper = new js.JSObject.create(jsObject); |
154 return jsObject; | 159 js.setDartHtmlWrapperFor(jsObject, wrapper); |
| 160 return wrapper; |
155 } | 161 } |
156 | 162 |
157 var dartClass_instance; | 163 var dartClass_instance; |
158 var customElementClass = null; | 164 var customElementClass = null; |
159 var extendsTag = ""; | 165 var extendsTag = ""; |
160 var custom = getCustomElementEntry(jsObject); | 166 var custom = getCustomElementEntry(jsObject); |
161 if (custom != null) { | 167 if (custom != null) { |
162 customElementClass = custom['type']; | 168 customElementClass = custom['type']; |
163 extendsTag = custom['extends']; | 169 extendsTag = custom['extends']; |
164 } | 170 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 if (func != null) { | 203 if (func != null) { |
198 dartClass_instance = func(); | 204 dartClass_instance = func(); |
199 | 205 |
200 // Wrap our Dart instance in both directions. | 206 // Wrap our Dart instance in both directions. |
201 dartClass_instance.blink_jsObject = jsObject; | 207 dartClass_instance.blink_jsObject = jsObject; |
202 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); | 208 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); |
203 } | 209 } |
204 | 210 |
205 // TODO(jacobr): cache that this is not a dart:html JS class. | 211 // TODO(jacobr): cache that this is not a dart:html JS class. |
206 return dartClass_instance; | 212 return dartClass_instance; |
207 } catch(e, stacktrace){ | 213 } catch (e, stacktrace) { |
208 if (interop_checks) { | 214 if (interop_checks) { |
209 if (e is DebugAssertException) | 215 if (e is DebugAssertException) window.console |
210 window.console.log("${e.message}\n ${stacktrace}"); | 216 .log("${e.message}\n ${stacktrace}"); |
211 else | 217 else window.console.log("${stacktrace}"); |
212 window.console.log("${stacktrace}"); | |
213 } | 218 } |
214 } | 219 } |
215 | 220 |
216 return null; | 221 return null; |
217 } | 222 } |
218 | 223 |
219 /** | 224 /** |
220 * Create Dart class that maps to the JS Type, add the JsObject as an expando | 225 * Create Dart class that maps to the JS Type, add the JsObject as an expando |
221 * on the Dart class and return the created Dart class. | 226 * on the Dart class and return the created Dart class. |
222 */ | 227 */ |
223 wrap_jso_no_SerializedScriptvalue(jsObject) { | 228 wrap_jso_no_SerializedScriptvalue(jsObject) { |
224 try { | 229 try { |
225 if (jsObject is! js.JsObject || jsObject == null) { | 230 if (jsObject is! js.JsObject || jsObject == null) { |
226 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. | 231 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. |
227 // or it's a simple type. | 232 // or it's a simple type. |
228 return jsObject; | 233 return jsObject; |
229 } | 234 } |
230 | 235 |
231 // TODO(alanknight): With upgraded custom elements this causes a failure bec
ause | 236 // TODO(alanknight): With upgraded custom elements this causes a failure bec
ause |
232 // we need a new wrapper after the type changes. We could possibly invalidat
e this | 237 // we need a new wrapper after the type changes. We could possibly invalidat
e this |
233 // if the constructor name didn't match? | 238 // if the constructor name didn't match? |
234 var wrapper = js.getDartHtmlWrapperFor(jsObject); | 239 var wrapper = js.getDartHtmlWrapperFor(jsObject); |
235 if (wrapper != null) { | 240 if (wrapper != null) { |
236 return wrapper; | 241 return wrapper; |
237 } | 242 } |
238 | 243 |
239 // TODO(jacobr): auomatically wrapping JsArray here is fundamentally broken | |
240 // as it hijacks adding custom methods on JS Array classes as part of the | |
241 // new typed DartJsInterop. | |
242 // To make this work we really need to make DartHtmlWrappingList extend | |
243 // JsArrayImpl. Fixing this issue needs to be part of a broader refactor | |
244 // that allows calling custom typed JS interop methods on all dart:html | |
245 // classes. | |
246 if (jsObject is js.JsArray) { | 244 if (jsObject is js.JsArray) { |
247 var wrappingList = new DartHtmlWrappingList(jsObject); | 245 wrapper = new js.JSArray.create(jsObject); |
248 js.setDartHtmlWrapperFor(jsObject, wrappingList); | 246 js.setDartHtmlWrapperFor(jsObject, wrapper); |
249 return wrappingList; | 247 return wrapper; |
| 248 } |
| 249 if (jsObject is js.JsFunction) { |
| 250 wrapper = new js.JSFunction.create(jsObject); |
| 251 js.setDartHtmlWrapperFor(jsObject, wrapper); |
| 252 return wrapper; |
250 } | 253 } |
251 | 254 |
252 var constructor = js.JsNative.getProperty(jsObject, 'constructor'); | 255 var constructor = js.JsNative.getProperty(jsObject, 'constructor'); |
253 if (constructor == null) { | 256 if (constructor == null) { |
254 // Perfectly valid case for JavaScript objects where __proto__ has | 257 // Perfectly valid case for JavaScript objects where __proto__ has |
255 // intentionally been set to null. | 258 // intentionally been set to null. |
256 js.setDartHtmlWrapperFor(jsObject, jsObject); | 259 js.setDartHtmlWrapperFor(jsObject, new js.JSObject.create(jsObject)); |
257 return jsObject; | 260 return jsObject; |
258 } | 261 } |
259 var jsTypeName = js.JsNative.getProperty(constructor, 'name'); | 262 var jsTypeName = js.JsNative.getProperty(constructor, 'name'); |
260 if (jsTypeName is! String || jsTypeName.length == 0) { | 263 if (jsTypeName is! String || jsTypeName.length == 0) { |
261 // Not an html type. | 264 // Not an html type. |
262 js.setDartHtmlWrapperFor(jsObject, jsObject); | 265 wrapper = new js.JSObject.create(jsObject); |
263 return jsObject; | 266 js.setDartHtmlWrapperFor(jsObject, wrapper); |
| 267 return wrapper; |
264 } | 268 } |
265 | 269 |
266 var func = getHtmlCreateFunction(jsTypeName); | 270 var func = getHtmlCreateFunction(jsTypeName); |
267 if (func != null) { | 271 if (func != null) { |
268 var dartClass_instance = func(); | 272 var dartClass_instance = func(); |
269 dartClass_instance.blink_jsObject = jsObject; | 273 dartClass_instance.blink_jsObject = jsObject; |
270 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); | 274 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); |
271 return dartClass_instance; | 275 return dartClass_instance; |
272 } | 276 } |
273 return jsObject; | 277 wrapper = new js.JSObject.create(jsObject); |
274 } catch(e, stacktrace){ | 278 js.setDartHtmlWrapperFor(jsObject, wrapper); |
| 279 return wrapper; |
| 280 } catch (e, stacktrace) { |
275 if (interop_checks) { | 281 if (interop_checks) { |
276 if (e is DebugAssertException) | 282 if (e is DebugAssertException) window.console |
277 window.console.log("${e.message}\n ${stacktrace}"); | 283 .log("${e.message}\n ${stacktrace}"); |
278 else | 284 else window.console.log("${stacktrace}"); |
279 window.console.log("${stacktrace}"); | |
280 } | 285 } |
281 } | 286 } |
282 | 287 |
283 return null; | 288 return null; |
284 } | 289 } |
285 | 290 |
286 /** | 291 /** |
287 * Create Dart class that maps to the JS Type that is the JS type being | 292 * Create Dart class that maps to the JS Type that is the JS type being |
288 * extended using JS interop createCallback (we need the base type of the | 293 * extended using JS interop createCallback (we need the base type of the |
289 * custom element) not the Dart created constructor. | 294 * custom element) not the Dart created constructor. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 var hasAttribute = false; | 331 var hasAttribute = false; |
327 | 332 |
328 var jsObject; | 333 var jsObject; |
329 var tag = ""; | 334 var tag = ""; |
330 var runtimeType = element.runtimeType; | 335 var runtimeType = element.runtimeType; |
331 if (runtimeType == HtmlElement) { | 336 if (runtimeType == HtmlElement) { |
332 tag = element.localName; | 337 tag = element.localName; |
333 } else if (runtimeType == TemplateElement) { | 338 } else if (runtimeType == TemplateElement) { |
334 // Data binding with a Dart class. | 339 // Data binding with a Dart class. |
335 tag = element.attributes['is']; | 340 tag = element.attributes['is']; |
336 } else if (runtimeType == js.JsObjectImpl) { | 341 } else if (runtimeType == js.JsObject) { |
337 // It's a Polymer core element (written in JS). | 342 // It's a Polymer core element (written in JS). |
338 // Make sure it's an element anything else we can ignore. | 343 // Make sure it's an element anything else we can ignore. |
339 if (element.hasProperty('nodeType') && element['nodeType'] == 1) { | 344 if (element.hasProperty('nodeType') && element['nodeType'] == 1) { |
340 if (js.JsNative.callMethod(element, 'hasAttribute', ['is'])) { | 345 if (js.JsNative.callMethod(element, 'hasAttribute', ['is'])) { |
341 hasAttribute = true; | 346 hasAttribute = true; |
342 // It's data binding use the is attribute. | 347 // It's data binding use the is attribute. |
343 tag = js.JsNative.callMethod(element, 'getAttribute', ['is']); | 348 tag = js.JsNative.callMethod(element, 'getAttribute', ['is']); |
344 } else { | 349 } else { |
345 // It's a custom element we want the local name. | 350 // It's a custom element we want the local name. |
346 tag = element['localName']; | 351 tag = element['localName']; |
347 } | 352 } |
348 } | 353 } |
349 } else { | 354 } else { |
350 throw new UnsupportedError('Element is incorrect type. Got ${runtimeType}, e
xpected HtmlElement/HtmlTemplate/JsObjectImpl.'); | 355 throw new UnsupportedError( |
| 356 'Element is incorrect type. Got ${runtimeType}, expected HtmlElement/Htm
lTemplate/JsObject.'); |
351 } | 357 } |
352 | 358 |
353 var entry = _knownCustomElements[tag]; | 359 var entry = _knownCustomElements[tag]; |
354 if (entry != null) { | 360 if (entry != null) { |
355 // If there's an 'is' attribute then check if the extends tag registered | 361 // If there's an 'is' attribute then check if the extends tag registered |
356 // matches the tag if so then return the entry that's registered for this | 362 // matches the tag if so then return the entry that's registered for this |
357 // extendsTag or if there's no 'is' tag then return the entry found. | 363 // extendsTag or if there's no 'is' tag then return the entry found. |
358 if ((hasAttribute && entry['extends'] == tag) || !hasAttribute) { | 364 if ((hasAttribute && entry['extends'] == tag) || !hasAttribute) { |
359 return entry; | 365 return entry; |
360 } | 366 } |
(...skipping 10 matching lines...) Expand all Loading... |
371 {'type': dartClass, 'extends': extendTag != null ? extendTag : "" }; | 377 {'type': dartClass, 'extends': extendTag != null ? extendTag : "" }; |
372 } | 378 } |
373 | 379 |
374 Type getCustomElementType(object) { | 380 Type getCustomElementType(object) { |
375 var entry = getCustomElementEntry(object); | 381 var entry = getCustomElementEntry(object); |
376 if (entry != null) { | 382 if (entry != null) { |
377 return entry['type']; | 383 return entry['type']; |
378 } | 384 } |
379 return null; | 385 return null; |
380 } | 386 } |
381 | |
382 /** | |
383 * Wraps a JsArray and will call wrap_jso on its entries. | |
384 */ | |
385 class DartHtmlWrappingList extends ListBase implements NativeFieldWrapperClass2
{ | |
386 DartHtmlWrappingList(this.blink_jsObject); | |
387 | |
388 final js.JsArray blink_jsObject; | |
389 | |
390 operator [](int index) => wrap_jso_no_SerializedScriptvalue(js.JsNative.getArr
ayIndex(blink_jsObject, index)); | |
391 | |
392 operator []=(int index, value) => blink_jsObject[index] = value; | |
393 | |
394 int get length => blink_jsObject.length; | |
395 int set length(int newLength) => blink_jsObject.length = newLength; | |
396 } | |
OLD | NEW |