Skip to content

Commit 311da73

Browse files
author
nfrasser
committed
Merge branch 'master' of https://github.com/SoapBox/linkifyjs
2 parents 09ed06b + d59985d commit 311da73

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### v2.0.0
44

55
* New link-detection technique based on lexicographical analysis via two-stage scanner - essentially regexp with more flexibility.
6+
* Faster, less destructive DOM manipulation.
67
* Node.js API via `var linkify = require('linkifyjs');`
78
* Internal plugin system so you can require only features you need. e.g., `require('linkifyjs/plugins/hashtag')(linkify);`
89
* Browser modules (Browserify, AMD)

src/linkify-element.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ import {tokenize, options} from './linkify';
66

77
const 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

src/linkify-string.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ function linkifyStr(str, opts={}) {
4040
if (token.isLink) {
4141

4242
let
43-
tagName = options.resolve(opts.tagName, token.type),
44-
linkClass = options.resolve(opts.linkClass, token.type),
45-
target = options.resolve(opts.target, token.type),
46-
formatted = options.resolve(opts.format, token.toString(), token.type),
4743
href = token.toHref(opts.defaultProtocol),
44+
formatted = options.resolve(opts.format, token.toString(), token.type),
4845
formattedHref = options.resolve(opts.formatHref, href, token.type),
49-
attributesHash = options.resolve(opts.attributes, token.type);
46+
attributesHash = options.resolve(opts.attributes, href, token.type),
47+
tagName = options.resolve(opts.tagName, href, token.type),
48+
linkClass = options.resolve(opts.linkClass, href, token.type),
49+
target = options.resolve(opts.target, href, token.type);
5050

51-
let link = `<${tagName} href="${cleanAttr(formattedHref)}" class="${linkClass}"`;
51+
let link = `<${tagName} href="${cleanAttr(formattedHref)}" class="${cleanAttr(linkClass)}"`;
5252
if (target) {
53-
link += ` target="${target}"`;
53+
link += ` target="${cleanAttr(target)}"`;
5454
}
5555

5656
if (attributesHash) {

src/linkify/utils/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function noop(val) {
22
return val;
33
}
44

5-
function typeToTarget(type) {
5+
function typeToTarget(href, type) {
66
return type === 'url' ? '_blank' : null;
77
}
88

0 commit comments

Comments
 (0)