Skip to content

Commit

Permalink
Remove obsolete ampPageHasErrors and improve isAmpDocument passing
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 5, 2020
1 parent 76c1caf commit 12378ae
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 87 deletions.
53 changes: 5 additions & 48 deletions assets/src/admin/paired-browsing/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { addQueryArgs, hasQueryArg, removeQueryArgs } from '@wordpress/url';
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -79,7 +79,6 @@ class PairedBrowsingApp {
constructor() {
this.nonAmpIframe = document.querySelector( '#non-amp iframe' );
this.ampIframe = document.querySelector( '#amp iframe' );
this.ampPageHasErrors = false; // @todo This is obsolete. Remove this and invalid-amp span.

this.currentNonAmpUrl = this.nonAmpIframe.src;
this.currentAmpUrl = this.ampIframe.src;
Expand All @@ -90,10 +89,6 @@ class PairedBrowsingApp {

// Overlay that is displayed on the client that becomes disconnected.
this.disconnectOverlay = document.querySelector( '.disconnect-overlay' );
this.disconnectText = {
general: document.querySelector( '.disconnect-overlay .dialog-text span.general' ),
invalidAmp: document.querySelector( '.disconnect-overlay .dialog-text span.invalid-amp' ),
};
this.disconnectButtons = {
exit: document.querySelector( '.disconnect-overlay .button.exit' ),
goBack: document.querySelector( '.disconnect-overlay .button.go-back' ),
Expand Down Expand Up @@ -211,16 +206,9 @@ class PairedBrowsingApp {
const isClientConnected = this.isClientConnected( iframe );

if ( ! isClientConnected ) {
if ( this.ampIframe === iframe && this.ampPageHasErrors ) {
this.disconnectText.general.classList.toggle( 'hidden', true );
this.disconnectText.invalidAmp.classList.toggle( 'hidden', false );
} else {
this.disconnectText.general.classList.toggle( 'hidden', false );
this.disconnectText.invalidAmp.classList.toggle( 'hidden', true );
}

// Show the 'Go Back' button if the parent window has history.
this.disconnectButtons.goBack.classList.toggle( 'hidden', 0 >= window.history.length );

// If the document is not available, the window URL cannot be accessed.
this.disconnectButtons.exit.classList.toggle( 'hidden', null === iframe.contentDocument );

Expand All @@ -246,7 +234,7 @@ class PairedBrowsingApp {
* @param {HTMLIFrameElement} iframe The iframe.
*/
isClientConnected( iframe ) {
if ( this.ampIframe === iframe && this.ampPageHasErrors ) {
if ( this.ampIframe === iframe ) {
return false;
}

Expand Down Expand Up @@ -292,16 +280,6 @@ class PairedBrowsingApp {
return parsedUrl.href;
}

/**
* Checks if a URL has the 'amp_validation_errors' query variable.
*
* @param {string} url URL string.
* @return {boolean} True if such query var exists, false if not.
*/
urlHasValidationErrorQueryVar( url ) {
return hasQueryArg( url, 'amp_validation_errors' );
}

/**
* Replace location.
*
Expand Down Expand Up @@ -365,37 +343,16 @@ class PairedBrowsingApp {
return;
}

// Force the AMP iframe to always have an AMP URL.
if ( ! isAmpDocument ) {
if ( this.urlHasValidationErrorQueryVar( ampUrl ) ) {
/*
* If the AMP page has validation errors, mark the page as invalid so that the
* 'disconnected' overlay can be shown.
*/
this.ampPageHasErrors = true;
this.toggleDisconnectOverlay( this.ampIframe );
return;
} else if ( ampUrl ) {
// Force the AMP iframe to always have an AMP URL, if an AMP version is available.
this.replaceLocation( sourceIframe, ampUrl );
return;
}

/*
* If the AMP iframe has loaded a non-AMP page and none of the conditions above are
* true, then explicitly mark it as having errors and display the 'disconnected
* overlay.
*/
this.ampPageHasErrors = true;
// this.toggleDisconnectOverlay( this.ampIframe );
this.replaceLocation( sourceIframe, ampUrl );
return;
}

this.currentAmpUrl = ampUrl;

// Update the AMP link above the iframe used for exiting paired browsing.
this.ampLink.href = removeQueryArgs( ampUrl, noampQueryVar );

this.ampPageHasErrors = false;
} else {
// Stop if the URL has not changed.
if ( this.currentNonAmpUrl === nonAmpUrl ) {
Expand Down
36 changes: 3 additions & 33 deletions assets/src/admin/paired-browsing/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,7 @@ import domReady from '@wordpress/dom-ready';
import { isNonAmpWindow, isAmpWindow } from './utils';

const { parent, ampPairedBrowsingClientData } = window;
const { ampUrl, nonAmpUrl } = ampPairedBrowsingClientData;

/**
* Validates whether or not the window document is AMP compatible.
*
* @return {boolean} True if AMP compatible, false if not.
*/
function documentIsAmp() {
return Boolean( document.querySelector( 'head > script[src="https://cdn.ampproject.org/v0.js"]' ) );
}

// /**
// * Get amphtml link URL.
// *
// * @return {string|null} URL or null if link not present.
// */
// function getAmphtmlLinkHref() {
// const link = document.querySelector( 'head > link[rel=amphtml]' );
// return link ? link.href : null;
// }
//
// /**
// * Get canonical link URL.
// *
// * @return {string|null} URL or null if link not present.
// */
// function getCanonicalLinkHref() {
// const link = document.querySelector( 'head > link[rel=canonical]' );
// return link ? link.href : null;
// }
const { ampUrl, nonAmpUrl, isAmpDocument } = ampPairedBrowsingClientData;

/**
* Modify document for paired browsing.
Expand All @@ -47,7 +18,7 @@ function modifyDocumentForPairedBrowsing() {
// Scrolling is not synchronized if `scroll-behavior` is set to `smooth`.
document.documentElement.style.setProperty( 'scroll-behavior', 'auto', 'important' );

if ( documentIsAmp() ) {
if ( isAmpDocument ) {
// Hide the paired browsing menu item.
const pairedBrowsingMenuItem = document.getElementById( 'wp-admin-bar-amp-paired-browsing' );
if ( pairedBrowsingMenuItem ) {
Expand Down Expand Up @@ -148,8 +119,7 @@ function sendHeartbeat() {
parent,
'heartbeat',
{
isAmpDocument: documentIsAmp(),
//locationHref: window.location.href,
isAmpDocument,
ampUrl,
nonAmpUrl,
documentTitle: document.title,
Expand Down
5 changes: 3 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -2370,8 +2370,9 @@ public static function setup_paired_browsing_client() {
'amp-paired-browsing-client',
'ampPairedBrowsingClientData',
[
'ampUrl' => $amp_url,
'nonAmpUrl' => $non_amp_url,
'isAmpDocument' => $is_amp_request,
'ampUrl' => $amp_url,
'nonAmpUrl' => $non_amp_url,
]
);

Expand Down
4 changes: 0 additions & 4 deletions includes/templates/amp-paired-browsing.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
<span class="general">
<?php esc_html_e( 'The navigated URL is not available for paired browsing.', 'amp' ); ?>
</span>

<span class="invalid-amp">
<?php esc_html_e( 'The navigated page does not have an AMP counterpart due to invalid AMP markup being kept.', 'amp' ); ?>
</span>
</div>

<div class="dialog-buttons">
Expand Down

0 comments on commit 12378ae

Please sign in to comment.