| 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 // DO NOT EDIT | 5 // DO NOT EDIT |
| 6 // Auto-generated dart:html library. | 6 // Auto-generated dart:html library. |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * HTML elements and other resources for web-based applications that need to | 9 * HTML elements and other resources for web-based applications that need to |
| 10 * interact with the browser and the DOM (Document Object Model). | 10 * interact with the browser and the DOM (Document Object Model). |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 return null; | 345 return null; |
| 346 } | 346 } |
| 347 | 347 |
| 348 | 348 |
| 349 /****************************************************************************** | 349 /****************************************************************************** |
| 350 ********** ********** | 350 ********** ********** |
| 351 ********** JS Interop Support ********** | 351 ********** JS Interop Support ********** |
| 352 ********** ********** | 352 ********** ********** |
| 353 ******************************************************************************/ | 353 ******************************************************************************/ |
| 354 | 354 |
| 355 // List of known tagName to DartClass for custom elements, used for upgrade. | |
| 356 var _knownCustomElements = new Map<String, Map<Type, String>>(); | |
| 357 | |
| 358 void _addCustomElementType(String tagName, Type dartClass, [String extendTag]) { | |
| 359 _knownCustomElements[tagName] = | |
| 360 {'type': dartClass, 'extends': extendTag != null ? extendTag : "" }; | |
| 361 } | |
| 362 | |
| 363 Type _getCustomElementType(object) { | |
| 364 var entry = _getCustomElementEntry(object); | |
| 365 if (entry != null) { | |
| 366 return entry['type']; | |
| 367 } | |
| 368 return null; | |
| 369 } | |
| 370 | |
| 371 String _getCustomElementExtends(object) { | 355 String _getCustomElementExtends(object) { |
| 372 var entry = _getCustomElementEntry(object); | 356 var entry = getCustomElementEntry(object); |
| 373 if (entry != null) { | 357 if (entry != null) { |
| 374 return entry['extends']; | 358 return entry['extends']; |
| 375 } | 359 } |
| 376 return null; | 360 return null; |
| 377 } | 361 } |
| 378 | 362 |
| 379 _getCustomElementEntry(element) { | |
| 380 var hasAttribute = false; | |
| 381 | |
| 382 var jsObject; | |
| 383 var tag = ""; | |
| 384 var runtimeType = element.runtimeType; | |
| 385 if (runtimeType == HtmlElement) { | |
| 386 tag = element.localName; | |
| 387 } else if (runtimeType == TemplateElement) { | |
| 388 // Data binding with a Dart class. | |
| 389 tag = element.attributes['is']; | |
| 390 } else if (runtimeType == js.JsObjectImpl) { | |
| 391 // It's a Polymer core element (written in JS). | |
| 392 // Make sure it's an element anything else we can ignore. | |
| 393 if (element.hasProperty('nodeType') && element['nodeType'] == 1) { | |
| 394 if (js.JsNative.callMethod(element, 'hasAttribute', ['is'])) { | |
| 395 hasAttribute = true; | |
| 396 // It's data binding use the is attribute. | |
| 397 tag = js.JsNative.callMethod(element, 'getAttribute', ['is']); | |
| 398 } else { | |
| 399 // It's a custom element we want the local name. | |
| 400 tag = element['localName']; | |
| 401 } | |
| 402 } | |
| 403 } else { | |
| 404 throw new UnsupportedError('Element is incorrect type. Got ${runtimeType}, e
xpected HtmlElement/HtmlTemplate/JsObjectImpl.'); | |
| 405 } | |
| 406 | |
| 407 var entry = _knownCustomElements[tag]; | |
| 408 if (entry != null) { | |
| 409 // If there's an 'is' attribute then check if the extends tag registered | |
| 410 // matches the tag if so then return the entry that's registered for this | |
| 411 // extendsTag or if there's no 'is' tag then return the entry found. | |
| 412 if ((hasAttribute && entry['extends'] == tag) || !hasAttribute) { | |
| 413 return entry; | |
| 414 } | |
| 415 } | |
| 416 | |
| 417 return null; | |
| 418 } | |
| 419 | |
| 420 // Return the tag name or is attribute of the custom element or data binding. | 363 // Return the tag name or is attribute of the custom element or data binding. |
| 421 String _getCustomElementName(element) { | 364 String _getCustomElementName(element) { |
| 422 var jsObject; | 365 var jsObject; |
| 423 var tag = ""; | 366 var tag = ""; |
| 424 var runtimeType = element.runtimeType; | 367 var runtimeType = element.runtimeType; |
| 425 if (runtimeType == HtmlElement) { | 368 if (runtimeType == HtmlElement) { |
| 426 tag = element.localName; | 369 tag = element.localName; |
| 427 } else if (runtimeType == TemplateElement) { | 370 } else if (runtimeType == TemplateElement) { |
| 428 // Data binding with a Dart class. | 371 // Data binding with a Dart class. |
| 429 tag = element.attributes['is']; | 372 tag = element.attributes['is']; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 448 | 391 |
| 449 /// An abstract class for all DOM objects we wrap in dart:html and related | 392 /// An abstract class for all DOM objects we wrap in dart:html and related |
| 450 /// libraries. | 393 /// libraries. |
| 451 class DartHtmlDomObject { | 394 class DartHtmlDomObject { |
| 452 | 395 |
| 453 /// The underlying JS DOM object. | 396 /// The underlying JS DOM object. |
| 454 js.JsObject blink_jsObject; | 397 js.JsObject blink_jsObject; |
| 455 | 398 |
| 456 } | 399 } |
| 457 | 400 |
| 458 // Flag to disable JS interop asserts. Setting to false will speed up the | 401 /// Upgrade a Dart HtmlElement to the user's Dart custom element class. |
| 459 // wrap_jso calls. | |
| 460 bool __interop_checks = false; | |
| 461 | |
| 462 /** Expando for JsObject, used by every Dart class associated with a Javascript | |
| 463 * class (e.g., DOM, WebAudio, etc.). | |
| 464 */ | |
| 465 | |
| 466 /** | |
| 467 * Return the JsObject associated with a Dart class [dartClass_instance]. | |
| 468 */ | |
| 469 unwrap_jso(dartClass_instance) => js.unwrap_jso(dartClass_instance); | |
| 470 | |
| 471 /** | |
| 472 * Create Dart class that maps to the JS Type, add the JsObject as an expando | |
| 473 * on the Dart class and return the created Dart class. | |
| 474 */ | |
| 475 wrap_jso(jsObject) { | |
| 476 try { | |
| 477 if (jsObject is! js.JsObject || jsObject == null) { | |
| 478 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. | |
| 479 // or it's a simple type. | |
| 480 return jsObject; | |
| 481 } | |
| 482 | |
| 483 var wrapper = js.getDartHtmlWrapperFor(jsObject); | |
| 484 // if we have a wrapper return the Dart instance. | |
| 485 if (wrapper != null) { | |
| 486 var customElementClass = _getCustomElementType(wrapper.blink_jsObject); | |
| 487 if (wrapper.runtimeType != customElementClass && customElementClass != nul
l) { | |
| 488 if (wrapper.runtimeType == HtmlElement && !wrapper._isBadUpgrade) { | |
| 489 // We're a Dart instance if it's HtmlElement and we have a customEleme
nt | |
| 490 // class then we need to upgrade. | |
| 491 if (customElementClass != null) { | |
| 492 var dartClass_instance; | |
| 493 try { | |
| 494 dartClass_instance = _blink.Blink_Utils.constructElement(customEle
mentClass, jsObject); | |
| 495 } finally { | |
| 496 dartClass_instance.blink_jsObject = jsObject; | |
| 497 return dartClass_instance; | |
| 498 } | |
| 499 } | |
| 500 } | |
| 501 } | |
| 502 | |
| 503 return wrapper; | |
| 504 } | |
| 505 | |
| 506 if (jsObject is js.JsArray) { | |
| 507 var wrappingList = new _DartHtmlWrappingList(jsObject); | |
| 508 js.setDartHtmlWrapperFor(jsObject, wrappingList); | |
| 509 return wrappingList; | |
| 510 } | |
| 511 | |
| 512 // Try the most general type conversions on it. | |
| 513 // TODO(alanknight): We may be able to do better. This maintains identity, | |
| 514 // which is useful, but expensive. And if we nest something that only | |
| 515 // this conversion handles, how does that work? e.g. a list of maps of eleme
nts. | |
| 516 var converted = convertNativeToDart_SerializedScriptValue(jsObject); | |
| 517 if (!identical(converted, jsObject)) { | |
| 518 return converted; | |
| 519 } | |
| 520 | |
| 521 var constructor = js.JsNative.getProperty(jsObject, 'constructor'); | |
| 522 if (constructor == null) { | |
| 523 // Perfectly valid case for JavaScript objects where __proto__ has | |
| 524 // intentionally been set to null. | |
| 525 js.setDartHtmlWrapperFor(jsObject, jsObject); | |
| 526 return jsObject; | |
| 527 } | |
| 528 var jsTypeName = js.JsNative.getProperty(constructor, 'name'); | |
| 529 if (jsTypeName is! String || jsTypeName.length == 0) { | |
| 530 // Not an html type. | |
| 531 js.setDartHtmlWrapperFor(jsObject, jsObject); | |
| 532 return jsObject; | |
| 533 } | |
| 534 | |
| 535 var dartClass_instance; | |
| 536 var customElementClass = null; | |
| 537 var extendsTag = ""; | |
| 538 var custom = _getCustomElementEntry(jsObject); | |
| 539 if (custom != null) { | |
| 540 customElementClass = custom['type']; | |
| 541 extendsTag = custom['extends']; | |
| 542 } | |
| 543 | |
| 544 // Custom Element to upgrade. | |
| 545 // Only allow custome elements to be created in the html or svg default | |
| 546 // namespace. | |
| 547 var defaultNS = jsObject['namespaceURI'] == 'http://www.w3.org/1999/xhtml' |
| | |
| 548 jsObject['namespaceURI'] == 'http://www.w3.org/2000/svg'; | |
| 549 if (customElementClass != null && extendsTag == "" && defaultNS) { | |
| 550 try { | |
| 551 dartClass_instance = _blink.Blink_Utils.constructElement(customElementCl
ass, jsObject); | |
| 552 } finally { | |
| 553 dartClass_instance.blink_jsObject = jsObject; | |
| 554 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); | |
| 555 } | |
| 556 } else { | |
| 557 var func = getHtmlCreateFunction(jsTypeName); | |
| 558 if (func == null) { | |
| 559 if (jsTypeName == 'auto-binding') { | |
| 560 func = getHtmlCreateFunction("HTMLTemplateElement"); | |
| 561 } else if (jsObject.toString() == "[object HTMLElement]") { | |
| 562 // One last ditch effort could be a JS custom element. | |
| 563 func = getHtmlCreateFunction("HTMLElement"); | |
| 564 } | |
| 565 } | |
| 566 if (func != null) { | |
| 567 dartClass_instance = func(); | |
| 568 dartClass_instance.blink_jsObject = jsObject; | |
| 569 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); | |
| 570 } | |
| 571 } | |
| 572 | |
| 573 // TODO(jacobr): cache that this is not a dart:html JS class. | |
| 574 return dartClass_instance; | |
| 575 } catch(e, stacktrace){ | |
| 576 if (__interop_checks) { | |
| 577 if (e is DebugAssertException) | |
| 578 window.console.log("${e.message}\n ${stacktrace}"); | |
| 579 else | |
| 580 window.console.log("${stacktrace}"); | |
| 581 } | |
| 582 } | |
| 583 | |
| 584 return null; | |
| 585 } | |
| 586 | |
| 587 /** | |
| 588 * Create Dart class that maps to the JS Type, add the JsObject as an expando | |
| 589 * on the Dart class and return the created Dart class. | |
| 590 */ | |
| 591 wrap_jso_no_SerializedScriptvalue(jsObject) { | |
| 592 try { | |
| 593 if (jsObject is! js.JsObject || jsObject == null) { | |
| 594 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. | |
| 595 // or it's a simple type. | |
| 596 return jsObject; | |
| 597 } | |
| 598 | |
| 599 // TODO(alanknight): With upgraded custom elements this causes a failure bec
ause | |
| 600 // we need a new wrapper after the type changes. We could possibly invalidat
e this | |
| 601 // if the constructor name didn't match? | |
| 602 var wrapper = js.getDartHtmlWrapperFor(jsObject); | |
| 603 if (wrapper != null) { | |
| 604 return wrapper; | |
| 605 } | |
| 606 | |
| 607 if (jsObject is js.JsArray) { | |
| 608 var wrappingList = new _DartHtmlWrappingList(jsObject); | |
| 609 js.setDartHtmlWrapperFor(jsObject, wrappingList); | |
| 610 return wrappingList; | |
| 611 } | |
| 612 | |
| 613 var constructor = js.JsNative.getProperty(jsObject, 'constructor'); | |
| 614 if (constructor == null) { | |
| 615 // Perfectly valid case for JavaScript objects where __proto__ has | |
| 616 // intentionally been set to null. | |
| 617 js.setDartHtmlWrapperFor(jsObject, jsObject); | |
| 618 return jsObject; | |
| 619 } | |
| 620 var jsTypeName = js.JsNative.getProperty(constructor, 'name'); | |
| 621 if (jsTypeName is! String || jsTypeName.length == 0) { | |
| 622 // Not an html type. | |
| 623 js.setDartHtmlWrapperFor(jsObject, jsObject); | |
| 624 return jsObject; | |
| 625 } | |
| 626 | |
| 627 var func = getHtmlCreateFunction(jsTypeName); | |
| 628 if (func != null) { | |
| 629 var dartClass_instance = func(); | |
| 630 dartClass_instance.blink_jsObject = jsObject; | |
| 631 js.setDartHtmlWrapperFor(jsObject, dartClass_instance); | |
| 632 return dartClass_instance; | |
| 633 } | |
| 634 return jsObject; | |
| 635 } catch(e, stacktrace){ | |
| 636 if (__interop_checks) { | |
| 637 if (e is DebugAssertException) | |
| 638 window.console.log("${e.message}\n ${stacktrace}"); | |
| 639 else | |
| 640 window.console.log("${stacktrace}"); | |
| 641 } | |
| 642 } | |
| 643 | |
| 644 return null; | |
| 645 } | |
| 646 | |
| 647 /** | |
| 648 * Create Dart class that maps to the JS Type that is the JS type being | |
| 649 * extended using JS interop createCallback (we need the base type of the | |
| 650 * custom element) not the Dart created constructor. | |
| 651 */ | |
| 652 wrap_jso_custom_element(jsObject) { | |
| 653 try { | |
| 654 if (jsObject is! js.JsObject) { | |
| 655 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. | |
| 656 return jsObject; | |
| 657 } | |
| 658 | |
| 659 // Find out what object we're extending. | |
| 660 var objectName = jsObject.toString(); | |
| 661 // Expect to see something like '[object HTMLElement]'. | |
| 662 if (!objectName.startsWith('[object ')) { | |
| 663 return jsObject; | |
| 664 } | |
| 665 | |
| 666 var extendsClass = objectName.substring(8, objectName.length - 1); | |
| 667 var func = getHtmlCreateFunction(extendsClass); | |
| 668 if (__interop_checks) | |
| 669 debug_or_assert("func != null name = ${extendsClass}", func != null); | |
| 670 var dartClass_instance = func(); | |
| 671 dartClass_instance.blink_jsObject = jsObject; | |
| 672 return dartClass_instance; | |
| 673 } catch(e, stacktrace){ | |
| 674 if (__interop_checks) { | |
| 675 if (e is DebugAssertException) | |
| 676 window.console.log("${e.message}\n ${stacktrace}"); | |
| 677 else | |
| 678 window.console.log("${stacktrace}"); | |
| 679 } | |
| 680 | |
| 681 // Problem? | |
| 682 return null; | |
| 683 } | |
| 684 } | |
| 685 | |
| 686 // Upgrade a Dart HtmlElement to the user's Dart custom element class. | |
| 687 _upgradeHtmlElement(dartInstance) { | 402 _upgradeHtmlElement(dartInstance) { |
| 688 // Only try upgrading HtmlElement (Dart class) if there is a failure then | 403 // Only try upgrading HtmlElement (Dart class) if there is a failure then |
| 689 // don't try it again - one failure is enough. | 404 // don't try it again - one failure is enough. |
| 690 if (dartInstance.runtimeType == HtmlElement && !dartInstance._isBadUpgrade) { | 405 if (dartInstance.runtimeType == HtmlElement && !dartInstance.isBadUpgrade) { |
| 691 // Must be exactly HtmlElement not something derived from it. | 406 // Must be exactly HtmlElement not something derived from it. |
| 692 | 407 |
| 693 var customElementClass = _getCustomElementType(dartInstance); | 408 var customElementClass = getCustomElementType(dartInstance); |
| 694 | 409 |
| 695 // Custom Element to upgrade. | 410 // Custom Element to upgrade. |
| 696 if (customElementClass != null) { | 411 if (customElementClass != null) { |
| 697 var jsObject = dartInstance.blink_jsObject; | 412 var jsObject = dartInstance.blink_jsObject; |
| 698 try { | 413 try { |
| 699 dartInstance = _blink.Blink_Utils.constructElement(customElementClass, j
sObject); | 414 dartInstance = _blink.Blink_Utils.constructElement(customElementClass, j
sObject); |
| 700 } catch (e) { | 415 } catch (e) { |
| 701 dartInstance._badUpgrade(); | 416 dartInstance._badUpgrade(); |
| 702 } finally { | 417 } finally { |
| 703 dartInstance.blink_jsObject = jsObject; | 418 dartInstance.blink_jsObject = jsObject; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 | 454 |
| 740 Map<String, dynamic> convertNativeObjectToDartMap(js.JsObject jsObject) { | 455 Map<String, dynamic> convertNativeObjectToDartMap(js.JsObject jsObject) { |
| 741 var result = new Map(); | 456 var result = new Map(); |
| 742 var keys = js.JsNative.callMethod(js.JsNative.getProperty(js.context, 'Object'
), 'keys', [jsObject]); | 457 var keys = js.JsNative.callMethod(js.JsNative.getProperty(js.context, 'Object'
), 'keys', [jsObject]); |
| 743 for (var key in keys) { | 458 for (var key in keys) { |
| 744 result[key] = wrap_jso(js.JsNative.getProperty(jsObject, key)); | 459 result[key] = wrap_jso(js.JsNative.getProperty(jsObject, key)); |
| 745 } | 460 } |
| 746 return result; | 461 return result; |
| 747 } | 462 } |
| 748 | 463 |
| 749 // Converts a flat Dart map into a JavaScript object with properties this is | |
| 750 // is the Dartium only version it uses dart:js. | |
| 751 // TODO(alanknight): This could probably be unified with the dart2js conversions | |
| 752 // code in html_common and be more general. | |
| 753 convertDartToNative_Dictionary(Map dict) { | |
| 754 if (dict == null) return null; | |
| 755 var jsObject = new js.JsObject(js.JsNative.getProperty(js.context, 'Object')); | |
| 756 dict.forEach((String key, value) { | |
| 757 if (value is List) { | |
| 758 var jsArray = new js.JsArray(); | |
| 759 value.forEach((elem) { | |
| 760 jsArray.add(elem is Map ? convertDartToNative_Dictionary(elem): elem); | |
| 761 }); | |
| 762 jsObject[key] = jsArray; | |
| 763 } else { | |
| 764 jsObject[key] = value; | |
| 765 } | |
| 766 }); | |
| 767 return jsObject; | |
| 768 } | |
| 769 | |
| 770 // Converts a Dart list into a JsArray. For the Dartium version only. | |
| 771 convertDartToNative_List(List input) => new js.JsArray()..addAll(input); | |
| 772 | |
| 773 // Conversion function place holder (currently not used in dart2js or dartium). | |
| 774 List convertDartToNative_StringArray(List<String> input) => input; | |
| 775 | |
| 776 /** | |
| 777 * Wraps a JsArray and will call wrap_jso on its entries. | |
| 778 */ | |
| 779 class _DartHtmlWrappingList extends ListBase implements NativeFieldWrapperClass2
{ | |
| 780 _DartHtmlWrappingList(this.blink_jsObject); | |
| 781 | |
| 782 final js.JsArray blink_jsObject; | |
| 783 | |
| 784 operator [](int index) => wrap_jso(js.JsNative.getArrayIndex(blink_jsObject, i
ndex)); | |
| 785 | |
| 786 operator []=(int index, value) => blink_jsObject[index] = value; | |
| 787 | |
| 788 int get length => blink_jsObject.length; | |
| 789 int set length(int newLength) => blink_jsObject.length = newLength; | |
| 790 } | |
| 791 | |
| 792 /** | 464 /** |
| 793 * Upgrade the JS HTMLElement to the Dart class. Used by Dart's Polymer. | 465 * Upgrade the JS HTMLElement to the Dart class. Used by Dart's Polymer. |
| 794 */ | 466 */ |
| 795 createCustomUpgrader(Type customElementClass, $this) { | 467 _createCustomUpgrader(Type customElementClass, $this) { |
| 796 var dartClass; | 468 var dartClass; |
| 797 try { | 469 try { |
| 798 dartClass = _blink.Blink_Utils.constructElement(customElementClass, $this); | 470 dartClass = _blink.Blink_Utils.constructElement(customElementClass, $this); |
| 799 } catch (e) { | 471 } catch (e) { |
| 800 dartClass._badUpgrade(); | 472 dartClass._badUpgrade(); |
| 801 throw e; | 473 throw e; |
| 802 } finally { | 474 } finally { |
| 803 // Need to remember the Dart class that was created for this custom so | 475 // Need to remember the Dart class that was created for this custom so |
| 804 // return it and setup the blink_jsObject to the $this that we'll be working | 476 // return it and setup the blink_jsObject to the $this that we'll be working |
| 805 // with as we talk to blink. | 477 // with as we talk to blink. |
| 806 js.setDartHtmlWrapperFor($this, dartClass); | 478 js.setDartHtmlWrapperFor($this, dartClass); |
| 807 } | 479 } |
| 808 | 480 |
| 809 return dartClass; | 481 return dartClass; |
| 810 } | 482 } |
| 811 | 483 |
| 812 $else | 484 $else |
| 813 class DartHtmlDomObject extends NativeFieldWrapperClass2 {} | 485 class DartHtmlDomObject extends NativeFieldWrapperClass2 {} |
| 814 | 486 |
| 815 unwrap_jso(dartClass_instance) => dartClass_instance; | 487 _createCustomUpgrader(Type customElementClass, $this) => $this; |
| 816 wrap_jso(jsObject) => jsObject; | |
| 817 convertDartToNative_Dictionary(Map dict) => dict; | |
| 818 List convertDartToNative_StringArray(List<String> input) => input; | |
| 819 convertDartToNative_List(List input) => input; | |
| 820 createCustomUpgrader(Type customElementClass, $this) => $this; | |
| 821 | 488 |
| 822 $endif | 489 $endif |
| OLD | NEW |