@readium/helpers 1.2.1 → 1.3.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/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # @readium/helpers
2
2
 
3
- General-purpose browser helpers for Readium packages: color parsing/contrast utilities, OS/browser detection, and WICG Text Fragment parsing/resolution/generation.
3
+ General-purpose browser helpers for Readium packages: color parsing/contrast utilities, OS/browser detection, URI fragment decoding, and WICG Text Fragment parsing/resolution/generation.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```ts
8
- import { colorToRgba, checkContrast, isDarkColor, isLightColor, getContrastingTextColor, adjustColorForContrast, getLuminance, sML, sMLWithRequest, decodeTextFragmentDirective, processTextFragmentDirective, generateFragmentFromRange } from "@readium/helpers";
8
+ import { colorToRgba, checkContrast, isDarkColor, isLightColor, getContrastingTextColor, adjustColorForContrast, getLuminance, sML, sMLWithRequest, decodeFragmentComponent, decodeTextFragmentDirective, processTextFragmentDirective, generateFragmentFromRange } from "@readium/helpers";
9
9
  ```
10
10
 
11
11
  ## Color utilities
@@ -45,6 +45,15 @@ sMLWithRequest.iOSRequest // "mobile" | "desktop" | undefined — iPadOS "Reques
45
45
 
46
46
  `sML` is a bundled subset of [sML.js](https://github.com/satorumurmur/sML) by Satoru Matsushima, MIT licensed. Falls back safely when `navigator` is unavailable (SSR).
47
47
 
48
+ ## URI fragments
49
+
50
+ ```ts
51
+ // Percent-decodes one fragment component (e.g. an element id), keeping a malformed escape as-is
52
+ decodeFragmentComponent("caf%C3%A9%20noir"): string // "café noir"
53
+ ```
54
+
55
+ A structured fragment must be split on its delimiters before its components are decoded.
56
+
48
57
  ## Text Fragments
49
58
 
50
59
  ```ts
@@ -0,0 +1 @@
1
+ function r(n){return n.replace(/(?:%[0-9A-Fa-f]{2})+/g,e=>{try{return decodeURIComponent(e)}catch{return e}})}export{r as decodeFragmentComponent};
package/dist/index.js CHANGED
@@ -1 +1 @@
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};
1
+ import{adjustColorForContrast as r,checkContrast as o,colorToRgba as n,getContrastingTextColor as a,getLuminance as s,isDarkColor as m,isLightColor as i}from"./color.js";import{decodeFragmentComponent as c}from"./fragment.js";import{sML as x,sMLWithRequest as F}from"./sML.js";import{BINDING_PUNCT_CLASS as l,ssmlAttrEscape as f,ssmlTextEscape as T,startsWithBindingPunct as d}from"./text.js";import{decodeTextFragmentDirective as L}from"./textFragmentDirective.js";import{forTesting as D,internal as G,processTextFragmentDirective as R}from"./vendor/text-fragments-polyfill/textFragmentMatcher.js";import{GenerateFragmentStatus as S,generateFragment as k,generateFragmentFromRange as v,isValidRangeForFragmentGeneration as A,setFragmentGenerationTimeout as B}from"./vendor/text-fragments-polyfill/textFragmentGenerator.js";export{l as BINDING_PUNCT_CLASS,S as GenerateFragmentStatus,r as adjustColorForContrast,o as checkContrast,n as colorToRgba,c as decodeFragmentComponent,L as decodeTextFragmentDirective,D as forTesting,k as generateFragment,v as generateFragmentFromRange,a as getContrastingTextColor,s as getLuminance,G as internal,m as isDarkColor,i as isLightColor,A as isValidRangeForFragmentGeneration,R as processTextFragmentDirective,x as sML,F as sMLWithRequest,B as setFragmentGenerationTimeout,f as ssmlAttrEscape,T as ssmlTextEscape,d as startsWithBindingPunct};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readium/helpers",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "type": "module",
5
5
  "description": "General-purpose browser helpers for Readium packages",
6
6
  "author": "readium",
@@ -0,0 +1,11 @@
1
+ // Percent-decodes one component of a URI fragment, after any structured
2
+ // fragment has been split on its delimiters. A malformed escape is kept as-is.
3
+ export function decodeFragmentComponent(component: string): string {
4
+ return component.replace(/(?:%[0-9A-Fa-f]{2})+/g, (escapes) => {
5
+ try {
6
+ return decodeURIComponent(escapes);
7
+ } catch {
8
+ return escapes;
9
+ }
10
+ });
11
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./color.ts";
2
+ export * from "./fragment.ts";
2
3
  export * from "./sML.ts";
3
4
  export * from "./text.ts";
4
5
  export * from "./textFragmentDirective.ts";
@@ -0,0 +1 @@
1
+ export declare function decodeFragmentComponent(component: string): string;
@@ -1,4 +1,5 @@
1
1
  export * from "./color.ts";
2
+ export * from "./fragment.ts";
2
3
  export * from "./sML.ts";
3
4
  export * from "./text.ts";
4
5
  export * from "./textFragmentDirective.ts";