Skip to content

Commit

Permalink
Split wrapElement into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck committed Nov 21, 2015
1 parent 3d35b4b commit d6cce14
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
29 changes: 1 addition & 28 deletions packages/live-resolver/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $ from 'jquery';
import insertLink from './insert-link.js';

function getKeywords(text, _regex) {
let regexList = _regex;
Expand All @@ -24,33 +24,6 @@ function getKeywords(text, _regex) {
return ret;
}

function insertLink(el, keywords, options, fromIndex = 0) {
let charIndex = fromIndex;

Array.prototype.forEach.call(el.childNodes, (child) => {
if (child.childElementCount) {
charIndex = insertLink(child, keywords, options, charIndex);
} else {
const $el = $(child);
const keyword = keywords[charIndex];

if (keyword) {
let attr = '';
if (options.debug) {
attr = ' class="ghl-link--debug-mode"';
}

const linkElement = `<a${attr}>`;
$el.wrap(linkElement);
}

charIndex += child.textContent.length;
}
});

return charIndex;
}

function blober(blob, options) {
blob.lines.forEach((item) => {
const keywords = getKeywords(item.text, options.regex);
Expand Down
36 changes: 36 additions & 0 deletions packages/live-resolver/insert-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import $ from 'jquery';

function wrapElement(child, keyword, options) {
const $el = $(child);
let attr = '';
if (options.debug) {
attr = ' class="ghl-link--debug-mode"';
}

const linkElement = `<a${attr}>`;
$el.wrap(linkElement);
}

function insertLink(el, keywords, options = {debug: false}, fromIndex = 0) {
let charIndex = fromIndex;

Array.prototype.forEach.call(el.childNodes, (child) => {
if (child.childElementCount) {
charIndex = insertLink(child, keywords, options, charIndex);
} else {
const keyword = keywords[charIndex];

if (keyword) {
wrapElement(child, keywords, options);
}

charIndex += child.textContent.length;
}
});

return charIndex;
}

export {
insertLink as default,
};
31 changes: 31 additions & 0 deletions packages/live-resolver/test/insert-link.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import assert from 'assert';
import insertLink from '../insert-link.js';

describe('insert-link', () => {
it('it wraps the elements based on their char position which is specified in the kewords map', () => {
const input = 'foo <span>foo</span> <span>foo</span>';
const expected = 'foo <a><span>foo</span></a> <a><span>foo</span></a>';
const keywords = {
4: 'foo',
8: 'foo',
};

const el = document.createElement('div');
el.innerHTML = input;
insertLink(el, keywords);
assert.equal(el.innerHTML, expected);
});

it('adds the cssClass for the debug mode', () => {
const input = 'foo <span>foo</span>';
const expected = 'foo <a class="ghl-link--debug-mode"><span>foo</span></a>';
const keywords = {
4: 'foo',
};

const el = document.createElement('div');
el.innerHTML = input;
insertLink(el, keywords, {debug: true});
assert.equal(el.innerHTML, expected);
});
});

0 comments on commit d6cce14

Please sign in to comment.