@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 +11 -2
- package/dist/fragment.js +1 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/fragment.ts +11 -0
- package/src/index.ts +1 -0
- package/types/src/fragment.d.ts +1 -0
- package/types/src/index.d.ts +1 -0
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
|
package/dist/fragment.js
ADDED
|
@@ -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
|
|
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
package/src/fragment.ts
ADDED
|
@@ -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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function decodeFragmentComponent(component: string): string;
|