polytypo 1.12.0 → 1.13.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 +18 -0
- package/dist/{chunk-MDYQWNEL.js → chunk-6PEHEU6F.js} +2 -2
- package/dist/{chunk-5XVEBCNU.js → chunk-ANHMGSED.js} +8 -7
- package/dist/chunk-ANHMGSED.js.map +1 -0
- package/dist/chunk-CJIH6CFO.cjs +34 -0
- package/dist/chunk-CJIH6CFO.cjs.map +1 -0
- package/dist/{chunk-IOW5V47B.js → chunk-E2HHFHG2.js} +8 -7
- package/dist/chunk-E2HHFHG2.js.map +1 -0
- package/dist/{chunk-BS2J3F7M.js → chunk-EL7R55TV.js} +2 -2
- package/dist/{chunk-ZLR7P7VH.cjs → chunk-FDNDYPPY.cjs} +3 -3
- package/dist/{chunk-ZLR7P7VH.cjs.map → chunk-FDNDYPPY.cjs.map} +1 -1
- package/dist/{chunk-3H6XONGO.js → chunk-MCQOMIWF.js} +2 -2
- package/dist/chunk-MDIDDG32.cjs +38 -0
- package/dist/chunk-MDIDDG32.cjs.map +1 -0
- package/dist/{chunk-QDF6EDLT.js → chunk-MF6PJO7O.js} +330 -9
- package/dist/chunk-MF6PJO7O.js.map +1 -0
- package/dist/chunk-OPHUDB2T.cjs +36 -0
- package/dist/chunk-OPHUDB2T.cjs.map +1 -0
- package/dist/{chunk-PMCNXHAP.js → chunk-OWTDZ6UD.js} +10 -9
- package/dist/{chunk-PMCNXHAP.js.map → chunk-OWTDZ6UD.js.map} +1 -1
- package/dist/{chunk-S3EQ4IDS.js → chunk-R5VKBH3P.js} +9 -2
- package/dist/chunk-R5VKBH3P.js.map +1 -0
- package/dist/{chunk-FRT6ATZR.cjs → chunk-TUDHJRJN.cjs} +6 -6
- package/dist/{chunk-FRT6ATZR.cjs.map → chunk-TUDHJRJN.cjs.map} +1 -1
- package/dist/{chunk-QZEJRXXP.cjs → chunk-W52W6XGA.cjs} +4 -4
- package/dist/{chunk-QZEJRXXP.cjs.map → chunk-W52W6XGA.cjs.map} +1 -1
- package/dist/{chunk-R2AZYV4E.cjs → chunk-WQEB33J2.cjs} +324 -3
- package/dist/chunk-WQEB33J2.cjs.map +1 -0
- package/dist/{chunk-VQB2LXMW.cjs → chunk-WWKSWVMX.cjs} +22 -21
- package/dist/chunk-WWKSWVMX.cjs.map +1 -0
- package/dist/{errors-DeHxbXRn.d.ts → errors-DrNAlWLt.d.cts} +12 -1
- package/dist/{errors-DeHxbXRn.d.cts → errors-DrNAlWLt.d.ts} +12 -1
- package/dist/html.cjs +9 -10
- package/dist/html.cjs.map +1 -1
- package/dist/html.d.cts +3 -3
- package/dist/html.d.ts +3 -3
- package/dist/html.js +4 -5
- package/dist/html.js.map +1 -1
- package/dist/index.cjs +17 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -8
- package/dist/index.js.map +1 -1
- package/dist/markdown.cjs +10 -11
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.d.cts +3 -3
- package/dist/markdown.d.ts +3 -3
- package/dist/markdown.js +5 -6
- package/dist/markdown.js.map +1 -1
- package/dist/text.cjs +8 -8
- package/dist/text.cjs.map +1 -1
- package/dist/text.d.cts +2 -2
- package/dist/text.d.ts +2 -2
- package/dist/text.js +3 -3
- package/dist/text.js.map +1 -1
- package/dist/yaml.cjs +9 -10
- package/dist/yaml.cjs.map +1 -1
- package/dist/yaml.d.cts +3 -3
- package/dist/yaml.d.ts +3 -3
- package/dist/yaml.js +4 -5
- package/dist/yaml.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-5XVEBCNU.js.map +0 -1
- package/dist/chunk-GLMNIX54.cjs +0 -31
- package/dist/chunk-GLMNIX54.cjs.map +0 -1
- package/dist/chunk-IOW5V47B.js.map +0 -1
- package/dist/chunk-IQIHJIZW.cjs +0 -35
- package/dist/chunk-IQIHJIZW.cjs.map +0 -1
- package/dist/chunk-OTHSRFQL.cjs +0 -33
- package/dist/chunk-OTHSRFQL.cjs.map +0 -1
- package/dist/chunk-QDF6EDLT.js.map +0 -1
- package/dist/chunk-R2AZYV4E.cjs.map +0 -1
- package/dist/chunk-S3EQ4IDS.js.map +0 -1
- package/dist/chunk-SUX5QWB6.js +0 -218
- package/dist/chunk-SUX5QWB6.js.map +0 -1
- package/dist/chunk-VQB2LXMW.cjs.map +0 -1
- package/dist/chunk-X7L2QLB6.cjs +0 -218
- package/dist/chunk-X7L2QLB6.cjs.map +0 -1
- /package/dist/{chunk-MDYQWNEL.js.map → chunk-6PEHEU6F.js.map} +0 -0
- /package/dist/{chunk-BS2J3F7M.js.map → chunk-EL7R55TV.js.map} +0 -0
- /package/dist/{chunk-3H6XONGO.js.map → chunk-MCQOMIWF.js.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/polytypo-js/polytypo-js/dist/chunk-
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/polytypo-js/polytypo-js/dist/chunk-W52W6XGA.cjs","../src/modes/html.ts","../src/modes/parse-error.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACA;ACJA,gCAAqC;ADMrC;AACA;AEAA,SAAS,QAAA,CAAS,KAAA,EAAwB;AACxC,EAAA,GAAA,CAAI,OAAO,MAAA,IAAU,SAAA,GAAY,MAAA,IAAU,IAAA,EAAM,OAAO,MAAA,CAAO,KAAK,CAAA;AACpE,EAAA,MAAM,WAAA,EAAa,KAAA;AACnB,EAAA,MAAM,OAAA,EACJ,OAAO,UAAA,CAAW,OAAA,IAAW,SAAA,EAAW,UAAA,CAAW,OAAA,EAAU,KAAA,CAAgB,OAAA;AAC/E,EAAA,MAAM,MAAA,EAAQ,UAAA,CAAW,KAAA;AACzB,EAAA,GAAA,CACE,MAAA,IAAU,KAAA,GACV,MAAA,IAAU,KAAA,EAAA,GACV,OAAO,KAAA,CAAM,KAAA,IAAS,SAAA,GACtB,OAAO,KAAA,CAAM,OAAA,IAAW,QAAA,EACxB;AACA,IAAA,OAAO,CAAA,EAAA;AACT,EAAA;AACO,EAAA;AACT;AAcgB;AACV,EAAA;AACK,IAAA;AACA,EAAA;AACH,IAAA;AACE,IAAA;AACJ,MAAA;AACA,MAAA;AACF,IAAA;AACF,EAAA;AACF;AFjBY;AACA;ACfN;AACJ,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACD;AAEe;AACP,EAAA;AACT;AAEM;AACA;AACO;AACP;AACA;AAGA;AAEG;AACA,EAAA;AACT;AAES;AACA,EAAA;AACT;AAES;AACA,EAAA;AACT;AAQS;AACH,EAAA;AACI,EAAA;AACC,EAAA;AAEL,EAAA;AACG,IAAA;AACC,IAAA;AACA,IAAA;AACG,IAAA;AACH,IAAA;AAEJ,IAAA;AAGK,MAAA;AACP,IAAA;AACI,IAAA;AACG,IAAA;AACT,EAAA;AAEM,EAAA;AAEJ,EAAA;AAIK,IAAA;AACP,EAAA;AACU,EAAA;AACH,EAAA;AACT;AAOgB;AACR,EAAA;AACF,EAAA;AACI,EAAA;AACD,EAAA;AACD,IAAA;AACI,MAAA;AACF,MAAA;AACE,QAAA;AACJ,QAAA;AACI,QAAA;AACJ,QAAA;AACF,MAAA;AACF,IAAA;AACK,IAAA;AACP,EAAA;AACU,EAAA;AACH,EAAA;AACT;AAES;AACA,EAAA;AACT;AAES;AACE,EAAA;AACD,IAAA;AACF,IAAA;AACJ,IAAA;AACQ,MAAA;AACR,IAAA;AACA,IAAA;AACF,EAAA;AAGS,EAAA;AAEL,EAAA;AAGK,EAAA;AACC,IAAA;AACR,IAAA;AACF,EAAA;AAEK,EAAA;AACL,EAAA;AACF;AAQgB;AACR,EAAA;AACA,EAAA;AACE,EAAA;AACD,EAAA;AACT;AAGgB;AACR,EAAA;AAA4B,IAAA;AAChC,IAAA;AACF,EAAA;AACM,EAAA;AACE,EAAA;AACD,EAAA;AAGT;AD9BY;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/polytypo-js/polytypo-js/dist/chunk-W52W6XGA.cjs","sourcesContent":[null,"import { parse, parseFragment } from \"parse5\";\nimport type { DefaultTreeAdapterMap } from \"parse5\";\nimport { wrapParserErrors } from \"./parse-error.js\";\nimport type { Span } from \"./spans.js\";\n\ntype Node = DefaultTreeAdapterMap[\"node\"];\ntype ParentNode = DefaultTreeAdapterMap[\"parentNode\"];\n\n/**\n * spec/rules/modes.md 3.6, exhaustive and **closed**: extending it is a spec change, not an\n * implementation decision. `svg` and `math` are here because in MathML a quotation mark, a\n * hyphen and a prime are operators and identifiers — substituting a curly glyph changes what\n * the expression means. Everything not listed is processable, **including unknown and custom\n * elements**: guessing from a tag name is exactly the heuristic that diverges across runtimes.\n */\nconst SKIPPED_ELEMENTS: ReadonlySet<string> = new Set([\n \"code\",\n \"pre\",\n \"kbd\",\n \"samp\",\n \"var\",\n \"script\",\n \"style\",\n \"textarea\",\n \"svg\",\n \"math\",\n]);\n\nexport function isSkippedElement(tagName: string): boolean {\n return SKIPPED_ELEMENTS.has(tagName);\n}\n\nconst AMPERSAND = 0x26;\nconst SEMICOLON = 0x3b;\nconst HASH = 0x23;\nconst LOWER_X = 0x78;\nconst UPPER_X = 0x58;\n\n/** Longest named reference in the WHATWG table is 32 characters; the guard is a bound, not a rule. */\nconst MAX_REFERENCE_BODY = 34;\n\nfunction isAsciiDigit(unit: number): boolean {\n return unit >= 0x30 && unit <= 0x39;\n}\n\nfunction isAsciiHexDigit(unit: number): boolean {\n return isAsciiDigit(unit) || (unit >= 0x61 && unit <= 0x66) || (unit >= 0x41 && unit <= 0x46);\n}\n\nfunction isAsciiAlphanumeric(unit: number): boolean {\n return isAsciiDigit(unit) || (unit >= 0x61 && unit <= 0x7a) || (unit >= 0x41 && unit <= 0x5a);\n}\n\n/**\n * The end offset of a well-formed character reference starting at `at`, or -1. A bare `&` that a\n * parser would repair is deliberately **not** treated as a reference: it stays inside the span,\n * where no rule can emit or delete it, rather than manufacturing a boundary that suppresses\n * conversions around it.\n */\nfunction characterReferenceEnd(source: string, at: number, limit: number): number {\n if (source.charCodeAt(at) !== AMPERSAND) return -1;\n let i = at + 1;\n if (i >= limit) return -1;\n\n if (source.charCodeAt(i) === HASH) {\n i += 1;\n const unit = i < limit ? source.charCodeAt(i) : -1;\n const hex = unit === LOWER_X || unit === UPPER_X;\n if (hex) i += 1;\n const digitsAt = i;\n while (\n i < limit &&\n (hex ? isAsciiHexDigit(source.charCodeAt(i)) : isAsciiDigit(source.charCodeAt(i)))\n ) {\n i += 1;\n }\n if (i === digitsAt) return -1;\n return i < limit && source.charCodeAt(i) === SEMICOLON ? i + 1 : -1;\n }\n\n const nameAt = i;\n while (\n i < limit &&\n i - nameAt < MAX_REFERENCE_BODY &&\n isAsciiAlphanumeric(source.charCodeAt(i))\n ) {\n i += 1;\n }\n if (i === nameAt) return -1;\n return i < limit && source.charCodeAt(i) === SEMICOLON ? i + 1 : -1;\n}\n\n/**\n * modes.md 3.6: every character reference is skipped as an **opaque unit**, and a text node\n * containing one is split into spans around it. That is the only way ` ` survives as\n * ` ` instead of becoming a literal U+00A0 on the way out.\n */\nexport function splitCharacterReferences(source: string, start: number, end: number): Span[] {\n const spans: Span[] = [];\n let cursor = start;\n let i = start;\n while (i < end) {\n if (source.charCodeAt(i) === AMPERSAND) {\n const stop = characterReferenceEnd(source, i, end);\n if (stop > 0) {\n if (i > cursor) spans.push({ start: cursor, end: i });\n cursor = stop;\n i = stop;\n continue;\n }\n }\n i += 1;\n }\n if (end > cursor) spans.push({ start: cursor, end });\n return spans;\n}\n\nfunction isParent(node: Node): node is ParentNode {\n return \"childNodes\" in node;\n}\n\nfunction collect(node: Node, source: string, spans: Span[]): void {\n if (node.nodeName === \"#text\") {\n const location = node.sourceCodeLocation;\n if (location === undefined || location === null) return;\n for (const span of splitCharacterReferences(source, location.startOffset, location.endOffset)) {\n spans.push(span);\n }\n return;\n }\n\n // Comments, doctype and processing instructions never contribute a span (modes.md 3.6).\n if (node.nodeName === \"#comment\" || node.nodeName === \"#documentType\") return;\n\n if (\"tagName\" in node && isSkippedElement(node.tagName)) return;\n\n // A `template`'s children live in a separate document fragment; `template` is not skipped.\n if (node.nodeName === \"template\" && \"content\" in node) {\n collect(node.content, source, spans);\n return;\n }\n\n if (!isParent(node)) return;\n for (const child of node.childNodes) collect(child, source, spans);\n}\n\n/**\n * Locate the processable spans of an HTML document. The tree is used only to find offsets and is\n * then discarded — modes.md 4 forbids reserialisation, which is what makes attribute quoting,\n * self-closing forms, entity spelling, tag case and malformed-input recovery preserved by\n * construction rather than by effort.\n */\nexport function htmlSpans(source: string): Span[] {\n const document = wrapParserErrors(\"HTML\", () => parse(source, { sourceCodeLocationInfo: true }));\n const spans: Span[] = [];\n collect(document, source, spans);\n return spans;\n}\n\n/** The same, for a fragment: used for HTML blocks embedded in markdown (modes.md 3.7). */\nexport function htmlFragmentSpans(source: string, offset: number): Span[] {\n const fragment = wrapParserErrors(\"HTML\", () =>\n parseFragment(source, { sourceCodeLocationInfo: true }),\n );\n const spans: Span[] = [];\n collect(fragment, source, spans);\n return offset === 0\n ? spans\n : spans.map((span) => ({ start: span.start + offset, end: span.end + offset }));\n}\n","import { PolytypoError } from \"../errors.js\";\n\ninterface PositionedError {\n readonly reason?: unknown;\n readonly place?: { readonly line?: unknown; readonly column?: unknown } | null;\n}\n\nfunction describe(error: unknown): string {\n if (typeof error !== \"object\" || error === null) return String(error);\n const positioned = error as PositionedError;\n const reason =\n typeof positioned.reason === \"string\" ? positioned.reason : (error as Error).message;\n const place = positioned.place;\n if (\n place !== null &&\n place !== undefined &&\n typeof place.line === \"number\" &&\n typeof place.column === \"number\"\n ) {\n return `${reason} (line ${place.line}, column ${place.column})`;\n }\n return reason;\n}\n\n/**\n * spec/rules/modes.md 3.7.2. **The parser's own error type must never escape.** A\n * `VFileMessage`, a `Nokogiri::SyntaxError` or a Python exception on the public surface puts a\n * dependency's type into the contract and is unreproducible in the other four runtimes; only the\n * code is contractual (ARCHITECTURE.md 4.6), while the message and the source position are\n * useful and are not.\n *\n * Reachable in exactly one place: `dialect: \"mdx\"`, because MDX embeds JavaScript. HTML parsing\n * is specified with total error recovery and every byte sequence is valid CommonMark, so neither\n * of the other two languages can fail. The wrapper is applied to all of them anyway — the\n * guarantee is that no parser type escapes, not that this particular parser is trusted.\n */\nexport function wrapParserErrors<T>(what: string, run: () => T): T {\n try {\n return run();\n } catch (error) {\n if (error instanceof PolytypoError) throw error;\n throw new PolytypoError(\n \"POLYTYPO_MALFORMED_INPUT\",\n `Input does not parse as ${what}: ${describe(error)}`,\n );\n }\n}\n"]}
|
|
@@ -5778,11 +5778,18 @@ function applyEditsToOrigin(origin, edits) {
|
|
|
5778
5778
|
for (let i = cursor; i < origin.length; i += 1) out.push(origin[i]);
|
|
5779
5779
|
return out;
|
|
5780
5780
|
}
|
|
5781
|
+
function endOf(origin, edit, inputLength) {
|
|
5782
|
+
for (let i = edit.end - 1; i >= edit.start; i -= 1) {
|
|
5783
|
+
const value = origin[i];
|
|
5784
|
+
if (value !== NO_ORIGIN) return value + 1;
|
|
5785
|
+
}
|
|
5786
|
+
return originAt(origin, edit.end, inputLength);
|
|
5787
|
+
}
|
|
5781
5788
|
function recordChanges(cp, edits, origin, inputLength, ruleId) {
|
|
5782
5789
|
return edits.map((edit) => ({
|
|
5783
5790
|
ruleId,
|
|
5784
5791
|
start: originAt(origin, edit.start, inputLength),
|
|
5785
|
-
end:
|
|
5792
|
+
end: endOf(origin, edit, inputLength),
|
|
5786
5793
|
before: fromCodePoints(cp.slice(edit.start, edit.end)),
|
|
5787
5794
|
after: fromCodePoints(edit.replacement)
|
|
5788
5795
|
}));
|
|
@@ -5831,6 +5838,320 @@ function runRulesRecording(cp, planned, locale, mode, narrowTarget, origin, inpu
|
|
|
5831
5838
|
return changes;
|
|
5832
5839
|
}
|
|
5833
5840
|
|
|
5841
|
+
// src/modes/spans.ts
|
|
5842
|
+
var SPACE5 = 32;
|
|
5843
|
+
var LINE_TERMINATORS = /* @__PURE__ */ new Set([
|
|
5844
|
+
10,
|
|
5845
|
+
13,
|
|
5846
|
+
11,
|
|
5847
|
+
12,
|
|
5848
|
+
133,
|
|
5849
|
+
8232,
|
|
5850
|
+
8233
|
|
5851
|
+
]);
|
|
5852
|
+
function gapIsLineBoundary(source, from, to) {
|
|
5853
|
+
for (let i = from; i < to; i += 1) {
|
|
5854
|
+
if (LINE_TERMINATORS.has(source.charCodeAt(i))) return true;
|
|
5855
|
+
}
|
|
5856
|
+
return false;
|
|
5857
|
+
}
|
|
5858
|
+
function normalizeSpans(spans) {
|
|
5859
|
+
const sorted = spans.filter((s) => s.end > s.start).sort((a, b) => a.start - b.start);
|
|
5860
|
+
const out = [];
|
|
5861
|
+
for (const span of sorted) {
|
|
5862
|
+
const last = out[out.length - 1];
|
|
5863
|
+
if (last === void 0) {
|
|
5864
|
+
out.push(span);
|
|
5865
|
+
continue;
|
|
5866
|
+
}
|
|
5867
|
+
if (span.start < last.end) {
|
|
5868
|
+
throw new PolytypoError(
|
|
5869
|
+
"POLYTYPO_RULE_CONTRACT",
|
|
5870
|
+
`mode extractor produced overlapping spans (${last.start}, ${last.end}) and (${span.start}, ${span.end})`
|
|
5871
|
+
);
|
|
5872
|
+
}
|
|
5873
|
+
if (span.start === last.end) {
|
|
5874
|
+
out[out.length - 1] = { start: last.start, end: span.end };
|
|
5875
|
+
continue;
|
|
5876
|
+
}
|
|
5877
|
+
out.push(span);
|
|
5878
|
+
}
|
|
5879
|
+
return out;
|
|
5880
|
+
}
|
|
5881
|
+
function concatenateSpans(source, spans) {
|
|
5882
|
+
const cp = [];
|
|
5883
|
+
let previous;
|
|
5884
|
+
for (const span of spans) {
|
|
5885
|
+
if (previous !== void 0) {
|
|
5886
|
+
cp.push(gapIsLineBoundary(source, previous.end, span.start) ? LINE_MARKER : MARKER);
|
|
5887
|
+
}
|
|
5888
|
+
for (const value of toCodePoints(source.slice(span.start, span.end))) cp.push(value);
|
|
5889
|
+
previous = span;
|
|
5890
|
+
}
|
|
5891
|
+
return cp;
|
|
5892
|
+
}
|
|
5893
|
+
function spanRangesOf(cp) {
|
|
5894
|
+
const ranges = [];
|
|
5895
|
+
let first = 0;
|
|
5896
|
+
for (let i = 0; i < cp.length; i += 1) {
|
|
5897
|
+
if (isMarker(cp[i])) {
|
|
5898
|
+
ranges.push({ first, last: i - 1 });
|
|
5899
|
+
first = i + 1;
|
|
5900
|
+
}
|
|
5901
|
+
}
|
|
5902
|
+
ranges.push({ first, last: cp.length - 1 });
|
|
5903
|
+
return ranges;
|
|
5904
|
+
}
|
|
5905
|
+
function spanContaining(ranges, p) {
|
|
5906
|
+
for (const range of ranges) {
|
|
5907
|
+
if (p >= range.first && p <= range.last + 1) return range;
|
|
5908
|
+
}
|
|
5909
|
+
return void 0;
|
|
5910
|
+
}
|
|
5911
|
+
function filterBoundaryEdits(cp, edits, ranges) {
|
|
5912
|
+
const out = [];
|
|
5913
|
+
for (const edit of edits) {
|
|
5914
|
+
let containsMarker = false;
|
|
5915
|
+
for (let i = edit.start; i < edit.end; i += 1) {
|
|
5916
|
+
if (isMarker(cp[i])) {
|
|
5917
|
+
containsMarker = true;
|
|
5918
|
+
break;
|
|
5919
|
+
}
|
|
5920
|
+
}
|
|
5921
|
+
if (containsMarker) continue;
|
|
5922
|
+
const p = edit.start;
|
|
5923
|
+
const q = edit.end - 1;
|
|
5924
|
+
const d = edit.end - edit.start;
|
|
5925
|
+
const r = edit.replacement.length;
|
|
5926
|
+
const span = spanContaining(ranges, p);
|
|
5927
|
+
if (span !== void 0 && (p === span.first || q === span.last)) {
|
|
5928
|
+
if (r > d) continue;
|
|
5929
|
+
const first = edit.replacement[0];
|
|
5930
|
+
const last = edit.replacement[r - 1];
|
|
5931
|
+
if (r > 0 && p === span.first && first === SPACE5 && cp[p] !== SPACE5) continue;
|
|
5932
|
+
if (r > 0 && q === span.last && last === SPACE5 && cp[q] !== SPACE5) continue;
|
|
5933
|
+
}
|
|
5934
|
+
out.push(edit);
|
|
5935
|
+
}
|
|
5936
|
+
return out;
|
|
5937
|
+
}
|
|
5938
|
+
function splitOnMarker(cp, expected) {
|
|
5939
|
+
const pieces = [[]];
|
|
5940
|
+
for (const value of cp) {
|
|
5941
|
+
if (isMarker(value)) {
|
|
5942
|
+
pieces.push([]);
|
|
5943
|
+
continue;
|
|
5944
|
+
}
|
|
5945
|
+
pieces[pieces.length - 1].push(value);
|
|
5946
|
+
}
|
|
5947
|
+
if (pieces.length !== expected) {
|
|
5948
|
+
throw new PolytypoError(
|
|
5949
|
+
"POLYTYPO_RULE_CONTRACT",
|
|
5950
|
+
`boundary markers did not survive the pipeline: expected ${expected} spans, found ${pieces.length}`
|
|
5951
|
+
);
|
|
5952
|
+
}
|
|
5953
|
+
return pieces;
|
|
5954
|
+
}
|
|
5955
|
+
function originOfSpans(source, spans) {
|
|
5956
|
+
const codePointIndexOf = new Array(source.length + 1);
|
|
5957
|
+
let cpIndex = 0;
|
|
5958
|
+
for (let i = 0; i < source.length; ) {
|
|
5959
|
+
codePointIndexOf[i] = cpIndex;
|
|
5960
|
+
const code = source.codePointAt(i);
|
|
5961
|
+
const width = code > 65535 ? 2 : 1;
|
|
5962
|
+
if (width === 2) codePointIndexOf[i + 1] = cpIndex;
|
|
5963
|
+
i += width;
|
|
5964
|
+
cpIndex += 1;
|
|
5965
|
+
}
|
|
5966
|
+
codePointIndexOf[source.length] = cpIndex;
|
|
5967
|
+
const origin = [];
|
|
5968
|
+
let previous;
|
|
5969
|
+
for (const span of spans) {
|
|
5970
|
+
if (previous !== void 0) origin.push(NO_ORIGIN);
|
|
5971
|
+
const base = codePointIndexOf[span.start];
|
|
5972
|
+
const length = toCodePoints(source.slice(span.start, span.end)).length;
|
|
5973
|
+
for (let k = 0; k < length; k += 1) origin.push(base + k);
|
|
5974
|
+
previous = span;
|
|
5975
|
+
}
|
|
5976
|
+
return origin;
|
|
5977
|
+
}
|
|
5978
|
+
|
|
5979
|
+
// src/engine/span-runner.ts
|
|
5980
|
+
function runRulesOverSpans(cp, planned, locale, mode, narrowTarget) {
|
|
5981
|
+
let current = cp;
|
|
5982
|
+
for (const id of planned) {
|
|
5983
|
+
const rule = RULES[id];
|
|
5984
|
+
if (rule === void 0) continue;
|
|
5985
|
+
const edits = rule.apply({ cp: current, locale, mode, narrowTarget });
|
|
5986
|
+
current = applyEdits(current, filterBoundaryEdits(current, edits, spanRangesOf(current)), id);
|
|
5987
|
+
}
|
|
5988
|
+
return current;
|
|
5989
|
+
}
|
|
5990
|
+
function replacementsOfUnit(source, spans, planned, locale, mode, narrowTarget) {
|
|
5991
|
+
const normalized = normalizeSpans(spans);
|
|
5992
|
+
if (normalized.length === 0) return [];
|
|
5993
|
+
const transformed = runRulesOverSpans(
|
|
5994
|
+
concatenateSpans(source, normalized),
|
|
5995
|
+
planned,
|
|
5996
|
+
locale,
|
|
5997
|
+
mode,
|
|
5998
|
+
narrowTarget
|
|
5999
|
+
);
|
|
6000
|
+
const pieces = splitOnMarker(transformed, normalized.length);
|
|
6001
|
+
return normalized.map((span, i) => ({ span, text: fromCodePoints(pieces[i]) }));
|
|
6002
|
+
}
|
|
6003
|
+
function emit2(source, replacements) {
|
|
6004
|
+
let out = "";
|
|
6005
|
+
let cursor = 0;
|
|
6006
|
+
for (const { span, text } of replacements) {
|
|
6007
|
+
const original = source.slice(span.start, span.end);
|
|
6008
|
+
out += source.slice(cursor, span.start);
|
|
6009
|
+
out += text === original ? original : text;
|
|
6010
|
+
cursor = span.end;
|
|
6011
|
+
}
|
|
6012
|
+
return out + source.slice(cursor);
|
|
6013
|
+
}
|
|
6014
|
+
function runOverSpans(source, spans, planned, locale, mode, narrowTarget) {
|
|
6015
|
+
return emit2(source, replacementsOfUnit(source, spans, planned, locale, mode, narrowTarget));
|
|
6016
|
+
}
|
|
6017
|
+
function runOverUnits(source, units, planned, locale, mode, narrowTarget) {
|
|
6018
|
+
const replacements = units.flatMap((spans) => replacementsOfUnit(source, spans, planned, locale, mode, narrowTarget)).sort((a, b) => a.span.start - b.span.start);
|
|
6019
|
+
return emit2(source, replacements);
|
|
6020
|
+
}
|
|
6021
|
+
function analyzeOverUnits(source, units, planned, locale, mode, narrowTarget) {
|
|
6022
|
+
return units.flatMap((spans) => analyzeOverSpans(source, spans, planned, locale, mode, narrowTarget)).sort((a, b) => a.start - b.start);
|
|
6023
|
+
}
|
|
6024
|
+
function analyzeOverSpans(source, spans, planned, locale, mode, narrowTarget) {
|
|
6025
|
+
const normalized = normalizeSpans(spans);
|
|
6026
|
+
if (normalized.length === 0) return [];
|
|
6027
|
+
return runRulesRecording(
|
|
6028
|
+
concatenateSpans(source, normalized),
|
|
6029
|
+
planned,
|
|
6030
|
+
locale,
|
|
6031
|
+
mode,
|
|
6032
|
+
narrowTarget,
|
|
6033
|
+
originOfSpans(source, normalized),
|
|
6034
|
+
toCodePoints(source).length,
|
|
6035
|
+
(current, edits) => filterBoundaryEdits(current, edits, spanRangesOf(current))
|
|
6036
|
+
);
|
|
6037
|
+
}
|
|
6038
|
+
|
|
6039
|
+
// src/engine/protect.ts
|
|
6040
|
+
var LINE_TERMINATORS2 = /* @__PURE__ */ new Set([
|
|
6041
|
+
10,
|
|
6042
|
+
13,
|
|
6043
|
+
11,
|
|
6044
|
+
12,
|
|
6045
|
+
133,
|
|
6046
|
+
8232,
|
|
6047
|
+
8233
|
|
6048
|
+
]);
|
|
6049
|
+
function invalid(message) {
|
|
6050
|
+
throw new PolytypoError("POLYTYPO_INVALID_OPTION", message);
|
|
6051
|
+
}
|
|
6052
|
+
function resolveProtect(value, length) {
|
|
6053
|
+
if (value === void 0 || value === null) return [];
|
|
6054
|
+
if (!Array.isArray(value)) invalid("protect must be an array of { start, end } ranges.");
|
|
6055
|
+
const holes = [];
|
|
6056
|
+
let previousEnd = 0;
|
|
6057
|
+
for (const range of value) {
|
|
6058
|
+
if (typeof range !== "object" || range === null) invalid("protect entries must be objects.");
|
|
6059
|
+
const { start, end } = range;
|
|
6060
|
+
if (!Number.isInteger(start) || !Number.isInteger(end)) {
|
|
6061
|
+
invalid("protect start and end must be integers.");
|
|
6062
|
+
}
|
|
6063
|
+
const s = start;
|
|
6064
|
+
const e = end;
|
|
6065
|
+
if (s < 0 || e <= s) invalid(`protect range [${s}, ${e}) is empty or negative.`);
|
|
6066
|
+
if (s < previousEnd)
|
|
6067
|
+
invalid(`protect range [${s}, ${e}) overlaps or precedes the previous one.`);
|
|
6068
|
+
if (e > length)
|
|
6069
|
+
invalid(`protect range [${s}, ${e}) runs past the end of the input (${length}).`);
|
|
6070
|
+
const last = holes[holes.length - 1];
|
|
6071
|
+
if (last !== void 0 && last.end === s)
|
|
6072
|
+
holes[holes.length - 1] = { start: last.start, end: e };
|
|
6073
|
+
else holes.push({ start: s, end: e });
|
|
6074
|
+
previousEnd = e;
|
|
6075
|
+
}
|
|
6076
|
+
return holes;
|
|
6077
|
+
}
|
|
6078
|
+
function assertNoProtect(value, mode) {
|
|
6079
|
+
if (value === void 0 || value === null) return;
|
|
6080
|
+
if (Array.isArray(value) && value.length === 0) return;
|
|
6081
|
+
invalid(`protect is only available in text mode, not ${mode}.`);
|
|
6082
|
+
}
|
|
6083
|
+
function layout(cp, holes) {
|
|
6084
|
+
const array = [];
|
|
6085
|
+
const origin = [];
|
|
6086
|
+
let cursor = 0;
|
|
6087
|
+
for (const hole of holes) {
|
|
6088
|
+
for (let i = cursor; i < hole.start; i += 1) {
|
|
6089
|
+
array.push(cp[i]);
|
|
6090
|
+
origin.push(i);
|
|
6091
|
+
}
|
|
6092
|
+
let line = false;
|
|
6093
|
+
for (let i = hole.start; i < hole.end; i += 1) {
|
|
6094
|
+
if (LINE_TERMINATORS2.has(cp[i])) {
|
|
6095
|
+
line = true;
|
|
6096
|
+
break;
|
|
6097
|
+
}
|
|
6098
|
+
}
|
|
6099
|
+
array.push(line ? LINE_MARKER : MARKER);
|
|
6100
|
+
origin.push(NO_ORIGIN);
|
|
6101
|
+
cursor = hole.end;
|
|
6102
|
+
}
|
|
6103
|
+
for (let i = cursor; i < cp.length; i += 1) {
|
|
6104
|
+
array.push(cp[i]);
|
|
6105
|
+
origin.push(i);
|
|
6106
|
+
}
|
|
6107
|
+
return { array, origin };
|
|
6108
|
+
}
|
|
6109
|
+
function runProtectedMapped(input, holes, planned, locale, narrowTarget) {
|
|
6110
|
+
const cp = toCodePoints(input);
|
|
6111
|
+
const { array } = layout(cp, holes);
|
|
6112
|
+
const out = runRulesOverSpans(array, planned, locale, "text", narrowTarget);
|
|
6113
|
+
const result = [];
|
|
6114
|
+
const mapped = [];
|
|
6115
|
+
let hole = 0;
|
|
6116
|
+
for (const value of out) {
|
|
6117
|
+
if (isMarker(value)) {
|
|
6118
|
+
const h = holes[hole];
|
|
6119
|
+
const start = result.length;
|
|
6120
|
+
for (let i = h.start; i < h.end; i += 1) result.push(cp[i]);
|
|
6121
|
+
mapped.push({ start, end: result.length });
|
|
6122
|
+
hole += 1;
|
|
6123
|
+
continue;
|
|
6124
|
+
}
|
|
6125
|
+
result.push(value);
|
|
6126
|
+
}
|
|
6127
|
+
if (hole !== holes.length) {
|
|
6128
|
+
throw new PolytypoError(
|
|
6129
|
+
"POLYTYPO_RULE_CONTRACT",
|
|
6130
|
+
"boundary markers did not survive the pipeline."
|
|
6131
|
+
);
|
|
6132
|
+
}
|
|
6133
|
+
return { text: fromCodePoints(result), holes: mapped };
|
|
6134
|
+
}
|
|
6135
|
+
function runProtected(input, holes, planned, locale, narrowTarget) {
|
|
6136
|
+
return runProtectedMapped(input, holes, planned, locale, narrowTarget).text;
|
|
6137
|
+
}
|
|
6138
|
+
function analyzeProtected(input, holes, planned, locale, narrowTarget) {
|
|
6139
|
+
const cp = toCodePoints(input);
|
|
6140
|
+
const { array, origin } = layout(cp, holes);
|
|
6141
|
+
return runRulesRecording(
|
|
6142
|
+
array,
|
|
6143
|
+
planned,
|
|
6144
|
+
locale,
|
|
6145
|
+
"text",
|
|
6146
|
+
narrowTarget,
|
|
6147
|
+
origin,
|
|
6148
|
+
cp.length,
|
|
6149
|
+
(current, edits) => filterBoundaryEdits(current, edits, spanRangesOf(current))
|
|
6150
|
+
);
|
|
6151
|
+
}
|
|
6152
|
+
|
|
6153
|
+
|
|
6154
|
+
|
|
5834
6155
|
|
|
5835
6156
|
|
|
5836
6157
|
|
|
@@ -5846,5 +6167,5 @@ function runRulesRecording(cp, planned, locale, mode, narrowTarget, origin, inpu
|
|
|
5846
6167
|
|
|
5847
6168
|
|
|
5848
6169
|
|
|
5849
|
-
exports.PolytypoError = PolytypoError; exports.toCodePoints = toCodePoints; exports.fromCodePoints = fromCodePoints; exports.getLocaleData = getLocaleData; exports.resolveNarrowTarget = resolveNarrowTarget; exports.
|
|
5850
|
-
//# sourceMappingURL=chunk-
|
|
6170
|
+
exports.PolytypoError = PolytypoError; exports.toCodePoints = toCodePoints; exports.fromCodePoints = fromCodePoints; exports.getLocaleData = getLocaleData; exports.resolveNarrowTarget = resolveNarrowTarget; exports.planRules = planRules; exports.runRules = runRules; exports.runRulesRecording = runRulesRecording; exports.runOverSpans = runOverSpans; exports.runOverUnits = runOverUnits; exports.analyzeOverUnits = analyzeOverUnits; exports.analyzeOverSpans = analyzeOverSpans; exports.resolveProtect = resolveProtect; exports.assertNoProtect = assertNoProtect; exports.runProtected = runProtected; exports.analyzeProtected = analyzeProtected;
|
|
6171
|
+
//# sourceMappingURL=chunk-WQEB33J2.cjs.map
|