@readium/helpers 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/color.js CHANGED
@@ -1 +1 @@
1
- let m=null,x=null,g=0;const l={r:255,g:255,b:255,a:1},i=new Map,R=()=>{if(!m)if(typeof OffscreenCanvas<"u")m=new OffscreenCanvas(5,5),x=m.getContext("2d",{willReadFrequently:!0,desynchronized:!0});else{const t=document.createElement("canvas");t.width=5,t.height=5,m=t,x=t.getContext("2d",{willReadFrequently:!0,desynchronized:!0})}return x},$=t=>{if(!t)return!0;const e=t.trim().toLowerCase();return e.startsWith("var(")||["transparent","currentcolor","inherit","initial","revert","unset","revert-layer"].includes(e)?!0:["linear-gradient","radial-gradient","conic-gradient","repeating-linear-gradient","repeating-radial-gradient","repeating-conic-gradient"].some(n=>e.includes(n))},p=(t,e)=>{console.warn(`[Decorator] Could not parse color: "${t}". ${e} Falling back to ${JSON.stringify(l)} to compute contrast. Please use a CSS color value that can be computed to RGB(A).`)},h=(t,e=null)=>{const r=e?`${t}|${e}`:t,s=i.get(r);if(s!==void 0)return s??l;if($(t))return p(t,"Unsupported color format or special value."),i.set(r,null),l;const n=R();if(!n)return p(t,"Could not get canvas context."),i.set(r,null),l;try{g===0&&n.clearRect(0,0,5,5);const a=g%5,c=Math.floor(g/5);n.clearRect(a,c,1,1),e&&(n.fillStyle=e,n.fillRect(a,c,1,1)),n.fillStyle=t,n.fillRect(a,c,1,1);const u=n.getImageData(a,c,1,1);g=(g+1)%25;const[o,y,f,d]=u.data;if(d===0)return p(t,"Fully transparent color."),i.set(r,null),l;const b={r:o,g:y,b:f,a:d/255};return i.set(r,b),b}catch(a){return p(t,`Error: ${a instanceof Error?a.message:String(a)}`),i.set(r,null),l}},v=t=>{const e=t/255;return e<=.03928?e/12.92:Math.pow((e+.055)/1.055,2.4)},M=t=>{const e=v(t.r),r=v(t.g),s=v(t.b);return .2126*e+.7152*r+.0722*s},C=(t,e)=>{const r=typeof t=="string"?h(t):t,s=typeof e=="string"?h(e):e,n=M(r),a=M(s),c=Math.max(n,a),u=Math.min(n,a);return(c+.05)/(u+.05)},w=(t,e=null)=>{const r=h(t,e),s=C(r,{r:255,g:255,b:255,a:1}),n=C(r,{r:0,g:0,b:0,a:1});return s>n},F=(t,e=null)=>!w(t,e),T=(t,e=null)=>w(t,e)?"white":"black",L=t=>{const e=t.a!==void 0?t.a:1;return`rgba(${Math.round(t.r)}, ${Math.round(t.g)}, ${Math.round(t.b)}, ${e})`},S=(t,e)=>({r:Math.min(255,t.r+(255-t.r)*e),g:Math.min(255,t.g+(255-t.g)*e),b:Math.min(255,t.b+(255-t.b)*e),a:t.a??1}),D=(t,e)=>({r:Math.max(0,t.r*(1-e)),g:Math.max(0,t.g*(1-e)),b:Math.max(0,t.b*(1-e)),a:t.a??1}),k=(t,e=null,r=3)=>{const s=h(t),n=e?h(e):{r:255,g:255,b:255,a:1};let a=C(s,n);if(a>=r)return t;const u=M(n)<.5;let o={...s,a:s.a??1};const y=20,f=.1;for(let d=0;d<y&&(u?o=S(o,f):o=D(o,f),a=C(o,n),!(a>=r));d++);return L(o)};export{k as adjustColorForContrast,C as checkContrast,h as colorToRgba,T as getContrastingTextColor,M as getLuminance,w as isDarkColor,F as isLightColor};
1
+ let f=null,x=null,g=0;const l={r:255,g:255,b:255,a:1},c=new Map,R=()=>{if(!f)if(typeof OffscreenCanvas<"u")f=new OffscreenCanvas(5,5),x=f.getContext("2d",{willReadFrequently:!0,desynchronized:!0});else{const t=document.createElement("canvas");t.width=5,t.height=5,f=t,x=t.getContext("2d",{willReadFrequently:!0,desynchronized:!0})}return x},$=t=>{if(!t)return!0;const e=t.trim().toLowerCase();return e.startsWith("var(")||["transparent","currentcolor","inherit","initial","revert","unset","revert-layer"].includes(e)?!0:["linear-gradient","radial-gradient","conic-gradient","repeating-linear-gradient","repeating-radial-gradient","repeating-conic-gradient"].some(n=>e.includes(n))},p=(t,e)=>{console.warn(`[@readium/helpers] Could not parse color: "${t}". ${e} Falling back to ${JSON.stringify(l)} to compute contrast. Please use a CSS color value that can be computed to RGB(A).`)},h=(t,e=null)=>{const r=e?`${t}|${e}`:t,s=c.get(r);if(s!==void 0)return s??l;if($(t))return p(t,"Unsupported color format or special value."),c.set(r,null),l;const n=R();if(!n)return p(t,"Could not get canvas context."),c.set(r,null),l;try{g===0&&n.clearRect(0,0,5,5);const a=g%5,i=Math.floor(g/5);n.clearRect(a,i,1,1),e&&(n.fillStyle=e,n.fillRect(a,i,1,1)),n.fillStyle=t,n.fillRect(a,i,1,1);const u=n.getImageData(a,i,1,1);g=(g+1)%25;const[o,y,m,d]=u.data;if(d===0)return p(t,"Fully transparent color."),c.set(r,null),l;const b={r:o,g:y,b:m,a:d/255};return c.set(r,b),b}catch(a){return p(t,`Error: ${a instanceof Error?a.message:String(a)}`),c.set(r,null),l}},v=t=>{const e=t/255;return e<=.03928?e/12.92:Math.pow((e+.055)/1.055,2.4)},M=t=>{const e=v(t.r),r=v(t.g),s=v(t.b);return .2126*e+.7152*r+.0722*s},C=(t,e)=>{const r=typeof t=="string"?h(t):t,s=typeof e=="string"?h(e):e,n=M(r),a=M(s),i=Math.max(n,a),u=Math.min(n,a);return(i+.05)/(u+.05)},w=(t,e=null)=>{const r=h(t,e),s=C(r,{r:255,g:255,b:255,a:1}),n=C(r,{r:0,g:0,b:0,a:1});return s>n},T=(t,e=null)=>!w(t,e),k=(t,e=null)=>w(t,e)?"white":"black",L=t=>{const e=t.a!==void 0?t.a:1;return`rgba(${Math.round(t.r)}, ${Math.round(t.g)}, ${Math.round(t.b)}, ${e})`},S=(t,e)=>({r:Math.min(255,t.r+(255-t.r)*e),g:Math.min(255,t.g+(255-t.g)*e),b:Math.min(255,t.b+(255-t.b)*e),a:t.a??1}),F=(t,e)=>({r:Math.max(0,t.r*(1-e)),g:Math.max(0,t.g*(1-e)),b:Math.max(0,t.b*(1-e)),a:t.a??1}),D=(t,e=null,r=3)=>{const s=h(t),n=e?h(e):{r:255,g:255,b:255,a:1};let a=C(s,n);if(a>=r)return t;const u=M(n)<.5;let o={...s,a:s.a??1};const y=20,m=.1;for(let d=0;d<y&&(u?o=S(o,m):o=F(o,m),a=C(o,n),!(a>=r));d++);return L(o)};export{D as adjustColorForContrast,C as checkContrast,h as colorToRgba,k as getContrastingTextColor,M as getLuminance,w as isDarkColor,T as isLightColor};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{adjustColorForContrast as r,checkContrast as o,colorToRgba as n,getContrastingTextColor as a,getLuminance as g,isDarkColor as i,isLightColor as m}from"./color.js";import{sML as F,sMLWithRequest as c}from"./sML.js";import{decodeTextFragmentDirective as l}from"./textFragmentDirective.js";import{forTesting as f,internal as p,processTextFragmentDirective as T}from"./vendor/text-fragments-polyfill/textFragmentMatcher.js";import{GenerateFragmentStatus as d,generateFragment as L,generateFragmentFromRange as R,isValidRangeForFragmentGeneration as h,setFragmentGenerationTimeout as D}from"./vendor/text-fragments-polyfill/textFragmentGenerator.js";export{d as GenerateFragmentStatus,r as adjustColorForContrast,o as checkContrast,n as colorToRgba,l as decodeTextFragmentDirective,f as forTesting,L as generateFragment,R as generateFragmentFromRange,a as getContrastingTextColor,g as getLuminance,p as internal,i as isDarkColor,m as isLightColor,h as isValidRangeForFragmentGeneration,T as processTextFragmentDirective,F as sML,c as sMLWithRequest,D as setFragmentGenerationTimeout};
1
+ import{adjustColorForContrast as r,checkContrast as o,colorToRgba as n,getContrastingTextColor as a,getLuminance as s,isDarkColor as i,isLightColor as g}from"./color.js";import{sML as c,sMLWithRequest as x}from"./sML.js";import{BINDING_PUNCT_CLASS as l,ssmlAttrEscape as p,ssmlTextEscape as C,startsWithBindingPunct as T}from"./text.js";import{decodeTextFragmentDirective as u}from"./textFragmentDirective.js";import{forTesting as L,internal as h,processTextFragmentDirective as D}from"./vendor/text-fragments-polyfill/textFragmentMatcher.js";import{GenerateFragmentStatus as R,generateFragment as N,generateFragmentFromRange as S,isValidRangeForFragmentGeneration as k,setFragmentGenerationTimeout as v}from"./vendor/text-fragments-polyfill/textFragmentGenerator.js";export{l as BINDING_PUNCT_CLASS,R as GenerateFragmentStatus,r as adjustColorForContrast,o as checkContrast,n as colorToRgba,u as decodeTextFragmentDirective,L as forTesting,N as generateFragment,S as generateFragmentFromRange,a as getContrastingTextColor,s as getLuminance,h as internal,i as isDarkColor,g as isLightColor,k as isValidRangeForFragmentGeneration,D as processTextFragmentDirective,c as sML,x as sMLWithRequest,v as setFragmentGenerationTimeout,p as ssmlAttrEscape,C as ssmlTextEscape,T as startsWithBindingPunct};
package/dist/text.js ADDED
@@ -0,0 +1 @@
1
+ const t="\\p{Pe}\\p{Pf}.,;:!?,。、;:!?،؛؟",c=new RegExp(`^[${t}]`,"u");function p(e){return c.test(e)}const s=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"),a=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");export{t as BINDING_PUNCT_CLASS,a as ssmlAttrEscape,s as ssmlTextEscape,p as startsWithBindingPunct};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readium/helpers",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "description": "General-purpose browser helpers for Readium packages",
6
6
  "author": "readium",
