@fairfox/adorn
Advanced tools
Comparing version 1.0.21 to 1.0.22
@@ -7,3 +7,3 @@ /* | ||
*/ | ||
var V=Object.create;var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var U=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty;var Y=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports);var q=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of P(t))!X.call(r,i)&&i!==e&&k(r,i,{get:()=>t[i],enumerable:!(n=B(t,i))||n.enumerable});return r};var G=(r,t,e)=>(e=r!=null?V(U(r)):{},q(t||!r||!r.__esModule?k(e,"default",{value:r,enumerable:!0}):e,r));var D=Y((yt,W)=>{var _="Expected a function",A=NaN,Q="[object Symbol]",Z=/^\s+|\s+$/g,tt=/^[-+]0x[0-9a-f]+$/i,et=/^0b[01]+$/i,rt=/^0o[0-7]+$/i,nt=parseInt,it=typeof global=="object"&&global&&global.Object===Object&&global,st=typeof self=="object"&&self&&self.Object===Object&&self,ot=it||st||Function("return this")(),at=Object.prototype,ct=at.toString,dt=Math.max,lt=Math.min,v=function(){return ot.Date.now()};function ut(r,t,e){var n,i,s,a,o,c,d=0,l=!1,h=!1,g=!0;if(typeof r!="function")throw new TypeError(_);t=F(t)||0,T(e)&&(l=!!e.leading,h="maxWait"in e,s=h?dt(F(e.maxWait)||0,t):s,g="trailing"in e?!!e.trailing:g);function p(u){var f=n,m=i;return n=i=void 0,d=u,a=r.apply(m,f),a}function K(u){return d=u,o=setTimeout(C,t),l?p(u):a}function j(u){var f=u-c,m=u-d,L=t-f;return h?lt(L,s-m):L}function x(u){var f=u-c,m=u-d;return c===void 0||f>=t||f<0||h&&m>=s}function C(){var u=v();if(x(u))return I(u);o=setTimeout(C,j(u))}function I(u){return o=void 0,g&&n?p(u):(n=i=void 0,a)}function H(){o!==void 0&&clearTimeout(o),d=0,n=c=i=o=void 0}function J(){return o===void 0?a:I(v())}function y(){var u=v(),f=x(u);if(n=arguments,i=this,c=u,f){if(o===void 0)return K(c);if(h)return o=setTimeout(C,t),p(c)}return o===void 0&&(o=setTimeout(C,t)),a}return y.cancel=H,y.flush=J,y}function ht(r,t,e){var n=!0,i=!0;if(typeof r!="function")throw new TypeError(_);return T(e)&&(n="leading"in e?!!e.leading:n,i="trailing"in e?!!e.trailing:i),ut(r,t,{leading:n,maxWait:t,trailing:i})}function T(r){var t=typeof r;return!!r&&(t=="object"||t=="function")}function ft(r){return!!r&&typeof r=="object"}function gt(r){return typeof r=="symbol"||ft(r)&&ct.call(r)==Q}function F(r){if(typeof r=="number")return r;if(gt(r))return A;if(T(r)){var t=typeof r.valueOf=="function"?r.valueOf():r;r=T(t)?t+"":t}if(typeof r!="string")return r===0?r:+r;r=r.replace(Z,"");var e=et.test(r);return e||rt.test(r)?nt(r.slice(2),e?2:8):tt.test(r)?A:+r}W.exports=ht});var b=(r,t,e)=>{let n=[];for(let i=r;i<=t;i++)n.push(e?String.fromCharCode(i):i.toString());return n},O=()=>new Set([...b(0,9,!1),...b(65,90,!0),...b(97,122,!0),"_"]),R=()=>new Set(b(9,13,!0)),z=(r,t)=>t instanceof Map?Array.from(t.entries()):t;var M=(r,t)=>JSON.stringify(r,z,t?2:0),w=r=>Array.from(new Set(r)),E=(r,t)=>t.filter((e,n)=>e!==r[n]);var N=class{_kw;wordChars;skipChars;trieCI;trieCS;opts;constructor(t,e,n){this._kw="_kw_",this.wordChars=O(),this.skipChars=R(),this.trieCI=new Map,this.trieCS=new Map,this.areKwListMapsValid(t,e),this.addKws(t,!1),this.addKws(e,!0),this.opts=n}getDetails(){return{trieAsString:`CI ===> ${M(this.trieCI)} || CS ===> ${M(this.trieCS)}`}}addKw(t,e,n){let i=n?this.trieCS:this.trieCI;e.split("").forEach(s=>{if(n)i.get(s)||i.set(s,new Map),i=i.get(s);else{let a=s.toLowerCase(),o=s.toUpperCase(),c=i.get(a);c||i.set(a,new Map),c=i.get(a),i.get(o)||i.set(o,c),i=c}}),i.set(this._kw,t)}areKwListMapsValid=(t,e)=>{if(t===null&&e===null)throw new Error("please provide a case-sensitive and/or case-insensitive kw map");let n=[];e!==null&&e.forEach(l=>l.forEach(h=>n.push(h)));let i=w(n);if(i.length!==n.length)throw new Error(`case-sensitive map contains duplicates => ${JSON.stringify(E(i,n))}`);let s=[];t!==null&&t.forEach(l=>l.forEach(h=>s.push(h.toLowerCase())));let a=w(s);if(a.length!==s.length)throw new Error(`lower-cased case-insensitive map contains duplicates => ${JSON.stringify(E(a,s))}`);let o=n.map(l=>l.toLowerCase()),c=[...w(o),...s],d=w(c);if(d.length!==c.length)throw new Error(`merged lower-cased case-insensitive list and deduped lower-cased case-sensitive map contains duplicates => ${JSON.stringify(E(d,c))}`);return!0};addKws(t,e){t!==null&&t.forEach((n,i)=>{n.forEach(s=>this.addKw(i,s,e))})}getMatchIndexes(t){let e=[],n=t.length,i=s=>{let a=s?this.trieCS:this.trieCI,o=["",-1,-1],c=!1;for(let d=0;d<=n;d++){let l=t[d],h=t[d-1],g=!this.wordChars.has(l),p=!this.wordChars.has(h);if(c=a.has(l),!this.skipChars.has(l)){if(!c){o[0]&&o[1]>=0&&o[2]>=0&&g&&e.push(o),a=s?this.trieCS:this.trieCI,o=["",-1,-1],a.has(l)&&(d=d-1);continue}o[1]<0&&p&&(o[1]=d),a=a.get(l),a.has(this._kw)&&(o=[a.get(this._kw),o[1],d])}}};return this.trieCI!==null&&i(!1),this.trieCS!==null&&i(!0),e.sort((s,a)=>s[1]-a[1])}extractMatchIds(t){let e=this.getMatchIndexes(t).map(n=>n[0]);return Array.from(new Set(e))}extractDirtyMatches(t){let e=this.getMatchIndexes(t).map(n=>[n[0],t.slice(n[1],n[2]+1)]);return new Map(e)}replaceKws(t){let e=0,n="";return this.getMatchIndexes(t).forEach(i=>{n=n+t.slice(e,i[1])+i[0],e=i[2]+1}),n+t.slice(e)}wrapKwsWithHtml(t){let e=this.getMatchIndexes(t);if(!e.length)return t;let n=0,i="";return e.forEach(s=>{i=`${i}${t.slice(n,s[1])}<${this.opts.tag} ${this.opts.getAttrs(s[0])}>${t.slice(s[1],s[2]+1)}</${this.opts.tag}>`,n=s[2]+1}),i+t.slice(n)}};var $=G(D(),1),pt=(r,t)=>100*Math.abs((r-t)/((r+t)/2)),S=class{baseElement;range;ownerDocument;ignoreNodeNames;nodeReg=/(head|script|style|meta|noscript|input|img|svg|cite|button|path|defs)/i;textNodes;constructor(t,e){this.baseElement=t,this.ownerDocument=this.baseElement.ownerDocument||document,this.range=this.ownerDocument.createRange(),this.textNodes=[],this.ignoreNodeNames=e||[]}getIsTextNodeInViewport=t=>{this.range.selectNode(t);let e=this.range.getBoundingClientRect(),n=window.visualViewport,i=e.top,s=e.bottom,a=e.left,o=e.right,c=e.width>0&&e.height>0,d=s<=0,l=i>=n.height,h=a<=0,g=o>=n.width;return c&&(!d&&!l&&!h&&!g)};walkerFilter=t=>{var e,n;return this.nodeReg.test(((e=t.parentElement)==null?void 0:e.tagName)||"")||this.ignoreNodeNames.includes(((n=t.parentElement)==null?void 0:n.tagName)||"")||!t.textContent||t.textContent.trim().length<3||!this.getIsTextNodeInViewport(t)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT};watchScroll=t=>{let e=window.scrollY,n=(0,$.default)(()=>{let i=e;e=window.scrollY,pt(i,e)>.5&&window.requestAnimationFrame(a=>{window.cancelAnimationFrame(a),t(this.walk(document.body))})},500);return document.addEventListener("scroll",n,!0),n};watchDOM=t=>{let e=(s,a)=>{var o,c;for(let d of s)if(d.type==="childList"){let l=d.addedNodes[0];if(l.parentElement===null||this.nodeReg.test(((o=l.parentElement)==null?void 0:o.tagName)||"")||this.ignoreNodeNames.includes(((c=l.parentElement)==null?void 0:c.tagName)||""))return;let h=this.walk(l.parentElement);h.length>0&&t(h)}},n=new MutationObserver(e),i={attributes:!0,attributeFilter:["style"],childList:!0,characterData:!0,subtree:!0};return n==null||n.observe(document.body,i),n};endWatchDOM(t){t==null||t.disconnect()}endWatchScroll(t){document.removeEventListener("scroll",t)}walk=t=>{let e=t||document.body;this.textNodes=[];let n=this.ownerDocument.createTreeWalker(e,NodeFilter.SHOW_TEXT,this.walkerFilter.bind(this));for(;n.nextNode();)this.textNodes.push(n.currentNode);return this.textNodes}};var mt=(r,t)=>{let e=document.createRange(),n=i=>{let s=i.textContent||"";t.getMatchIndexes(s).reverse().forEach(o=>{let c=document.createElement("annotation-anchor");c.setAttribute("data-content-id",o[0]),c.setAttribute("data-annotation-variant","underline"),e.setStart(i,o[1]),e.setEnd(i,o[2]+1),e.surroundContents(c)})};r.forEach(n)},wt=(r,t)=>{mt(r,t)};export{N as Match,S as TextNodesFromDOM,wt as annotateDOM}; | ||
var X=Object.create;var M=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames,O=Object.getOwnPropertySymbols,G=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty,z=Object.prototype.propertyIsEnumerable;var R=(r,t,e)=>t in r?M(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,F=(r,t)=>{for(var e in t||(t={}))A.call(t,e)&&R(r,e,t[e]);if(O)for(var e of O(t))z.call(t,e)&&R(r,e,t[e]);return r};var Q=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports);var Z=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!A.call(r,i)&&i!==e&&M(r,i,{get:()=>t[i],enumerable:!(n=Y(t,i))||n.enumerable});return r};var tt=(r,t,e)=>(e=r!=null?X(G(r)):{},Z(t||!r||!r.__esModule?M(e,"default",{value:r,enumerable:!0}):e,r));var H=Q((vt,j)=>{var K="Expected a function",W=NaN,rt="[object Symbol]",nt=/^\s+|\s+$/g,it=/^[-+]0x[0-9a-f]+$/i,st=/^0b[01]+$/i,ot=/^0o[0-7]+$/i,at=parseInt,ct=typeof global=="object"&&global&&global.Object===Object&&global,lt=typeof self=="object"&&self&&self.Object===Object&&self,dt=ct||lt||Function("return this")(),ut=Object.prototype,ht=ut.toString,gt=Math.max,ft=Math.min,v=function(){return dt.Date.now()};function pt(r,t,e){var n,i,o,a,s,c,l=0,d=!1,h=!1,f=!0;if(typeof r!="function")throw new TypeError(K);t=D(t)||0,T(e)&&(d=!!e.leading,h="maxWait"in e,o=h?gt(D(e.maxWait)||0,t):o,f="trailing"in e?!!e.trailing:f);function p(u){var g=n,m=i;return n=i=void 0,l=u,a=r.apply(m,g),a}function V(u){return l=u,s=setTimeout(C,t),d?p(u):a}function B(u){var g=u-c,m=u-l,k=t-g;return h?ft(k,o-m):k}function I(u){var g=u-c,m=u-l;return c===void 0||g>=t||g<0||h&&m>=o}function C(){var u=v();if(I(u))return L(u);s=setTimeout(C,B(u))}function L(u){return s=void 0,f&&n?p(u):(n=i=void 0,a)}function P(){s!==void 0&&clearTimeout(s),l=0,n=c=i=s=void 0}function U(){return s===void 0?a:L(v())}function y(){var u=v(),g=I(u);if(n=arguments,i=this,c=u,g){if(s===void 0)return V(c);if(h)return s=setTimeout(C,t),p(c)}return s===void 0&&(s=setTimeout(C,t)),a}return y.cancel=P,y.flush=U,y}function mt(r,t,e){var n=!0,i=!0;if(typeof r!="function")throw new TypeError(K);return T(e)&&(n="leading"in e?!!e.leading:n,i="trailing"in e?!!e.trailing:i),pt(r,t,{leading:n,maxWait:t,trailing:i})}function T(r){var t=typeof r;return!!r&&(t=="object"||t=="function")}function wt(r){return!!r&&typeof r=="object"}function Ct(r){return typeof r=="symbol"||wt(r)&&ht.call(r)==rt}function D(r){if(typeof r=="number")return r;if(Ct(r))return W;if(T(r)){var t=typeof r.valueOf=="function"?r.valueOf():r;r=T(t)?t+"":t}if(typeof r!="string")return r===0?r:+r;r=r.replace(nt,"");var e=st.test(r);return e||ot.test(r)?at(r.slice(2),e?2:8):it.test(r)?W:+r}j.exports=mt});var b=(r,t,e)=>{let n=[];for(let i=r;i<=t;i++)n.push(e?String.fromCharCode(i):i.toString());return n},_=()=>new Set([...b(0,9,!1),...b(65,90,!0),...b(97,122,!0),"_"]),$=()=>new Set(b(9,13,!0)),et=(r,t)=>t instanceof Map?Array.from(t.entries()):t;var N=(r,t)=>JSON.stringify(r,et,t?2:0),w=r=>Array.from(new Set(r)),E=(r,t)=>t.filter((e,n)=>e!==r[n]);var S=class{_kw;wordChars;skipChars;trieCI;trieCS;opts;constructor(t,e,n){this._kw="_kw_",this.wordChars=_(),this.skipChars=$(),this.trieCI=new Map,this.trieCS=new Map,this.areKwListMapsValid(t,e),this.addKws(t,!1),this.addKws(e,!0),this.opts=F({tag:"span",getAttrs:i=>[["data-match-id",i]],shouldSkipChars:!0},n)}getDetails(){return{trieAsString:`CI ===> ${N(this.trieCI)} || CS ===> ${N(this.trieCS)}`}}addKw(t,e,n){let i=n?this.trieCS:this.trieCI;e.split("").forEach(o=>{if(n)i.get(o)||i.set(o,new Map),i=i.get(o);else{let a=o.toLowerCase(),s=o.toUpperCase(),c=i.get(a);c||i.set(a,new Map),c=i.get(a),i.get(s)||i.set(s,c),i=c}}),i.set(this._kw,t)}areKwListMapsValid=(t,e)=>{if(t===null&&e===null)throw new Error("please provide a case-sensitive and/or case-insensitive kw map");let n=[];e!==null&&e.forEach(d=>d.forEach(h=>n.push(h)));let i=w(n);if(i.length!==n.length)throw new Error(`case-sensitive map contains duplicates => ${JSON.stringify(E(i,n))}`);let o=[];t!==null&&t.forEach(d=>d.forEach(h=>o.push(h.toLowerCase())));let a=w(o);if(a.length!==o.length)throw new Error(`lower-cased case-insensitive map contains duplicates => ${JSON.stringify(E(a,o))}`);let s=n.map(d=>d.toLowerCase()),c=[...w(s),...o],l=w(c);if(l.length!==c.length)throw new Error(`merged lower-cased case-insensitive list and deduped lower-cased case-sensitive map contains duplicates => ${JSON.stringify(E(l,c))}`);return!0};addKws(t,e){t!==null&&t.forEach((n,i)=>{n.forEach(o=>this.addKw(i,o,e))})}getMatchIndexes(t){let e=[],n=t.length,i=o=>{let a=o?this.trieCS:this.trieCI,s=["",-1,-1],c=!1;for(let l=0;l<=n;l++){let d=t[l],h=t[l-1],f=!this.wordChars.has(d),p=!this.wordChars.has(h);if(c=a.has(d),!(this.opts.shouldSkipChars&&this.skipChars.has(d))){if(!c){s[0]&&s[1]>=0&&s[2]>=0&&f&&e.push(s),a=o?this.trieCS:this.trieCI,s=["",-1,-1],a.has(d)&&(l=l-1);continue}s[1]<0&&p&&(s[1]=l),a=a.get(d),a.has(this._kw)&&(s=[a.get(this._kw),s[1],l])}}};return this.trieCI!==null&&i(!1),this.trieCS!==null&&i(!0),e.sort((o,a)=>o[1]-a[1])}extractMatchIds(t){let e=this.getMatchIndexes(t).map(n=>n[0]);return Array.from(new Set(e))}extractDirtyMatches(t){let e=this.getMatchIndexes(t).map(n=>[n[0],t.slice(n[1],n[2]+1)]);return new Map(e)}replaceKws(t){let e=0,n="";return this.getMatchIndexes(t).forEach(i=>{n=n+t.slice(e,i[1])+i[0],e=i[2]+1}),n+t.slice(e)}wrapKwsWithHtml(t){let e=this.getMatchIndexes(t);if(!e.length)return t;let n=0,i="";return e.forEach(o=>{let a=this.opts.getAttrs(o[0]).reduce((s,c,l)=>`${s}${l>0?" ":""}${c[0]}="${c[1]}"`,"");i=`${i}${t.slice(n,o[1])}<${this.opts.tag} ${a}>${t.slice(o[1],o[2]+1)}</${this.opts.tag}>`,n=o[2]+1}),i+t.slice(n)}};var J=tt(H(),1),bt=(r,t)=>100*Math.abs((r-t)/((r+t)/2)),x=class{baseElement;range;ownerDocument;ignoreNodeNames;nodeReg=/(head|script|style|meta|noscript|input|img|svg|cite|button|path|defs)/i;textNodes;constructor(t,e){this.baseElement=t,this.ownerDocument=this.baseElement.ownerDocument||document,this.range=this.ownerDocument.createRange(),this.textNodes=[],this.ignoreNodeNames=e||[]}getIsTextNodeInViewport=t=>{this.range.selectNode(t);let e=this.range.getBoundingClientRect(),n=window.visualViewport,i=e.top,o=e.bottom,a=e.left,s=e.right,c=e.width>0&&e.height>0,l=o<=0,d=i>=n.height,h=a<=0,f=s>=n.width;return c&&(!l&&!d&&!h&&!f)};walkerFilter=t=>{var e,n;return this.nodeReg.test(((e=t.parentElement)==null?void 0:e.tagName)||"")||this.ignoreNodeNames.includes(((n=t.parentElement)==null?void 0:n.tagName)||"")||!t.textContent||t.textContent.trim().length<3||!this.getIsTextNodeInViewport(t)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT};watchScroll=t=>{let e=window.scrollY,n=(0,J.default)(()=>{let i=e;e=window.scrollY,bt(i,e)>.5&&window.requestAnimationFrame(a=>{window.cancelAnimationFrame(a),t(this.walk(document.body))})},500);return document.addEventListener("scroll",n,!0),n};watchDOM=t=>{let e=(o,a)=>{var s,c;for(let l of o)if(l.type==="childList"){let d=l.addedNodes[0];if(d.parentElement===null||this.nodeReg.test(((s=d.parentElement)==null?void 0:s.tagName)||"")||this.ignoreNodeNames.includes(((c=d.parentElement)==null?void 0:c.tagName)||""))return;let h=this.walk(d.parentElement);h.length>0&&t(h)}},n=new MutationObserver(e),i={attributes:!0,attributeFilter:["style"],childList:!0,characterData:!0,subtree:!0};return n==null||n.observe(document.body,i),n};endWatchDOM(t){t==null||t.disconnect()}endWatchScroll(t){document.removeEventListener("scroll",t)}walk=t=>{let e=t||document.body;this.textNodes=[];let n=this.ownerDocument.createTreeWalker(e,NodeFilter.SHOW_TEXT,this.walkerFilter.bind(this));for(;n.nextNode();)this.textNodes.push(n.currentNode);return this.textNodes}};var Et=(r,t)=>{let e=document.createRange(),n=i=>{let o=i.textContent||"";t.getMatchIndexes(o).reverse().forEach(s=>{let c=document.createElement("annotation-anchor");t.opts.getAttrs(s[0]).forEach(([l,d])=>c.setAttribute(l,d)),e.setStart(i,s[1]),e.setEnd(i,s[2]+1),e.surroundContents(c)})};r.forEach(n)},Tt=(r,t)=>{Et(r,t)};export{S as Match,x as TextNodesFromDOM,Tt as annotateDOM}; | ||
//# sourceMappingURL=index.js.map |
declare type KwListMap = Map<string, string[]>; | ||
declare type Opts = { | ||
tag: string; | ||
getAttrs: (id: string) => string; | ||
getAttrs: (id: string) => Array<[string, string]>; | ||
shouldSkipChars?: boolean; | ||
}; | ||
@@ -12,3 +13,3 @@ export declare class Match { | ||
private readonly trieCS; | ||
private readonly opts; | ||
readonly opts: Opts; | ||
constructor(insensitive: KwListMap | null, sensitive: KwListMap | null, opts: Opts); | ||
@@ -15,0 +16,0 @@ getDetails(): { |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "1.0.21", | ||
"version": "1.0.22", | ||
"exports": { | ||
@@ -15,0 +15,0 @@ ".": { |
@@ -70,3 +70,3 @@ # Adorn | ||
const sensitive = new Map([['321', ['Nullam']]]); | ||
const opts = { tag: 'x-annotate', getAttrs: (id: string) => `data-match-id="${id}"` }; | ||
const opts = { tag: 'x-annotate', getAttrs: (id: string) => [['data-match-id', id]] }; | ||
@@ -96,3 +96,3 @@ const match = new Match(insensitive, sensitive, opts); | ||
const sensitive = new Map([['321', ['Nullam']]]); | ||
const opts = { tag: 'x-annotate', getAttrs: (id: string) => `data-match-id="${id}"` }; | ||
const opts = { tag: 'x-annotate', getAttrs: (id: string) => [['data-match-id', id]] }; | ||
const match = new Match(ipsumCaseInsensitive, ipsumCaseSensitive, opts); | ||
@@ -99,0 +99,0 @@ const textNodesFromDOM = new TextNodesFromDOM(document.body, [opts.tag.toUpperCase()]); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60246
100