Chromium Code Reviews| Index: tools/dom/src/dart2js_CustomElementSupport.dart |
| diff --git a/tools/dom/src/dart2js_CustomElementSupport.dart b/tools/dom/src/dart2js_CustomElementSupport.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..10b0f1b7a531ade80ee1aac3455c5e47a814d89f |
| --- /dev/null |
| +++ b/tools/dom/src/dart2js_CustomElementSupport.dart |
| @@ -0,0 +1,74 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of html; |
|
blois
2013/08/09 16:06:23
should be dart.dom.html
sra1
2013/08/12 23:31:26
Done.
|
| + |
| + |
| +/* |
| + var proto = Object.create(HTMLElement.prototype, { |
| + createdCallback: { |
| + get: function() { |
| + window.console.log('here'); |
| + } |
| + } |
| + }); |
| + document.register('x-foo', { |
| + prototype: proto |
| + }); |
| + |
| + var e = document.createElement('x-foo'); |
| + |
| + */ |
| + |
| +callOnCreated(receiver) { |
|
blois
2013/08/09 16:06:23
Can these either be private or not top-level?
sra1
2013/08/12 23:31:26
Done.
|
| + return receiver.onCreated(); |
| +} |
| + |
| +makeCreatedCallbackMethod() { |
| + return JS('', |
| + '''((function(invokeCallback) { |
|
blois
2013/08/09 16:06:23
Is this outer function closure needed?
sra1
2013/08/12 23:31:26
Yes, see
https://code.google.com/p/dart/source/bro
|
| + return function() { |
| + return invokeCallback(this); |
| + }; |
| + })(#))''', |
| + convertDartClosureToJS(callOnCreated, 1)); |
| +} |
| + |
| +void registerCustomElement(context, document, String tag, Type type) { |
| + var interceptorClass = findInterceptorConstructorForType(type); |
| + if (interceptorClass == null) { |
| + throw new ArgumentError(type); |
| + } |
| + |
| + String baseClassName = findDispatchTagForInterceptorClass(interceptorClass); |
| + if (baseClassName == null) { |
| + throw new ArgumentError(type); |
| + } |
| + if (baseClassName == 'Element') baseClassName = 'HTMLElement'; |
| + |
| + var baseConstructor = JS('=Object', '#[#]', context, baseClassName); |
| + if (JS('bool', "typeof(#) != 'function'", baseConstructor)) { |
| + throw new ArgumentError(type); |
| + } |
| + |
| + var properties = JS('=Object', '{}'); |
| + |
| + var jsCreatedCallback = makeCreatedCallbackMethod(); |
| + |
| + JS('void', '#.createdCallback = #', properties, |
| + JS('=Object', '{value: #}', jsCreatedCallback)); |
| + |
| + var baseProto = JS('=Object', '#.prototype', baseConstructor); |
| + var proto = JS('=Object', 'Object.create(#, #)', baseProto, properties); |
| + |
| + var interceptor = JS('=Object', '#.prototype', interceptorClass); |
| + |
| + setNativeSubclassDispatchRecord(proto, interceptor); |
| + |
| + |
| + JS('void', '#.register(#, #)', |
| + document, tag, JS('', '{prototype: #}', proto)); |
| + |
| + print('Registered $type'); |
|
blois
2013/08/09 16:06:23
Debugging
sra1
2013/08/12 23:31:26
Done.
|
| +} |