12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import {attachOriginToPatched, isBrowser, isMix, ObjectGetOwnPropertyDescriptor, wrapWithCurrentZone} from '../common/utils';
  9. import {_redefineProperty} from './define-property';
  10. export function registerElementPatch(_global: any) {
  11. if ((!isBrowser && !isMix) || !('registerElement' in (<any>_global).document)) {
  12. return;
  13. }
  14. const _registerElement = (<any>document).registerElement;
  15. const callbacks =
  16. ['createdCallback', 'attachedCallback', 'detachedCallback', 'attributeChangedCallback'];
  17. (<any>document).registerElement = function(name: any, opts: any) {
  18. if (opts && opts.prototype) {
  19. callbacks.forEach(function(callback) {
  20. const source = 'Document.registerElement::' + callback;
  21. const prototype = opts.prototype;
  22. if (prototype.hasOwnProperty(callback)) {
  23. const descriptor = ObjectGetOwnPropertyDescriptor(prototype, callback);
  24. if (descriptor && descriptor.value) {
  25. descriptor.value = wrapWithCurrentZone(descriptor.value, source);
  26. _redefineProperty(opts.prototype, callback, descriptor);
  27. } else {
  28. prototype[callback] = wrapWithCurrentZone(prototype[callback], source);
  29. }
  30. } else if (prototype[callback]) {
  31. prototype[callback] = wrapWithCurrentZone(prototype[callback], source);
  32. }
  33. });
  34. }
  35. return _registerElement.call(document, name, opts);
  36. };
  37. attachOriginToPatched((<any>document).registerElement, _registerElement);
  38. }