Skip to content

Commit

Permalink
simplified code, updated docs, prepared for releasing v2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
angezid committed Oct 10, 2023
1 parent 8d305dd commit bf6c3e4
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 258 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

### 2.3.0

* Added `preserveTerms` value for `separateWordSearch` option that allows highlight exact term(s) alone side with separate word(s).
* Added 'startsWith' value for accuracy option that allows highlight whole word(s) just typing the start of it(s). It also works with `preserveTerms`.

### 2.2.0

* Added ability to mark line ranges (`markRanges()` API with `markLines` option).
Expand Down
61 changes: 30 additions & 31 deletions dist/jquery.mark.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,26 +336,26 @@ class RegExpCreator {
}
checkWildcardsEscape(str) {
if (this.opt.wildcards !== 'disabled') {
str = str.replace(/(\\)*\?/g, (m, gr1) => gr1 ? '?' : '\u0001')
.replace(/(\\)*\*/g, (m, gr1) => gr1 ? '*' : '\u0002');
str = str.replace(/(\\)*\?/g, (m, gr1) => gr1 ? '?' : '\x01')
.replace(/(\\)*\*/g, (m, gr1) => gr1 ? '*' : '\x02');
}
return this.escape(str);
}
createWildcards(str) {
const spaces = this.opt.wildcards === 'withSpaces',
boundary = this.opt.blockElementsBoundary,
anyChar = spaces && boundary ? '[^' + (boundary.char ? boundary.char : '\x01') + ']*?' : '[\\S\\s]*?';
boundary = spaces && this.opt.acrossElements && this.opt.blockElementsBoundary,
anyChar = `[^${boundary ? boundary.char ? boundary.char.charAt(0) : '\x01' : ''}]*?`;
return str
.replace(/\u0001/g, spaces ? '[\\S\\s]?' : '\\S?')
.replace(/\u0002/g, spaces ? anyChar : '\\S*');
.replace(/\x01/g, spaces ? '[^]?' : '\\S?')
.replace(/\x02/g, spaces ? anyChar : '\\S*');
}
setupIgnoreJoiners(str) {
return str.replace(/(\(\?:|\|)|\\?.(?=([|)]|$)|.)/g, (m, gr1, gr2) => {
return gr1 || typeof gr2 !== 'undefined' ? m : m + '\u0000';
return gr1 || typeof gr2 !== 'undefined' ? m : m + '\x00';
});
}
createJoiners(str, joiners) {
return str.split(/\u0000+/).join(`[${joiners}]*`);
return str.split(/\x00+/).join(`[${joiners}]*`);
}
getJoinersPunctuation() {
let punct = this.preprocess(this.opt.ignorePunctuation),
Expand Down Expand Up @@ -422,7 +422,6 @@ class RegExpCreator {
class Mark {
constructor(ctx) {
this.ctx = ctx;
this.cacheDict = {};
this.nodeNames = ['script', 'style', 'title', 'head', 'html'];
}
set opt(val) {
Expand Down Expand Up @@ -477,22 +476,24 @@ class Mark {
}
});
}
checkOption(opt) {
let clear = true,
type = this.cacheDict.type;
if (type && opt && opt.cacheTextNodes) {
if (opt.acrossElements) {
if (type === 'across') {
checkOption(opt, del) {
this.opt = opt;
let dict = this.cacheDict,
clear = true;
if (dict) {
if ( !del && this.opt.cacheTextNodes) {
if (this.opt.acrossElements) {
if (dict.type === 'across') {
clear = false;
}
} else if (dict.type === 'every') {
clear = false;
}
} else if (type === 'every') {
clear = false;
}
if (clear) {
this.cacheDict = null;
}
}
if (clear) {
this.cacheDict = {};
}
return opt;
}
getSeachTerms(sv) {
const search = this.isString(sv) ? [sv] : sv,
Expand Down Expand Up @@ -578,7 +579,7 @@ class Mark {
tags['br'] = 3;
}
getTextNodesAcross(cb) {
if (this.opt.cacheTextNodes && this.cacheDict.nodes) {
if (this.opt.cacheTextNodes && this.cacheDict) {
this.cacheDict.lastIndex = 0;
this.cacheDict.lastTextIndex = 0;
cb(this.cacheDict);
Expand All @@ -595,9 +596,9 @@ class Mark {
map : 1, fieldset : 1, textarea : 1, track : 1, video : 1, audio : 1,
body : 1, iframe : 1, meter : 1, object : 1, svg : 1 };
const nodes = [],
boundary = this.opt.blockElementsBoundary;
boundary = this.opt.blockElementsBoundary,
priorityType = boundary ? 2 : 1;
let ch = '\x01',
priorityType = boundary ? 2 : 1,
tempType, type, prevNode;
if (boundary) {
this.setType(tags, boundary);
Expand Down Expand Up @@ -677,7 +678,7 @@ class Mark {
return this.createInfo(prevNode, obj.text.length, (obj.text += text).length - offset, offset, startOffset);
}
getTextNodes(cb) {
if (this.opt.cacheTextNodes && this.cacheDict.nodes) {
if (this.opt.cacheTextNodes && this.cacheDict) {
cb(this.cacheDict);
return;
}
Expand Down Expand Up @@ -1262,7 +1263,7 @@ class Mark {
}
}
markRegExp(regexp, opt) {
this.opt = this.checkOption(opt);
this.checkOption(opt);
let totalMarks = 0,
matchesSoFar = 0,
fn = this.opt.separateGroups ? 'wrapSeparateGroups' : 'wrapMatches';
Expand All @@ -1289,7 +1290,7 @@ class Mark {
});
}
mark(sv, opt) {
this.opt = this.checkOption(opt);
this.checkOption(opt);
if (this.opt.combinePatterns) {
this.markCombinePatterns(sv);
return;
Expand Down Expand Up @@ -1428,8 +1429,7 @@ class Mark {
return { patterns, termsParts : array };
}
markRanges(ranges, opt) {
this.opt = opt;
this.cacheDict = {};
this.checkOption(opt, true);
if (this.isArrayOfObjects(ranges)) {
let totalMarks = 0;
this.wrapRanges(ranges, (node, range, match, index) => {
Expand All @@ -1447,8 +1447,7 @@ class Mark {
}
}
unmark(opt) {
this.opt = opt;
this.cacheDict = {};
this.checkOption(opt, true);
let selector = (this.opt.element ? this.opt.element : 'mark') + '[data-markjs]';
if (this.opt.className) {
selector += `.${this.opt.className}`;
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.mark.es6.min.js

Large diffs are not rendered by default.

59 changes: 29 additions & 30 deletions dist/jquery.mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@
value: function checkWildcardsEscape(str) {
if (this.opt.wildcards !== 'disabled') {
str = str.replace(/(\\)*\?/g, function (m, gr1) {
return gr1 ? '?' : "\x01";
return gr1 ? '?' : '\x01';
}).replace(/(\\)*\*/g, function (m, gr1) {
return gr1 ? '*' : "\x02";
return gr1 ? '*' : '\x02';
});
}
return this.escape(str);
Expand All @@ -483,21 +483,21 @@
key: "createWildcards",
value: function createWildcards(str) {
var spaces = this.opt.wildcards === 'withSpaces',
boundary = this.opt.blockElementsBoundary,
anyChar = spaces && boundary ? '[^' + (boundary["char"] ? boundary["char"] : '\x01') + ']*?' : '[\\S\\s]*?';
return str.replace(/\u0001/g, spaces ? '[\\S\\s]?' : '\\S?').replace(/\u0002/g, spaces ? anyChar : '\\S*');
boundary = spaces && this.opt.acrossElements && this.opt.blockElementsBoundary,
anyChar = "[^".concat(boundary ? boundary["char"] ? boundary["char"].charAt(0) : '\x01' : '', "]*?");
return str.replace(/\x01/g, spaces ? '[^]?' : '\\S?').replace(/\x02/g, spaces ? anyChar : '\\S*');
}
}, {
key: "setupIgnoreJoiners",
value: function setupIgnoreJoiners(str) {
return str.replace(/(\(\?:|\|)|\\?.(?=([|)]|$)|.)/g, function (m, gr1, gr2) {
return gr1 || typeof gr2 !== 'undefined' ? m : m + "\0";
return gr1 || typeof gr2 !== 'undefined' ? m : m + '\x00';
});
}
}, {
key: "createJoiners",
value: function createJoiners(str, joiners) {
return str.split(/\u0000+/).join("[".concat(joiners, "]*"));
return str.split(/\x00+/).join("[".concat(joiners, "]*"));
}
}, {
key: "getJoinersPunctuation",
Expand Down Expand Up @@ -570,7 +570,6 @@
function Mark(ctx) {
_classCallCheck(this, Mark);
this.ctx = ctx;
this.cacheDict = {};
this.nodeNames = ['script', 'style', 'title', 'head', 'html'];
}
_createClass(Mark, [{
Expand Down Expand Up @@ -641,22 +640,24 @@
}
}, {
key: "checkOption",
value: function checkOption(opt) {
var clear = true,
type = this.cacheDict.type;
if (type && opt && opt.cacheTextNodes) {
if (opt.acrossElements) {
if (type === 'across') {
value: function checkOption(opt, del) {
this.opt = opt;
var dict = this.cacheDict,
clear = true;
if (dict) {
if (!del && this.opt.cacheTextNodes) {
if (this.opt.acrossElements) {
if (dict.type === 'across') {
clear = false;
}
} else if (dict.type === 'every') {
clear = false;
}
} else if (type === 'every') {
clear = false;
}
if (clear) {
this.cacheDict = null;
}
}
if (clear) {
this.cacheDict = {};
}
return opt;
}
}, {
key: "getSeachTerms",
Expand Down Expand Up @@ -780,7 +781,7 @@
key: "getTextNodesAcross",
value: function getTextNodesAcross(cb) {
var _this4 = this;
if (this.opt.cacheTextNodes && this.cacheDict.nodes) {
if (this.opt.cacheTextNodes && this.cacheDict) {
this.cacheDict.lastIndex = 0;
this.cacheDict.lastTextIndex = 0;
cb(this.cacheDict);
Expand Down Expand Up @@ -848,9 +849,9 @@
svg: 1
};
var nodes = [],
boundary = this.opt.blockElementsBoundary;
boundary = this.opt.blockElementsBoundary,
priorityType = boundary ? 2 : 1;
var ch = '\x01',
priorityType = boundary ? 2 : 1,
tempType,
type,
prevNode;
Expand Down Expand Up @@ -941,7 +942,7 @@
key: "getTextNodes",
value: function getTextNodes(cb) {
var _this5 = this;
if (this.opt.cacheTextNodes && this.cacheDict.nodes) {
if (this.opt.cacheTextNodes && this.cacheDict) {
cb(this.cacheDict);
return;
}
Expand Down Expand Up @@ -1647,7 +1648,7 @@
key: "markRegExp",
value: function markRegExp(regexp, opt) {
var _this12 = this;
this.opt = this.checkOption(opt);
this.checkOption(opt);
var totalMarks = 0,
matchesSoFar = 0,
fn = this.opt.separateGroups ? 'wrapSeparateGroups' : 'wrapMatches';
Expand Down Expand Up @@ -1677,7 +1678,7 @@
key: "mark",
value: function mark(sv, opt) {
var _this13 = this;
this.opt = this.checkOption(opt);
this.checkOption(opt);
if (this.opt.combinePatterns) {
this.markCombinePatterns(sv);
return;
Expand Down Expand Up @@ -1833,8 +1834,7 @@
key: "markRanges",
value: function markRanges(ranges, opt) {
var _this15 = this;
this.opt = opt;
this.cacheDict = {};
this.checkOption(opt, true);
if (this.isArrayOfObjects(ranges)) {
var totalMarks = 0;
this.wrapRanges(ranges, function (node, range, match, index) {
Expand All @@ -1859,8 +1859,7 @@
key: "unmark",
value: function unmark(opt) {
var _this16 = this;
this.opt = opt;
this.cacheDict = {};
this.checkOption(opt, true);
var selector = (this.opt.element ? this.opt.element : 'mark') + '[data-markjs]';
if (this.opt.className) {
selector += ".".concat(this.opt.className);
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.mark.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit bf6c3e4

Please sign in to comment.