@readium/guided-navigation 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +4 -1
  2. package/dist/converter.js +1 -1
  3. package/dist/elementHandlers.js +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/object.js +1 -1
  6. package/dist/roles.js +1 -1
  7. package/fixtures/ROLES_COVERAGE.md +13 -11
  8. package/fixtures/abbreviation-nested-term-punctuation/gnd.json +6 -6
  9. package/fixtures/abbreviation-nested-term-punctuation/input.html +1 -1
  10. package/fixtures/abbreviation-nested-term-punctuation/meta.json +1 -1
  11. package/fixtures/abbreviation-period-mid-sentence/gnd.json +9 -0
  12. package/fixtures/abbreviation-period-mid-sentence/input.html +1 -0
  13. package/fixtures/abbreviation-period-mid-sentence/meta.json +11 -0
  14. package/fixtures/abbreviation-period-sentence-end/gnd.json +9 -0
  15. package/fixtures/abbreviation-period-sentence-end/input.html +1 -0
  16. package/fixtures/abbreviation-period-sentence-end/meta.json +11 -0
  17. package/fixtures/accessible-name-labelledby-aria-hidden/gnd.json +1 -1
  18. package/fixtures/accessible-name-labelledby-hidden-attribute/gnd.json +1 -1
  19. package/fixtures/biblioref-target-descendant-ids/gnd.json +3 -3
  20. package/fixtures/biblioref-target-in-bibliography-role-aria/gnd.json +1 -1
  21. package/fixtures/biblioref-target-mixed-content/gnd.json +3 -3
  22. package/fixtures/description-roleless-lone-child/gnd.json +24 -0
  23. package/fixtures/description-roleless-lone-child/input.html +1 -0
  24. package/fixtures/description-roleless-lone-child/meta.json +11 -0
  25. package/fixtures/description-roleless-wrapper/gnd.json +25 -0
  26. package/fixtures/description-roleless-wrapper/input.html +1 -0
  27. package/fixtures/description-roleless-wrapper/meta.json +10 -0
  28. package/fixtures/glossary-role-aria/gnd.json +23 -31
  29. package/fixtures/glossref-role-aria-mid-sentence/gnd.json +1 -1
  30. package/fixtures/glossref-sole-content/gnd.json +12 -20
  31. package/fixtures/glossref-target-in-glossary-role-aria/gnd.json +13 -21
  32. package/fixtures/glossref-target-percent-encoded/gnd.json +1 -1
  33. package/fixtures/language-alt-inherited/gnd.json +25 -0
  34. package/fixtures/language-alt-inherited/input.html +1 -0
  35. package/fixtures/language-alt-inherited/meta.json +11 -0
  36. package/fixtures/language-alt-own-lang/gnd.json +25 -0
  37. package/fixtures/language-alt-own-lang/input.html +1 -0
  38. package/fixtures/language-alt-own-lang/meta.json +11 -0
  39. package/fixtures/language-block-empty-lang/gnd.json +8 -0
  40. package/fixtures/language-block-empty-lang/input.html +1 -0
  41. package/fixtures/language-block-empty-lang/meta.json +10 -0
  42. package/fixtures/language-describedby-shared/gnd.json +49 -0
  43. package/fixtures/language-describedby-shared/input.html +1 -0
  44. package/fixtures/language-describedby-shared/meta.json +12 -0
  45. package/fixtures/language-empty-lang-stops-inheritance/gnd.json +24 -0
  46. package/fixtures/language-empty-lang-stops-inheritance/input.html +1 -0
  47. package/fixtures/language-empty-lang-stops-inheritance/meta.json +11 -0
  48. package/fixtures/language-figcaption/gnd.json +25 -0
  49. package/fixtures/language-figcaption/input.html +1 -0
  50. package/fixtures/language-figcaption/meta.json +11 -0
  51. package/fixtures/language-figcaption-credit/gnd.json +41 -0
  52. package/fixtures/language-figcaption-credit/input.html +1 -0
  53. package/fixtures/language-figcaption-credit/meta.json +12 -0
  54. package/fixtures/language-figcaption-switches/gnd.json +24 -0
  55. package/fixtures/language-figcaption-switches/input.html +1 -0
  56. package/fixtures/language-figcaption-switches/meta.json +11 -0
  57. package/fixtures/language-figcaption-switches-throughout/gnd.json +25 -0
  58. package/fixtures/language-figcaption-switches-throughout/input.html +1 -0
  59. package/fixtures/language-figcaption-switches-throughout/meta.json +11 -0
  60. package/fixtures/language-inline-case-insensitive/gnd.json +9 -0
  61. package/fixtures/language-inline-case-insensitive/input.html +1 -0
  62. package/fixtures/language-inline-case-insensitive/meta.json +10 -0
  63. package/fixtures/language-inline-case-insensitive-ssml/gnd.json +9 -0
  64. package/fixtures/language-inline-case-insensitive-ssml/input.html +1 -0
  65. package/fixtures/language-inline-case-insensitive-ssml/meta.json +10 -0
  66. package/fixtures/language-inline-empty-lang/gnd.json +9 -0
  67. package/fixtures/language-inline-empty-lang/input.html +1 -0
  68. package/fixtures/language-inline-empty-lang/meta.json +10 -0
  69. package/fixtures/language-inline-empty-lang-formatted/gnd.json +8 -0
  70. package/fixtures/language-inline-empty-lang-formatted/input.html +1 -0
  71. package/fixtures/language-inline-empty-lang-formatted/meta.json +10 -0
  72. package/fixtures/language-inline-empty-lang-only/gnd.json +8 -0
  73. package/fixtures/language-inline-empty-lang-only/input.html +1 -0
  74. package/fixtures/language-inline-empty-lang-only/meta.json +10 -0
  75. package/fixtures/language-inline-empty-lang-undeclared/gnd.json +9 -0
  76. package/fixtures/language-inline-empty-lang-undeclared/input.html +1 -0
  77. package/fixtures/language-inline-empty-lang-undeclared/meta.json +10 -0
  78. package/fixtures/language-labelledby-target/gnd.json +26 -0
  79. package/fixtures/language-labelledby-target/input.html +1 -0
  80. package/fixtures/language-labelledby-target/meta.json +11 -0
  81. package/fixtures/language-labelledby-target-aria-label/gnd.json +32 -0
  82. package/fixtures/language-labelledby-target-aria-label/input.html +1 -0
  83. package/fixtures/language-labelledby-target-aria-label/meta.json +11 -0
  84. package/fixtures/language-labelledby-target-switches/gnd.json +25 -0
  85. package/fixtures/language-labelledby-target-switches/input.html +1 -0
  86. package/fixtures/language-labelledby-target-switches/meta.json +11 -0
  87. package/fixtures/language-labelledby-targets-case/gnd.json +36 -0
  88. package/fixtures/language-labelledby-targets-case/input.html +1 -0
  89. package/fixtures/language-labelledby-targets-case/meta.json +11 -0
  90. package/fixtures/language-labelledby-targets-differ/gnd.json +35 -0
  91. package/fixtures/language-labelledby-targets-differ/input.html +1 -0
  92. package/fixtures/language-labelledby-targets-differ/meta.json +11 -0
  93. package/fixtures/language-link-switches/gnd.json +20 -0
  94. package/fixtures/language-link-switches/input.html +1 -0
  95. package/fixtures/language-link-switches/meta.json +11 -0
  96. package/fixtures/language-pagebreak-label/gnd.json +22 -0
  97. package/fixtures/language-pagebreak-label/input.html +1 -0
  98. package/fixtures/language-pagebreak-label/meta.json +11 -0
  99. package/fixtures/language-undeclared-description/gnd.json +23 -0
  100. package/fixtures/language-undeclared-description/input.html +1 -0
  101. package/fixtures/language-undeclared-description/meta.json +11 -0
  102. package/fixtures/language-xml-lang-empty-html-inherits/gnd.json +25 -0
  103. package/fixtures/language-xml-lang-empty-html-inherits/input.html +1 -0
  104. package/fixtures/language-xml-lang-empty-html-inherits/meta.json +11 -0
  105. package/fixtures/language-xml-lang-empty-html-lang-applies/gnd.json +25 -0
  106. package/fixtures/language-xml-lang-empty-html-lang-applies/input.html +1 -0
  107. package/fixtures/language-xml-lang-empty-html-lang-applies/meta.json +11 -0
  108. package/fixtures/language-xml-lang-empty-xhtml/gnd.json +24 -0
  109. package/fixtures/language-xml-lang-empty-xhtml/input.xhtml +2 -0
  110. package/fixtures/language-xml-lang-empty-xhtml/meta.json +11 -0
  111. package/fixtures/language-xml-lang-over-lang/gnd.json +25 -0
  112. package/fixtures/language-xml-lang-over-lang/input.html +1 -0
  113. package/fixtures/language-xml-lang-over-lang/meta.json +11 -0
  114. package/fixtures/link-containing-image/gnd.json +35 -0
  115. package/fixtures/link-containing-image/input.html +1 -0
  116. package/fixtures/link-containing-image/meta.json +12 -0
  117. package/fixtures/link-containing-pagebreak/gnd.json +33 -0
  118. package/fixtures/link-containing-pagebreak/input.html +1 -0
  119. package/fixtures/link-containing-pagebreak/meta.json +12 -0
  120. package/fixtures/link-content-conversion/gnd.json +31 -0
  121. package/fixtures/link-content-conversion/input.html +1 -0
  122. package/fixtures/link-content-conversion/meta.json +12 -0
  123. package/fixtures/link-content-identity/gnd.json +21 -0
  124. package/fixtures/link-content-identity/input.html +1 -0
  125. package/fixtures/link-content-identity/meta.json +10 -0
  126. package/fixtures/link-content-words/gnd.json +46 -0
  127. package/fixtures/link-content-words/input.html +1 -0
  128. package/fixtures/link-content-words/meta.json +10 -0
  129. package/fixtures/manifest.json +431 -1
  130. package/fixtures/math-inline/gnd.json +22 -0
  131. package/fixtures/math-inline/input.html +1 -0
  132. package/fixtures/math-inline/meta.json +11 -0
  133. package/fixtures/nested-blockquote/gnd.json +22 -0
  134. package/fixtures/nested-blockquote/input.html +5 -0
  135. package/fixtures/nested-blockquote/meta.json +11 -0
  136. package/fixtures/nested-section/gnd.json +22 -0
  137. package/fixtures/nested-section/input.html +5 -0
  138. package/fixtures/nested-section/meta.json +11 -0
  139. package/fixtures/redundant-role-body-chapter/gnd.json +23 -0
  140. package/fixtures/redundant-role-body-chapter/input.xhtml +10 -0
  141. package/fixtures/redundant-role-body-chapter/meta.json +13 -0
  142. package/fixtures/redundant-role-nested-chapter/gnd.json +30 -0
  143. package/fixtures/redundant-role-nested-chapter/input.xhtml +12 -0
  144. package/fixtures/redundant-role-nested-chapter/meta.json +13 -0
  145. package/fixtures/table-cell-link/gnd.json +48 -0
  146. package/fixtures/table-cell-link/input.html +1 -0
  147. package/fixtures/table-cell-link/meta.json +14 -0
  148. package/fixtures/term-span-in-dt/gnd.json +24 -0
  149. package/fixtures/term-span-in-dt/input.html +4 -0
  150. package/fixtures/term-span-in-dt/meta.json +12 -0
  151. package/package.json +1 -1
  152. package/src/converter.ts +72 -15
  153. package/src/elementHandlers.ts +31 -11
  154. package/src/emptyPairs.ts +2 -1
  155. package/src/index.ts +1 -0
  156. package/src/object.ts +7 -10
  157. package/src/roles.ts +49 -0
  158. package/types/src/converter.d.ts +21 -2
  159. package/types/src/emptyPairs.d.ts +2 -0
  160. package/types/src/index.d.ts +1 -0
  161. package/types/src/object.d.ts +1 -0
  162. package/types/src/roles.d.ts +1 -0
