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

Allow variable substitutions in hidden fields. #6130

Merged
merged 3 commits into from Nov 30, 2016

Conversation

mkhatib
Copy link
Contributor

@mkhatib mkhatib commented Nov 10, 2016

Fix #5654

for (let i = 0; i < varSubsFields.length; i++) {
const variable = varSubsFields[i].getAttribute('default-value');
varSubPromises.push(
this.urlReplacement_.expandAsync(variable).then(value => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Careful, UrlReplacements isn't a generic variable substitution library. I added an assert that the "protocol" of the url remains the same (because they were all URLs before now), but that doesn't apply here. So if the default value had something like RANDOM:X:Y, this would break because of the changing "protocol".

We should split UrlReplacements into a core class (VarSubstitutions?), and leave the URL specific code out of 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.

Yeah that makes sense.

I can get started in moving VarSubstitutions to a separate service. My guess is for any call to that service directly (outside of url-replacement-impl.js) shouldn't allow URL replacements then?

Copy link
Contributor

Choose a reason for hiding this comment

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

My guess is for any call to that service directly (outside of url-replacement-impl.js) shouldn't allow URL replacements then?

Hm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I meant to say that we'd need a way to prevent direct calls to var-sub service with a URL to avoid circumventing the protocol assert.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can presubmit and whitelist Forms and UrlReplacements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sent a PR #6257

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.

* @private
*/
waitOnPromisesOrTimeout_(promises, timeout) {
return Promise.race(
Copy link
Contributor

Choose a reason for hiding this comment

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

Timer#timeoutPromise takes a second "race" promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That one rejects though when timeout happens, I guess I could call the same callback in both then and catch clauses, or is there a better way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, this is fine then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack

this.renderTemplate_(error.responseJson || {});
rethrowAsync('Form submission failed:', error);
// Wait until all variables have been substituted or 100ms timeout.
return this.waitOnPromisesOrTimeout_(varSubPromises, 100).then(() => {

Choose a reason for hiding this comment

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

This changes the method signature from void return. Consider returning null in other cases and updating return type to ?Promise.

Copy link
Contributor Author

@mkhatib mkhatib Nov 29, 2016

Choose a reason for hiding this comment

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

Dropped this return since we don't use it.

// Wait until all variables have been substituted or 100ms timeout.
return this.waitOnPromisesOrTimeout_(varSubPromises, 100).then(() => {
if (isHeadOrGet) {
xhrUrl = addParamsToUrl(this.xhrAction_, this.getFormAsObject_());

Choose a reason for hiding this comment

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

Nit: This would be more readable if xhrUrl was declared closer to its usage and the isHeadOrGet conditional wasn't duped below for body. E.g.

let xhrUrl, body;
if (isHeadOrGet) {
  xhrUrl = addParamsToUrl(this.xhrAction_, this.getFormAsObject_());
  body = new FormData(this.form_);
} else {
  xhrUrl = this.xhrAction_;
}
return this.xhr_.fetchJson(xhrUrl, {
  body: body,

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

@@ -91,6 +99,7 @@ describe('amp-form', () => {
sandbox.restore();
});

// asd

Choose a reason for hiding this comment

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

Typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped

@mkhatib
Copy link
Contributor Author

mkhatib commented Nov 29, 2016

PTAL 👀

@mkhatib
Copy link
Contributor Author

mkhatib commented Nov 30, 2016

@choumx @jridgewell this is ready for another 👀

@@ -201,47 +210,86 @@ export class AmpForm {
return;
}

const isVarSubExpOn = isExperimentOn(this.win_, 'amp-form-var-sub');
// Fields that support var substitutions.
const varSubsFields = !isVarSubExpOn ? [] : this.form_.querySelectorAll(
Copy link
Contributor

Choose a reason for hiding this comment

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

Flip please.

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

@mkhatib mkhatib merged commit 4edeae3 into ampproject:master Nov 30, 2016
@mkhatib mkhatib deleted the forms-vars branch November 30, 2016 23:08
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants