bidi-kit 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +364 -3
- package/bin/bidi-kit.mjs +77 -0
- package/dist/bidi-kit.global.js +606 -0
- package/dist/cjs/audit.js +72 -0
- package/dist/cjs/css.js +59 -0
- package/dist/cjs/fonts.js +84 -0
- package/dist/cjs/html.js +96 -0
- package/dist/cjs/index.js +49 -0
- package/dist/cjs/isolate.js +84 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/segment.js +217 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/audit.d.ts +12 -0
- package/dist/esm/audit.js +68 -0
- package/dist/esm/css.d.ts +26 -0
- package/dist/esm/css.js +55 -0
- package/dist/esm/fonts.d.ts +36 -0
- package/dist/esm/fonts.js +78 -0
- package/dist/esm/html.d.ts +32 -0
- package/dist/esm/html.js +89 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +15 -0
- package/dist/esm/isolate.d.ts +43 -0
- package/dist/esm/isolate.js +77 -0
- package/dist/esm/segment.d.ts +40 -0
- package/dist/esm/segment.js +209 -0
- package/dist/esm/types.d.ts +92 -0
- package/dist/esm/types.js +1 -0
- package/package.json +64 -6
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
const RTL_RANGES = [
|
|
2
|
+
[0x0590, 0x05ff], // Hebrew
|
|
3
|
+
[0x0600, 0x06ff], // Arabic
|
|
4
|
+
[0x0700, 0x074f], // Syriac
|
|
5
|
+
[0x0750, 0x077f], // Arabic Supplement
|
|
6
|
+
[0x0780, 0x07bf], // Thaana
|
|
7
|
+
[0x07c0, 0x07ff], // NKo
|
|
8
|
+
[0x0800, 0x083f], // Samaritan
|
|
9
|
+
[0x0840, 0x085f], // Mandaic
|
|
10
|
+
[0x0860, 0x086f], // Syriac Supplement
|
|
11
|
+
[0x0870, 0x08ff], // Arabic Extended-B and -A
|
|
12
|
+
[0xfb1d, 0xfb4f], // Hebrew presentation forms
|
|
13
|
+
[0xfb50, 0xfdff], // Arabic presentation forms A
|
|
14
|
+
[0xfe70, 0xfeff], // Arabic presentation forms B
|
|
15
|
+
[0x1e800, 0x1e8df], // Mende Kikakui
|
|
16
|
+
[0x1e900, 0x1e95f], // Adlam
|
|
17
|
+
[0x1ee00, 0x1eeff], // Arabic mathematical alphabetic symbols
|
|
18
|
+
];
|
|
19
|
+
/** Arabic comma, semicolon, question mark, percent and the rest of the set. */
|
|
20
|
+
const ARABIC_PUNCTUATION = /[\u060C\u061B\u061F\u066A-\u066D\u06D4]/u;
|
|
21
|
+
const ARABIC_DIGITS = /[\u0660-\u0669\u06F0-\u06F9]/u;
|
|
22
|
+
const ANY_DIGIT = /\p{Nd}/u;
|
|
23
|
+
const MARK = /\p{M}/u;
|
|
24
|
+
const WHITESPACE_OR_FORMAT = /[\s\p{Z}\p{M}\p{Cf}]/u;
|
|
25
|
+
/** A neutral that joins a number when it sits between two digits: `1,000.5%`. */
|
|
26
|
+
const NUMERIC_JOINER = /[.,:+\-\u066B\u066C]/u;
|
|
27
|
+
/** A percent sign is part of the number it follows, not a sentence of its own. */
|
|
28
|
+
const NUMERIC_SUFFIX = /[%\u2030\u066A]/u;
|
|
29
|
+
/** True when the code point belongs to a script written right to left. */
|
|
30
|
+
export function isRtlCodePoint(codePoint) {
|
|
31
|
+
for (const [start, end] of RTL_RANGES) {
|
|
32
|
+
if (codePoint >= start && codePoint <= end)
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
/** True when the string contains at least one right-to-left character. */
|
|
38
|
+
export function hasRtl(text) {
|
|
39
|
+
for (const char of text) {
|
|
40
|
+
if (isRtlCodePoint(char.codePointAt(0) ?? 0))
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
/** True when the string contains Arabic letters, as opposed to just Arabic digits. */
|
|
46
|
+
export function hasArabic(text) {
|
|
47
|
+
for (const char of text) {
|
|
48
|
+
const codePoint = char.codePointAt(0) ?? 0;
|
|
49
|
+
const inArabicScript = (codePoint >= 0x0600 && codePoint <= 0x06ff) ||
|
|
50
|
+
(codePoint >= 0x0750 && codePoint <= 0x077f) ||
|
|
51
|
+
(codePoint >= 0xfb50 && codePoint <= 0xfdff) ||
|
|
52
|
+
(codePoint >= 0xfe70 && codePoint <= 0xfeff);
|
|
53
|
+
if (inArabicScript && /\p{L}/u.test(char))
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function baseClass(char) {
|
|
59
|
+
if (char === "\u200f")
|
|
60
|
+
return "rtl";
|
|
61
|
+
if (char === "\u200e")
|
|
62
|
+
return "ltr";
|
|
63
|
+
if (ARABIC_DIGITS.test(char))
|
|
64
|
+
return "rtlWeak";
|
|
65
|
+
if (ANY_DIGIT.test(char))
|
|
66
|
+
return "ltrWeak";
|
|
67
|
+
if (MARK.test(char))
|
|
68
|
+
return "join";
|
|
69
|
+
if (WHITESPACE_OR_FORMAT.test(char))
|
|
70
|
+
return "join";
|
|
71
|
+
if (ARABIC_PUNCTUATION.test(char))
|
|
72
|
+
return "rtlWeak";
|
|
73
|
+
if (isRtlCodePoint(char.codePointAt(0) ?? 0))
|
|
74
|
+
return "rtl";
|
|
75
|
+
// Letters are what is left: a symbol or a bracket is neutral, a letter is not.
|
|
76
|
+
return /\p{L}/u.test(char) ? "ltr" : "neutral";
|
|
77
|
+
}
|
|
78
|
+
function classAt(chars, index) {
|
|
79
|
+
const char = chars[index];
|
|
80
|
+
const cls = baseClass(char);
|
|
81
|
+
if (cls !== "neutral")
|
|
82
|
+
return cls;
|
|
83
|
+
const previous = chars[index - 1];
|
|
84
|
+
const next = chars[index + 1];
|
|
85
|
+
const numeric = (digit) => digit === undefined || !ANY_DIGIT.test(digit)
|
|
86
|
+
? null
|
|
87
|
+
: ARABIC_DIGITS.test(digit)
|
|
88
|
+
? "rtlWeak"
|
|
89
|
+
: "ltrWeak";
|
|
90
|
+
const beforeIsDigit = numeric(previous);
|
|
91
|
+
if (beforeIsDigit === null)
|
|
92
|
+
return cls;
|
|
93
|
+
if (NUMERIC_SUFFIX.test(char))
|
|
94
|
+
return beforeIsDigit;
|
|
95
|
+
if (!NUMERIC_JOINER.test(char))
|
|
96
|
+
return cls;
|
|
97
|
+
return numeric(next) === null ? cls : beforeIsDigit;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Splits text into runs that each have one direction.
|
|
101
|
+
*
|
|
102
|
+
* The browser implements the Unicode bidi algorithm correctly; what it cannot
|
|
103
|
+
* know is where the author meant a boundary to be. This answers that question,
|
|
104
|
+
* and nothing else:
|
|
105
|
+
*
|
|
106
|
+
* - a strong character starts a run and decides its direction;
|
|
107
|
+
* - Arabic digits and Arabic punctuation stay inside an Arabic run, so `مرحبا،`
|
|
108
|
+
* does not break in half;
|
|
109
|
+
* - Latin digits read left to right even inside Arabic, so `123.45%` stays one
|
|
110
|
+
* piece;
|
|
111
|
+
* - brackets, quotes and symbols are neutral: they take the direction of the
|
|
112
|
+
* runs on both sides when those agree, and otherwise the base direction — so
|
|
113
|
+
* an English full stop after an Arabic name ends the English sentence instead
|
|
114
|
+
* of moving to the wrong side of the name.
|
|
115
|
+
*
|
|
116
|
+
* Pass `base` when you know the paragraph direction — say, because the column
|
|
117
|
+
* is Arabic and this is one row of it. By default the base is the direction of
|
|
118
|
+
* the first strong character, which is what the browser would assume anyway.
|
|
119
|
+
*/
|
|
120
|
+
export function segments(text, base) {
|
|
121
|
+
const fallback = base ?? (detectDirection(text) === "rtl" ? "rtl" : "ltr");
|
|
122
|
+
const chars = Array.from(text);
|
|
123
|
+
const drafts = [];
|
|
124
|
+
let current = null;
|
|
125
|
+
let offset = 0;
|
|
126
|
+
for (let index = 0; index < chars.length; index += 1) {
|
|
127
|
+
const char = chars[index];
|
|
128
|
+
const start = offset;
|
|
129
|
+
const end = offset + char.length;
|
|
130
|
+
offset = end;
|
|
131
|
+
const cls = classAt(chars, index);
|
|
132
|
+
const dir = cls === "rtl" || cls === "rtlWeak" ? "rtl" : cls === "ltr" || cls === "ltrWeak" ? "ltr" : cls === "join" ? (current ? current.dir : "neutral") : "neutral";
|
|
133
|
+
if (current && current.dir === dir) {
|
|
134
|
+
current.text += char;
|
|
135
|
+
current.end = end;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (current)
|
|
139
|
+
drafts.push(current);
|
|
140
|
+
current = { text: char, dir, start, end };
|
|
141
|
+
}
|
|
142
|
+
if (current)
|
|
143
|
+
drafts.push(current);
|
|
144
|
+
const resolved = resolveNeutrals(drafts, fallback);
|
|
145
|
+
return merge(resolved);
|
|
146
|
+
}
|
|
147
|
+
/** N1/N2 in miniature: a neutral takes the direction of agreeing neighbours, else the base. */
|
|
148
|
+
function resolveNeutrals(drafts, base) {
|
|
149
|
+
return drafts.map((draft, index) => {
|
|
150
|
+
if (draft.dir !== "neutral")
|
|
151
|
+
return draft;
|
|
152
|
+
const before = previousDirection(drafts, index);
|
|
153
|
+
const after = nextDirection(drafts, index);
|
|
154
|
+
const fallback = before !== "neutral" && before === after ? before : base;
|
|
155
|
+
return { ...draft, dir: fallback };
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function previousDirection(drafts, index) {
|
|
159
|
+
for (let cursor = index - 1; cursor >= 0; cursor -= 1) {
|
|
160
|
+
const draft = drafts[cursor];
|
|
161
|
+
if (draft && draft.dir !== "neutral")
|
|
162
|
+
return draft.dir;
|
|
163
|
+
}
|
|
164
|
+
return "neutral";
|
|
165
|
+
}
|
|
166
|
+
function nextDirection(drafts, index) {
|
|
167
|
+
for (let cursor = index + 1; cursor < drafts.length; cursor += 1) {
|
|
168
|
+
const draft = drafts[cursor];
|
|
169
|
+
if (draft && draft.dir !== "neutral")
|
|
170
|
+
return draft.dir;
|
|
171
|
+
}
|
|
172
|
+
return "neutral";
|
|
173
|
+
}
|
|
174
|
+
function merge(drafts) {
|
|
175
|
+
const merged = [];
|
|
176
|
+
for (const draft of drafts) {
|
|
177
|
+
const last = merged[merged.length - 1];
|
|
178
|
+
if (last && last.dir === draft.dir) {
|
|
179
|
+
last.text += draft.text;
|
|
180
|
+
last.end = draft.end;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
merged.push({ text: draft.text, dir: draft.dir, start: draft.start, end: draft.end });
|
|
184
|
+
}
|
|
185
|
+
return merged;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The direction of the first strong character, which is what `dir="auto"`
|
|
189
|
+
* resolves to. `neutral` means the text has no strong character at all.
|
|
190
|
+
*/
|
|
191
|
+
export function detectDirection(text) {
|
|
192
|
+
const chars = Array.from(text);
|
|
193
|
+
for (let index = 0; index < chars.length; index += 1) {
|
|
194
|
+
const cls = baseClass(chars[index]);
|
|
195
|
+
if (cls === "rtl" || cls === "rtlWeak")
|
|
196
|
+
return "rtl";
|
|
197
|
+
if (cls === "ltr" || cls === "ltrWeak")
|
|
198
|
+
return "ltr";
|
|
199
|
+
}
|
|
200
|
+
return "neutral";
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Like {@link detectDirection}, but resolves `neutral` to `ltr` — the same
|
|
204
|
+
* fallback HTML uses for `dir="auto"`, so this is always safe to write to an
|
|
205
|
+
* attribute.
|
|
206
|
+
*/
|
|
207
|
+
export function resolveDirection(text) {
|
|
208
|
+
return detectDirection(text) === "rtl" ? "rtl" : "ltr";
|
|
209
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** The direction of a piece of text, as resolved by the bidi algorithm. */
|
|
2
|
+
export type Direction = "ltr" | "rtl" | "neutral";
|
|
3
|
+
/** A direction that can be written to an attribute: `auto` lets the browser infer it. */
|
|
4
|
+
export type AttributeDirection = Direction | "auto";
|
|
5
|
+
/** One run of text with a single resolved direction. */
|
|
6
|
+
export interface Segment {
|
|
7
|
+
/** The text of the run, exactly as it appears in the input. */
|
|
8
|
+
text: string;
|
|
9
|
+
/** The run's direction. `neutral` means no strong character was found. */
|
|
10
|
+
dir: Direction;
|
|
11
|
+
/** Index of the first character, in UTF-16 code units. */
|
|
12
|
+
start: number;
|
|
13
|
+
/** Index just past the last character, in UTF-16 code units. */
|
|
14
|
+
end: number;
|
|
15
|
+
}
|
|
16
|
+
/** Options for {@link isolate} and {@link toPlainIsolated}. */
|
|
17
|
+
export interface IsolateOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Which isolate to wrap in. `auto` (the default) uses the first-strong
|
|
20
|
+
* isolate, which lets the reader's own bidi algorithm decide.
|
|
21
|
+
*/
|
|
22
|
+
dir?: AttributeDirection;
|
|
23
|
+
}
|
|
24
|
+
/** Options shared by the HTML helpers. */
|
|
25
|
+
export interface HtmlOptions {
|
|
26
|
+
/** `auto` (the default) infers the element's direction from its content. */
|
|
27
|
+
dir?: AttributeDirection;
|
|
28
|
+
/** BCP-47 tag written to `lang`. */
|
|
29
|
+
lang?: string;
|
|
30
|
+
/** Extra class names. */
|
|
31
|
+
class?: string;
|
|
32
|
+
/** A `font-family` value applied as an inline style. */
|
|
33
|
+
font?: string;
|
|
34
|
+
/** Which element to emit. `<bdi>` (the default) isolates by design. */
|
|
35
|
+
element?: "bdi" | "span";
|
|
36
|
+
}
|
|
37
|
+
/** A piece of a paragraph: a string, or an explicit run with its own direction. */
|
|
38
|
+
export type Part = string | {
|
|
39
|
+
text: string;
|
|
40
|
+
dir?: AttributeDirection;
|
|
41
|
+
lang?: string;
|
|
42
|
+
/** A `font-family` value applied as an inline style. */
|
|
43
|
+
font?: string;
|
|
44
|
+
};
|
|
45
|
+
/** Options for {@link paragraph}. */
|
|
46
|
+
export interface ParagraphOptions {
|
|
47
|
+
/** `auto` (the default) resolves from the content's first strong character. */
|
|
48
|
+
dir?: AttributeDirection;
|
|
49
|
+
/** BCP-47 tag written to `lang`. */
|
|
50
|
+
lang?: string;
|
|
51
|
+
/** Extra class names. */
|
|
52
|
+
class?: string;
|
|
53
|
+
/**
|
|
54
|
+
* When true, string parts are treated as trusted HTML and inserted as-is.
|
|
55
|
+
* Never use it with user input.
|
|
56
|
+
*/
|
|
57
|
+
html?: boolean;
|
|
58
|
+
}
|
|
59
|
+
/** The smallest element shape {@link apply} needs, so it works without the DOM lib. */
|
|
60
|
+
export interface DirectionalElement {
|
|
61
|
+
textContent: string | null;
|
|
62
|
+
setAttribute?(name: string, value: string): void;
|
|
63
|
+
}
|
|
64
|
+
/** Font stacks for the scripts that meet in a mixed paragraph. */
|
|
65
|
+
export type Script = "arabic" | "hebrew" | "latin";
|
|
66
|
+
/** Options for {@link fontFace}. */
|
|
67
|
+
export interface FontFaceOptions {
|
|
68
|
+
/** The family name used in `src` and in the stack. */
|
|
69
|
+
family: string;
|
|
70
|
+
/** One or more `url(...)` or `local(...)` sources. */
|
|
71
|
+
src: string | string[];
|
|
72
|
+
/** Which script the face covers; decides the generated `unicode-range`. */
|
|
73
|
+
script?: Script;
|
|
74
|
+
weight?: string | number;
|
|
75
|
+
style?: string;
|
|
76
|
+
display?: "auto" | "block" | "swap" | "fallback" | "optional";
|
|
77
|
+
/** Overrides the generated `unicode-range`. */
|
|
78
|
+
unicodeRange?: string;
|
|
79
|
+
/** Nudges the rendered size for a fallback face, in percent. */
|
|
80
|
+
sizeAdjust?: string;
|
|
81
|
+
}
|
|
82
|
+
export type AuditRule = "bidi-control-character" | "inline-direction-without-isolation" | "physical-css-property" | "physical-text-align" | "document-without-lang";
|
|
83
|
+
/** One finding from {@link audit}. */
|
|
84
|
+
export interface AuditIssue {
|
|
85
|
+
rule: AuditRule;
|
|
86
|
+
severity: "error" | "warning";
|
|
87
|
+
message: string;
|
|
88
|
+
/** Index of the match in the audited string, in UTF-16 code units. */
|
|
89
|
+
index: number;
|
|
90
|
+
/** The offending text, truncated to keep output readable. */
|
|
91
|
+
snippet: string;
|
|
92
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "bidi-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "bidi-kit",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Right-to-left text that behaves: Arabic inside English, French inside Arabic, no bidi glitches.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bidi",
|
|
7
|
+
"rtl",
|
|
8
|
+
"arabic",
|
|
9
|
+
"i18n",
|
|
10
|
+
"unicode",
|
|
11
|
+
"text",
|
|
12
|
+
"font",
|
|
13
|
+
"isolation",
|
|
14
|
+
"hebrew"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "Nour Yahyaoui",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/Nour-yahyaoui/ui-storm.git",
|
|
21
|
+
"directory": "packages/bidi-kit"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"main": "./dist/cjs/index.js",
|
|
26
|
+
"module": "./dist/esm/index.js",
|
|
27
|
+
"types": "./dist/esm/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/esm/index.d.ts",
|
|
31
|
+
"import": "./dist/esm/index.js",
|
|
32
|
+
"require": "./dist/cjs/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"bin": {
|
|
37
|
+
"bidi-kit": "bin/bidi-kit.mjs"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"bin",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "node scripts/build.mjs",
|
|
50
|
+
"typecheck": "tsc -p tsconfig.esm.json --noEmit",
|
|
51
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
52
|
+
"demo": "node scripts/serve.mjs",
|
|
53
|
+
"prepublishOnly": "npm test && node scripts/sync-static.mjs --check",
|
|
54
|
+
"test:only": "node --test tests/*.test.mjs",
|
|
55
|
+
"pack:check": "npm run build && node scripts/check-tarball.mjs",
|
|
56
|
+
"static:sync": "npm run build && node scripts/sync-static.mjs",
|
|
57
|
+
"static:check": "npm run build && node scripts/sync-static.mjs --check"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"typescript": "^5.9.3"
|
|
61
|
+
},
|
|
62
|
+
"unpkg": "./dist/bidi-kit.global.js",
|
|
63
|
+
"jsdelivr": "./dist/bidi-kit.global.js"
|
|
64
|
+
}
|