Skip to content

Commit 8a38854

Browse files
committed
Merge pull request #58 from yads/master
Correctly handling html character entities that follow a url
2 parents 8f509f2 + 3ec00ca commit 8a38854

File tree

5 files changed

+117
-47
lines changed

5 files changed

+117
-47
lines changed

dist/Autolinker.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@
160160
'>'
161161
].join( "" ), 'g' );
162162
} )(),
163+
164+
/**
165+
* @private
166+
* @property {RegExp} htmlCharacterEntities
167+
*
168+
* The regular expression that matches common HTML character entities.
169+
*
170+
* Ignoring & as it coule be part of a query string, handling it separately
171+
*/
172+
htmlCharacterEntities: /( | |<|<|>|>)/i,
163173

164174
/**
165175
* @private
@@ -330,7 +340,11 @@
330340
inBetweenTagsText,
331341
lastIndex = 0,
332342
anchorTagStackCount = 0,
333-
resultHtml = [];
343+
resultHtml = [],
344+
htmlCharacterEntities = this.htmlCharacterEntities,
345+
unescapedText,
346+
textToProcess,
347+
i;
334348

335349
while( ( currentResult = htmlRegex.exec( html ) ) !== null ) {
336350
var tagText = currentResult[ 0 ],
@@ -339,36 +353,49 @@
339353

340354
inBetweenTagsText = html.substring( lastIndex, currentResult.index );
341355
lastIndex = currentResult.index + tagText.length;
342-
343-
// Process around anchor tags, and any inner text / html they may have
344-
if( tagName === 'a' ) {
345-
if( !isClosingTag ) { // it's the start <a> tag
346-
anchorTagStackCount++;
347-
resultHtml.push( this.processTextNode( inBetweenTagsText ) );
348-
349-
} else { // it's the end </a> tag
350-
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
351-
if( anchorTagStackCount === 0 ) {
352-
resultHtml.push( inBetweenTagsText ); // We hit the matching </a> tag, simply add all of the text from the start <a> tag to the end </a> tag without linking it
356+
357+
//split at html entities
358+
unescapedText = inBetweenTagsText.split( htmlCharacterEntities );
359+
360+
for ( i = 0; i < unescapedText.length; i++ ) {
361+
textToProcess = unescapedText[i];
362+
363+
// Process around anchor tags, and any inner text / html they may have
364+
if( tagName === 'a' ) {
365+
if( !isClosingTag ) { // its the start <a> tag
366+
anchorTagStackCount++;
367+
resultHtml.push( this.processTextNode( textToProcess ) );
368+
369+
} else { // its the end </a> tag
370+
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
371+
if( anchorTagStackCount === 0 ) {
372+
resultHtml.push( textToProcess ); // We hit the matching </a> tag, simply add all of the text from the start <a> tag to the end </a> tag without linking it
373+
}
353374
}
375+
376+
} else if( anchorTagStackCount === 0 ) { // not within an anchor tag, link the "in between" text
377+
resultHtml.push( this.processTextNode( textToProcess ) );
378+
379+
} else {
380+
// if we have a tag that is in between anchor tags (ex: <a href="..."><b>google.com</b></a>),
381+
// just append the inner text
382+
resultHtml.push( textToProcess );
354383
}
355-
356-
} else if( anchorTagStackCount === 0 ) { // not within an anchor tag, link the "in between" text
357-
resultHtml.push( this.processTextNode( inBetweenTagsText ) );
358-
359-
} else {
360-
// if we have a tag that is in between anchor tags (ex: <a href="..."><b>google.com</b></a>),
361-
// just append the inner text
362-
resultHtml.push( inBetweenTagsText );
363384
}
364385

365386
resultHtml.push( tagText ); // now add the text of the tag itself verbatim
366387
}
367388

368389
// Process any remaining text after the last HTML element. Will process all of the text if there were no HTML elements.
369390
if( lastIndex < html.length ) {
370-
var processedTextNode = this.processTextNode( html.substring( lastIndex ) );
371-
resultHtml.push( processedTextNode );
391+
//split at html entities
392+
unescapedText = html.substring( lastIndex ).split( htmlCharacterEntities );
393+
394+
for ( i = 0; i < unescapedText.length; i++ ) {
395+
textToProcess = unescapedText[i];
396+
var processedTextNode = this.processTextNode( textToProcess );
397+
resultHtml.push( processedTextNode );
398+
}
372399
}
373400

374401
return resultHtml.join( "" );
@@ -993,7 +1020,8 @@
9931020
* @return {String}
9941021
*/
9951022
getAnchorHref : function() {
996-
return this.getUrl();
1023+
var url = this.getUrl();
1024+
return url.replace('&amp;', '&');
9971025
},
9981026

9991027

dist/Autolinker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)