Skip to content

Commit

Permalink
Merge pull request #181 from xwp/fix/lint-fixes
Browse files Browse the repository at this point in the history
Fix lint issues to meet WordPressVIPMinimum standard
  • Loading branch information
spacedmonkey committed Jun 26, 2020
2 parents 2159074 + fc2ee90 commit cff27a8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
23 changes: 16 additions & 7 deletions assets/src/media-selector/views/button.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import DOMPurify from 'dompurify';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -44,16 +49,20 @@ const Button = wp.media.view.Button.extend( {
spinner.hide();
/* istanbul ignore next */
if ( error && error.responseJSON && error.responseJSON.message ) {
const message = error.responseJSON.message.replace(
/(<([^>]+)>)/gi,
''
);
const message = DOMPurify.sanitize( error.responseJSON.message, {
ALLOWED_TAGS: [], // strip all HTML tags.
} );
console.error( message ); // eslint-disable-line
alert( message ); // eslint-disable-line
} else {
const errors = getConfig( 'errors' );
console.error( errors.generic ); // eslint-disable-line
alert( errors.generic ); // eslint-disable-line
const genericError = DOMPurify.sanitize(
getConfig( 'errors' ).generic,
{
ALLOWED_TAGS: [], // strip all HTML tags.
}
);
console.error( genericError ); // eslint-disable-line
alert( genericError ); // eslint-disable-line
}
} );
},
Expand Down
7 changes: 6 additions & 1 deletion assets/src/media-selector/views/image-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ const ImageView = wp.media.view.Attachment.extend( {

this.views.detach();

this.$el.html( this.template( options ) );
/**
* Whitelist because this is using the WP core `tmpl-attachment` Backbone template.
*
* @see https://github.com/WordPress/WordPress/blob/5.4-branch/wp-includes/media-template.php#L536
*/
this.$el.html( this.template( options ) ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
const img = this.$el.find( '.centered img' );
if (
1 === img.length &&
Expand Down
16 changes: 14 additions & 2 deletions assets/src/media-selector/views/images-browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import DOMPurify from 'dompurify';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -133,7 +138,12 @@ const ImagesBrowser = wp.media.view.AttachmentsBrowser.extend( {
} );

this.attachmentsNoResults.$el.addClass( 'hidden no-media' );
this.attachmentsNoResults.$el.append( `<h2>${ noResults.noMedia }</h2>` );

const noMedia = document.createElement( 'h2' );
noMedia.textContent = noResults.noMedia;

// Whitelist because of how the element is built. See above.
this.attachmentsNoResults.$el.append( noMedia ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.append

this.views.add( this.attachmentsNoResults );

Expand Down Expand Up @@ -178,7 +188,9 @@ const ImagesBrowser = wp.media.view.AttachmentsBrowser.extend( {
this.collection.respErrorMessage()
) {
const error = this.collection.respErrorMessage();
errorView.$el.html( error.message );

// Whitelist because the HTML is sanitized.
errorView.$el.html( DOMPurify.sanitize( error.message ) ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
if ( [ 401, 403 ].includes( error.data?.status ) ) {
errorView.$el.removeClass( 'notice-error' );
errorView.$el.addClass( 'notice-warning' );
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"cross-env": "7.0.0",
"css-loader": "3.4.2",
"cssnano": "4.1.10",
"dompurify": "^2.0.12",
"dotenv": "8.2.0",
"eslint": "6.8.0",
"eslint-plugin-eslint-comments": "3.1.2",
Expand Down

0 comments on commit cff27a8

Please sign in to comment.