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

fix: custom validation refactor #11

Merged
merged 7 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script src="./messaging.js"></script>
</head>
<body>
<h1>Hi</h1>
<div class="messages" data-pp-message></div>
<script>
paypal
Expand All @@ -20,5 +21,6 @@
})
.render('.messages');
</script>
<h1>Bye</h1>
</body>
</html>
4 changes: 2 additions & 2 deletions src/models/Banner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ const Banner = {

function render(totalOptions) {
const options = validateOptions(totalOptions);
const renderProm = getBannerMarkup(options) // Promise<Object({ markup, options })>
const renderProm = getBannerMarkup(options) // Promise<Object( markup, options )>
.then(
insertMarkup // Promise<Object(meta)>
insertMarkup // Promise<Object(meta, options)>
)
.then(
pipe(
Expand Down
7 changes: 5 additions & 2 deletions src/services/banner/customTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ function fetcher(url) {
}

function getCustomTemplate(styles) {
let markupRegex = /https:\/\/www\.paypalobjects\.com/;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it feasible to use String.prototype.startsWith() instead of a regex for this scenario? And for __DEMO__ we can just accept anything since it's for internal use only?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, have we considered how this will work with the customization app since everything is now some form of URL?

if (__DEMO__) {
markupRegex = /^(?:(https?:\/\/)|\.{0,2}\/)/;
}
const source = styles.markup;
const markupProm = ZalgoPromise.resolve(source.match(/^(?:(https?:\/\/)|\.{0,2}\/)/) ? fetcher(source) : source);
return markupProm.then(markup => markup);
return ZalgoPromise.resolve(source.match(markupRegex) ? fetcher(source) : null);
}

export default memoize(getCustomTemplate);
19 changes: 15 additions & 4 deletions src/services/banner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import objectEntries from 'core-js-pure/stable/object/entries';
import { ZalgoPromise } from 'zalgo-promise';

import { memoizeOnProps, objectGet, objectMerge, objectFlattenToArray } from '../../utils';
import { logger, EVENTS } from '../logger';
import { logger, EVENTS, ERRORS } from '../logger';
import getCustomTemplate from './customTemplate';

// Specific dimensions tied to JSON banners in Campaign Studio
Expand Down Expand Up @@ -87,7 +87,12 @@ function fetcher(options) {
function getBannerOptions(markup) {
const annotationsString = markup.match(/^<!--([\s\S]+?)-->/);
if (annotationsString) {
return JSON.parse(annotationsString[1]);
try {
const bannerOptions = JSON.parse(annotationsString[1]);
return bannerOptions;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't we just return JSON.parse(annotationsString[1])?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup - remnant of some previous code.

} catch (err) {
throw new Error(ERRORS.INVALID_CUSTOM_BANNER_JSON);
}
}
return {};
}
Expand All @@ -104,8 +109,14 @@ export default function getBannerMarkup(options) {
data.markup.template = template;

const bannerOptions = getBannerOptions(template);
options = objectMerge(options, { ...bannerOptions });
options.style._flattened = objectFlattenToArray(options.style);
if (!bannerOptions) {
return null;
}

const mergedOptions = objectMerge(options, bannerOptions);
mergedOptions.style._flattened = objectFlattenToArray(mergedOptions.style);

return { markup: data.markup, options: mergedOptions };
}
return { markup: data.markup, options };
});
Expand Down
3 changes: 2 additions & 1 deletion src/services/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const ERRORS = {
HIDDEN: 'Overflow fallback failed. Hiding banner.',
INVALID_STYLE_OPTIONS: 'Invalid account, styles, signature combination.',
INVALID_LEGACY_BANNER: 'Invalid legacy banner placement/offerType combination',
MODAL_LOAD_FAILURE: 'Modal failed to initialize.'
MODAL_LOAD_FAILURE: 'Modal failed to initialize.',
INVALID_CUSTOM_BANNER_JSON: 'Invalid JSON in custom banner creative'
};

const logs = [];
Expand Down