mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-23 12:44:30 +00:00
Move sanitizeText and stripTags from press this to wp.sanitize.
Introduce the `wp.sanitize` namespace and add two helpers for text sanitization. `stripTags` strips HTML tags from a string using regex. Fixes #40635. git-svn-id: https://develop.svn.wordpress.org/trunk@41061 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
$window = $( window ),
|
||||
$document = $( document ),
|
||||
saveAlert = false,
|
||||
textarea = document.createElement( 'textarea' ),
|
||||
sidebarIsOpen = false,
|
||||
settings = window.wpPressThisConfig || {},
|
||||
data = window.wpPressThisData || {},
|
||||
@@ -55,38 +54,6 @@
|
||||
return key || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips HTML tags
|
||||
*
|
||||
* @param string string Text to have the HTML tags striped out of.
|
||||
* @returns string Stripped text.
|
||||
*/
|
||||
function stripTags( string ) {
|
||||
string = string || '';
|
||||
|
||||
return string
|
||||
.replace( /<!--[\s\S]*?(-->|$)/g, '' )
|
||||
.replace( /<(script|style)[^>]*>[\s\S]*?(<\/\1>|$)/ig, '' )
|
||||
.replace( /<\/?[a-z][\s\S]*?(>|$)/ig, '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip HTML tags and convert HTML entities.
|
||||
*
|
||||
* @param text string Text.
|
||||
* @returns string Sanitized text.
|
||||
*/
|
||||
function sanitizeText( text ) {
|
||||
var _text = stripTags( text );
|
||||
|
||||
try {
|
||||
textarea.innerHTML = _text;
|
||||
_text = stripTags( textarea.value );
|
||||
} catch ( er ) {}
|
||||
|
||||
return _text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow only HTTP or protocol relative URLs.
|
||||
*
|
||||
@@ -97,7 +64,7 @@
|
||||
url = $.trim( url || '' );
|
||||
|
||||
if ( /^(?:https?:)?\/\//.test( url ) ) {
|
||||
url = stripTags( url );
|
||||
url = wp.sanitize.stripTags( url );
|
||||
return url.replace( /["\\]+/g, '' );
|
||||
}
|
||||
|
||||
@@ -224,7 +191,7 @@
|
||||
$image.replaceWith( $( '<span>' ).text( $image.attr( 'alt' ) ) );
|
||||
});
|
||||
|
||||
return sanitizeText( $element.text() );
|
||||
return wp.sanitize.sanitizeText( $element.text() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user