@readium/guided-navigation 1.1.2 → 1.2.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.
Files changed (134) hide show
  1. package/README.md +1 -1
  2. package/dist/a11y.js +1 -1
  3. package/dist/converter.js +1 -1
  4. package/dist/dom.js +1 -1
  5. package/dist/elementHandlers.js +1 -1
  6. package/dist/emptyPairs.js +1 -0
  7. package/dist/language.js +1 -0
  8. package/dist/object.js +1 -1
  9. package/dist/prescan.js +1 -1
  10. package/dist/roles.js +1 -1
  11. package/dist/text.js +1 -1
  12. package/dist/textrefFragment.js +1 -1
  13. package/fixtures/ROLES_COVERAGE.md +5 -4
  14. package/fixtures/abbreviation-acronym-html-native/gnd.json +9 -0
  15. package/fixtures/abbreviation-acronym-html-native/input.html +1 -0
  16. package/fixtures/abbreviation-acronym-html-native/meta.json +11 -0
  17. package/fixtures/abbreviation-before-formatting/gnd.json +9 -0
  18. package/fixtures/abbreviation-before-formatting/input.html +1 -0
  19. package/fixtures/abbreviation-before-formatting/meta.json +11 -0
  20. package/fixtures/abbreviation-before-image/gnd.json +24 -0
  21. package/fixtures/abbreviation-before-image/input.html +1 -0
  22. package/fixtures/abbreviation-before-image/meta.json +12 -0
  23. package/fixtures/abbreviation-html-native/gnd.json +9 -0
  24. package/fixtures/abbreviation-html-native/input.html +1 -0
  25. package/fixtures/abbreviation-html-native/meta.json +11 -0
  26. package/fixtures/abbreviation-language/gnd.json +9 -0
  27. package/fixtures/abbreviation-language/input.html +1 -0
  28. package/fixtures/abbreviation-language/meta.json +11 -0
  29. package/fixtures/abbreviation-line-break/gnd.json +9 -0
  30. package/fixtures/abbreviation-line-break/input.html +1 -0
  31. package/fixtures/abbreviation-line-break/meta.json +11 -0
  32. package/fixtures/abbreviation-nested/gnd.json +9 -0
  33. package/fixtures/abbreviation-nested/input.html +1 -0
  34. package/fixtures/abbreviation-nested/meta.json +11 -0
  35. package/fixtures/abbreviation-nested-emphasis/gnd.json +9 -0
  36. package/fixtures/abbreviation-nested-emphasis/input.html +1 -0
  37. package/fixtures/abbreviation-nested-emphasis/meta.json +11 -0
  38. package/fixtures/abbreviation-nested-term/gnd.json +21 -0
  39. package/fixtures/abbreviation-nested-term/input.html +1 -0
  40. package/fixtures/abbreviation-nested-term/meta.json +12 -0
  41. package/fixtures/abbreviation-nested-term-punctuation/gnd.json +26 -0
  42. package/fixtures/abbreviation-nested-term-punctuation/input.html +1 -0
  43. package/fixtures/abbreviation-nested-term-punctuation/meta.json +12 -0
  44. package/fixtures/abbreviation-no-title/gnd.json +8 -0
  45. package/fixtures/abbreviation-no-title/input.html +1 -0
  46. package/fixtures/abbreviation-no-title/meta.json +11 -0
  47. package/fixtures/abbreviation-spanning-terms/gnd.json +33 -0
  48. package/fixtures/abbreviation-spanning-terms/input.html +1 -0
  49. package/fixtures/abbreviation-spanning-terms/meta.json +12 -0
  50. package/fixtures/abbreviation-with-image/gnd.json +24 -0
  51. package/fixtures/abbreviation-with-image/input.html +1 -0
  52. package/fixtures/abbreviation-with-image/meta.json +12 -0
  53. package/fixtures/abbreviation-with-svg-and-math/gnd.json +39 -0
  54. package/fixtures/abbreviation-with-svg-and-math/input.html +1 -0
  55. package/fixtures/abbreviation-with-svg-and-math/meta.json +13 -0
  56. package/fixtures/biblioref-target-descendant-ids/gnd.json +90 -0
  57. package/fixtures/biblioref-target-descendant-ids/input.html +1 -0
  58. package/fixtures/biblioref-target-descendant-ids/meta.json +14 -0
  59. package/fixtures/biblioref-target-in-bibliography-role-aria/gnd.json +59 -0
  60. package/fixtures/biblioref-target-in-bibliography-role-aria/input.html +6 -0
  61. package/fixtures/biblioref-target-in-bibliography-role-aria/meta.json +14 -0
  62. package/fixtures/biblioref-target-mixed-content/gnd.json +63 -0
  63. package/fixtures/biblioref-target-mixed-content/input.html +1 -0
  64. package/fixtures/biblioref-target-mixed-content/meta.json +11 -0
  65. package/fixtures/glossref-target-in-glossary-role-aria/gnd.json +76 -0
  66. package/fixtures/glossref-target-in-glossary-role-aria/input.html +5 -0
  67. package/fixtures/glossref-target-in-glossary-role-aria/meta.json +14 -0
  68. package/fixtures/glossref-target-percent-encoded/gnd.json +68 -0
  69. package/fixtures/glossref-target-percent-encoded/input.html +1 -0
  70. package/fixtures/glossref-target-percent-encoded/meta.json +14 -0
  71. package/fixtures/manifest.json +330 -0
  72. package/fixtures/noteref-after-closing-straight-quote/gnd.json +32 -0
  73. package/fixtures/noteref-after-closing-straight-quote/input.html +1 -0
  74. package/fixtures/noteref-after-closing-straight-quote/meta.json +13 -0
  75. package/fixtures/noteref-after-closing-straight-quote-split/gnd.json +38 -0
  76. package/fixtures/noteref-after-closing-straight-quote-split/input.html +1 -0
  77. package/fixtures/noteref-after-closing-straight-quote-split/meta.json +12 -0
  78. package/fixtures/noteref-enclosed-across-formatting/gnd.json +32 -0
  79. package/fixtures/noteref-enclosed-across-formatting/input.html +1 -0
  80. package/fixtures/noteref-enclosed-across-formatting/meta.json +13 -0
  81. package/fixtures/noteref-enclosed-adjacent-pairs/gnd.json +108 -0
  82. package/fixtures/noteref-enclosed-adjacent-pairs/input.html +1 -0
  83. package/fixtures/noteref-enclosed-adjacent-pairs/meta.json +13 -0
  84. package/fixtures/noteref-enclosed-adjacent-text/gnd.json +34 -0
  85. package/fixtures/noteref-enclosed-adjacent-text/input.html +1 -0
  86. package/fixtures/noteref-enclosed-adjacent-text/meta.json +13 -0
  87. package/fixtures/noteref-enclosed-before-formatting/gnd.json +34 -0
  88. package/fixtures/noteref-enclosed-before-formatting/input.html +1 -0
  89. package/fixtures/noteref-enclosed-before-formatting/meta.json +13 -0
  90. package/fixtures/noteref-enclosed-guillemets-french/gnd.json +103 -0
  91. package/fixtures/noteref-enclosed-guillemets-french/input.html +1 -0
  92. package/fixtures/noteref-enclosed-guillemets-french/meta.json +13 -0
  93. package/fixtures/noteref-enclosed-quotes-chinese/gnd.json +47 -0
  94. package/fixtures/noteref-enclosed-quotes-chinese/input.html +1 -0
  95. package/fixtures/noteref-enclosed-quotes-chinese/meta.json +13 -0
  96. package/fixtures/noteref-enclosed-quotes-german/gnd.json +124 -0
  97. package/fixtures/noteref-enclosed-quotes-german/input.html +1 -0
  98. package/fixtures/noteref-enclosed-quotes-german/meta.json +13 -0
  99. package/fixtures/noteref-enclosed-quotes-swedish/gnd.json +70 -0
  100. package/fixtures/noteref-enclosed-quotes-swedish/input.html +1 -0
  101. package/fixtures/noteref-enclosed-quotes-swedish/meta.json +13 -0
  102. package/fixtures/noteref-enclosed-straight-quotes/gnd.json +32 -0
  103. package/fixtures/noteref-enclosed-straight-quotes/input.html +1 -0
  104. package/fixtures/noteref-enclosed-straight-quotes/meta.json +13 -0
  105. package/fixtures/noteref-in-lone-block/gnd.json +29 -0
  106. package/fixtures/noteref-in-lone-block/input.html +1 -0
  107. package/fixtures/noteref-in-lone-block/meta.json +12 -0
  108. package/fixtures/noteref-target-malformed-escape/gnd.json +32 -0
  109. package/fixtures/noteref-target-malformed-escape/input.html +1 -0
  110. package/fixtures/noteref-target-malformed-escape/meta.json +13 -0
  111. package/fixtures/noteref-target-percent-encoded/gnd.json +32 -0
  112. package/fixtures/noteref-target-percent-encoded/input.html +1 -0
  113. package/fixtures/noteref-target-percent-encoded/meta.json +13 -0
  114. package/package.json +1 -1
  115. package/src/a11y.ts +50 -39
  116. package/src/converter.ts +140 -50
  117. package/src/dom.ts +0 -10
  118. package/src/elementHandlers.ts +76 -22
  119. package/src/emptyPairs.ts +74 -0
  120. package/src/language.ts +37 -0
  121. package/src/object.ts +11 -1
  122. package/src/prescan.ts +2 -1
  123. package/src/roles.ts +2 -0
  124. package/src/text.ts +5 -2
  125. package/src/textrefFragment.ts +9 -1
  126. package/types/src/a11y.d.ts +9 -4
  127. package/types/src/converter.d.ts +4 -0
  128. package/types/src/dom.d.ts +0 -1
  129. package/types/src/elementHandlers.d.ts +3 -2
  130. package/types/src/emptyPairs.d.ts +3 -0
  131. package/types/src/language.d.ts +4 -0
  132. package/types/src/object.d.ts +2 -0
  133. package/types/src/text.d.ts +2 -1
  134. package/types/src/textrefFragment.d.ts +4 -0
