Manual:HtmlArmor

From MediaWiki.org
Jump to navigation Jump to search

Other languages:
English • ‎español • ‎日本語
MediaWiki version: 1.28
Gerrit change 284750

HtmlArmor is a small utility class for situations where you want to accept a parameter of text that is normally escaped, but in some cases needs to accept raw HTML.

Usage[edit]

/**
 * @param string|HtmlArmor $text
 * @return string
 */
function fooBar( $text ) {
	$html = HtmlArmor::getHtml( $text );
	return "<stuff>$html</stuff>";
}

In the above example, the HtmlArmor parameter can either be a to-be-escaped string, or an HtmlArmor object that should remain the same. The HtmlArmor::getHtml() function will take care of the escaping for you, providing you with content you know that is safe HTML.

fooBar( "this will be <b>escaped!</b>" );
// <stuff>this will be &lt;b&gt;escaped!&lt;/b&gt;</stuff>

fooBar( new HtmlArmor( "this will <i>not</i> be <b>escaped!</b>" ) );
// <stuff>this will <i>not</i> be <b>escaped!</b></stuff>"