@@ -0,0 +1,11 @@
1
+ {
2
+ "id": "math-inline",
3
+ "description": "Inline MathML in a sentence becomes a placeholder in the sentence's SSML, with the math object as a child",
4
+ "role": "math",
5
+ "rolesCovered": [
6
+ "paragraph",
7
+ "math"
8
+ ],
9
+ "sourceRef": "https://github.com/readium/guided-navigation/tree/main/examples/read-aloud",
10
+ "inputKind": "fragment"
11
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "children": [
3
+ {
4
+ "children": [
5
+ {
6
+ "role": [
7
+ "paragraph"
8
+ ],
9
+ "text": {
10
+ "plain": "The light must never go out."
11
+ }
12
+ }
13
+ ],
14
+ "role": [
15
+ "blockquote"
16
+ ]
17
+ }
18
+ ],
19
+ "role": [
20
+ "blockquote"
21
+ ]
22
+ }
@@ -0,0 +1,5 @@
1
+ <blockquote>
2
+ <blockquote>
3
+ <p>The light must never go out.</p>
4
+ </blockquote>
5
+ </blockquote>
@@ -0,0 +1,11 @@
1
+ {
2
+ "id": "nested-blockquote",
3
+ "description": "A blockquote whose sole child is another blockquote, a quotation within a quotation: both keep the blockquote role",
4
+ "role": "blockquote",
5
+ "rolesCovered": [
6
+ "blockquote",
7
+ "paragraph"
8
+ ],
9
+ "sourceRef": "https://github.com/readium/guided-navigation/blob/main/roles.md",
10
+ "inputKind": "fragment"
11
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "children": [
3
+ {
4
+ "children": [
5
+ {
6
+ "role": [
7
+ "paragraph"
8
+ ],
9
+ "text": {
10
+ "plain": "The keeper climbed the stairs."
11
+ }
12
+ }
13
+ ],
14
+ "role": [
15
+ "section"
16
+ ]
17
+ }
18
+ ],
19
+ "role": [
20
+ "section"
21
+ ]
22
+ }
@@ -0,0 +1,5 @@
1
+ <section>
2
+ <section>
3
+ <p>The keeper climbed the stairs.</p>
4
+ </section>
5
+ </section>
@@ -0,0 +1,11 @@
1
+ {
2
+ "id": "nested-section",
3
+ "description": "A section whose sole child is another section: both keep the section role",
4
+ "role": "section",
5
+ "rolesCovered": [
6
+ "section",
7
+ "paragraph"
8
+ ],
9
+ "sourceRef": "https://github.com/readium/guided-navigation/blob/main/roles.md",
10
+ "inputKind": "fragment"
11
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "children": [
3
+ {
4
+ "children": [
5
+ {
6
+ "role": [
7
+ "paragraph"
8
+ ],
9
+ "text": {
10
+ "plain": "It was a dark and stormy night."
11
+ }
12
+ }
13
+ ],
14
+ "role": [
15
+ "section"
16
+ ]
17
+ }
18
+ ],
19
+ "role": [
20
+ "body",
21
+ "chapter"
22
+ ]
23
+ }
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE html>
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
4
+ <head><meta charset="utf-8"/><title>Fixture</title></head>
5
+ <body epub:type="chapter">
6
+ <section epub:type="chapter">
7
+ <p>It was a dark and stormy night.</p>
8
+ </section>
9
+ </body>
10
+ </html>
@@ -0,0 +1,13 @@
1
+ {
2
+ "id": "redundant-role-body-chapter",
3
+ "description": "A chapter whose body and sole section both declare it via epub:type: the body keeps the role, the section doesn't repeat it",
4
+ "role": "chapter",
5
+ "rolesCovered": [
6
+ "body",
7
+ "section",
8
+ "chapter",
9
+ "paragraph"
10
+ ],
11
+ "sourceRef": "https://www.w3.org/TR/epub-ssv-11/",
12
+ "inputKind": "document"
13
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "children": [
3
+ {
4
+ "children": [
5
+ {
6
+ "children": [
7
+ {
8
+ "role": [
9
+ "paragraph"
10
+ ],
11
+ "text": {
12
+ "plain": "It was a dark and stormy night."
13
+ }
14
+ }
15
+ ],
16
+ "role": [
17
+ "section"
18
+ ]
19
+ }
20
+ ],
21
+ "role": [
22
+ "section",
23
+ "chapter"
24
+ ]
25
+ }
26
+ ],
27
+ "role": [
28
+ "body"
29
+ ]
30
+ }
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE html>
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
4
+ <head><meta charset="utf-8"/><title>Fixture</title></head>
5
+ <body>
6
+ <section epub:type="chapter">
7
+ <section epub:type="chapter">
8
+ <p>It was a dark and stormy night.</p>
9
+ </section>
10
+ </section>
11
+ </body>
12
+ </html>
@@ -0,0 +1,13 @@
1
+ {
2
+ "id": "redundant-role-nested-chapter",
3
+ "description": "A chapter section whose sole child section declares chapter again via epub:type: the outer section keeps it, the inner one stays a section",
4
+ "role": "chapter",
5
+ "rolesCovered": [
6
+ "body",
7
+ "section",
8
+ "chapter",
9
+ "paragraph"
10
+ ],
11
+ "sourceRef": "https://www.w3.org/TR/epub-ssv-11/",
12
+ "inputKind": "document"
13
+ }
@@ -0,0 +1,48 @@
1
+ {
2
+ "role": [
3
+ "table"
4
+ ],
5
+ "children": [
6
+ {
7
+ "role": [
8
+ "row"
9
+ ],
10
+ "children": [
11
+ {
12
+ "role": [
13
+ "columnheader"
14
+ ],
15
+ "text": {
16
+ "plain": "Keeper"
17
+ }
18
+ }
19
+ ]
20
+ },
21
+ {
22
+ "role": [
23
+ "row"
24
+ ],
25
+ "children": [
26
+ {
27
+ "role": [
28
+ "cell"
29
+ ],
30
+ "text": {
31
+ "plain": "Henry Winstanley himself",
32
+ "ssml": "Henry <readium:link id=\"link1\" /> himself",
33
+ "language": "en"
34
+ },
35
+ "children": [
36
+ {
37
+ "id": "link1",
38
+ "text": {
39
+ "plain": "Winstanley"
40
+ },
41
+ "textref": "c.html"
42
+ }
43
+ ]
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ }
@@ -0,0 +1 @@
1
+ <table><tr><th>Keeper</th></tr><tr><td>Henry <a href="c.html">Winstanley</a> himself</td></tr></table>
@@ -0,0 +1,14 @@
1
+ {
2
+ "id": "table-cell-link",
3
+ "description": "A link in a table cell; the cell's plain keeps the link's words",
4
+ "role": "link",
5
+ "rolesCovered": [
6
+ "table",
7
+ "row",
8
+ "columnheader",
9
+ "cell",
10
+ "link"
11
+ ],
12
+ "sourceRef": "https://github.com/readium/guided-navigation/tree/main/examples/read-aloud",
13
+ "inputKind": "fragment"
14
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "children": [
3
+ {
4
+ "id": "lens",
5
+ "role": [
6
+ "term"
7
+ ],
8
+ "text": {
9
+ "plain": "Lens"
10
+ }
11
+ },
12
+ {
13
+ "role": [
14
+ "definition"
15
+ ],
16
+ "text": {
17
+ "plain": "A piece of glass that focuses light."
18
+ }
19
+ }
20
+ ],
21
+ "role": [
22
+ "glossary"
23
+ ]
24
+ }
@@ -0,0 +1,4 @@
1
+ <dl role="doc-glossary">
2
+ <dt id="lens"><span role="term">Lens</span></dt>
3
+ <dd>A piece of glass that focuses light.</dd>
4
+ </dl>
@@ -0,0 +1,12 @@
1
+ {
2
+ "id": "term-span-in-dt",
3
+ "description": "A glossary term whose dt wraps a span with role=\"term\": the term is marked once, on the dt",
4
+ "role": "term",
5
+ "rolesCovered": [
6
+ "glossary",
7
+ "term",
8
+ "definition"
9
+ ],
10
+ "sourceRef": "https://github.com/readium/guided-navigation/blob/main/roles.md",
11
+ "inputKind": "fragment"
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readium/guided-navigation",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "type": "module",
5
5
  "description": "Guided Navigation document generation from HTML and XHTML content",
6
6
  "author": "readium",
package/src/converter.ts CHANGED
@@ -33,6 +33,7 @@ const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
33
33
 
34
34
  const TEXT_NODE = 3;
35
35
  const ELEMENT_NODE = 1;
36
+ const DOCUMENT_POSITION_FOLLOWING = 4;
36
37
 
37
38
  // These elements are normally treated as inline (text flows through them
38
39
  // without opening their own object), but when they carry a role of their own
@@ -46,13 +47,15 @@ function isBlockNode(tagName: string, roles: GndRole[]): boolean {
46
47
 
47
48
  type SegmentKind = "text" | "break" | "placeholder";
48
49
 
49
- interface Segment {
50
+ export interface Segment {
50
51
  kind: SegmentKind;
51
52
  text?: string;
52
53
  ctx?: SSMLContext;
53
54
  tag?: string;
54
55
  child?: NavObject;
55
56
  candidateID?: string;
57
+ // A placeholder's own pieces of the sentence's plain text, e.g. a link's words.
58
+ words?: Segment[];
56
59
  }
57
60
 
58
61
  /** Walks a DOM subtree, building the Guided Navigation object tree. */
@@ -109,6 +112,8 @@ export class Converter {
109
112
  // The plain-text string of the flow flushText() just flushed, read by
110
113
  // tail() right after calling it as the text-fragment candidate text.
111
114
  private lastFlowText: string | null = null;
115
+ // When set, records the converted content's plain-text pieces in source order, across flows: a link's words.
116
+ words: Segment[] | null = null;
112
117
 
113
118
  constructor(xmlParsed: boolean) {
114
119
  this.xmlParsed = xmlParsed;
@@ -193,7 +198,8 @@ export class Converter {
193
198
 
194
199
  private walk(node: Node) {
195
200
  if (node.nodeType === TEXT_NODE) {
196
- this.text(node);
201
+ // Punctuation belonging to an object is taken in by that object's own walk.
202
+ if (!Converter.punctuationOwner(node)) this.text(node);
197
203
  return;
198
204
  }
199
205
  if (node.nodeType !== ELEMENT_NODE) return;
@@ -202,7 +208,11 @@ export class Converter {
202
208
  const parent = this.current;
203
209
  const wasSkip = this.head(el);
204
210
  if (!wasSkip) {
211
+ const punctuation = this.current !== parent && this.current.el === el ? Converter.ownPunctuation(el) : [];
212
+ const isBefore = (n: Node) => (n.compareDocumentPosition(el) & DOCUMENT_POSITION_FOLLOWING) !== 0;
213
+ for (const n of punctuation) if (isBefore(n)) this.text(n);
205
214
  for (let c = el.firstChild; c; c = c.nextSibling) this.walk(c);
215
+ for (const n of punctuation) if (!isBefore(n)) this.text(n);
206
216
  }
207
217
  this.tail(el, wasSkip, parent);
208
218
  }
@@ -233,6 +243,7 @@ export class Converter {
233
243
  if (!this.current.noText && !this.textContext(el).abbr) {
234
244
  this.closeSegment();
235
245
  this.segments.push({ kind: "break" });
246
+ this.words?.push({ kind: "break" });
236
247
  this.flowEndsWithSpace = true;
237
248
  }
238
249
  return true;
@@ -307,6 +318,7 @@ export class Converter {
307
318
  }
308
319
 
309
320
  this.flushText();
321
+ if (!isInlineTag(tagName)) this.words?.push({ kind: "break" });
310
322
  this.descend(el, roles);
311
323
 
312
324
  const cur = this.current.object;
@@ -361,6 +373,7 @@ export class Converter {
361
373
  if (roles.includes("pagebreak")) return;
362
374
  if (isBlockNode(tagName, roles)) {
363
375
  this.flushText();
376
+ if (!isInlineTag(tagName)) this.words?.push({ kind: "break" });
364
377
  // A child block splits el's text into several flows; the last one alone doesn't represent el.
365
378
  if (this.current.hasChildBlock) {
366
379
  this.lastFlowRange = null;
@@ -406,6 +419,7 @@ export class Converter {
406
419
  private text(node: Node) {
407
420
  if (this.current.noText) return;
408
421
  const data = node.nodeValue ?? "";
422
+ this.words?.push({ kind: "text", text: normalizeWhitespace(data, false) });
409
423
  if (/^\s*$/.test(data)) {
410
424
  if (this.textAcc.length > 0 || this.segments.length > 0) {
411
425
  this.textAcc += normalizeWhitespace(data, this.flowEndsWithSpace);
@@ -436,17 +450,18 @@ export class Converter {
436
450
  // An abbreviation's text is replaced by its expansion as a whole, so it wins over any formatting inside it.
437
451
  // It applies only if all its text is in one object, so its alias is never repeated or misplaced.
438
452
  // The outermost one wins, its expansion covering any nested inside it.
439
- let abbr: SSMLContext | undefined;
440
- for (let p = node.parentElement, enclosing = false; p; p = p.parentElement) {
441
- const alias = abbreviationExpansion(p);
442
- if (alias) {
443
- if (!enclosing && Converter.containsObject(p)) break;
444
- abbr = { lang: declaredLanguage(p), tag: "sub", attrs: { alias }, abbr: p };
453
+ const owner = Converter.punctuationOwner(node);
454
+ let abbr = owner ? Converter.abbreviationOver(owner) : null;
455
+ if (!owner) {
456
+ let p = node.parentElement;
457
+ for (; p && p !== this.current.el; p = p.parentElement) {
458
+ if (!abbreviationExpansion(p)) continue;
459
+ if (Converter.containsObject(p)) break;
460
+ abbr = p;
445
461
  }
446
- if (p === this.current.el) enclosing = true;
447
- if (enclosing && !Converter.isSoleContent(p)) break;
462
+ if (p && p === this.current.el) abbr = Converter.abbreviationOver(p) ?? abbr;
448
463
  }
449
- if (abbr) return abbr;
464
+ if (abbr) return { lang: declaredLanguage(abbr), tag: "sub", attrs: { alias: abbreviationExpansion(abbr) }, abbr };
450
465
  const ctx: SSMLContext = { lang: declaredLanguage(node.parentElement), tag: "" };
451
466
  for (let p = node.parentElement; p && p !== this.current.el; p = p.parentElement) {
452
467
  const [tag, attrs] = convertElementToSSMLTag(p.tagName.toLowerCase());
@@ -475,7 +490,45 @@ export class Converter {
475
490
  return ["img", "svg", "audio", "video"].includes(tagName) || (tagName === "a" && !!el.getAttribute("href"));
476
491
  }
477
492
 
478
- // Punctuation alongside doesn't count, e.g. the last period of <abbr><dfn>W.H.O</dfn>.</abbr>.
493
+ // The outermost abbreviation whose text is all in obj, apart from punctuation alongside it.
494
+ private static abbreviationOver(obj: Element): Element | null {
495
+ let abbr: Element | null = null;
496
+ for (let p: Element | null = obj; p; p = p.parentElement) {
497
+ if (abbreviationExpansion(p)) abbr = p;
498
+ if (!Converter.isSoleContent(p)) break;
499
+ }
500
+ return abbr;
501
+ }
502
+
503
+ // The innermost object an abbreviation holds as its whole content, when node is punctuation alongside it:
504
+ // the object whose text carries the expansion, so the punctuation joins it there.
505
+ private static punctuationOwner(node: Node): Element | null {
506
+ const data = node.nodeValue ?? "";
507
+ if (node.nodeType !== TEXT_NODE || !/\S/.test(data) || !/^[\s\p{P}]*$/u.test(data)) return null;
508
+ let owner: Element | null = null;
509
+ for (let obj = node.parentElement?.firstElementChild ?? null; obj && Converter.isSoleContent(obj); obj = obj.firstElementChild) {
510
+ const tagName = obj.tagName.toLowerCase();
511
+ const roles = extractNodeRoles(obj);
512
+ if (skippedElements.has(tagName) || Converter.isPlaceholder(obj, tagName, roles) || !extractNodeAria(obj)[1]) break;
513
+ if (isBlockNode(tagName, roles)) owner = obj;
514
+ }
515
+ return owner && Converter.abbreviationOver(owner)?.contains(node) ? owner : null;
516
+ }
517
+
518
+ // The punctuation alongside el that its abbreviation holds, in document order.
519
+ private static ownPunctuation(el: Element): Node[] {
520
+ const abbr = Converter.abbreviationOver(el);
521
+ const found: Node[] = [];
522
+ const collect = (n: Node) => {
523
+ if (n === el) return;
524
+ if (n.nodeType === TEXT_NODE && Converter.punctuationOwner(n) === el) found.push(n);
525
+ for (let c = n.firstChild; c; c = c.nextSibling) collect(c);
526
+ };
527
+ if (abbr) collect(abbr);
528
+ return found;
529
+ }
530
+
531
+ // Punctuation alongside doesn't count, e.g. the period of <abbr><dfn>Dr</dfn>.</abbr>.
479
532
  private static isSoleContent(el: Element): boolean {
480
533
  for (let s = el.parentNode?.firstChild; s; s = s.nextSibling) {
481
534
  if (s !== el && (s.nodeType === ELEMENT_NODE || (s.nodeType === TEXT_NODE && !/^[\s\p{P}]*$/u.test(s.nodeValue ?? "")))) return false;
@@ -507,7 +560,8 @@ export class Converter {
507
560
  this.flowLastOffset = 0;
508
561
  }
509
562
 
510
- placeholder(el: Element, tag: string, object: ObjBuilder, candidateID?: string) {
563
+ // `words`: the object's pieces of plain text when they're part of the sentence it sits in, e.g. a link's.
564
+ placeholder(el: Element, tag: string, object: ObjBuilder, { candidateID, words }: { candidateID?: string; words?: Segment[] } = {}) {
511
565
  if (isEmptyObj(object)) return;
512
566
  const child = new NavObject();
513
567
  child.el = el;
@@ -523,7 +577,9 @@ export class Converter {
523
577
  tag,
524
578
  child,
525
579
  candidateID: candidateID ?? el.getAttribute("id") ?? undefined,
580
+ words: words?.length ? words : undefined,
526
581
  });
582
+ this.words?.push(...(words?.length ? words : [{ kind: "placeholder" as const }]));
527
583
  this.flowEndsWithSpace = false;
528
584
  }
529
585
 
@@ -616,7 +672,8 @@ export class Converter {
616
672
  if (!textLangs.some((other) => sameLanguage(other, lang))) textLangs.push(lang);
617
673
  }
618
674
  }
619
- let baseLang = declaredLanguage(this.current.el ?? null);
675
+ // A sub-conversion's own content is in the language of the element it converts.
676
+ let baseLang = declaredLanguage(this.current.el ?? this.allowNode);
620
677
  if (textLangs.length === 1) {
621
678
  baseLang = textLangs[0];
622
679
  }
@@ -654,7 +711,7 @@ export class Converter {
654
711
  let sawBreak = false;
655
712
  let sawPlaceholder = false;
656
713
  let midSpace = false;
657
- for (const seg of segments) {
714
+ for (const seg of segments.flatMap((s) => s.words ?? [s])) {
658
715
  if (seg.kind === "text") {
659
716
  const lead = seg.text!.startsWith(" ");
660
717
  const t = seg.text!.replace(/^ +| +$/g, "");
@@ -6,10 +6,10 @@ import type { GndRole } from "./types.ts";
6
6
  import { ssmlAttrEscape } from "@readium/helpers";
7
7
  import { normalizedNodeText } from "./a11y.ts";
8
8
  import { normalizeWhitespace, textIsEmpty } from "./text.ts";
9
- import { type ObjBuilder, ariaSubstitutedBuilders, substitutedOwnSelectorBuilders } from "./object.ts";
9
+ import { type ObjBuilder, ariaSubstitutedBuilders, isHoistable, substitutedOwnSelectorBuilders } from "./object.ts";
10
10
  import { hasElementChild, isAncestorOf } from "./dom.ts";
11
11
  import { nodeLanguage } from "./language.ts";
12
- import { Converter } from "./converter.ts";
12
+ import { Converter, type Segment } from "./converter.ts";
13
13
  import { selectorForElement, textrefForSelector } from "./selectorGenerator.ts";
14
14
  import { decodeIdFragment, detachSelfTextref } from "./textrefFragment.ts";
15
15
 
@@ -70,16 +70,18 @@ export function noteref(converter: Converter, el: Element, roles: GndRole[]): vo
70
70
  obj.children = [{ textref: href }];
71
71
  }
72
72
 
73
- converter.placeholder(el, "noteref", obj, candidateID || undefined);
73
+ converter.placeholder(el, "noteref", obj, { candidateID: candidateID || undefined });
74
74
  }
75
75
 
76
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));
77
+ const { obj, words } = linkObject(converter, el, roles, aria, ariaLanguage);
78
+ // A backlink is navigation, not part of the text it follows.
79
+ converter.placeholder(el, roles[0] ?? "link", obj, { words: roles.includes("backlink") ? undefined : words });
78
80
  }
79
81
 
80
82
  // A glossref or biblioref: a link that also embeds its same-document target, like a noteref its footnote.
81
83
  export function reference(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): void {
82
- const obj = linkObject(converter, el, roles, aria, ariaLanguage);
84
+ const { obj, words } = linkObject(converter, el, roles, aria, ariaLanguage);
83
85
  const href = el.getAttribute("href") ?? "";
84
86
  if (href.startsWith("#")) {
85
87
  const target = converter.ids.get(decodeIdFragment(href) ?? href.slice(1));
@@ -92,27 +94,45 @@ export function reference(converter: Converter, el: Element, roles: GndRole[], a
92
94
  });
93
95
  // Still output at its own location, which keeps its ids.
94
96
  removeCopiedIds(children);
95
- if (children.length > 0) obj.children = children;
97
+ if (children.length > 0) obj.children = [...(obj.children ?? []), ...children];
96
98
  }
97
99
  }
98
- converter.placeholder(el, roles[0], obj);
100
+ converter.placeholder(el, roles[0], obj, { words });
99
101
  }
100
102
 
101
- function linkObject(converter: Converter, el: Element, roles: GndRole[], aria: string | null, ariaLanguage: string): ObjBuilder {
103
+ // A link's content is converted like any other: its text, inline semantics and objects become the link's own.
104
+ // `words`: the link's pieces of the plain text of the sentence it sits in.
105
+ function linkObject(
106
+ converter: Converter,
107
+ el: Element,
108
+ roles: GndRole[],
109
+ aria: string | null,
110
+ ariaLanguage: string,
111
+ ): { obj: ObjBuilder; words: Segment[] } {
102
112
  const obj: ObjBuilder = {};
103
113
  if (roles.length > 0) obj.role = roles;
114
+ const words: Segment[] = aria ? [{ kind: "text", text: aria }] : [];
104
115
  if (aria) {
105
116
  obj.text = { plain: aria, ssml: "", language: ariaLanguage };
106
117
  ariaSubstitutedBuilders.add(obj);
107
118
  const selector = selectorForElement(el, converter.selectorRoot, converter.selectorRootAnchor);
108
119
  if (selector) substitutedOwnSelectorBuilders.set(obj, selector);
109
120
  } else {
110
- const { text, language } = normalizedNodeText(el);
111
- if (text) obj.text = { plain: text, ssml: "", language };
121
+ const sub = converter.spawnChild(el, 0);
122
+ sub.words = words;
123
+ sub.convertChildren(el);
124
+ const content = sub.resultBuilders();
125
+ // A lone hoistable result is the content's own text; anything else is objects of its own.
126
+ if (content.length === 1 && content[0].text && isHoistable(content[0])) {
127
+ obj.text = content[0].text;
128
+ if (content[0].children) obj.children = content[0].children;
129
+ } else if (content.length > 0) {
130
+ obj.children = content;
131
+ }
112
132
  }
113
133
  const href = el.getAttribute("href");
114
134
  if (href) obj.textref = href;
115
- return obj;
135
+ return { obj, words };
116
136
  }
117
137
 
118
138
  function removeId(obj: ObjBuilder) {
package/src/emptyPairs.ts CHANGED
@@ -48,7 +48,8 @@ export function bindsToPrevious(text: string, lang: string | undefined): boolean
48
48
  return !(marksFor(lang).opening.has(text[0]) && text.length > 1 && !/[\s\p{P}]/u.test(text[1]));
49
49
  }
50
50
 
51
- // Takes dropped objects out of plain text, with any bracket or quote pair they leave empty.
51
+ /** Takes each `DROPPED` out of plain text in `lang`, with any bracket or quote pair left enclosing only fillers (spaces, commas, other `DROPPED`).
52
+ * Spaces around what's removed collapse to one, dropped at either end of the text and before binding punctuation; no space is added where there was none. */
52
53
  export function removeDropped(plain: string, lang: string | undefined): string {
53
54
  const { pairs, opening, closing } = marksFor(lang);
54
55
  for (let i = plain.indexOf(DROPPED); i !== -1; i = plain.indexOf(DROPPED, i + 1)) {
package/src/index.ts CHANGED
@@ -13,3 +13,4 @@ export {
13
13
  } from "./textrefFragment.ts";
14
14
  export type { DomRangeJSON, DecodedTextref } from "./textrefFragment.ts";
15
15
  export { isAriaSubstituted, substitutedOwnSelector } from "./object.ts";
16
+ export { DROPPED, removeDropped } from "./emptyPairs.ts";