Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install global submit listener only when amp-form extension is registered #17246

Merged
merged 11 commits into from
Aug 9, 2018
12 changes: 11 additions & 1 deletion src/document-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ import {
} from './url';
import {Services} from './services';
import {dev, user} from './log';
import {getElementServiceIfAvailableForDoc} from './element-service';

/**
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @return {!Promise}
*/
export function installGlobalSubmitListenerForDoc(ampdoc) {
ampdoc.getRootNode().addEventListener('submit', onDocumentFormSubmit_, true);
// Register global submit event listener only if the amp-form
// extension is used. Allowing the usage of native forms, otherwise.
return getElementServiceIfAvailableForDoc(ampdoc, 'amp-form', 'amp-form')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will delay installation of the event listener until amp-form extension is installed. We should instead simply check if the extension will be installed and synchronously install or skip the event listener.

I think we can add a new function to element-service.js that uses extensionScriptsInNode() and rework waitForExtensionIfPresent to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Thanks

.then(ampFormService => {
if (ampFormService) {
ampdoc.getRootNode().addEventListener(
'submit', onDocumentFormSubmit_, true);
}
});
}


Expand Down
36 changes: 35 additions & 1 deletion test/functional/test-document-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/

import * as elementService from '../../src/element-service';
import * as sinon from 'sinon';
import {Services} from '../../src/services';
import {onDocumentFormSubmit_} from '../../src/document-submit';
import {installGlobalSubmitListenerForDoc, onDocumentFormSubmit_} from '../../src/document-submit';

describe('test-document-submit onDocumentFormSubmit_', () => {
let sandbox;
Expand Down Expand Up @@ -46,6 +48,38 @@ describe('test-document-submit onDocumentFormSubmit_', () => {
sandbox.restore();
});

describe('installGlobalSubmitListenerForDoc', () => {
let headNode;
let ampDoc;
beforeEach(() => {
headNode = document.createElement('body');
ampDoc = {
getRootNode: () => headNode,
};
});

afterEach(() => sandbox.reset());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be necessary if you don't create the sandbox in beforeEach().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


it('should not register submit listener if amp-form is not registered.',
() => {
sandbox.stub(elementService, 'getElementServiceIfAvailableForDoc')
.returns(Promise.resolve(null));
sandbox.spy(headNode, 'addEventListener');
installGlobalSubmitListenerForDoc(ampDoc).then(() => {
expect(headNode.addEventListener).not.to.have.been.called;
});
});

it('should register submit listener if amp-form extension is registered.',
() => {
sandbox.stub(elementService, 'getElementServiceIfAvailableForDoc')
.returns(Promise.resolve({}));
sandbox.spy(headNode, 'addEventListener');
installGlobalSubmitListenerForDoc(ampDoc).then(() => {
expect(headNode.addEventListener).called;
});
});
});

it('should check target and action attributes', () => {
tgt.removeAttribute('action');
Expand Down