@@ -6,6 +6,19 @@ import {tokenize, options} from './linkify';
66
77const HTML_NODE = 1 , TXT_NODE = 3 ;
88
9+ /**
10+ Given a parent element and child node that the parent contains, replaces
11+ that child with the given aary of new children
12+ */
13+ function replaceChildWithChildren ( parent , oldChild , newChildren ) {
14+ let lastNewChild = newChildren [ newChildren . length - 1 ] ;
15+ parent . replaceChild ( lastNewChild , oldChild ) ;
16+ for ( let i = newChildren . length - 2 ; i >= 0 ; i -- ) {
17+ parent . insertBefore ( newChildren [ i ] , lastNewChild ) ;
18+ lastNewChild = newChildren [ i ] ;
19+ }
20+ }
21+
922/**
1023 Given an array of MultiTokens, return an array of Nodes that are either
1124 (a) Plain Text nodes (node type 3)
@@ -22,14 +35,14 @@ function tokensToNodes(tokens, opts, doc) {
2235 if ( token . isLink ) {
2336
2437 let
25- tagName = options . resolve ( opts . tagName , token . type ) ,
26- linkClass = options . resolve ( opts . linkClass , token . type ) ,
27- target = options . resolve ( opts . target , token . type ) ,
28- formatted = options . resolve ( opts . format , token . toString ( ) , token . type ) ,
2938 href = token . toHref ( opts . defaultProtocol ) ,
39+ formatted = options . resolve ( opts . format , token . toString ( ) , token . type ) ,
3040 formattedHref = options . resolve ( opts . formatHref , href , token . type ) ,
31- attributesHash = options . resolve ( opts . attributes , token . type ) ,
32- events = options . resolve ( opts . events , token . type ) ;
41+ attributesHash = options . resolve ( opts . attributes , href , token . type ) ,
42+ tagName = options . resolve ( opts . tagName , href , token . type ) ,
43+ linkClass = options . resolve ( opts . linkClass , href , token . type ) ,
44+ target = options . resolve ( opts . target , href , token . type ) ,
45+ events = options . resolve ( opts . events , href , token . type ) ;
3346
3447 // Build the link
3548 let link = doc . createElement ( tagName ) ;
@@ -41,13 +54,13 @@ function tokensToNodes(tokens, opts, doc) {
4154
4255 // Build up additional attributes
4356 if ( attributesHash ) {
44- for ( let attr in attributesHash ) {
57+ for ( var attr in attributesHash ) {
4558 link . setAttribute ( attr , attributesHash [ attr ] ) ;
4659 }
4760 }
4861
4962 if ( events ) {
50- for ( let event in events ) {
63+ for ( var event in events ) {
5164 if ( link . addEventListener ) {
5265 link . addEventListener ( event , events [ event ] ) ;
5366 } else if ( link . attachEvent ) {
@@ -78,7 +91,7 @@ function linkifyElementHelper(element, opts, doc) {
7891 }
7992
8093 // Is this element already a link?
81- if ( element . tagName . toLowerCase ( ) === 'a ' /*|| element.hasClass('linkified')*/ ) {
94+ if ( element . tagName === 'A ' /*|| element.hasClass('linkified')*/ ) {
8295 // No need to linkify
8396 return element ;
8497 }
@@ -97,8 +110,14 @@ function linkifyElementHelper(element, opts, doc) {
97110
98111 let
99112 str = childElement . nodeValue ,
100- tokens = tokenize ( str ) ;
101- children . push ( ...tokensToNodes ( tokens , opts , doc ) ) ;
113+ tokens = tokenize ( str ) ,
114+ nodes = tokensToNodes ( tokens , opts , doc ) ;
115+
116+ // Swap out the current child for the set of nodes
117+ replaceChildWithChildren ( element , childElement , nodes ) ;
118+
119+ // so that the correct sibling is selected
120+ childElement = nodes [ nodes . length - 1 ] ;
102121
103122 break ;
104123
@@ -108,16 +127,6 @@ function linkifyElementHelper(element, opts, doc) {
108127 childElement = childElement . nextSibling ;
109128 }
110129
111- // Clear out the element
112- while ( element . firstChild ) {
113- element . removeChild ( element . firstChild ) ;
114- }
115-
116- // Replace with all the new nodes
117- for ( let i = 0 ; i < children . length ; i ++ ) {
118- element . appendChild ( children [ i ] ) ;
119- }
120-
121130 return element ;
122131}
123132
0 commit comments