findandreplacedomtext
Advanced tools
Comparing version 0.4.5 to 0.4.6
{ | ||
"name": "findandreplacedomtext", | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"main": "./src/findAndReplaceDOMText.js", | ||
@@ -5,0 +5,0 @@ "description": "findAndReplaceDOMText: DOM find/replace utility", |
@@ -265,2 +265,3 @@ # findAndReplaceDOMText | ||
* 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)). | ||
@@ -267,0 +268,0 @@ * 0.4.3 (28 Apr 2015): Add `preset:prose` and `forceContext` features. [See #29](https://github.com/padolsey/findAndReplaceDOMText/issues/29). |
/** | ||
* findAndReplaceDOMText v 0.4.5 | ||
* findAndReplaceDOMText v 0.4.6 | ||
* @author James Padolsey http://james.padolsey.com | ||
@@ -337,4 +337,5 @@ * @license http://unlicense.org/UNLICENSE | ||
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 = { | ||
@@ -344,4 +345,7 @@ node: curNode, | ||
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, | ||
@@ -464,3 +468,3 @@ isEnd: true | ||
default: | ||
replacement = match[+t]; | ||
replacement = match[+t] || ''; | ||
} | ||
@@ -467,0 +471,0 @@ return replacement; |
@@ -331,2 +331,13 @@ | ||
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'); | ||
@@ -484,1 +495,42 @@ | ||
}); | ||
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>'); | ||
}); |
109548
2979
304