Skip to content

Commit

Permalink
✨ Launch minimal-wrapper native CEv1 (#26360)
Browse files Browse the repository at this point in the history
* Launch minmal wrapper native CEv1

* Add comment
  • Loading branch information
jridgewell committed Jan 23, 2020
1 parent 9faa367 commit 553061d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
9 changes: 1 addition & 8 deletions build-system/global-configs/experiments-config.json
@@ -1,12 +1,5 @@
{
"experimentA": {},
"experimentB": {},
"experimentC": {
"name": "Native Custom Elements V1",
"environment": "AMP",
"command": "gulp dist --defineExperimentConstant=NATIVE_CUSTOM_ELEMENTS_V1",
"issue": "https://github.com/ampproject/amphtml/issues/17243",
"expirationDateUTC": "2020-01-01",
"defineExperimentConstant": "NATIVE_CUSTOM_ELEMENTS_V1"
}
"experimentC": {}
}
5 changes: 1 addition & 4 deletions src/friendly-iframe-embed.js
Expand Up @@ -866,10 +866,7 @@ function installPolyfillsInChildWindow(parentWin, childWin) {
installDOMTokenList(childWin);
// The anonymous class parameter allows us to detect native classes vs
// transpiled classes.
installCustomElements(
childWin,
NATIVE_CUSTOM_ELEMENTS_V1 ? class {} : undefined
);
installCustomElements(childWin, class {});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/polyfills.js
Expand Up @@ -41,7 +41,7 @@ if (self.document) {
installGetBoundingClientRect(self);
// The anonymous class parameter allows us to detect native classes vs
// transpiled classes.
installCustomElements(self, NATIVE_CUSTOM_ELEMENTS_V1 ? class {} : undefined);
installCustomElements(self, class {});
}

// TODO(#18268, erwinm): For whatever reason imports to modules that have no
Expand Down
15 changes: 8 additions & 7 deletions src/polyfills/custom-elements.js
Expand Up @@ -937,9 +937,9 @@ export function copyProperties(obj, prototype) {
* done.
*
* @param {!Window} win
* @param {!Function=} opt_ctor
* @param {!Function} ctor
*/
export function install(win, opt_ctor) {
export function install(win, ctor) {
// Don't install in no-DOM environments e.g. worker.
const shouldInstall = win.document;
const hasCE = hasCustomElements(win);
Expand All @@ -950,23 +950,24 @@ export function install(win, opt_ctor) {
let install = true;
let installWrapper = false;

if (opt_ctor && hasCE) {
if (ctor && hasCE) {
// If ctor is constructable without new, it's a function. That means it was
// compiled down, and we need to do the minimal polyfill because all you
// cannot extend HTMLElement without native classes.
try {
const {Reflect} = win;

// "Construct" ctor using ES5 idioms
const instance = /** @type {!Function} */ (Object.create(
opt_ctor.prototype
));
// I'm not sure why, but Closure will complain at the
// `Function.call.call()` below unless we cast to a Function instance
// here.
const instance = /** @type {!Function} */ (Object.create(ctor.prototype));

// This will throw an error unless we're in a transpiled environemnt.
// Native classes must be called as `new Ctor`, not `Ctor.call(instance)`.
// We use `Function.call.call` because Closure is too smart for regular
// `Ctor.call`.
Function.call.call(opt_ctor, instance);
Function.call.call(ctor, instance);

// If that didn't throw, we're transpiled.
// Let's find out if we can wrap HTMLElement and avoid a full patch.
Expand Down
5 changes: 1 addition & 4 deletions testing/describes.js
Expand Up @@ -648,10 +648,7 @@ class RealWinFixture {
} else {
// The anonymous class parameter allows us to detect native classes
// vs transpiled classes.
installCustomElements(
win,
NATIVE_CUSTOM_ELEMENTS_V1 ? class {} : undefined
);
installCustomElements(win, class {});
}

// Intercept event listeners
Expand Down
5 changes: 1 addition & 4 deletions testing/iframe.js
Expand Up @@ -245,10 +245,7 @@ export function createIframePromise(opt_runtimeOff, opt_beforeLayoutCallback) {
installRuntimeServices(iframe.contentWindow);
// The anonymous class parameter allows us to detect native classes vs
// transpiled classes.
installCustomElements(
iframe.contentWindow,
NATIVE_CUSTOM_ELEMENTS_V1 ? class {} : undefined
);
installCustomElements(iframe.contentWindow, class {});
installAmpdocServices(ampdoc);
Services.resourcesForDoc(ampdoc).ampInitComplete();
// Act like no other elements were loaded by default.
Expand Down

0 comments on commit 553061d

Please sign in to comment.