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

Support for expansion of variables with params in parts of amp-analytics. #4855

Merged
merged 4 commits into from Oct 5, 2016

Conversation

avimehta
Copy link
Contributor

@avimehta avimehta commented Sep 8, 2016

Specifically, the vars and extraUrlParams` section encoded the
variables with params before they could be expanded.

Fixes #4825

@@ -35,8 +35,58 @@
};
})();

babelHelpers.slice = Array.prototype.slice;
babelHelpers.bind = Function.prototype.bind;
babelHelpers.slicedToArray = (function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes were autogenerated by gulp babel-helpers.

*/
getNameArgs_(key) {
if (!key) {
return ['', ''];
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not an object...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:) Now it is.

Copy link
Contributor

@jridgewell jridgewell Oct 3, 2016

Choose a reason for hiding this comment

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

Still isn't...

* Returns an array containing two values: name and args parsed from the key.
*
* @param {string} key The key to be parsed.
* @return {Object<string>}
Copy link
Contributor

Choose a reason for hiding this comment

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

!

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.

if (!key) {
return ['', ''];
}
const match = key.match(/([^(]*)(\([^)]*\))?/);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can match be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't be. If it is, that is an error that we would like to surface. Added a user.error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Added a user.error.

Not yet....

return encodeURIComponent(raw);
// Separate out names and arguments from the value and encode the value.
const {name, argList} = this.getNameArgs_(String(raw));
return encodeURIComponent(name) + argList;
Copy link
Contributor

Choose a reason for hiding this comment

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

What if name is '' here?

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 will be encoded to '' and passed along. It shouldn't be empty though. That is like saying you are trying to expand an empty string.

@dvoytenko
Copy link
Contributor

Added @jridgewell to review for a second pair of eyes and since he was already on this review earlier.

Copy link
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

Code LGTM. One request.

let actual = analytics.expandTemplate_('${1}', vars, {}, 3);
expect(actual).to.equal('1234%25252524%2525257B1%2525257D');

actual = analytics.expandTemplate_('${1}', vars, {}, 5);
expect(actual).to.equal('123412%252525252524%25252525257B3%25252525257D');
});

it('works with params', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get a test that shows it grabbing the actually query param?

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 wasn't sure if you wanted me to add a unittest for getNameArgs() or something else. I added two tests. the one that goes through the whole code flow is not a unittest as it mocks parts of url replacement.

(getting actual query params is difficult here because the win.loc is about:srcdoc or something - no query params.)

@avimehta
Copy link
Contributor Author

ptal.

@@ -135,7 +135,7 @@ export function parseUrl(url, opt_nocache) {
* @param {boolean=} opt_addToFront
* @return {string}
*/
function appendParamStringToUrl(url, paramString, opt_addToFront) {
export function appendParamStringToUrl(url, paramString, opt_addToFront) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I have relatively big concerns about exposing this method given potential for XSS. At the very least, if we want to do this, could you please massage the naming to something like:

  • appendEncodedParamStringToUrl
  • encodedParamString

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Used the first name. If you would prefer, I can copy this method into amp-analytics and keep it private.

…ics config.

Specifically, the `vars` and extraUrlParams` section encoded the
variables with params before they could be expanded.

Fixes ampproject#4825
@avimehta
Copy link
Contributor Author

avimehta commented Oct 3, 2016

ptal.

*/
getNameArgs_(key) {
if (!key) {
return ['', ''];
Copy link
Contributor

@jridgewell jridgewell Oct 3, 2016

Choose a reason for hiding this comment

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

Still isn't...

if (!key) {
return ['', ''];
}
const match = key.match(/([^(]*)(\([^)]*\))?/);
Copy link
Contributor

Choose a reason for hiding this comment

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

Added a user.error.

Not yet....

@jridgewell
Copy link
Contributor

Ha, apparently I reviewed a dated PR. LGTM.

@dvoytenko
Copy link
Contributor

LGTM. A clear name should be enough to pick up issues in review.

@avimehta avimehta merged commit c609e02 into ampproject:master Oct 5, 2016
@avimehta avimehta deleted the bug branch October 5, 2016 20:04
dreamofabear pushed a commit to dreamofabear/amphtml that referenced this pull request Oct 6, 2016
…ics. (ampproject#4855)

* Support for expansion of variables with params in parts of amp-analytics config.

Specifically, the `vars` and extraUrlParams` section encoded the
variables with params before they could be expanded.

Fixes ampproject#4825
dreamofabear pushed a commit to dreamofabear/amphtml that referenced this pull request Oct 12, 2016
…ics. (ampproject#4855)

* Support for expansion of variables with params in parts of amp-analytics config.

Specifically, the `vars` and extraUrlParams` section encoded the
variables with params before they could be expanded.

Fixes ampproject#4825
mityaha pushed a commit to ooyala/amphtml that referenced this pull request Nov 30, 2016
…ics. (ampproject#4855)

* Support for expansion of variables with params in parts of amp-analytics config.

Specifically, the `vars` and extraUrlParams` section encoded the
variables with params before they could be expanded.

Fixes ampproject#4825
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants