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
Conversation
@@ -35,8 +35,58 @@ | |||
}; | |||
})(); | |||
|
|||
babelHelpers.slice = Array.prototype.slice; | |||
babelHelpers.bind = Function.prototype.bind; | |||
babelHelpers.slicedToArray = (function () { |
There was a problem hiding this comment.
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
.
88e86ca
to
a6cbdba
Compare
*/ | ||
getNameArgs_(key) { | ||
if (!key) { | ||
return ['', '']; |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:) Now it is.
There was a problem hiding this comment.
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>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!
There was a problem hiding this comment.
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(/([^(]*)(\([^)]*\))?/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can match
be null?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Added @jridgewell to review for a second pair of eyes and since he was already on this review earlier. |
There was a problem hiding this 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', () => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
ptal. |
1f3d9b7
to
b2da985
Compare
@@ -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) { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
ptal. |
*/ | ||
getNameArgs_(key) { | ||
if (!key) { | ||
return ['', '']; |
There was a problem hiding this comment.
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(/([^(]*)(\([^)]*\))?/); |
There was a problem hiding this comment.
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....
Ha, apparently I reviewed a dated PR. LGTM. |
LGTM. A clear name should be enough to pick up issues in review. |
…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
…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
…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
Specifically, the
vars
and extraUrlParams` section encoded thevariables with params before they could be expanded.
Fixes #4825