define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function highlightSearch(element, query, ignoreCase, type) {
var isHtmlElement = /<[^>]*>/g.test(element.innerText);
if (isHtmlElement) {
element.innerText = element.innerText.replace(/[\u00A0-\u9999<>&]/g, function (match) { return "&#" + match.charCodeAt(0) + ";"; });
}
if (query === '') {
return;
}
else {
var ignoreRegex = ignoreCase ? 'gim' : 'gm';
query = /^[a-zA-Z0-9- ]*$/.test(query) ? query : query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
var replaceQuery = type === 'StartsWith' ? '^(' + query + ')' : type === 'EndsWith' ?
'(' + query + ')$' : '(' + query + ')';
findTextNode(element, new RegExp(replaceQuery, ignoreRegex));
}
}
exports.highlightSearch = highlightSearch;
function findTextNode(element, pattern) {
for (var index = 0; element.childNodes && (index < element.childNodes.length); index++) {
if (element.childNodes[index].nodeType === 3 && element.childNodes[index].textContent.trim() !== '') {
var value = element.childNodes[index].nodeValue.trim().replace(pattern, '<span class="e-highlight">$1</span>');
element.childNodes[index].nodeValue = '';
element.innerHTML = element.innerHTML.trim() + value;
break;
}
else {
findTextNode(element.childNodes[index], pattern);
}
}
}
function revertHighlightSearch(content) {
var contentElement = content.querySelectorAll('.e-highlight');
for (var i = contentElement.length - 1; i >= 0; i--) {
var parent_1 = contentElement[i].parentNode;
var text = document.createTextNode(contentElement[i].textContent);
parent_1.replaceChild(text, contentElement[i]);
}
}
exports.revertHighlightSearch = revertHighlightSearch;
});
|