From 67e62c0b25e991843043794770c02f9392d28fe1 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 21 Apr 2016 13:13:21 -0700 Subject: Add LinkRenderer (rewrite of Linker::link()) This is a rewrite of Linker::link() to a non-static, LinkTarget-based interface. Users of plain Linker::link() with no options can use the LinkRenderer instance provided by MediaWikiServices. Others that have specific options should create and configure their own instance, which can be used to create as many links as necessary. The main entrypoints for making links are: * ->makeLink( $target, $text, $attribs, $query ); * ->makeKnownLink( $target, $text, $attribs, $query ); * ->makeBrokenLink( $target, $text, $attribs, $query ); The order of the parameters are the same as Linker::link(), except $options are now part of the LinkRenderer instance, and known/broken status requires calling the function explicitly. Additionally, instead of passing in raw $html for the link text, the $text parameter will automatically be escaped unless it is specially marked as safe HTML using the MediaWiki\Linker\HtmlArmor class. The LinkBegin and LinkEnd hooks are now deprecated, but still function for backwards-compatability. Clients should migrate to the nearly- equivalent LinkRendererBegin and LinkRendererEnd hooks. The main differences between the hooks are: * Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker * Using LinkTarget instead of Title * Begin hook can no longer change known/broken status of link. Use the TitleIsAlwaysKnown hook for that. * $options are no longer passed, they can be read (but shouldn't be modified!) from the LinkRenderer object. Bug: T469 Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5 --- includes/libs/HtmlArmor.php | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 includes/libs/HtmlArmor.php (limited to 'includes/libs/HtmlArmor.php') diff --git a/includes/libs/HtmlArmor.php b/includes/libs/HtmlArmor.php new file mode 100644 index 000000000000..511e1c985a62 --- /dev/null +++ b/includes/libs/HtmlArmor.php @@ -0,0 +1,56 @@ + + */ + +/** + * Marks HTML that shouldn't be escaped + * + * @since 1.28 + */ +class HtmlArmor { + + /** + * @var string + */ + private $value; + + /** + * @param string $value + */ + public function __construct( $value ) { + $this->value = $value; + } + + /** + * Provide a string or HtmlArmor object + * and get safe HTML back + * + * @param string|HtmlArmor $input + * @return string safe for usage in HTML + */ + public static function getHtml( $input ) { + if ( $input instanceof HtmlArmor ) { + return $input->value; + } else { + return htmlspecialchars( $input ); + } + } +} -- cgit v1.2.3