Skip to content

Commit e30002a

Browse files
committed
Fix issue with unquoted anchor href's that contain an '=' sign (as part of the query string) causing the HTML tag to not properly parse
Fixes #253
1 parent 945b671 commit e30002a

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "autolinker",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Utility to automatically link the URLs, email addresses, phone numbers, hashtags, and mentions (Twitter, Instagram) in a given block of text/HTML",
55
"main": "./dist/commonjs/index.js",
66
"typings": "./dist/commonjs/index.d.ts",

src/htmlParser/parse-html.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function parseHtml( html: string, { onOpenTag, onCloseTag, onText, onComm
8181
var char = html.charAt( charIdx );
8282

8383
// For debugging: search for other "For debugging" lines
84+
// ALSO: Temporarily remove the 'const' keyword on the State enum
8485
// table.push(
8586
// [ charIdx, char, State[ state ], currentDataIdx, currentTag.idx, currentTag.idx === -1 ? '' : currentTag.type ]
8687
// );
@@ -113,6 +114,7 @@ export function parseHtml( html: string, { onOpenTag, onCloseTag, onText, onComm
113114
}
114115

115116
// For debugging: search for other "For debugging" lines
117+
// ALSO: Temporarily remove the 'const' keyword on the State enum
116118
// table.push(
117119
// [ charIdx, char, State[ state ], currentDataIdx, currentTag.idx, currentTag.idx === -1 ? '' : currentTag.type ]
118120
// );
@@ -125,7 +127,7 @@ export function parseHtml( html: string, { onOpenTag, onCloseTag, onText, onComm
125127
}
126128

127129
// For debugging: search for other "For debugging" lines
128-
//console.log( '\n' + table.toString() );
130+
// console.log( '\n' + table.toString() );
129131

130132

131133

@@ -356,12 +358,6 @@ export function parseHtml( html: string, { onOpenTag, onCloseTag, onText, onComm
356358
// start of another tag (ignore the previous, incomplete one)
357359
startNewTag();
358360

359-
} else if( quoteRe.test( char ) || /[=`]/.test( char ) ) {
360-
// "Parse error" characters that, according to the spec, should be
361-
// appended to the attribute value, but we'll treat these characters
362-
// as not forming a real HTML tag
363-
resetToDataState();
364-
365361
} else {
366362
// Any other character, treat it as part of the attribute value
367363
}

tests/autolinker.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ describe( "Autolinker", function() {
141141
} );
142142

143143

144+
it( "when unquoted anchor href's exist, should NOT automatically link the text inside", function() {
145+
let result = autolinker.link( '<p>Joe went to <a href=http://www.yahoo.com>yahoo</a></p>' );
146+
expect( result ).toBe( '<p>Joe went to <a href=http://www.yahoo.com>yahoo</a></p>' );
147+
148+
let result2 = autolinker.link( '<p>Joe went to <a href=http://www.yahoo.com?query=1>yahoo</a></p>' );
149+
expect( result2 ).toBe( '<p>Joe went to <a href=http://www.yahoo.com?query=1>yahoo</a></p>' );
150+
} );
151+
152+
144153
it( "should NOT automatically link URLs within self-closing tags", function() {
145154
let result = autolinker.link( 'Just a flower image <img src="https://farm9.staticflickr.com/8378/8578790632_83c6471f3f_b.jpg" />' );
146155
expect( result ).toBe( 'Just a flower image <img src="https://farm9.staticflickr.com/8378/8578790632_83c6471f3f_b.jpg" />' );

tests/htmlParser/parse-html.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ describe( "Autolinker.htmlParser.HtmlParser", () => {
136136
} );
137137

138138

139+
it( `should handle html tags with unqouted attributes`, () => {
140+
let nodes = parseHtmlAndCapture( `<div id=hi class=some-class align=center>Test</div>` );
141+
142+
expect( nodes ).toEqual( [
143+
{ type: 'openTag', tagName: 'div', offset: 0 },
144+
{ type: 'text', text: 'Test', offset: 41 },
145+
{ type: 'closeTag', tagName: 'div', offset: 45 }
146+
] );
147+
} );
148+
149+
150+
it( `should handle anchor tags with unqouted href's and query strings (Issue #263)`, () => {
151+
let nodes = parseHtmlAndCapture( `<a href=https://www.facebook.com/hashtag/muntcentrum?epa=HASHTAG>#Muntcentrum</a>` );
152+
153+
expect( nodes ).toEqual( [
154+
{ type: 'openTag', tagName: 'a', offset: 0 },
155+
{ type: 'text', text: '#Muntcentrum', offset: 65 },
156+
{ type: 'closeTag', tagName: 'a', offset: 77 }
157+
] );
158+
} );
159+
160+
139161
it( `when there are two << characters, the first one should be ignored
140162
if the second one forms a tag`,
141163
() => {

0 commit comments

Comments
 (0)