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
package/dist/cjs/css.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ROOT_CSS = exports.CSS = void 0;
|
|
4
|
+
exports.inlineStyle = inlineStyle;
|
|
5
|
+
const fonts_js_1 = require("./fonts.js");
|
|
6
|
+
/**
|
|
7
|
+
* The rules that keep mixed-script text from breaking, as a string you can
|
|
8
|
+
* drop in a `<style>` tag or a stylesheet.
|
|
9
|
+
*
|
|
10
|
+
* Every line here fixes a specific, reproducible glitch:
|
|
11
|
+
*
|
|
12
|
+
* - `unicode-bidi: isolate` stops an inline element's direction from leaking
|
|
13
|
+
* into its neighbours;
|
|
14
|
+
* - `text-align: start` makes alignment follow the text instead of the page —
|
|
15
|
+
* `left` is a bug the moment one paragraph is Arabic;
|
|
16
|
+
* - the line-height on Arabic blocks is not aesthetics: naskh faces put marks
|
|
17
|
+
* above and below the baseline, and a 1.2 line-height cuts them off;
|
|
18
|
+
* - `white-space: normal` (and no `text-overflow: ellipsis` on RTL) keeps a
|
|
19
|
+
* wrapped Arabic sentence from being reordered across lines.
|
|
20
|
+
*/
|
|
21
|
+
exports.CSS = `:where([dir], [lang]):not(html, body) { unicode-bidi: isolate; }
|
|
22
|
+
[dir="rtl"] { text-align: right; direction: rtl; }
|
|
23
|
+
[dir="ltr"] { text-align: left; direction: ltr; }
|
|
24
|
+
[dir="auto"] { text-align: start; }
|
|
25
|
+
:where([lang^="ar"], [lang^="fa"], [lang^="ur"], [lang^="ps"], .script-arabic) {
|
|
26
|
+
font-family: ${fonts_js_1.FONT_STACKS.arabic};
|
|
27
|
+
font-feature-settings: ${fonts_js_1.ARABIC_FEATURES};
|
|
28
|
+
line-height: 1.9;
|
|
29
|
+
}
|
|
30
|
+
:where([lang^="he"], [lang^="yi"], .script-hebrew) {
|
|
31
|
+
font-family: ${fonts_js_1.FONT_STACKS.hebrew};
|
|
32
|
+
line-height: 1.7;
|
|
33
|
+
}
|
|
34
|
+
:where(bdi, [dir="auto"]) { unicode-bidi: isolate; }
|
|
35
|
+
`;
|
|
36
|
+
/**
|
|
37
|
+
* Rules to add once, on `html` or `body`, when a page mixes scripts and wants
|
|
38
|
+
* the browser to stop guessing.
|
|
39
|
+
*/
|
|
40
|
+
exports.ROOT_CSS = `html { unicode-bidi: plaintext; }
|
|
41
|
+
body { text-align: start; }
|
|
42
|
+
:lang(ar) { font-variant-numeric: normal; }
|
|
43
|
+
`;
|
|
44
|
+
/** A single declaration block for one element, for inline-style applications. */
|
|
45
|
+
function inlineStyle(options = {}) {
|
|
46
|
+
const declarations = [];
|
|
47
|
+
if (options.dir) {
|
|
48
|
+
declarations.push(`direction: ${options.dir === "auto" ? "inherit" : options.dir}`);
|
|
49
|
+
declarations.push("unicode-bidi: isolate");
|
|
50
|
+
}
|
|
51
|
+
if (options.script) {
|
|
52
|
+
declarations.push(`font-family: ${fonts_js_1.FONT_STACKS[options.script]}`);
|
|
53
|
+
if (options.script === "arabic") {
|
|
54
|
+
declarations.push(`font-feature-settings: ${fonts_js_1.ARABIC_FEATURES}`);
|
|
55
|
+
declarations.push("line-height: 1.9");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return declarations.join("; ");
|
|
59
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UNICODE_RANGES = exports.ARABIC_FEATURES = exports.FONT_STACKS = void 0;
|
|
4
|
+
exports.fontStack = fontStack;
|
|
5
|
+
exports.fontFace = fontFace;
|
|
6
|
+
exports.fontFamilyRule = fontFamilyRule;
|
|
7
|
+
/**
|
|
8
|
+
* Font stacks per script.
|
|
9
|
+
*
|
|
10
|
+
* Two failures produce the "broken Arabic" look people blame on bidi:
|
|
11
|
+
*
|
|
12
|
+
* 1. the Latin face has no Arabic glyphs, so the browser falls back per glyph —
|
|
13
|
+
* the word changes shape halfway through and the diacritics sit too low;
|
|
14
|
+
* 2. the Arabic face is a *naskh* text face rendered at a Latin line-height,
|
|
15
|
+
* and the kasra, the hamza below and the tail of a final yeh are clipped.
|
|
16
|
+
*
|
|
17
|
+
* The stacks below put a known-good Arabic face first, keep a Latin face that
|
|
18
|
+
* does not interfere, and end in `sans-serif` rather than in nothing.
|
|
19
|
+
*/
|
|
20
|
+
exports.FONT_STACKS = {
|
|
21
|
+
arabic: [
|
|
22
|
+
'"Noto Naskh Arabic"',
|
|
23
|
+
'"Noto Sans Arabic"',
|
|
24
|
+
'"Amiri"',
|
|
25
|
+
'"Scheherazade New"',
|
|
26
|
+
'"Geeza Pro"',
|
|
27
|
+
'"Segoe UI"',
|
|
28
|
+
'"Noto Sans"',
|
|
29
|
+
"sans-serif",
|
|
30
|
+
].join(", "),
|
|
31
|
+
hebrew: ['"Noto Sans Hebrew"', '"Arial Hebrew"', '"Gisha"', '"David CLM"', "sans-serif"].join(", "),
|
|
32
|
+
latin: ['"Noto Sans"', "system-ui", "-apple-system", '"Segoe UI"', "Roboto", "sans-serif"].join(", "),
|
|
33
|
+
};
|
|
34
|
+
/** The stack to use for a locale tag; anything not RTL gets the Latin stack. */
|
|
35
|
+
function fontStack(locale) {
|
|
36
|
+
if (!locale)
|
|
37
|
+
return exports.FONT_STACKS.latin;
|
|
38
|
+
const language = locale.toLowerCase().split(/[-_]/)[0] ?? "";
|
|
39
|
+
if (language === "ar" || language === "fa" || language === "ur" || language === "ps") {
|
|
40
|
+
return exports.FONT_STACKS.arabic;
|
|
41
|
+
}
|
|
42
|
+
if (language === "he" || language === "yi")
|
|
43
|
+
return exports.FONT_STACKS.hebrew;
|
|
44
|
+
return exports.FONT_STACKS.latin;
|
|
45
|
+
}
|
|
46
|
+
/** The `font-feature-settings` that keep Arabic words from looking two-tone. */
|
|
47
|
+
exports.ARABIC_FEATURES = '"kern" 1, "liga" 1, "calt" 1, "rlig" 1, "init" 1, "medi" 1, "fina" 1';
|
|
48
|
+
/**
|
|
49
|
+
* The `unicode-range` for a script, so a downloaded face is only fetched for
|
|
50
|
+
* the characters it actually covers. Without it, a page with one Arabic
|
|
51
|
+
* headline downloads the Arabic font for every reader.
|
|
52
|
+
*/
|
|
53
|
+
exports.UNICODE_RANGES = {
|
|
54
|
+
arabic: "U+0600-06FF, U+0750-077F, U+0870-088E, U+08A0-08FF, U+FB50-FDFF, U+FE70-FEFF, U+1EE00-1EEFF",
|
|
55
|
+
hebrew: "U+0590-05FF, U+FB1D-FB4F",
|
|
56
|
+
latin: "U+0000-00FF, U+0131, U+0152-0153, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193",
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Builds an `@font-face` rule.
|
|
60
|
+
*
|
|
61
|
+
* It is deliberately a string and not a file: the package ships no fonts, so a
|
|
62
|
+
* consumer decides where the bytes come from — a local install, a CDN, or a
|
|
63
|
+
* self-hosted file — and this only removes the chance of getting the
|
|
64
|
+
* `unicode-range` and the metrics wrong while doing it.
|
|
65
|
+
*/
|
|
66
|
+
function fontFace(options) {
|
|
67
|
+
const script = options.script ?? "arabic";
|
|
68
|
+
const sources = Array.isArray(options.src) ? options.src : [options.src];
|
|
69
|
+
const lines = [
|
|
70
|
+
` font-family: "${options.family}";`,
|
|
71
|
+
` src: ${sources.join(", ")};`,
|
|
72
|
+
` font-weight: ${options.weight ?? "400"};`,
|
|
73
|
+
` font-style: ${options.style ?? "normal"};`,
|
|
74
|
+
` font-display: ${options.display ?? "swap"};`,
|
|
75
|
+
` unicode-range: ${options.unicodeRange ?? exports.UNICODE_RANGES[script]};`,
|
|
76
|
+
];
|
|
77
|
+
if (options.sizeAdjust)
|
|
78
|
+
lines.push(` size-adjust: ${options.sizeAdjust};`);
|
|
79
|
+
return `@font-face {\n${lines.join("\n")}\n}`;
|
|
80
|
+
}
|
|
81
|
+
/** A `font-family` declaration that pairs an Arabic face with a Latin one. */
|
|
82
|
+
function fontFamilyRule(selector, script = "arabic") {
|
|
83
|
+
return `${selector} {\n font-family: ${exports.FONT_STACKS[script]};\n}`;
|
|
84
|
+
}
|
package/dist/cjs/html.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.escapeHtml = escapeHtml;
|
|
4
|
+
exports.bdi = bdi;
|
|
5
|
+
exports.inline = inline;
|
|
6
|
+
exports.paragraph = paragraph;
|
|
7
|
+
exports.apply = apply;
|
|
8
|
+
const segment_js_1 = require("./segment.js");
|
|
9
|
+
const ESCAPES = {
|
|
10
|
+
"&": "&",
|
|
11
|
+
"<": "<",
|
|
12
|
+
">": ">",
|
|
13
|
+
'"': """,
|
|
14
|
+
"'": "'",
|
|
15
|
+
};
|
|
16
|
+
/** Escapes text for use as element content or in a quoted attribute value. */
|
|
17
|
+
function escapeHtml(text) {
|
|
18
|
+
return text.replace(/[&<>"']/g, (char) => ESCAPES[char] ?? char);
|
|
19
|
+
}
|
|
20
|
+
function attribute(name, value) {
|
|
21
|
+
return value === undefined || value === "" ? "" : ` ${name}="${escapeHtml(value)}"`;
|
|
22
|
+
}
|
|
23
|
+
function styleValue(font) {
|
|
24
|
+
return font ? `font-family: ${font}` : undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A `<bdi>` element wrapping `text`.
|
|
28
|
+
*
|
|
29
|
+
* `<bdi>` is the one HTML element whose whole job is this problem: it isolates
|
|
30
|
+
* its content from the surrounding paragraph, so neither the reader's text nor
|
|
31
|
+
* the browser's bidi algorithm can drag a neighbouring word into the wrong
|
|
32
|
+
* place. Reach for it before reaching for a mark you cannot see.
|
|
33
|
+
*/
|
|
34
|
+
function bdi(text, options = {}) {
|
|
35
|
+
return inline(text, { ...options, element: options.element ?? "bdi" });
|
|
36
|
+
}
|
|
37
|
+
/** A `<span dir>` that isolates by style instead of by element. */
|
|
38
|
+
function inline(text, options = {}) {
|
|
39
|
+
const element = options.element ?? "span";
|
|
40
|
+
const dir = options.dir ?? "auto";
|
|
41
|
+
const attributes = [
|
|
42
|
+
dir === "auto" && element === "bdi" ? "" : attribute("dir", dir),
|
|
43
|
+
attribute("lang", options.lang),
|
|
44
|
+
attribute("class", options.class),
|
|
45
|
+
attribute("style", styleValue(options.font)),
|
|
46
|
+
].join("");
|
|
47
|
+
return `<${element}${attributes}>${escapeHtml(text)}</${element}>`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Builds one paragraph out of mixed parts.
|
|
51
|
+
*
|
|
52
|
+
* Named parts get an explicit direction and, if you pass one, a font: that is
|
|
53
|
+
* how an Arabic sentence in a French paragraph ends up in a face that actually
|
|
54
|
+
* has the glyphs, instead of a fallback the operating system picked without
|
|
55
|
+
* asking. Strings are treated as plain text and escaped unless `html` is set.
|
|
56
|
+
*/
|
|
57
|
+
function paragraph(parts, options = {}) {
|
|
58
|
+
const plain = parts
|
|
59
|
+
.map((part) => {
|
|
60
|
+
const text = typeof part === "string" ? part : part.text;
|
|
61
|
+
// The direction of a paragraph comes from its words, not from its tags.
|
|
62
|
+
return options.html ? text.replace(/<[^>]*>/g, " ") : text;
|
|
63
|
+
})
|
|
64
|
+
.join("");
|
|
65
|
+
const dir = options.dir === undefined || options.dir === "auto" ? (0, segment_js_1.resolveDirection)(plain) : options.dir;
|
|
66
|
+
const body = parts
|
|
67
|
+
.map((part) => {
|
|
68
|
+
if (typeof part === "string")
|
|
69
|
+
return options.html ? part : escapeHtml(part);
|
|
70
|
+
const attributes = [
|
|
71
|
+
attribute("dir", part.dir ?? "auto"),
|
|
72
|
+
attribute("lang", part.lang),
|
|
73
|
+
attribute("style", styleValue(part.font)),
|
|
74
|
+
].join("");
|
|
75
|
+
const inner = options.html ? part.text : escapeHtml(part.text);
|
|
76
|
+
return `<bdi${attributes}>${inner}</bdi>`;
|
|
77
|
+
})
|
|
78
|
+
.join("");
|
|
79
|
+
const attributes = [
|
|
80
|
+
attribute("dir", dir),
|
|
81
|
+
attribute("lang", options.lang),
|
|
82
|
+
attribute("class", options.class),
|
|
83
|
+
].join("");
|
|
84
|
+
return `<p${attributes}>${body}</p>`;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Sets element text and direction in one call. The container decides the
|
|
88
|
+
* paragraph's base direction; each run inside it is left to the bidi
|
|
89
|
+
* algorithm, which is what makes an English sentence with an Arabic name look
|
|
90
|
+
* like an English sentence.
|
|
91
|
+
*/
|
|
92
|
+
function apply(element, text, options = {}) {
|
|
93
|
+
element.textContent = text;
|
|
94
|
+
element.setAttribute?.("dir", options.dir ?? (0, segment_js_1.resolveDirection)(text));
|
|
95
|
+
return element;
|
|
96
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* bidi-kit — mixed-direction text that behaves.
|
|
4
|
+
*
|
|
5
|
+
* The browser already implements the Unicode bidi algorithm correctly. What it
|
|
6
|
+
* cannot know is where the author meant a boundary to be, which face has the
|
|
7
|
+
* glyphs, or that a paragraph is Arabic until it has read the first character.
|
|
8
|
+
* This package supplies those answers, and nothing else: no runtime
|
|
9
|
+
* dependencies, no fonts, no framework.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.formatAudit = exports.audit = exports.inlineStyle = exports.ROOT_CSS = exports.CSS = exports.fontFamilyRule = exports.fontFace = exports.fontStack = exports.UNICODE_RANGES = exports.ARABIC_FEATURES = exports.FONT_STACKS = exports.apply = exports.escapeHtml = exports.paragraph = exports.inline = exports.bdi = exports.PDI = exports.FSI = exports.RLI = exports.LRI = exports.BIDI_CONTROL_CHARACTERS = exports.stripBidiControls = exports.hasBidiControls = exports.isolateRunsText = exports.isolate = exports.isRtlCodePoint = exports.hasArabic = exports.hasRtl = exports.resolveDirection = exports.detectDirection = exports.segments = void 0;
|
|
13
|
+
var segment_js_1 = require("./segment.js");
|
|
14
|
+
Object.defineProperty(exports, "segments", { enumerable: true, get: function () { return segment_js_1.segments; } });
|
|
15
|
+
Object.defineProperty(exports, "detectDirection", { enumerable: true, get: function () { return segment_js_1.detectDirection; } });
|
|
16
|
+
Object.defineProperty(exports, "resolveDirection", { enumerable: true, get: function () { return segment_js_1.resolveDirection; } });
|
|
17
|
+
Object.defineProperty(exports, "hasRtl", { enumerable: true, get: function () { return segment_js_1.hasRtl; } });
|
|
18
|
+
Object.defineProperty(exports, "hasArabic", { enumerable: true, get: function () { return segment_js_1.hasArabic; } });
|
|
19
|
+
Object.defineProperty(exports, "isRtlCodePoint", { enumerable: true, get: function () { return segment_js_1.isRtlCodePoint; } });
|
|
20
|
+
var isolate_js_1 = require("./isolate.js");
|
|
21
|
+
Object.defineProperty(exports, "isolate", { enumerable: true, get: function () { return isolate_js_1.isolate; } });
|
|
22
|
+
Object.defineProperty(exports, "isolateRunsText", { enumerable: true, get: function () { return isolate_js_1.isolateRunsText; } });
|
|
23
|
+
Object.defineProperty(exports, "hasBidiControls", { enumerable: true, get: function () { return isolate_js_1.hasBidiControls; } });
|
|
24
|
+
Object.defineProperty(exports, "stripBidiControls", { enumerable: true, get: function () { return isolate_js_1.stripBidiControls; } });
|
|
25
|
+
Object.defineProperty(exports, "BIDI_CONTROL_CHARACTERS", { enumerable: true, get: function () { return isolate_js_1.BIDI_CONTROL_CHARACTERS; } });
|
|
26
|
+
Object.defineProperty(exports, "LRI", { enumerable: true, get: function () { return isolate_js_1.LRI; } });
|
|
27
|
+
Object.defineProperty(exports, "RLI", { enumerable: true, get: function () { return isolate_js_1.RLI; } });
|
|
28
|
+
Object.defineProperty(exports, "FSI", { enumerable: true, get: function () { return isolate_js_1.FSI; } });
|
|
29
|
+
Object.defineProperty(exports, "PDI", { enumerable: true, get: function () { return isolate_js_1.PDI; } });
|
|
30
|
+
var html_js_1 = require("./html.js");
|
|
31
|
+
Object.defineProperty(exports, "bdi", { enumerable: true, get: function () { return html_js_1.bdi; } });
|
|
32
|
+
Object.defineProperty(exports, "inline", { enumerable: true, get: function () { return html_js_1.inline; } });
|
|
33
|
+
Object.defineProperty(exports, "paragraph", { enumerable: true, get: function () { return html_js_1.paragraph; } });
|
|
34
|
+
Object.defineProperty(exports, "escapeHtml", { enumerable: true, get: function () { return html_js_1.escapeHtml; } });
|
|
35
|
+
Object.defineProperty(exports, "apply", { enumerable: true, get: function () { return html_js_1.apply; } });
|
|
36
|
+
var fonts_js_1 = require("./fonts.js");
|
|
37
|
+
Object.defineProperty(exports, "FONT_STACKS", { enumerable: true, get: function () { return fonts_js_1.FONT_STACKS; } });
|
|
38
|
+
Object.defineProperty(exports, "ARABIC_FEATURES", { enumerable: true, get: function () { return fonts_js_1.ARABIC_FEATURES; } });
|
|
39
|
+
Object.defineProperty(exports, "UNICODE_RANGES", { enumerable: true, get: function () { return fonts_js_1.UNICODE_RANGES; } });
|
|
40
|
+
Object.defineProperty(exports, "fontStack", { enumerable: true, get: function () { return fonts_js_1.fontStack; } });
|
|
41
|
+
Object.defineProperty(exports, "fontFace", { enumerable: true, get: function () { return fonts_js_1.fontFace; } });
|
|
42
|
+
Object.defineProperty(exports, "fontFamilyRule", { enumerable: true, get: function () { return fonts_js_1.fontFamilyRule; } });
|
|
43
|
+
var css_js_1 = require("./css.js");
|
|
44
|
+
Object.defineProperty(exports, "CSS", { enumerable: true, get: function () { return css_js_1.CSS; } });
|
|
45
|
+
Object.defineProperty(exports, "ROOT_CSS", { enumerable: true, get: function () { return css_js_1.ROOT_CSS; } });
|
|
46
|
+
Object.defineProperty(exports, "inlineStyle", { enumerable: true, get: function () { return css_js_1.inlineStyle; } });
|
|
47
|
+
var audit_js_1 = require("./audit.js");
|
|
48
|
+
Object.defineProperty(exports, "audit", { enumerable: true, get: function () { return audit_js_1.audit; } });
|
|
49
|
+
Object.defineProperty(exports, "formatAudit", { enumerable: true, get: function () { return audit_js_1.formatAudit; } });
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BIDI_CONTROL_CHARACTERS = exports.PDI = exports.FSI = exports.RLI = exports.LRI = void 0;
|
|
4
|
+
exports.isolate = isolate;
|
|
5
|
+
exports.hasBidiControls = hasBidiControls;
|
|
6
|
+
exports.stripBidiControls = stripBidiControls;
|
|
7
|
+
exports.isolateRunsText = isolateRunsText;
|
|
8
|
+
const segment_js_1 = require("./segment.js");
|
|
9
|
+
/** Left-to-right isolate. */
|
|
10
|
+
exports.LRI = "\u2066";
|
|
11
|
+
/** Right-to-left isolate. */
|
|
12
|
+
exports.RLI = "\u2067";
|
|
13
|
+
/** First-strong isolate: the reader's bidi algorithm picks the direction. */
|
|
14
|
+
exports.FSI = "\u2068";
|
|
15
|
+
/** Pop directional isolate. */
|
|
16
|
+
exports.PDI = "\u2069";
|
|
17
|
+
/** Every character that can change the visual order without being visible. */
|
|
18
|
+
const BIDI_CONTROLS = /[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g;
|
|
19
|
+
/**
|
|
20
|
+
* Wraps text in an isolate, for plain-text contexts where an element is not an
|
|
21
|
+
* option: an `aria-label`, a canvas, a terminal, a spreadsheet cell, a JSON
|
|
22
|
+
* field that something else will render.
|
|
23
|
+
*
|
|
24
|
+
* In HTML, prefer {@link bdi} — an element is inspectable, stylable and cannot
|
|
25
|
+
* leak a stray `PDI` when a string is concatenated badly.
|
|
26
|
+
*/
|
|
27
|
+
function isolate(text, options = {}) {
|
|
28
|
+
if (text === "")
|
|
29
|
+
return "";
|
|
30
|
+
const dir = options.dir ?? "auto";
|
|
31
|
+
if (dir === "rtl")
|
|
32
|
+
return exports.RLI + text + exports.PDI;
|
|
33
|
+
if (dir === "ltr")
|
|
34
|
+
return exports.LRI + text + exports.PDI;
|
|
35
|
+
return exports.FSI + text + exports.PDI;
|
|
36
|
+
}
|
|
37
|
+
/** True when the string carries invisible bidi control characters. */
|
|
38
|
+
function hasBidiControls(text) {
|
|
39
|
+
return /[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/u.test(text);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Removes every invisible bidi control character.
|
|
43
|
+
*
|
|
44
|
+
* Use this on anything a stranger wrote. Overrides and isolates are the raw
|
|
45
|
+
* material of display-spoofing bugs — the class of attack that turned source
|
|
46
|
+
* code review into a guessing game — and a text field that passes them through
|
|
47
|
+
* to a terminal, a diff or a filename is a bug, not a feature.
|
|
48
|
+
*/
|
|
49
|
+
function stripBidiControls(text) {
|
|
50
|
+
return text.replace(BIDI_CONTROLS, "");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Rewrites text so each run is explicitly isolated, for plain-text output.
|
|
54
|
+
*
|
|
55
|
+
* Unlike {@link isolate}, which wraps the whole string, this keeps an English
|
|
56
|
+
* sentence readable by a terminal that only understands the bidi algorithm: the
|
|
57
|
+
* Arabic run gets an RLI/PDI pair and everything else is left alone.
|
|
58
|
+
*/
|
|
59
|
+
function isolateRunsText(text) {
|
|
60
|
+
return (0, segment_js_1.segments)(text)
|
|
61
|
+
.map((segment) => {
|
|
62
|
+
if (segment.dir === "rtl")
|
|
63
|
+
return exports.RLI + segment.text + exports.PDI;
|
|
64
|
+
if (segment.dir === "ltr")
|
|
65
|
+
return exports.LRI + segment.text + exports.PDI;
|
|
66
|
+
return segment.text;
|
|
67
|
+
})
|
|
68
|
+
.join("");
|
|
69
|
+
}
|
|
70
|
+
/** The invisible characters, exported so a UI can explain what it stripped. */
|
|
71
|
+
exports.BIDI_CONTROL_CHARACTERS = [
|
|
72
|
+
"\u061C",
|
|
73
|
+
"\u200E",
|
|
74
|
+
"\u200F",
|
|
75
|
+
"\u202A",
|
|
76
|
+
"\u202B",
|
|
77
|
+
"\u202C",
|
|
78
|
+
"\u202D",
|
|
79
|
+
"\u202E",
|
|
80
|
+
"\u2066",
|
|
81
|
+
"\u2067",
|
|
82
|
+
"\u2068",
|
|
83
|
+
"\u2069",
|
|
84
|
+
];
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRtlCodePoint = isRtlCodePoint;
|
|
4
|
+
exports.hasRtl = hasRtl;
|
|
5
|
+
exports.hasArabic = hasArabic;
|
|
6
|
+
exports.segments = segments;
|
|
7
|
+
exports.detectDirection = detectDirection;
|
|
8
|
+
exports.resolveDirection = resolveDirection;
|
|
9
|
+
const RTL_RANGES = [
|
|
10
|
+
[0x0590, 0x05ff], // Hebrew
|
|
11
|
+
[0x0600, 0x06ff], // Arabic
|
|
12
|
+
[0x0700, 0x074f], // Syriac
|
|
13
|
+
[0x0750, 0x077f], // Arabic Supplement
|
|
14
|
+
[0x0780, 0x07bf], // Thaana
|
|
15
|
+
[0x07c0, 0x07ff], // NKo
|
|
16
|
+
[0x0800, 0x083f], // Samaritan
|
|
17
|
+
[0x0840, 0x085f], // Mandaic
|
|
18
|
+
[0x0860, 0x086f], // Syriac Supplement
|
|
19
|
+
[0x0870, 0x08ff], // Arabic Extended-B and -A
|
|
20
|
+
[0xfb1d, 0xfb4f], // Hebrew presentation forms
|
|
21
|
+
[0xfb50, 0xfdff], // Arabic presentation forms A
|
|
22
|
+
[0xfe70, 0xfeff], // Arabic presentation forms B
|
|
23
|
+
[0x1e800, 0x1e8df], // Mende Kikakui
|
|
24
|
+
[0x1e900, 0x1e95f], // Adlam
|
|
25
|
+
[0x1ee00, 0x1eeff], // Arabic mathematical alphabetic symbols
|
|
26
|
+
];
|
|
27
|
+
/** Arabic comma, semicolon, question mark, percent and the rest of the set. */
|
|
28
|
+
const ARABIC_PUNCTUATION = /[\u060C\u061B\u061F\u066A-\u066D\u06D4]/u;
|
|
29
|
+
const ARABIC_DIGITS = /[\u0660-\u0669\u06F0-\u06F9]/u;
|
|
30
|
+
const ANY_DIGIT = /\p{Nd}/u;
|
|
31
|
+
const MARK = /\p{M}/u;
|
|
32
|
+
const WHITESPACE_OR_FORMAT = /[\s\p{Z}\p{M}\p{Cf}]/u;
|
|
33
|
+
/** A neutral that joins a number when it sits between two digits: `1,000.5%`. */
|
|
34
|
+
const NUMERIC_JOINER = /[.,:+\-\u066B\u066C]/u;
|
|
35
|
+
/** A percent sign is part of the number it follows, not a sentence of its own. */
|
|
36
|
+
const NUMERIC_SUFFIX = /[%\u2030\u066A]/u;
|
|
37
|
+
/** True when the code point belongs to a script written right to left. */
|
|
38
|
+
function isRtlCodePoint(codePoint) {
|
|
39
|
+
for (const [start, end] of RTL_RANGES) {
|
|
40
|
+
if (codePoint >= start && codePoint <= end)
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
/** True when the string contains at least one right-to-left character. */
|
|
46
|
+
function hasRtl(text) {
|
|
47
|
+
for (const char of text) {
|
|
48
|
+
if (isRtlCodePoint(char.codePointAt(0) ?? 0))
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
/** True when the string contains Arabic letters, as opposed to just Arabic digits. */
|
|
54
|
+
function hasArabic(text) {
|
|
55
|
+
for (const char of text) {
|
|
56
|
+
const codePoint = char.codePointAt(0) ?? 0;
|
|
57
|
+
const inArabicScript = (codePoint >= 0x0600 && codePoint <= 0x06ff) ||
|
|
58
|
+
(codePoint >= 0x0750 && codePoint <= 0x077f) ||
|
|
59
|
+
(codePoint >= 0xfb50 && codePoint <= 0xfdff) ||
|
|
60
|
+
(codePoint >= 0xfe70 && codePoint <= 0xfeff);
|
|
61
|
+
if (inArabicScript && /\p{L}/u.test(char))
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
function baseClass(char) {
|
|
67
|
+
if (char === "\u200f")
|
|
68
|
+
return "rtl";
|
|
69
|
+
if (char === "\u200e")
|
|
70
|
+
return "ltr";
|
|
71
|
+
if (ARABIC_DIGITS.test(char))
|
|
72
|
+
return "rtlWeak";
|
|
73
|
+
if (ANY_DIGIT.test(char))
|
|
74
|
+
return "ltrWeak";
|
|
75
|
+
if (MARK.test(char))
|
|
76
|
+
return "join";
|
|
77
|
+
if (WHITESPACE_OR_FORMAT.test(char))
|
|
78
|
+
return "join";
|
|
79
|
+
if (ARABIC_PUNCTUATION.test(char))
|
|
80
|
+
return "rtlWeak";
|
|
81
|
+
if (isRtlCodePoint(char.codePointAt(0) ?? 0))
|
|
82
|
+
return "rtl";
|
|
83
|
+
// Letters are what is left: a symbol or a bracket is neutral, a letter is not.
|
|
84
|
+
return /\p{L}/u.test(char) ? "ltr" : "neutral";
|
|
85
|
+
}
|
|
86
|
+
function classAt(chars, index) {
|
|
87
|
+
const char = chars[index];
|
|
88
|
+
const cls = baseClass(char);
|
|
89
|
+
if (cls !== "neutral")
|
|
90
|
+
return cls;
|
|
91
|
+
const previous = chars[index - 1];
|
|
92
|
+
const next = chars[index + 1];
|
|
93
|
+
const numeric = (digit) => digit === undefined || !ANY_DIGIT.test(digit)
|
|
94
|
+
? null
|
|
95
|
+
: ARABIC_DIGITS.test(digit)
|
|
96
|
+
? "rtlWeak"
|
|
97
|
+
: "ltrWeak";
|
|
98
|
+
const beforeIsDigit = numeric(previous);
|
|
99
|
+
if (beforeIsDigit === null)
|
|
100
|
+
return cls;
|
|
101
|
+
if (NUMERIC_SUFFIX.test(char))
|
|
102
|
+
return beforeIsDigit;
|
|
103
|
+
if (!NUMERIC_JOINER.test(char))
|
|
104
|
+
return cls;
|
|
105
|
+
return numeric(next) === null ? cls : beforeIsDigit;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Splits text into runs that each have one direction.
|
|
109
|
+
*
|
|
110
|
+
* The browser implements the Unicode bidi algorithm correctly; what it cannot
|
|
111
|
+
* know is where the author meant a boundary to be. This answers that question,
|
|
112
|
+
* and nothing else:
|
|
113
|
+
*
|
|
114
|
+
* - a strong character starts a run and decides its direction;
|
|
115
|
+
* - Arabic digits and Arabic punctuation stay inside an Arabic run, so `مرحبا،`
|
|
116
|
+
* does not break in half;
|
|
117
|
+
* - Latin digits read left to right even inside Arabic, so `123.45%` stays one
|
|
118
|
+
* piece;
|
|
119
|
+
* - brackets, quotes and symbols are neutral: they take the direction of the
|
|
120
|
+
* runs on both sides when those agree, and otherwise the base direction — so
|
|
121
|
+
* an English full stop after an Arabic name ends the English sentence instead
|
|
122
|
+
* of moving to the wrong side of the name.
|
|
123
|
+
*
|
|
124
|
+
* Pass `base` when you know the paragraph direction — say, because the column
|
|
125
|
+
* is Arabic and this is one row of it. By default the base is the direction of
|
|
126
|
+
* the first strong character, which is what the browser would assume anyway.
|
|
127
|
+
*/
|
|
128
|
+
function segments(text, base) {
|
|
129
|
+
const fallback = base ?? (detectDirection(text) === "rtl" ? "rtl" : "ltr");
|
|
130
|
+
const chars = Array.from(text);
|
|
131
|
+
const drafts = [];
|
|
132
|
+
let current = null;
|
|
133
|
+
let offset = 0;
|
|
134
|
+
for (let index = 0; index < chars.length; index += 1) {
|
|
135
|
+
const char = chars[index];
|
|
136
|
+
const start = offset;
|
|
137
|
+
const end = offset + char.length;
|
|
138
|
+
offset = end;
|
|
139
|
+
const cls = classAt(chars, index);
|
|
140
|
+
const dir = cls === "rtl" || cls === "rtlWeak" ? "rtl" : cls === "ltr" || cls === "ltrWeak" ? "ltr" : cls === "join" ? (current ? current.dir : "neutral") : "neutral";
|
|
141
|
+
if (current && current.dir === dir) {
|
|
142
|
+
current.text += char;
|
|
143
|
+
current.end = end;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (current)
|
|
147
|
+
drafts.push(current);
|
|
148
|
+
current = { text: char, dir, start, end };
|
|
149
|
+
}
|
|
150
|
+
if (current)
|
|
151
|
+
drafts.push(current);
|
|
152
|
+
const resolved = resolveNeutrals(drafts, fallback);
|
|
153
|
+
return merge(resolved);
|
|
154
|
+
}
|
|
155
|
+
/** N1/N2 in miniature: a neutral takes the direction of agreeing neighbours, else the base. */
|
|
156
|
+
function resolveNeutrals(drafts, base) {
|
|
157
|
+
return drafts.map((draft, index) => {
|
|
158
|
+
if (draft.dir !== "neutral")
|
|
159
|
+
return draft;
|
|
160
|
+
const before = previousDirection(drafts, index);
|
|
161
|
+
const after = nextDirection(drafts, index);
|
|
162
|
+
const fallback = before !== "neutral" && before === after ? before : base;
|
|
163
|
+
return { ...draft, dir: fallback };
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
function previousDirection(drafts, index) {
|
|
167
|
+
for (let cursor = index - 1; cursor >= 0; cursor -= 1) {
|
|
168
|
+
const draft = drafts[cursor];
|
|
169
|
+
if (draft && draft.dir !== "neutral")
|
|
170
|
+
return draft.dir;
|
|
171
|
+
}
|
|
172
|
+
return "neutral";
|
|
173
|
+
}
|
|
174
|
+
function nextDirection(drafts, index) {
|
|
175
|
+
for (let cursor = index + 1; cursor < drafts.length; cursor += 1) {
|
|
176
|
+
const draft = drafts[cursor];
|
|
177
|
+
if (draft && draft.dir !== "neutral")
|
|
178
|
+
return draft.dir;
|
|
179
|
+
}
|
|
180
|
+
return "neutral";
|
|
181
|
+
}
|
|
182
|
+
function merge(drafts) {
|
|
183
|
+
const merged = [];
|
|
184
|
+
for (const draft of drafts) {
|
|
185
|
+
const last = merged[merged.length - 1];
|
|
186
|
+
if (last && last.dir === draft.dir) {
|
|
187
|
+
last.text += draft.text;
|
|
188
|
+
last.end = draft.end;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
merged.push({ text: draft.text, dir: draft.dir, start: draft.start, end: draft.end });
|
|
192
|
+
}
|
|
193
|
+
return merged;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The direction of the first strong character, which is what `dir="auto"`
|
|
197
|
+
* resolves to. `neutral` means the text has no strong character at all.
|
|
198
|
+
*/
|
|
199
|
+
function detectDirection(text) {
|
|
200
|
+
const chars = Array.from(text);
|
|
201
|
+
for (let index = 0; index < chars.length; index += 1) {
|
|
202
|
+
const cls = baseClass(chars[index]);
|
|
203
|
+
if (cls === "rtl" || cls === "rtlWeak")
|
|
204
|
+
return "rtl";
|
|
205
|
+
if (cls === "ltr" || cls === "ltrWeak")
|
|
206
|
+
return "ltr";
|
|
207
|
+
}
|
|
208
|
+
return "neutral";
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Like {@link detectDirection}, but resolves `neutral` to `ltr` — the same
|
|
212
|
+
* fallback HTML uses for `dir="auto"`, so this is always safe to write to an
|
|
213
|
+
* attribute.
|
|
214
|
+
*/
|
|
215
|
+
function resolveDirection(text) {
|
|
216
|
+
return detectDirection(text) === "rtl" ? "rtl" : "ltr";
|
|
217
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AuditIssue } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reports the markers that predict a bidi bug, for a string of HTML or CSS.
|
|
4
|
+
*
|
|
5
|
+
* This is a lint, not a proof: it finds patterns that are almost always wrong
|
|
6
|
+
* and never claims a document that passes is correct. It is meant for a CI step
|
|
7
|
+
* on content, and for the moment a translator asks why their paragraph is
|
|
8
|
+
* backwards.
|
|
9
|
+
*/
|
|
10
|
+
export declare function audit(source: string): AuditIssue[];
|
|
11
|
+
/** Formats {@link audit} output as one line per finding, for a terminal. */
|
|
12
|
+
export declare function formatAudit(issues: readonly AuditIssue[], file?: string): string;
|