Skip to content

Commit

Permalink
Fixes for new linkify-element refactor
Browse files Browse the repository at this point in the history
Including proper indexing and childElement selection.
  • Loading branch information
nfrasser committed Jun 19, 2015
1 parent 7802955 commit d1605b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### v2.0.0

* New link-detection technique based on lexicographical analysis via two-stage scanner - essentially regexp with more flexibility.
* Faster, less destructive DOM manipulation.
* Node.js API via `var linkify = require('linkifyjs');`
* Internal plugin system so you can require only features you need. e.g., `require('linkifyjs/plugins/hashtag')(linkify);`
* Browser modules (Browserify, AMD)
Expand Down
18 changes: 13 additions & 5 deletions src/linkify-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import {tokenize, options} from './linkify';

const HTML_NODE = 1, TXT_NODE = 3;

/**
Given a parent element and child node that the parent contains, replaces
that child with the given aary of new children
*/
function replaceChildWithChildren(parent, oldChild, newChildren) {
let lastNewChild = newChildren[newChildren.length - 1];
parent.replaceChild(lastNewChild, oldChild);
debugger;
for (let i = newChildren.length - 2; i >= 0; i++) {
for (let i = newChildren.length - 2; i >= 0; i--) {
parent.insertBefore(newChildren[i], lastNewChild);
lastNewChild = newChildren[i];
}
}

Expand Down Expand Up @@ -50,13 +54,13 @@ function tokensToNodes(tokens, opts, doc) {

// Build up additional attributes
if (attributesHash) {
for (let attr in attributesHash) {
for (var attr in attributesHash) {
link.setAttribute(attr, attributesHash[attr]);
}
}

if (events) {
for (let event in events) {
for (var event in events) {
if (link.addEventListener) {
link.addEventListener(event, events[event]);
} else if (link.attachEvent) {
Expand Down Expand Up @@ -87,7 +91,7 @@ function linkifyElementHelper(element, opts, doc) {
}

// Is this element already a link?
if (element.tagName.toLowerCase() === 'a' /*|| element.hasClass('linkified')*/) {
if (element.tagName === 'A' /*|| element.hasClass('linkified')*/) {
// No need to linkify
return element;
}
Expand All @@ -109,8 +113,12 @@ function linkifyElementHelper(element, opts, doc) {
tokens = tokenize(str),
nodes = tokensToNodes(tokens, opts, doc);

// Swap out the current child for the set of nodes
replaceChildWithChildren(element, childElement, nodes);

// so that the correct sibling is selected
childElement = nodes[nodes.length - 1];

break;

default: children.push(childElement); break;
Expand Down

0 comments on commit d1605b7

Please sign in to comment.