Skip to content

Commit

Permalink
clean up svg code (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoalvesdulce authored and fernandoabolafio committed Dec 10, 2018
1 parent c0f5b97 commit 4e30dcd
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 135 deletions.
1 change: 0 additions & 1 deletion mocks/api/v1/policy/GET.json
Expand Up @@ -14,7 +14,6 @@
"validmimetypes":[
"image/jpeg",
"image/png",
"image/svg+xml",
"text/plain",
"text/plain; charset=utf-8"
],
Expand Down
9 changes: 2 additions & 7 deletions src/components/Form/Fields/FilesField.js
Expand Up @@ -4,11 +4,7 @@ import ReactFileReader from "react-file-reader";
import { change } from "redux-form";
import ProposalImages from "../../ProposalImages";
import PolicyErrors from "./PolicyErrors";
import {
validateFiles,
getFormattedFiles,
sanitizeSVGFiles
} from "../../ProposalImages/helpers";
import { validateFiles, getFormattedFiles } from "../../ProposalImages/helpers";

class FilesField extends React.Component {
constructor(props) {
Expand All @@ -29,13 +25,12 @@ class FilesField extends React.Component {
? formattedFiles.concat(input.value)
: formattedFiles;
const validation = validateFiles(inputAndNewFiles, policy);
const sanitizedFiles = sanitizeSVGFiles(validation.files);

this.setState({
policyErrors: validation.errors ? validation.errors : []
});

return dispatch(change("form/proposal", "files", sanitizedFiles));
return dispatch(change("form/proposal", "files", inputAndNewFiles));
};

render() {
Expand Down
19 changes: 0 additions & 19 deletions src/components/ProposalImages/helpers.js
@@ -1,5 +1,3 @@
import DOMPurify from "dompurify";

export const errorTypes = {
MAX_SIZE: "max_size",
MAX_IMAGES: "max_length",
Expand Down Expand Up @@ -112,20 +110,3 @@ function validateMimeTypes({ files, errors, policy }) {
errors
};
}

const isSVG = file => file.mime === "image/svg+xml";

export const sanitizeSVGFiles = files => {
files = files.map(file => {
if (!isSVG(file)) return file;

const decodedStr = window.atob(file.payload);
const sanitizedPayload = DOMPurify.sanitize(decodedStr);
const payload = window.btoa(sanitizedPayload);
return {
...file,
payload
};
});
return files;
};
4 changes: 1 addition & 3 deletions src/components/ProposalImages/index.js
@@ -1,6 +1,5 @@
import React, { Component } from "react";
import PropTypes from "prop-types";
import { sanitizeSVGFiles } from "./helpers";

class ProposalImages extends Component {
constructor(props) {
Expand All @@ -15,11 +14,10 @@ class ProposalImages extends Component {

render() {
const { files, readOnly } = this.props;
const sanitizedFiles = sanitizeSVGFiles(files || []);

return (
<div className="attached-images">
{sanitizedFiles.map(({ name, mime, payload }, idx) => (
{files.map(({ name, mime, payload }, idx) => (
<div key={`prop-image-${idx}`} className="attached-image-ct">
<div style={{ display: "flex", alignItems: "center" }}>
<h5 className="attached-image-title">{name}</h5>
Expand Down
37 changes: 0 additions & 37 deletions src/components/ProposalImages/test/ProposalImages.test.js

This file was deleted.

This file was deleted.

0 comments on commit 4e30dcd

Please sign in to comment.