Welcome! » Log In » Create A New Profile

Relative Links & HTML.TargetBlank

Posted by laurin1 
Relative Links & HTML.TargetBlank
January 09, 2017 12:30PM

So this does not work with relative links? Seems like I've asked this question before, but can't find it. I even tried changing these to absolute links:

URI.Base = "http://mytestdomain URI.MakeAbsolute = true

That part works, the links are now absolute, but the target="_blank" is still not set.

Re: Relative Links & HTML.TargetBlank
January 10, 2017 02:07PM

I guess the trouble is probably the TargetBlank gets applied before we make URLs absolute, so there's an ordering problem. There isn't any way to affect the ordering but you could take library/HTMLPurifier/AttrTransform/TargetBlank.php and make your own version which unconditionally applies the target attribute.

Re: Relative Links & HTML.TargetBlank
January 27, 2017 04:18PM

That would require forking the repo on GitHub and creating a Packagist alternate package as well. Lot of a work for this one issue. I used to fix the target blank issue myself, before I used HTMLPurifier. I might have to go doing that part myself.

Re: Relative Links & HTML.TargetBlank
January 27, 2017 04:23PM

Well, if you want to submit your change as a patch, I'd be happy to take that too :)

Re: Relative Links & HTML.TargetBlank
January 27, 2017 04:45PM

I went the other direction - easier to just add code to force absolute URLs before run the purifier.

	 * @param string $sHTML
	 * @return string
	private function getHTMLWithRelativeLinksMadeAbsolute(string $sHTML){

		preg_match_all('/"(\\/.*?\.(?:php|htm|html|txt).*?)"/', $sHTML, $aMatches);

			return $sHTML;

		$sReturn = $sHTML;

		foreach($aMatches[1] as $sMatch){

			$sNewLink = _SERVER_HTTP.$sMatch;
			$sReturn  = str_replace("\"".$sMatch."\"", "\"".$sNewLink."\"", $sReturn);


		return $sReturn;

Re: Relative Links & HTML.TargetBlank
January 28, 2017 08:25AM

Well, if you want to submit your change as a patch, I'd be happy to take that too :)

I didn't see this until after my last post. I'll try to get this done - what would you prefer? Add a config option (like URI.MakeAbsoluteAll - to include relative) or some other method?

Re: Relative Links & HTML.TargetBlank
February 03, 2017 07:53PM

I'm not sure. Maybe the best thing to do is fix the ordering the problem, but that might be fragile. Alternately, a new option to unconditionally apply _blank to everything?

Your Email:


HTML input is enabled. Make sure you escape all HTML and angled brackets with < and >.

Auto-paragraphing is enabled. Double newlines will be converted to paragraphs; for single newlines, use the pre tag.

Allowed tags: a, abbr, acronym, b, blockquote, caption, cite, code, dd, del, dfn, div, dl, dt, em, i, ins, kbd, li, ol, p, pre, s, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, var.

For inputting literal code such as HTML and PHP for display, use CDATA tags to auto-escape your angled brackets, and pre to preserve newlines:

Place code here

Power users, you can hide this notice with:

.htmlpurifier-help {display:none;}