-
Notifications
You must be signed in to change notification settings - Fork 821
OpenTable: Accept more embed options #14639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
40854ee
get the restaurant id from the URL if it's available
scruffian 8fa3eaf
Accept URLs pasted into the editor
scruffian df90de8
Adds tests for getValidatedAttributes
scruffian 66455cb
Change the regex to use \s for all whitespace
pablinos 6c55fcb
Add tests for invalid embed codes and URLs
pablinos 211c300
Converted spaces to tabs
pablinos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getAttributesFromEmbedCode } from '../utils'; | ||
|
||
const widgetEmbedCode = "<script type='text/javascript' src='//www.opentable.com/widget/reservation/loader?rid=1&type=standard&theme=standard&iframe=true&domain=com&lang=en-US&newtab=false&ot_source=Restaurant%20website'></script>"; | ||
|
||
const invalidEmbedCode = "<script type='text/javascript' src='https://www.widgets-r-us.com/widget/widgetygoddness?rid=1&type=standard&theme=standard&iframe=true&domain=com&lang=en-US&newtab=false&ot_source=Restaurant%20website'></script>"; | ||
|
||
const marketingUrl = "https://www.opentable.com/vongs-thai-kitchen-reservations-chicago?restref=1&lang=en-US&ot_source=Restaurant%20website"; | ||
|
||
const customUrl1 = "https://www.opentable.com/restref/client/?restref=412810&lang=en-US&ot_source=Restaurant%20website&corrid=e413926b-0352-46d6-a8d8-d1d525932310"; | ||
|
||
const customUrl2 = "https://www.opentable.com/restref/client/?restref=1&lang=es-MX&ot_source=Restaurant%20website&corrid=09f44cc6-f0cb-4e98-9298-f4ba8cc20183"; | ||
|
||
const customUrl3 = "https://www.opentable.com/restref/client/?rid=1&corrid=010a3136-569e-42a5-a381-e111887b4cf5"; | ||
|
||
const invalidUrl = "https://www.widgets-r-us.com/widget/widgetygoddness?rid=1&type=standard&theme=standard&iframe=true&domain=com&lang=en-US&newtab=false&ot_source=Restaurant%20website"; | ||
|
||
describe( 'getAttributesFromEmbedCode', () => { | ||
test( 'Widget embed code', () => { | ||
expect( | ||
getAttributesFromEmbedCode( widgetEmbedCode ) | ||
).toEqual( | ||
{ | ||
"domain": "com", | ||
"iframe": 'true', | ||
"lang": "en-US", | ||
"newtab": 'false', | ||
"rid": [ "1" ], | ||
"style": "standard", | ||
} | ||
); | ||
} ); | ||
|
||
test( 'Marketing URL', () => { | ||
expect( | ||
getAttributesFromEmbedCode( marketingUrl ) | ||
).toEqual( | ||
{ | ||
"lang": "en-US", | ||
"rid": [ "1" ], | ||
} | ||
); | ||
} ); | ||
|
||
test( 'Custom URL 1', () => { | ||
expect( | ||
getAttributesFromEmbedCode( customUrl1 ) | ||
).toEqual( | ||
{ | ||
"lang": "en-US", | ||
"rid": [ "412810" ], | ||
} | ||
); | ||
} ); | ||
|
||
test( 'Custom URL 2', () => { | ||
expect( | ||
getAttributesFromEmbedCode( customUrl2 ) | ||
).toEqual( | ||
{ | ||
"lang": "es-MX", | ||
"rid": [ "1" ], | ||
} | ||
); | ||
} ); | ||
|
||
test( 'Custom URL 3', () => { | ||
expect( | ||
getAttributesFromEmbedCode( customUrl3 ) | ||
).toEqual( | ||
{ | ||
"rid": [ "1" ], | ||
} | ||
); | ||
} ); | ||
|
||
test( 'Invaild Embed Code', () => { | ||
expect( | ||
getAttributesFromEmbedCode( invalidEmbedCode ) | ||
).toBeUndefined(); | ||
} ); | ||
|
||
test( 'Invaild URL', () => { | ||
expect( | ||
getAttributesFromEmbedCode( invalidUrl ) | ||
).toBeUndefined(); | ||
} ); | ||
} ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export const embedRegex = /<\s*script[^>]*src\s*=\s*["']?([^"']*)/i; | ||
export const restRefRegex = /restref=([0-9]+)&/; | ||
export const ridRegex = /rid=([0-9]+)&/; | ||
|
||
const getAttributesFromUrl = url => { | ||
let src = ''; | ||
if ( url.indexOf( 'http' ) === 0 ) { | ||
src = new URL( url ); | ||
} else { | ||
src = new URL( 'http:' + url ); | ||
} | ||
|
||
if ( ! src.host || src.host.indexOf( 'opentable' ) === -1 || ! src.search ) { | ||
return; | ||
} | ||
|
||
const searchParams = new URLSearchParams( src.search ); | ||
let styleSetting = searchParams.get( 'theme' ); | ||
if ( searchParams.get( 'type' ) === 'button' ) { | ||
styleSetting = searchParams.get( 'type' ); | ||
} | ||
|
||
let restaurantId = searchParams.getAll( 'rid' ); | ||
if ( ! restaurantId || restaurantId.length === 0 ) { | ||
restaurantId = searchParams.getAll( 'restref' ); | ||
} | ||
if ( ! restaurantId || restaurantId.length === 0 ) { | ||
return; | ||
} | ||
|
||
const newAttributes = {}; | ||
if ( restaurantId ) { | ||
newAttributes.rid = restaurantId; | ||
} | ||
|
||
const domain = searchParams.get( 'domain' ); | ||
if ( domain ) { | ||
newAttributes.domain = domain; | ||
} | ||
|
||
const iframe = searchParams.get( 'iframe' ); | ||
if ( iframe ) { | ||
newAttributes.iframe = iframe; | ||
} | ||
|
||
const lang = searchParams.get( 'lang' ); | ||
if ( lang ) { | ||
newAttributes.lang = lang; | ||
} | ||
|
||
const newtab = searchParams.get( 'newtab' ); | ||
if ( newtab ) { | ||
newAttributes.newtab = newtab; | ||
} | ||
|
||
if ( styleSetting ) { | ||
newAttributes.style = styleSetting; | ||
} | ||
|
||
return newAttributes; | ||
}; | ||
|
||
const getUrlFromEmbedCode = embedCode => { | ||
const scriptTagAttributes = embedCode.match( embedRegex ); | ||
if ( scriptTagAttributes && scriptTagAttributes[ 1 ] ) { | ||
return scriptTagAttributes[ 1 ]; | ||
} | ||
|
||
if ( restRefRegex.test( embedCode ) || ridRegex.test( embedCode ) ) { | ||
return embedCode; | ||
} | ||
}; | ||
|
||
export const getAttributesFromEmbedCode = embedCode => { | ||
if ( ! embedCode ) { | ||
return; | ||
} | ||
|
||
return getAttributesFromUrl( getUrlFromEmbedCode( embedCode ) ); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 know this works, but should we add a negative test to make sure that we don't get any attributes with some other invalid URL/embed code? I think this is the point we would get to if someone was to paste in something silly like
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.
Yeah we should probably, but I think that would be good for a different PR :)