@@ -29,7 +29,7 @@
29
29
  "types"
30
30
  ],
31
31
  "engines": {
32
- "node": ">=18"
32
+ "node": ">=24"
33
33
  },
34
34
  "dependencies": {
35
35
  "user-agent-data-types": "^0.4.2"
package/src/color.ts CHANGED
@@ -75,7 +75,7 @@ const isSpecialColorValue = (color: string): boolean => {
75
75
 
76
76
  const warnAboutInvalidColor = (color: string, reason: string): void => {
77
77
  console.warn(
78
- `[Decorator] Could not parse color: "${color}". ${reason} Falling back to ${JSON.stringify(DEFAULT_COLOR)} to compute contrast. Please use a CSS color value that can be computed to RGB(A).`
78
+ `[@readium/helpers] Could not parse color: "${color}". ${reason} Falling back to ${JSON.stringify(DEFAULT_COLOR)} to compute contrast. Please use a CSS color value that can be computed to RGB(A).`
79
79
  );
80
80
  };
81
81
 
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./color.ts";
2
2
  export * from "./sML.ts";
3
+ export * from "./text.ts";
3
4
  export * from "./textFragmentDirective.ts";
4
5
  export * from "./vendor/text-fragments-polyfill/textFragmentMatcher.ts";
5
6
  export * from "./vendor/text-fragments-polyfill/textFragmentGenerator.ts";
package/src/text.ts ADDED
@@ -0,0 +1,13 @@
1
+ // Backward-binding: closing brackets/quotes (Pe/Pf) plus terminal marks (.,;:!? and CJK/Arabic equivalents).
2
+ export const BINDING_PUNCT_CLASS = "\\p{Pe}\\p{Pf}.,;:!?,。、;:!?،؛؟";
3
+
4
+ const BINDING_PUNCT_RE = new RegExp(`^[${BINDING_PUNCT_CLASS}]`, "u");
5
+
6
+ export function startsWithBindingPunct(s: string): boolean {
7
+ return BINDING_PUNCT_RE.test(s);
8
+ }
9
+
10
+ export const ssmlTextEscape = (s: string) =>
11
+ s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
12
+ export const ssmlAttrEscape = (s: string) =>
13
+ s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
@@ -1,5 +1,6 @@
1
1
  export * from "./color.ts";
2
2
  export * from "./sML.ts";
3
+ export * from "./text.ts";
3
4
  export * from "./textFragmentDirective.ts";
4
5
  export * from "./vendor/text-fragments-polyfill/textFragmentMatcher.ts";
5
6
  export * from "./vendor/text-fragments-polyfill/textFragmentGenerator.ts";
@@ -0,0 +1,4 @@
1
+ export declare const BINDING_PUNCT_CLASS = "\\p{Pe}\\p{Pf}.,;:!?\uFF0C\u3002\u3001\uFF1B\uFF1A\uFF01\uFF1F\u060C\u061B\u061F";
2
+ export declare function startsWithBindingPunct(s: string): boolean;
3
+ export declare const ssmlTextEscape: (s: string) => string;
4
+ export declare const ssmlAttrEscape: (s: string) => string;