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

Add url REPLACE macro #13130

Merged
merged 4 commits into from Jan 30, 2018
Merged

Add url REPLACE macro #13130

merged 4 commits into from Jan 30, 2018

Conversation

calebcordry
Copy link
Member

Follow up to #12682. All the new macros should now be available behind the url-replacement-v2 flag.

This PR adds the REPLACE macro that can be used in url expansion. It also adds the ``` character that tells the new parser to ignore the special characters that may remain inside.

Closes #2198

@@ -193,6 +193,14 @@ describe('amp-analytics.VariableService', function() {
'S7Uc5ZmODduHWdplzrZ7Jsnqx')
);
});

it('replaces', () => {
return check('REPLACE(this-is-a-test, `-`, )', 'thisisatest');
Copy link
Contributor

Choose a reason for hiding this comment

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

i thought this should gives this is a test (with spaces)

Copy link
Member Author

Choose a reason for hiding this comment

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

we are currently stripping spaces so people can do things like DEFAULT(one, two) vs only allowing DEFAULT(one,two). We could change this but I think allowing it is better? It does make it tricky to pass in a space though

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't that both tests are testing replace with no third arg? here newSubStr = undefined?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, newSubStr === '' in the first. I will add a few more tests to make this more clear

Copy link
Contributor

Choose a reason for hiding this comment

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

so how do i get this is a test? Is it supported?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was spending some time thinking about this last night. Right now it is not supported without using a different macro that would return a space. We could support something like REPLACE(this-is-a-test, `-`, ` `) If we do that should we make the back ticks on the third parameter required? Also no matter what the spaces will be encoded so it would return this%20is%20a%20test

Copy link
Member Author

Choose a reason for hiding this comment

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

We are now respecting whitespaces inside all back ticks. More tests were written to cover this behavior.

@@ -98,6 +98,17 @@ function defaultMacro(value, defaultValue) {
return value;
}

function replaceFilter(string, matchPattern, newSubStr) {
Copy link
Contributor

@zhouyx zhouyx Jan 30, 2018

Choose a reason for hiding this comment

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

jsDoc

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops. Done.

@@ -193,6 +193,14 @@ describe('amp-analytics.VariableService', function() {
'S7Uc5ZmODduHWdplzrZ7Jsnqx')
);
});

it('replaces', () => {
return check('REPLACE(this-is-a-test, `-`, )', 'thisisatest');
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't that both tests are testing replace with no third arg? here newSubStr = undefined?

@lannka lannka merged commit b40aac2 into ampproject:master Jan 30, 2018
if (!ignoringChars) {
ignoringChars = true;
nextArgShouldBeRaw = true;
builder = '';
Copy link
Contributor

Choose a reason for hiding this comment

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

user().assert(builder === '') first. This can hide bugs such as

build`test`

missing the build

Copy link
Member Author

Choose a reason for hiding this comment

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

this is a good idea. Will make a separate PR since this is now merged


const evaluateNextLevel = () => {
let builder = '';
const results = [];

while (urlIndex < url.length && matchIndex <= matches.length) {

if (match && urlIndex === match.start) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why can we get a match in the middle of a literal string?

Copy link
Member Author

Choose a reason for hiding this comment

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

If I understand your question correctly, this is to support things like example.com?cid=CLIENT_ID(__ga)

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I mean why would CLIENT_ID match inside of

cid=`CLIENT_ID(__GA)`

the tick marks a literal string ("don't parse me" section).

Copy link
Member Author

Choose a reason for hiding this comment

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

the way it was designed is so that the ticks are only respected inside a macro call (inside parenthesis). We are currently reserving keywords like CLIENT_ID to be replaced anywhere in the input string. We could potentially change the parser to look for this if you think its a valid use case?

@calebcordry calebcordry deleted the replace-filter branch January 30, 2018 22:15
RanAbram pushed a commit to RanAbram/amphtml that referenced this pull request Mar 12, 2018
* add-replace-filter

* Refactor space handling

* Toggle off next arg

* Do what test describes
protonate pushed a commit to protonate/amphtml that referenced this pull request Mar 15, 2018
* add-replace-filter

* Refactor space handling

* Toggle off next arg

* Do what test describes
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

6 participants