Skip to content

Commit bb7fd38

Browse files
author
Vadim Kazakov
committed
correctly treating html character entities
any html character entities following a url are now not being treated like part of the query string
1 parent 8f509f2 commit bb7fd38

File tree

4 files changed

+102
-45
lines changed

4 files changed

+102
-45
lines changed

dist/Autolinker.js

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@
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+
htmlCharacterEntities: /( | |<|<|>|>|&|&)/i,
163171

164172
/**
165173
* @private
@@ -330,7 +338,11 @@
330338
inBetweenTagsText,
331339
lastIndex = 0,
332340
anchorTagStackCount = 0,
333-
resultHtml = [];
341+
resultHtml = [],
342+
htmlCharacterEntities = this.htmlCharacterEntities,
343+
unescapedText,
344+
textToProcess,
345+
i;
334346

335347
while( ( currentResult = htmlRegex.exec( html ) ) !== null ) {
336348
var tagText = currentResult[ 0 ],
@@ -339,36 +351,49 @@
339351

340352
inBetweenTagsText = html.substring( lastIndex, currentResult.index );
341353
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
354+
355+
//split at html entities
356+
unescapedText = inBetweenTagsText.split( htmlCharacterEntities );
357+
358+
for ( i = 0; i < unescapedText.length; i++ ) {
359+
textToProcess = unescapedText[i];
360+
361+
// Process around anchor tags, and any inner text / html they may have
362+
if( tagName === 'a' ) {
363+
if( !isClosingTag ) { // its the start <a> tag
364+
anchorTagStackCount++;
365+
resultHtml.push( this.processTextNode( textToProcess ) );
366+
367+
} else { // its the end </a> tag
368+
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
369+
if( anchorTagStackCount === 0 ) {
370+
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
371+
}
353372
}
373+
374+
} else if( anchorTagStackCount === 0 ) { // not within an anchor tag, link the "in between" text
375+
resultHtml.push( this.processTextNode( textToProcess ) );
376+
377+
} else {
378+
// if we have a tag that is in between anchor tags (ex: <a href="..."><b>google.com</b></a>),
379+
// just append the inner text
380+
resultHtml.push( textToProcess );
354381
}
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 );
363382
}
364383

365384
resultHtml.push( tagText ); // now add the text of the tag itself verbatim
366385
}
367386

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

374399
return resultHtml.join( "" );

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)