Dont't add empty nodes

This commit is contained in:
Stefan Parviainen 2017-11-14 19:33:12 +01:00
parent 2acd42e7c5
commit 772550a24a

View file

@ -139,24 +139,34 @@ export function replaceByRegexes(text, mapping) {
// Return the raw translation before the *match* followed by the return value of sub() followed // Return the raw translation before the *match* followed by the return value of sub() followed
// by the raw translation after the *match* (not captured group). // by the raw translation after the *match* (not captured group).
output.push(inputText.substr(0, match.index));
let toPush; const head = inputText.substr(0, match.index);
// If substitution is a function, call it if (head !== '') { // Don't push empty nodes, they are of no use
if(mapping[regexpString] instanceof Function) { output.push(head);
toPush = mapping[regexpString].apply(null, capturedGroups);
} else {
toPush = mapping[regexpString];
} }
output.push(toPush); let replaced;
// If substitution is a function, call it
if(mapping[regexpString] instanceof Function) {
replaced = mapping[regexpString].apply(null, capturedGroups);
} else {
replaced = mapping[regexpString];
}
// Check if we need to wrap the output into a span at the end // Here we also need to check that it actually is a string before comparing against one
if(typeof toPush === 'object') { // The head and tail are always strings
if (typeof replaced !== 'string' || replaced !== '') {
output.push(replaced);
}
if(typeof replaced === 'object') {
wrap = true; wrap = true;
} }
output.push(inputText.substr(match.index + match[0].length)); const tail = inputText.substr(match.index + match[0].length);
if (tail !== '') {
output.push(tail);
}
} }
if(wrap) { if(wrap) {