@@ -1,31 +1,34 @@
1
1
  // Handlers for element kinds that build one placeholder ObjBuilder each
2
- // (pagebreak/noteref/link), calling back into the converter for
3
- // placeholder() and, for noteref's footnote embedding, convert()/resultBuilders()
2
+ // (pagebreak/noteref/reference/link), calling back into the converter for
3
+ // placeholder() and, for noteref's and reference's target embedding, convert()/resultBuilders()
4
4
  // on a sub-Converter of its own.
5
5
  import type { GndRole } from "./types.ts";
6
+ import { ssmlAttrEscape } from "@readium/helpers";
6
7
  import { normalizedNodeText } from "./a11y.ts";
7
- import { textIsEmpty } from "./text.ts";
8
+ import { normalizeWhitespace, textIsEmpty } from "./text.ts";
8
9
  import { type ObjBuilder, ariaSubstitutedBuilders, substitutedOwnSelectorBuilders } from "./object.ts";
9
10
  import { hasElementChild, isAncestorOf } from "./dom.ts";
11
+ import { nodeLanguage } from "./language.ts";
10
12
  import { Converter } from "./converter.ts";
11
13
  import { selectorForElement, textrefForSelector } from "./selectorGenerator.ts";
14
+ import { decodeIdFragment, detachSelfTextref } from "./textrefFragment.ts";
12
15
 
