Skip to content

Allow variable substitutions in hidden fields.#6130

Merged
mkhatib merged 3 commits intoampproject:masterfrom
mkhatib:forms-vars
Nov 30, 2016
Merged

Allow variable substitutions in hidden fields.#6130
mkhatib merged 3 commits intoampproject:masterfrom
mkhatib:forms-vars

Conversation

@mkhatib
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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(() => {
Copy link
Copy Markdown

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
Copy Markdown
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_());
Copy link
Copy Markdown

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

sandbox.restore();
});

// asd
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Typo?

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

mkhatib commented Nov 29, 2016

PTAL 👀

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Nov 30, 2016

@choumx @jridgewell this is ready for another 👀


const isVarSubExpOn = isExperimentOn(this.win_, 'amp-form-var-sub');
// Fields that support var substitutions.
const varSubsFields = !isVarSubExpOn ? [] : this.form_.querySelectorAll(
Copy link
Copy Markdown
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
Copy Markdown
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.

3 participants