Skip to content

Commit

Permalink
Fix #60 and #70
Browse files Browse the repository at this point in the history
  • Loading branch information
padolsey committed Dec 5, 2017
1 parent 2ad352a commit 59e642c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "findandreplacedomtext",
"version": "0.4.5",
"version": "0.4.6",
"main": "./src/findAndReplaceDOMText.js",
"description": "findAndReplaceDOMText: DOM find/replace utility",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ exposed.PRESETS = {

### Changelog

* 0.4.6 (5 Dec 2017): Fix indexInMatch ([See #60](https://github.com/padolsey/findAndReplaceDOMText/issues/60)). Fix undefined being echoed in optional+empty capture groups ([See #70](https://github.com/padolsey/findAndReplaceDOMText/issues/70)).
* 0.4.4 (4 May 2015): Remove duplicate key from `NON_CONTIGUOUS_PROSE_ELEMENTS`. Expose library via UMD ([See #32](https://github.com/padolsey/findAndReplaceDOMText/issues/32)).
* 0.4.3 (28 Apr 2015): Add `preset:prose` and `forceContext` features. [See #29](https://github.com/padolsey/findAndReplaceDOMText/issues/29).
* 0.4.2 (30 Mar 2014): Fix IE to avoid incorrectly-closed-tags issue ([see #20](https://github.com/padolsey/findAndReplaceDOMText/issues/20)). Thanks to [shauryamal](https://github.com/shauryamal)!
Expand Down
14 changes: 9 additions & 5 deletions src/findAndReplaceDOMText.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* findAndReplaceDOMText v 0.4.5
* findAndReplaceDOMText v 0.4.6
* @author James Padolsey http://james.padolsey.com
* @license http://unlicense.org/UNLICENSE
*
Expand Down Expand Up @@ -334,14 +334,18 @@
if (curNode.nodeType === Node.TEXT_NODE) {

if (!endPortion && curNode.length + atIndex >= match.endIndex) {

// We've found the ending
// (Note that, in the case of a single portion, it'll be an
// endPortion, not a startPortion.)
endPortion = {
node: curNode,
index: portionIndex++,
text: curNode.data.substring(match.startIndex - atIndex, match.endIndex - atIndex),
indexInMatch: atIndex - match.startIndex,
indexInNode: match.startIndex - atIndex, // always zero for end-portions

// If it's the first match (atIndex==0) we should just return 0
indexInMatch: atIndex === 0 ? 0 : atIndex - match.startIndex,

indexInNode: match.startIndex - atIndex,
endIndexInNode: match.endIndex - atIndex,
isEnd: true
};
Expand Down Expand Up @@ -459,7 +463,7 @@
replacement = match.input.substring(match.endIndex);
break;
default:
replacement = match[+t];
replacement = match[+t] || '';
}
return replacement;
});
Expand Down
52 changes: 52 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ test('Right (\')', function() {
htmlEqual(d.innerHTML, 'this is [ test] test');
});

test('Empty captured groups', function() {
var d = document.createElement('div');
d.innerHTML = '111333';
findAndReplaceDOMText(d, {
find: /(1+)(\s+)?(3+)/g,
replace: '$3$2$1'
});
// $2 is empty, so should be replaced by nothing (empty string):
htmlEqual(d.innerHTML, '333111');
});

module('Filtering');

test('Element filtering', function() {
Expand Down Expand Up @@ -480,3 +491,44 @@ test('prose', function() {
');

});

module('indexInMatch');

test('Single portion', function() {
var d = document.createElement('div');
d.innerHTML = '___AAAAA';
// ^ 0
findAndReplaceDOMText(d, {
find: /A+/g,
replace: function(portion) {
return portion.indexInMatch;
}
});
htmlEqual(d.innerHTML, '___0');
});

test('Two portions', function() {
var d = document.createElement('div');
d.innerHTML = '___AAA<em>AA</em>';
// ^ 0 ^ 3
findAndReplaceDOMText(d, {
find: /A+/g,
replace: function(portion) {
return portion.indexInMatch;
}
});
htmlEqual(d.innerHTML, '___0<em>3</em>');
});

test('>Two portions', function() {
var d = document.createElement('div');
d.innerHTML = '___AA<em>A</em>A<u>A</u>';
// ^ 0 ^ 2 ^ 3 ^ 4
findAndReplaceDOMText(d, {
find: /A+/g,
replace: function(portion) {
return portion.indexInMatch;
}
});
htmlEqual(d.innerHTML, '___0<em>2</em>3<u>4</u>');
});

0 comments on commit 59e642c

Please sign in to comment.