Skip to content

Commit

Permalink
Jero/hotfix/product card long description (#892)
Browse files Browse the repository at this point in the history
* feat (utils) add sanitizeHTML function

* feat (pages/Store/Product) use sanitizeHTML for long description
  • Loading branch information
aneurysmjs authored Jan 21, 2019
1 parent 3151d79 commit b16f098
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
34 changes: 2 additions & 32 deletions src/pages/Store/About/About.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import React, { PureComponent } from 'react';
import { createFragmentContainer, graphql } from 'react-relay';
import xss from 'xss';
import { addressToString, getNameText } from 'utils';
import { addressToString, getNameText, sanitizeHTML } from 'utils';

// import ImageLoader from 'libs/react-image-loader';
// import BannerLoading from 'components/Banner/BannerLoading';
Expand All @@ -26,36 +25,7 @@ class About extends PureComponent<PropsType> {
// $FlowIgnoreMe
const address = addressToString(shop.addressFull);
/* eslint-disable no-underscore-dangle */
const __html = xss(longDescription, {
whiteList: {
br: [],
hr: [],
a: ['class', 'style', 'href', 'target', 'rel'],
p: ['class', 'style'],
ol: ['class', 'style'],
h1: ['class', 'style'],
h2: ['class', 'style'],
h3: ['class', 'style'],
h4: ['class', 'style'],
h5: ['class', 'style'],
h6: ['class', 'style'],
ul: ['class', 'style'],
li: ['class', 'style'],
em: ['class', 'style'],
img: ['class', 'style', 'src', 'sizes', 'srcset', 'width', 'height'],
sub: ['class', 'style'],
sup: ['class', 'style'],
div: ['class', 'style'],
span: ['class', 'style'],
strong: ['class', 'style'],
iframe: ['class', 'style', 'src', 'width', 'height', 'frameborder'],
table: ['class', 'style'],
tr: ['class', 'style'],
td: ['class', 'style'],
tbody: ['class', 'style'],
thead: ['class', 'style'],
},
});
const __html = sanitizeHTML(longDescription);
return (
<div styleName="container">
<div styleName="title">
Expand Down
20 changes: 10 additions & 10 deletions src/pages/Store/Product/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, { Component } from 'react';
import { createFragmentContainer, graphql } from 'react-relay';
import { routerShape } from 'found';
import PropTypes from 'prop-types';
import xss from 'xss';
import {
isNil,
head,
Expand All @@ -25,7 +24,13 @@ import { AppContext, Page } from 'components/App';
import { Col, Row } from 'layout';
import { AddInCartMutation } from 'relay/mutations';
import { withShowAlert } from 'components/Alerts/AlertContext';
import { extractText, isEmpty, log, convertCountries } from 'utils';
import {
extractText,
isEmpty,
log,
convertCountries,
sanitizeHTML,
} from 'utils';
import { productViewTracker, addToCartTracker } from 'rrHalper';

import type { AddAlertInputType } from 'components/Alerts/AlertContext';
Expand Down Expand Up @@ -283,6 +288,8 @@ class Product extends Component<PropsType, StateType> {
'EN',
t.noLongDescription,
).replace(/\n/g, '<hr />');
/* eslint-disable no-underscore-dangle */
const __html = sanitizeHTML(modifLongDescription);
const tabs: Array<TabType> = [
{
id: '0',
Expand All @@ -292,14 +299,7 @@ class Product extends Component<PropsType, StateType> {
styleName="longDescription"
// eslint-disable-next-line
dangerouslySetInnerHTML={{
__html: xss(`${modifLongDescription}`, {
whiteList: {
img: ['src', 'style', 'sizes', 'srcset'],
br: [],
hr: [],
div: ['style'],
},
}),
__html,
}}
/>
),
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export { default as vendorCodeGenerator } from './vendorCodeGenerator';
export { default as jwt } from './jwt';
export { default as getQueryRefParams } from './getQueryRefParams';
export { default as isJson } from './isJson';
export { default as sanitizeHTML } from './sanitizeHTML';
37 changes: 37 additions & 0 deletions src/utils/sanitizeHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @flow strict
// $FlowIgnoreMe
import xss from 'xss';

const sanitizeHTML = (html: string): string =>
xss(html, {
whiteList: {
a: ['class', 'style', 'href', 'target', 'rel'],
p: ['class', 'style'],
br: ['class', 'style'],
hr: ['class', 'style'],
ol: ['class', 'style'],
h1: ['class', 'style'],
h2: ['class', 'style'],
h3: ['class', 'style'],
h4: ['class', 'style'],
h5: ['class', 'style'],
h6: ['class', 'style'],
ul: ['class', 'style'],
li: ['class', 'style'],
em: ['class', 'style'],
img: ['class', 'style', 'src', 'sizes', 'srcset', 'width', 'height'],
sub: ['class', 'style'],
sup: ['class', 'style'],
div: ['class', 'style'],
span: ['class', 'style'],
strong: ['class', 'style'],
iframe: ['class', 'style', 'src', 'width', 'height', 'frameborder'],
table: ['class', 'style'],
tr: ['class', 'style'],
td: ['class', 'style'],
tbody: ['class', 'style'],
thead: ['class', 'style'],
},
});

export default sanitizeHTML;

0 comments on commit b16f098

Please sign in to comment.