Opened 16 years ago

Closed 15 years ago

#105 closed defect (fixed)

Incorrect regular expression in fixRelativeLinks function invalidates SelfNamedAnchors fix

Reported by: egk10@… Owned by: gogo
Priority: normal Milestone: Version 1.0
Component: Xinha Core Version: trunk
Severity: normal Keywords:


The fixRelativeLinks function of htmlarea.js contains an incorrect regular expression which causes only the first instance of a self-named-anchor (e.g. <a href="#mybookmark">) to be 'fixed' when stripSelfNamedAnchors is set. The incorrect code is:

var stripRe = new RegExp?(document.location.href.replace HTMLArea.RE_Specials, '
$1') + '(#.*)', 'g');

The problem is that (#.*) matches the first anchor plus ALL CHARACTERS (.) to the end of the document (or the next newline)! Hence, any subsequent anchors are ignored. The correct parenthetical expression should be (#[\'\"]*), which will match from the # of the first anchor up to and not including the next single or double quote mark. I have verified this fix.
However, there is a further problem with the fixRelativeLinks function running on IE WinXP. IE prepends only the protocol and hostname to self-named anchors, so instead of reading href="#mybookmark", we get href="", which is usually nonsense. The fixRelativeLinks function doesn't search for this instance. The following correction to the first if clause of the function works for me IF I SET config.baseHref to, but has not been tested on Firefox:

HTMLArea.prototype.fixRelativeLinks = function(html)

if(typeof this.config.stripSelfNamedAnchors != 'undefined' && this.config.stripSelfNamedAnchors)

var stripRe = null;
if(typeof this.config.baseHref != 'undefined' && this.config.baseHref != null)

stripRe = new RegExp?(this.config.baseHref.replace(HTMLArea.RE_Specials, '
$1') + '(#[\'\"]*)', 'g');


stripRe = new RegExp?(document.location.href.replace(HTMLArea.RE_Specials, '
$1') + '(#[\'\"]*)', 'g');

html = html.replace(stripRe, '$1');


Change History (2)

comment:1 Changed 15 years ago by gogo

  • Milestone set to Version 1.0
  • Version set to trunk

comment:2 Changed 15 years ago by gogo

  • Resolution set to fixed
  • Status changed from new to closed

The regexp is fixed in changeset:263 but I suspect that you didn't have stripBaseHref enabled in your config which would have taken care of the second part of your ticket (which is thus not required).

Note: See TracTickets for help on using tickets.