13
16
  // Returns true if el's children should still be descended into (a
14
17
  // pagebreak with element children of its own, in HTML parsing).
15
- export function pagebreak(converter: Converter, el: Element, aria: string | null, roles: GndRole[]): boolean {
18
+ export function pagebreak(converter: Converter, el: Element, aria: string | null, ariaLanguage: string, roles: GndRole[]): boolean {
16
19
  const obj: ObjBuilder = { role: roles };
17
- const title = (el.getAttribute("title") ?? "").trim();
20
+ const title = normalizeWhitespace(el.getAttribute("title") ?? "", true).trim();
18
21
  if (title) {
19
- obj.text = { plain: title, ssml: "", language: "" };
22
+ obj.text = { plain: title, ssml: "", language: nodeLanguage(el) };
20
23
  } else if (aria) {
21
- obj.text = { plain: aria, ssml: "", language: "" };
24
+ obj.text = { plain: aria, ssml: "", language: ariaLanguage };
22
25
  ariaSubstitutedBuilders.add(obj);
23
26
  }
24
27
  const labelled = !!(obj.text && !textIsEmpty(obj.text));
25
28
  const descend = !converter.xmlParsed && (hasElementChild(el) || (labelled && el.firstChild !== null));
26
29
  if (!labelled && !descend) {
27
- const text = normalizedNodeText(el);
28
- if (text) obj.text = { plain: text, ssml: "", language: "" };
30
+ const { text, language } = normalizedNodeText(el);
31
+ if (text) obj.text = { plain: text, ssml: "", language };
29
32
  }
30
33
  const selector = selectorForElement(el, converter.selectorRoot, converter.selectorRootAnchor);
31
34
  const textref = textrefForSelector(selector, el);
@@ -36,19 +39,19 @@ export function pagebreak(converter: Converter, el: Element, aria: string | null
36
39
 
37
40
  export function noteref(converter: Converter, el: Element, roles: GndRole[]): void {
38
41
  const obj: ObjBuilder = { role: roles };
39
- const text = normalizedNodeText(el);
40
- if (text) obj.text = { plain: text, ssml: "", language: "" };
42
+ const { text, language } = normalizedNodeText(el);
43
+ if (text) obj.text = { plain: text, ssml: "", language };
41
44
 
42
45
  const href = el.getAttribute("href") ?? "";
46
+ const fragment = href.startsWith("#") ? (decodeIdFragment(href) ?? href.slice(1)) : undefined;
43
47
  let candidateID = el.getAttribute("id") ?? "";
44
- if (!candidateID && href.startsWith("#")) {
48
+ if (!candidateID && fragment !== undefined) {
45
49
  // A target still output at its own location keeps its id there.
46
- const target = converter.ids.get(href.slice(1));
47
- if (!target || converter.suppressed.has(target)) candidateID = href.slice(1);
50
+ const target = converter.ids.get(fragment);
51
+ if (!target || converter.suppressed.has(target)) candidateID = fragment;
48
52
  }
49
53
 
50
- if (href.startsWith("#")) {
51
- const fragment = href.slice(1);
54
+ if (fragment !== undefined) {
52
55
  const target = converter.ids.get(fragment);
53
56
  if (target && !isAncestorOf(target, el) && converter.noterefDepth < 3) {
54
57
  const sub = converter.spawnChild(target);
@@ -58,7 +61,7 @@ export function noteref(converter: Converter, el: Element, roles: GndRole[]): vo
58
61
  // The target's own id already carries its meaning via this
59
62
  // noteref's own id — repeating it on the embedded content would
60
63
  // be redundant.
61
- for (const c of children) delete c.id;
64
+ for (const c of children) removeId(c);
62
65
  obj.children = children;
63
66
  }
64
67
  }
@@ -70,19 +73,70 @@ export function noteref(converter: Converter, el: Element, roles: GndRole[]): vo
70
73
  converter.placeholder(el, "noteref", obj, candidateID || undefined);
71
74
  }
72
75
 
73
- export function link(converter: Converter, el: Element, roles: GndRole[], aria: string | null): void {
76
+ export function link(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): void {
77
+ converter.placeholder(el, roles[0] ?? "link", linkObject(converter, el, roles, aria, ariaLanguage));
78
+ }
79
+
80
+ // A glossref or biblioref: a link that also embeds its same-document target, like a noteref its footnote.
81
+ export function reference(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): void {
82
+ const obj = linkObject(converter, el, roles, aria, ariaLanguage);
83
+ const href = el.getAttribute("href") ?? "";
84
+ if (href.startsWith("#")) {
85
+ const target = converter.ids.get(decodeIdFragment(href) ?? href.slice(1));
86
+ if (target && !isAncestorOf(target, el) && converter.noterefDepth < 3) {
87
+ const children = referencedContent(target).flatMap((content) => {
88
+ const sub = converter.spawnChild(content);
89
+ sub.copy = true;
90
+ sub.convert(content);
91
+ return sub.resultBuilders();
92
+ });
93
+ // Still output at its own location, which keeps its ids.
94
+ removeCopiedIds(children);
95
+ if (children.length > 0) obj.children = children;
96
+ }
97
+ }
98
+ converter.placeholder(el, roles[0], obj);
99
+ }
100
+
101
+ function linkObject(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): ObjBuilder {
74
102
  const obj: ObjBuilder = {};
75
103
  if (roles.length > 0) obj.role = roles;
76
104
  if (aria) {
77
- obj.text = { plain: aria, ssml: "", language: "" };
105
+ obj.text = { plain: aria, ssml: "", language: ariaLanguage };
78
106
  ariaSubstitutedBuilders.add(obj);
79
107
  const selector = selectorForElement(el, converter.selectorRoot, converter.selectorRootAnchor);
80
108
  if (selector) substitutedOwnSelectorBuilders.set(obj, selector);
81
109
  } else {
82
- const text = normalizedNodeText(el);
83
- if (text) obj.text = { plain: text, ssml: "", language: "" };
110
+ const { text, language } = normalizedNodeText(el);
111
+ if (text) obj.text = { plain: text, ssml: "", language };
84
112
  }
85
113
  const href = el.getAttribute("href");
86
114
  if (href) obj.textref = href;
87
- converter.placeholder(el, roles[0] ?? "link", obj);
115
+ return obj;
116
+ }
117
+
118
+ function removeId(obj: ObjBuilder) {
119
+ const id = obj.id;
120
+ if (id === undefined) return;
121
+ delete obj.id;
122
+ detachSelfTextref(obj, id);
123
+ }
124
+
125
+ // Removes the ids a copy repeats from its original, keeping placeholders', allocated unique and referenced by their parent's SSML.
126
+ function removeCopiedIds(objs: ObjBuilder[], parent?: ObjBuilder) {
127
+ for (const obj of objs) {
128
+ if (obj.id === undefined || !parent?.text?.ssml.includes(`id="${ssmlAttrEscape(obj.id)}"`)) removeId(obj);
129
+ if (obj.children) removeCopiedIds(obj.children, obj);
130
+ }
131
+ }
132
+
133
+ // A glossary term and its definitions, else the target itself.
134
+ function referencedContent(target: Element): Element[] {
135
+ const term = target.tagName.toLowerCase() === "dt" ? target : target.closest("dt");
136
+ if (!term) return [target];
137
+ let n = term.nextElementSibling;
138
+ while (n && n.tagName.toLowerCase() === "dt") n = n.nextElementSibling;
139
+ const definitions: Element[] = [];
140
+ for (; n && n.tagName.toLowerCase() === "dd"; n = n.nextElementSibling) definitions.push(n);
141
+ return definitions.length > 0 ? [term, ...definitions] : [target];
88
142
  }
@@ -0,0 +1,74 @@
1
+ // Brackets and quotes left empty in plain text by the objects it drops, e.g. "1870s (◆) along".
2
+ import { startsWithBindingPunct } from "@readium/helpers";
3
+
4
+ // Stands in plain text for a dropped object, until removeDropped() takes it out.
5
+ export const DROPPED = "\u0000";
6
+
7
+ // Opening and closing marks, two characters per pair.
8
+ const BRACKET_PAIRS = "()[]{}()[]{}「」『』【】〔〕〈〉《》";
9
+ const STRAIGHT_QUOTE_PAIRS = "\"\"''";
10
+ // From CLDR's delimiters, plus guillemets: »…« in German and Danish books, «…» in Swiss German, »…» in Swedish and Finnish.
11
+ const QUOTE_PAIRS: Record<string, string> = {
12
+ ar: "”“’‘", bg: "„“", cs: "„“‚‘", da: "“”‘’»«›‹", de: "„“‚‘»«›‹«»‹›", el: "«»“”", en: "“”‘’", es: "“”‘’", et: "„“‚‘",
13
+ fi: "””’’»»››", fr: "«»", he: "””’’", hr: "„“‚‘", hu: "„”»«", hy: "«»", is: "„“‚‘", it: "«»“”", ja: "「」『』",
14
+ ka: "„“«»", ko: "“”‘’", lt: "„“", lv: "“”‘’", nb: "«»‘’", nl: "‘’", pl: "„”«»", pt: "“”‘’", ro: "„”«»", ru: "«»„“",
15
+ sk: "„“‚‘", sl: "„“‚‘", sr: "„”’’", sv: "””’’»»››", tr: "“”‘’", uk: "«»„“", zh: "“”‘’", "zh-hant": "「」『』",
16
+ };
17
+ // What may sit between a pair's marks and still leave it empty.
18
+ const FILLER = /[\s​-‍⁠,;、,\u0000]/;
19
+
20
+ interface Marks {
21
+ pairs: Set<string>;
22
+ opening: Set<string>;
23
+ closing: Set<string>;
24
+ }
25
+
26
+ const marksByLanguage = new Map<string, Marks>();
27
+
28
+ function marksFor(lang: string | undefined): Marks {
29
+ const tag = lang?.toLowerCase() ?? "";
30
+ let marks = marksByLanguage.get(tag);
31
+ if (marks) return marks;
32
+ const chars = BRACKET_PAIRS + STRAIGHT_QUOTE_PAIRS + (QUOTE_PAIRS[tag] ?? QUOTE_PAIRS[tag.split("-")[0]] ?? QUOTE_PAIRS.en);
33
+ marks = { pairs: new Set(), opening: new Set(), closing: new Set() };
34
+ for (let i = 0; i < chars.length; i += 2) {
35
+ marks.pairs.add(chars[i] + chars[i + 1]);
36
+ marks.opening.add(chars[i]);
37
+ marks.closing.add(chars[i + 1]);
38
+ }
39
+ marksByLanguage.set(tag, marks);
40
+ return marks;
41
+ }
42
+
43
+ // Whether text starts with punctuation attaching to what precedes it, unlike a quote it opens, e.g. German »Welt«.
44
+ // French spaces its closing guillemet and high punctuation instead.
45
+ export function bindsToPrevious(text: string, lang: string | undefined): boolean {
46
+ if (!startsWithBindingPunct(text)) return false;
47
+ if (lang?.toLowerCase().split("-")[0] === "fr" && "»;:!?".includes(text[0])) return false;
48
+ return !(marksFor(lang).opening.has(text[0]) && text.length > 1 && !/[\s\p{P}]/u.test(text[1]));
49
+ }
50
+
51
+ // Takes dropped objects out of plain text, with any bracket or quote pair they leave empty.
52
+ export function removeDropped(plain: string, lang: string | undefined): string {
53
+ const { pairs, opening, closing } = marksFor(lang);
54
+ for (let i = plain.indexOf(DROPPED); i !== -1; i = plain.indexOf(DROPPED, i + 1)) {
55
+ let start = i;
56
+ let end = i + 1;
57
+ while (start > 0 && FILLER.test(plain[start - 1])) start--;
58
+ while (end < plain.length && FILLER.test(plain[end])) end++;
59
+ const open = plain[start - 1];
60
+ const close = plain[end];
61
+ if (open === undefined || close === undefined || !pairs.has(open + close)) continue;
62
+ // A mark that both opens and closes must be placed as the one it is here.
63
+ if (closing.has(open) && start > 1 && !/[\s\p{Ps}\p{Pi}\p{Pd}]/u.test(plain[start - 2])) continue;
64
+ if (opening.has(close) && end < plain.length - 1 && !/[\s\p{P}]/u.test(plain[end + 1])) continue;
65
+ // The pair becomes one dropped object, which may leave an enclosing pair empty in turn.
66
+ plain = plain.slice(0, start - 1) + DROPPED + plain.slice(end + 1);
67
+ i = start - 2;
68
+ }
69
+ // A space is kept only where there was one around the dropped objects, and never before binding punctuation.
70
+ return plain.replace(/ *\u0000[\u0000 ]*/g, (match: string, offset: number) => {
71
+ const rest = plain.slice(offset + match.length);
72
+ return offset > 0 && rest !== "" && match.includes(" ") && !bindsToPrevious(rest, lang) ? " " : "";
73
+ });
74
+ }
@@ -0,0 +1,37 @@
1
+ const XML_NS = "http://www.w3.org/XML/1998/namespace";
2
+
3
+ // An element's own language declaration, `null` when it has none. An HTML `xml:lang` (no namespace)
4
+ // isn't in the XML namespace, so it only counts when not empty, as it never makes a language unknown.
5
+ function ownLanguage(el: Element): string | null {
6
+ const xmlLang = el.getAttributeNS(XML_NS, "lang") ?? (el.getAttribute("xml:lang")?.trim() || null);
7
+ return xmlLang ?? el.getAttribute("lang");
8
+ }
9
+
10
+ // The language declared on `el` or its nearest ancestor, `undefined` when none is.
11
+ // An empty declaration (`lang=""`) means the language is unknown, so it isn't inherited past it.
12
+ export function declaredLanguage(el: Element | null): string | undefined {
13
+ for (let n: Element | null = el; n; n = n.parentElement) {
14
+ const language = ownLanguage(n);
15
+ if (language !== null) return language.trim();
16
+ }
17
+ return undefined;
18
+ }
19
+
20
+ export function nodeLanguage(el: Element | null): string {
21
+ return declaredLanguage(el) ?? "";
22
+ }
23
+
24
+ // BCP 47 tags are case-insensitive: "en-US" and "en-us" are the same language.
25
+ export function sameLanguage(a: string | undefined, b: string | undefined): boolean {
26
+ return a === b || (a !== undefined && b !== undefined && a.toLowerCase() === b.toLowerCase());
27
+ }
28
+
29
+ // The language all of `languages` are in, as first spelled, unknown ("") when they differ.
30
+ export function sharedLanguage(languages: Iterable<string>): string {
31
+ let shared: string | undefined;
32
+ for (const language of languages) {
33
+ if (shared === undefined) shared = language;
34
+ else if (!sameLanguage(shared, language)) return "";
35
+ }
36
+ return shared ?? "";
37
+ }
package/src/object.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { GuidedNavigationDescription, GuidedNavigationObject, GuidedNavigationText } from "@readium/shared";
2
2
  import type { GndRole } from "./types.ts";
3
3
  import { type TextBuilder, textIsEmpty, finalizeText } from "./text.ts";
4
+ import { detachSelfTextref } from "./textrefFragment.ts";
4
5
 
5
6
  export interface ObjBuilder {
6
7
  id?: string;
@@ -12,6 +13,12 @@ export interface ObjBuilder {
12
13
  role?: GndRole[];
13
14
  children?: ObjBuilder[];
14
15
  description?: string;
16
+ descriptionLanguage?: string;
17
+ }
18
+
19
+ export function setDescription(o: ObjBuilder, text: string, language: string): void {
20
+ o.description = text;
21
+ o.descriptionLanguage = language;
15
22
  }
16
23
 
17
24
  // Roles whose entire information is the role itself, with no ref/text/
@@ -137,6 +144,9 @@ export class NavObject {
137
144
  const textref = selfTextref(this.el);
138
145
  if (textref) result.textref = textref;
139
146
  }
147
+ // An object given another id than its element's references that element by selector.
148
+ const ownId = this.el?.getAttribute("id");
149
+ if (ownId && result.id !== ownId) detachSelfTextref(result, ownId);
140
150
 
141
151
  return result;
142
152
  }
@@ -165,7 +175,7 @@ export function finalizeToGuidedNavigationObject(o: ObjBuilder): GuidedNavigatio
165
175
  role: o.role && o.role.length > 0 ? new Set(o.role) : undefined,
166
176
  children: o.children && o.children.length > 0 ? o.children.map(finalizeToGuidedNavigationObject) : undefined,
167
177
  description: o.description
168
- ? new GuidedNavigationDescription({ text: new GuidedNavigationText({ plain: o.description }) })
178
+ ? new GuidedNavigationDescription({ text: new GuidedNavigationText({ plain: o.description, language: o.descriptionLanguage || undefined }) })
169
179
  : undefined,
170
180
  });
171
181
  if (ariaSubstitutedBuilders.has(o)) ariaSubstitutedNodes.add(node);
package/src/prescan.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { extractNodeRoles } from "./roles.ts";
2
2
  import { isAncestorOf } from "./dom.ts";
3
+ import { decodeIdFragment } from "./textrefFragment.ts";
3
4
 
4
5
  function hasEndnotesAncestor(el: Element): boolean {
5
6
  for (let p: Element | null = el.parentElement; p; p = p.parentElement) {
@@ -22,7 +23,7 @@ export function prescan(root: Element, ids: Map<string, Element>, suppressed: Se
22
23
  if (roles.includes("noteref")) {
23
24
  const href = n.getAttribute("href") ?? "";
24
25
  if (href.startsWith("#")) {
25
- noterefTargets.push({ id: href.slice(1), ref: n });
26
+ noterefTargets.push({ id: decodeIdFragment(href) ?? href.slice(1), ref: n });
26
27
  }
27
28
  }
28
29
  }
package/src/roles.ts CHANGED
@@ -143,6 +143,8 @@ const epubTypeRoles: Record<string, GndRole> = {
143
143
 
144
144
  // Elements whose role depends only on their tag name.
145
145
  const simpleElementTypeRoles: Record<string, GndRole> = {
146
+ abbr: "abbreviation",
147
+ acronym: "abbreviation",
146
148
  article: "article",
147
149
  aside: "aside",
148
150
  audio: "audio",
package/src/text.ts CHANGED
@@ -20,13 +20,16 @@ export function finalizeText(t?: TextBuilder): GuidedNavigationText | undefined
20
20
  }
21
21
 
22
22
  export interface SSMLContext {
23
- lang: string;
23
+ // `undefined` when no language is declared, "" when it's declared unknown.
24
+ lang: string | undefined;
24
25
  tag: string;
25
26
  attrs?: Record<string, string>;
27
+ // The abbreviation a `sub` expands, so adjacent identical ones stay separate segments.
28
+ abbr?: Element;
26
29
  }
27
30
 
28
31
  export function ctxEqual(a: SSMLContext, b: SSMLContext): boolean {
29
- if (a.lang !== b.lang || a.tag !== b.tag) return false;
32
+ if (a.lang !== b.lang || a.tag !== b.tag || a.abbr !== b.abbr) return false;
30
33
  const ae = Object.entries(a.attrs ?? {});
31
34
  const be = Object.entries(b.attrs ?? {});
32
35
  if (ae.length !== be.length) return false;
@@ -15,6 +15,14 @@ export function encodeCssSelectorFragment(selector: string): string {
15
15
  return `${CSS_PREFIX}${encodeURIComponent(selector)}${CSS_SUFFIX}`;
16
16
  }
17
17
 
18
+ // Without id as its own, node's bare "#id" textref would decode as a link, so it's re-expressed as that id's selector.
19
+ export function detachSelfTextref(node: { textref?: string }, id: string) {
20
+ const fragment = `#${encodeURIComponent(id)}`;
21
+ if (node.textref === fragment || node.textref?.startsWith(`${fragment}:~:`)) {
22
+ node.textref = encodeCssSelectorFragment(`#${CSS.escape(id)}`) + node.textref.slice(fragment.length);
23
+ }
24
+ }
25
+
18
26
  export function decodeCssSelectorFragment(textref: string | undefined): string | undefined {
19
27
  if (!textref || !textref.startsWith(CSS_PREFIX) || !textref.endsWith(CSS_SUFFIX)) return undefined;
20
28
  return decodeURIComponent(textref.slice(CSS_PREFIX.length, -CSS_SUFFIX.length));
@@ -131,7 +139,7 @@ export function combineDomRangeTextrefs(first: DecodedTextref, last: DecodedText
131
139
  return combined;
132
140
  }
133
141
 
134
- function decodeIdFragment(base: string): string | undefined {
142
+ export function decodeIdFragment(base: string): string | undefined {
135
143
  if (!base.startsWith("#")) return undefined;
136
144
  try {
137
145
  return decodeURIComponent(base.slice(1));
@@ -1,15 +1,20 @@
1
- /** Normalized (whitespace-coalesced and trimmed) text content of a node's subtree. */
2
- export declare function normalizedNodeText(el: Node): string;
1
+ export interface NodeText {
2
+ text: string;
3
+ language: string;
4
+ }
5
+ /** Normalized (whitespace-coalesced and trimmed) text content of a node's subtree, and its language. */
6
+ export declare function normalizedNodeText(el: Node): NodeText;
3
7
  /** Same as `normalizedNodeText`, excluding explicitly-role-bearing descendants. */
4
- export declare function normalizedNodeTextExcludingExplicitRoles(el: Node): string;
8
+ export declare function normalizedNodeTextExcludingExplicitRoles(el: Node): NodeText;
5
9
  /**
6
10
  * Computes the text that becomes a node's `GuidedNavigationObject.description`, and
7
11
  * whether the node is visible in the first place. Follows the AccName
8
12
  * precedence order (https://www.w3.org/TR/accname/#terminology, 2.A-2.D)
9
13
  * for its accessible-name sources, with a non-AccName `aria-describedby`
10
14
  * fallback spliced in between 2.C and 2.D — see that branch below.
15
+ * Also returns that text's language.
11
16
  */
12
- export declare function extractNodeAria(el: Element): [string | null, boolean];
17
+ export declare function extractNodeAria(el: Element): [string | null, boolean, string];
13
18
  /**
14
19
  * Maps an HTML element to the SSML tag its text should be wrapped in.
15
20
  * https://www.w3.org/TR/speech-synthesis11/#S3.2.2
@@ -20,6 +20,7 @@ export declare class Converter {
20
20
  baseHref?: string;
21
21
  selectorRoot: Element | null;
22
22
  selectorRootAnchor: string | null;
23
+ copy: boolean;
23
24
  private root;
24
25
  private current;
25
26
  private foldedCaptions;
@@ -50,6 +51,9 @@ export declare class Converter {
50
51
  private applyTextref;
51
52
  private text;
52
53
  private textContext;
54
+ private static containsObject;
55
+ private static isPlaceholder;
56
+ private static isSoleContent;
53
57
  private updateFlowSpace;
54
58
  private closeSegment;
55
59
  private resetFlow;
@@ -1,4 +1,3 @@
1
- export declare function nodeLanguage(el: Element | null): string;
2
1
  export declare function hasElementChild(el: Element): boolean;
3
2
  export declare function isAncestorOf(anc: Element, n: Element): boolean;
4
3
  export declare function isInlineTag(tagName: string): boolean;
@@ -1,5 +1,6 @@
1
1
  import type { GndRole } from "./types.ts";
2
2
  import { Converter } from "./converter.ts";
3
- export declare function pagebreak(converter: Converter, el: Element, aria: string | null, roles: GndRole[]): boolean;
3
+ export declare function pagebreak(converter: Converter, el: Element, aria: string | null, ariaLanguage: string, roles: GndRole[]): boolean;
4
4
  export declare function noteref(converter: Converter, el: Element, roles: GndRole[]): void;
5
- export declare function link(converter: Converter, el: Element, roles: GndRole[], aria: string | null): void;
5
+ export declare function link(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): void;
6
+ export declare function reference(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): void;
@@ -0,0 +1,3 @@
1
+ export declare const DROPPED = "\0";
2
+ export declare function bindsToPrevious(text: string, lang: string | undefined): boolean;
3
+ export declare function removeDropped(plain: string, lang: string | undefined): string;
@@ -0,0 +1,4 @@
1
+ export declare function declaredLanguage(el: Element | null): string | undefined;
2
+ export declare function nodeLanguage(el: Element | null): string;
3
+ export declare function sameLanguage(a: string | undefined, b: string | undefined): boolean;
4
+ export declare function sharedLanguage(languages: Iterable<string>): string;
@@ -11,7 +11,9 @@ export interface ObjBuilder {
11
11
  role?: GndRole[];
12
12
  children?: ObjBuilder[];
13
13
  description?: string;
14
+ descriptionLanguage?: string;
14
15
  }
16
+ export declare function setDescription(o: ObjBuilder, text: string, language: string): void;
15
17
  export declare const ariaSubstitutedBuilders: WeakSet<ObjBuilder>;
16
18
  export declare const substitutedOwnSelectorBuilders: WeakMap<ObjBuilder, string>;
17
19
  export declare function isAriaSubstituted(node: GuidedNavigationObject): boolean;
@@ -7,9 +7,10 @@ export interface TextBuilder {
7
7
  export declare function textIsEmpty(t: TextBuilder): boolean;
8
8
  export declare function finalizeText(t?: TextBuilder): GuidedNavigationText | undefined;
9
9
  export interface SSMLContext {
10
- lang: string;
10
+ lang: string | undefined;
11
11
  tag: string;
12
12
  attrs?: Record<string, string>;
13
+ abbr?: Element;
13
14
  }
14
15
  export declare function ctxEqual(a: SSMLContext, b: SSMLContext): boolean;
15
16
  export declare function isNoBreakSpace(ch: string): boolean;
@@ -1,4 +1,7 @@
1
1
  export declare function encodeCssSelectorFragment(selector: string): string;
2
+ export declare function detachSelfTextref(node: {
3
+ textref?: string;
4
+ }, id: string): void;
2
5
  export declare function decodeCssSelectorFragment(textref: string | undefined): string | undefined;
3
6
  export interface DomRangeJSON {
4
7
  start: {
@@ -35,6 +38,7 @@ export interface DecodedTextref {
35
38
  fragment?: string;
36
39
  }
37
40
  export declare function combineDomRangeTextrefs(first: DecodedTextref, last: DecodedTextref): DecodedTextref | undefined;
41
+ export declare function decodeIdFragment(base: string): string | undefined;
38
42
  export declare function decodeTextref(node: {
39
43
  id?: string;
40
44
  textref?: string;