@readium/guided-navigation 1.2.3 → 1.2.4

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 (148) 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/object.js +1 -1
  5. package/dist/roles.js +1 -1
  6. package/fixtures/ROLES_COVERAGE.md +12 -10
  7. package/fixtures/abbreviation-nested-term-punctuation/gnd.json +6 -6
  8. package/fixtures/abbreviation-nested-term-punctuation/input.html +1 -1
  9. package/fixtures/abbreviation-nested-term-punctuation/meta.json +1 -1
  10. package/fixtures/accessible-name-labelledby-aria-hidden/gnd.json +1 -1
  11. package/fixtures/accessible-name-labelledby-hidden-attribute/gnd.json +1 -1
  12. package/fixtures/biblioref-target-descendant-ids/gnd.json +3 -3
  13. package/fixtures/biblioref-target-in-bibliography-role-aria/gnd.json +1 -1
  14. package/fixtures/biblioref-target-mixed-content/gnd.json +3 -3
  15. package/fixtures/description-roleless-lone-child/gnd.json +24 -0
  16. package/fixtures/description-roleless-lone-child/input.html +1 -0
  17. package/fixtures/description-roleless-lone-child/meta.json +11 -0
  18. package/fixtures/description-roleless-wrapper/gnd.json +25 -0
  19. package/fixtures/description-roleless-wrapper/input.html +1 -0
  20. package/fixtures/description-roleless-wrapper/meta.json +10 -0
  21. package/fixtures/glossary-role-aria/gnd.json +23 -31
  22. package/fixtures/glossref-role-aria-mid-sentence/gnd.json +1 -1
  23. package/fixtures/glossref-sole-content/gnd.json +12 -20
  24. package/fixtures/glossref-target-in-glossary-role-aria/gnd.json +13 -21
  25. package/fixtures/glossref-target-percent-encoded/gnd.json +1 -1
  26. package/fixtures/language-alt-inherited/gnd.json +25 -0
  27. package/fixtures/language-alt-inherited/input.html +1 -0
  28. package/fixtures/language-alt-inherited/meta.json +11 -0
  29. package/fixtures/language-alt-own-lang/gnd.json +25 -0
  30. package/fixtures/language-alt-own-lang/input.html +1 -0
  31. package/fixtures/language-alt-own-lang/meta.json +11 -0
  32. package/fixtures/language-block-empty-lang/gnd.json +8 -0
  33. package/fixtures/language-block-empty-lang/input.html +1 -0
  34. package/fixtures/language-block-empty-lang/meta.json +10 -0
  35. package/fixtures/language-describedby-shared/gnd.json +49 -0
  36. package/fixtures/language-describedby-shared/input.html +1 -0
  37. package/fixtures/language-describedby-shared/meta.json +12 -0
  38. package/fixtures/language-empty-lang-stops-inheritance/gnd.json +24 -0
  39. package/fixtures/language-empty-lang-stops-inheritance/input.html +1 -0
  40. package/fixtures/language-empty-lang-stops-inheritance/meta.json +11 -0
  41. package/fixtures/language-figcaption/gnd.json +25 -0
  42. package/fixtures/language-figcaption/input.html +1 -0
  43. package/fixtures/language-figcaption/meta.json +11 -0
  44. package/fixtures/language-figcaption-credit/gnd.json +41 -0
  45. package/fixtures/language-figcaption-credit/input.html +1 -0
  46. package/fixtures/language-figcaption-credit/meta.json +12 -0
  47. package/fixtures/language-figcaption-switches/gnd.json +24 -0
  48. package/fixtures/language-figcaption-switches/input.html +1 -0
  49. package/fixtures/language-figcaption-switches/meta.json +11 -0
  50. package/fixtures/language-figcaption-switches-throughout/gnd.json +25 -0
  51. package/fixtures/language-figcaption-switches-throughout/input.html +1 -0
  52. package/fixtures/language-figcaption-switches-throughout/meta.json +11 -0
  53. package/fixtures/language-inline-case-insensitive/gnd.json +9 -0
  54. package/fixtures/language-inline-case-insensitive/input.html +1 -0
  55. package/fixtures/language-inline-case-insensitive/meta.json +10 -0
  56. package/fixtures/language-inline-case-insensitive-ssml/gnd.json +9 -0
  57. package/fixtures/language-inline-case-insensitive-ssml/input.html +1 -0
  58. package/fixtures/language-inline-case-insensitive-ssml/meta.json +10 -0
  59. package/fixtures/language-inline-empty-lang/gnd.json +9 -0
  60. package/fixtures/language-inline-empty-lang/input.html +1 -0
  61. package/fixtures/language-inline-empty-lang/meta.json +10 -0
  62. package/fixtures/language-inline-empty-lang-formatted/gnd.json +8 -0
  63. package/fixtures/language-inline-empty-lang-formatted/input.html +1 -0
  64. package/fixtures/language-inline-empty-lang-formatted/meta.json +10 -0
  65. package/fixtures/language-inline-empty-lang-only/gnd.json +8 -0
  66. package/fixtures/language-inline-empty-lang-only/input.html +1 -0
  67. package/fixtures/language-inline-empty-lang-only/meta.json +10 -0
  68. package/fixtures/language-inline-empty-lang-undeclared/gnd.json +9 -0
  69. package/fixtures/language-inline-empty-lang-undeclared/input.html +1 -0
  70. package/fixtures/language-inline-empty-lang-undeclared/meta.json +10 -0
  71. package/fixtures/language-labelledby-target/gnd.json +26 -0
  72. package/fixtures/language-labelledby-target/input.html +1 -0
  73. package/fixtures/language-labelledby-target/meta.json +11 -0
  74. package/fixtures/language-labelledby-target-aria-label/gnd.json +32 -0
  75. package/fixtures/language-labelledby-target-aria-label/input.html +1 -0
  76. package/fixtures/language-labelledby-target-aria-label/meta.json +11 -0
  77. package/fixtures/language-labelledby-target-switches/gnd.json +25 -0
  78. package/fixtures/language-labelledby-target-switches/input.html +1 -0
  79. package/fixtures/language-labelledby-target-switches/meta.json +11 -0
  80. package/fixtures/language-labelledby-targets-case/gnd.json +36 -0
  81. package/fixtures/language-labelledby-targets-case/input.html +1 -0
  82. package/fixtures/language-labelledby-targets-case/meta.json +11 -0
  83. package/fixtures/language-labelledby-targets-differ/gnd.json +35 -0
  84. package/fixtures/language-labelledby-targets-differ/input.html +1 -0
  85. package/fixtures/language-labelledby-targets-differ/meta.json +11 -0
  86. package/fixtures/language-link-switches/gnd.json +20 -0
  87. package/fixtures/language-link-switches/input.html +1 -0
  88. package/fixtures/language-link-switches/meta.json +11 -0
  89. package/fixtures/language-pagebreak-label/gnd.json +22 -0
  90. package/fixtures/language-pagebreak-label/input.html +1 -0
  91. package/fixtures/language-pagebreak-label/meta.json +11 -0
  92. package/fixtures/language-undeclared-description/gnd.json +23 -0
  93. package/fixtures/language-undeclared-description/input.html +1 -0
  94. package/fixtures/language-undeclared-description/meta.json +11 -0
  95. package/fixtures/language-xml-lang-empty-html-inherits/gnd.json +25 -0
  96. package/fixtures/language-xml-lang-empty-html-inherits/input.html +1 -0
  97. package/fixtures/language-xml-lang-empty-html-inherits/meta.json +11 -0
  98. package/fixtures/language-xml-lang-empty-html-lang-applies/gnd.json +25 -0
  99. package/fixtures/language-xml-lang-empty-html-lang-applies/input.html +1 -0
  100. package/fixtures/language-xml-lang-empty-html-lang-applies/meta.json +11 -0
  101. package/fixtures/language-xml-lang-empty-xhtml/gnd.json +24 -0
  102. package/fixtures/language-xml-lang-empty-xhtml/input.xhtml +2 -0
  103. package/fixtures/language-xml-lang-empty-xhtml/meta.json +11 -0
  104. package/fixtures/language-xml-lang-over-lang/gnd.json +25 -0
  105. package/fixtures/language-xml-lang-over-lang/input.html +1 -0
  106. package/fixtures/language-xml-lang-over-lang/meta.json +11 -0
  107. package/fixtures/link-containing-image/gnd.json +35 -0
  108. package/fixtures/link-containing-image/input.html +1 -0
  109. package/fixtures/link-containing-image/meta.json +12 -0
  110. package/fixtures/link-containing-pagebreak/gnd.json +33 -0
  111. package/fixtures/link-containing-pagebreak/input.html +1 -0
  112. package/fixtures/link-containing-pagebreak/meta.json +12 -0
  113. package/fixtures/link-content-conversion/gnd.json +31 -0
  114. package/fixtures/link-content-conversion/input.html +1 -0
  115. package/fixtures/link-content-conversion/meta.json +12 -0
  116. package/fixtures/link-content-identity/gnd.json +21 -0
  117. package/fixtures/link-content-identity/input.html +1 -0
  118. package/fixtures/link-content-identity/meta.json +10 -0
  119. package/fixtures/link-content-words/gnd.json +46 -0
  120. package/fixtures/link-content-words/input.html +1 -0
  121. package/fixtures/link-content-words/meta.json +10 -0
  122. package/fixtures/manifest.json +401 -1
  123. package/fixtures/math-inline/gnd.json +22 -0
  124. package/fixtures/math-inline/input.html +1 -0
  125. package/fixtures/math-inline/meta.json +11 -0
  126. package/fixtures/nested-blockquote/gnd.json +22 -0
  127. package/fixtures/nested-blockquote/input.html +5 -0
  128. package/fixtures/nested-blockquote/meta.json +11 -0
  129. package/fixtures/nested-section/gnd.json +22 -0
  130. package/fixtures/nested-section/input.html +5 -0
  131. package/fixtures/nested-section/meta.json +11 -0
  132. package/fixtures/redundant-role-body-chapter/gnd.json +23 -0
  133. package/fixtures/redundant-role-body-chapter/input.xhtml +10 -0
  134. package/fixtures/redundant-role-body-chapter/meta.json +13 -0
  135. package/fixtures/redundant-role-nested-chapter/gnd.json +30 -0
  136. package/fixtures/redundant-role-nested-chapter/input.xhtml +12 -0
  137. package/fixtures/redundant-role-nested-chapter/meta.json +13 -0
  138. package/fixtures/term-span-in-dt/gnd.json +24 -0
  139. package/fixtures/term-span-in-dt/input.html +4 -0
  140. package/fixtures/term-span-in-dt/meta.json +12 -0
  141. package/package.json +1 -1
  142. package/src/converter.ts +72 -15
  143. package/src/elementHandlers.ts +31 -11
  144. package/src/object.ts +7 -10
  145. package/src/roles.ts +49 -0
  146. package/types/src/converter.d.ts +21 -2
  147. package/types/src/object.d.ts +1 -0
  148. package/types/src/roles.d.ts +1 -0
@@ -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,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.2.4",
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/object.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { GuidedNavigationDescription, GuidedNavigationObject, GuidedNavigationText } from "@readium/shared";
2
2
  import type { GndRole } from "./types.ts";
3
+ import { nonSelfNestingRoles } from "./roles.ts";
3
4
  import { type TextBuilder, textIsEmpty, finalizeText } from "./text.ts";
4
5
  import { detachSelfTextref } from "./textrefFragment.ts";
5
6
 
@@ -81,7 +82,7 @@ function isChildrenOnly(o: ObjBuilder): boolean {
81
82
  // own ref (e.g. an <a href>'s textref) is never hoisted — refs identify a
82
83
  // distinct target and merging them into the parent would silently discard
83
84
  // whichever of the two textrefs isn't kept.
84
- function isHoistable(o: ObjBuilder): boolean {
85
+ export function isHoistable(o: ObjBuilder): boolean {
85
86
  return (
86
87
  !(o.role && o.role.length > 0) &&
87
88
  !o.id &&
@@ -116,17 +117,13 @@ export class NavObject {
116
117
  }
117
118
  if (finalChildren.length > 0) result.children = finalChildren;
118
119
 
119
- // A wrapper whose own role is entirely redundant with its sole child's
120
- // role (e.g. <dt><dfn>term</dfn></dt> naming one term twice) sheds that
121
- // role — the child already carries it. Excludes "presentation": unlike
122
- // every other role, it cascades from an ancestor onto real structural
123
- // descendants (HTML-AAM's presentational-table rule) rather than
124
- // marking the same entity twice, so identical nesting there is genuine.
120
+ // A sole child repeating its wrapper's non-self-nesting role (<dt><dfn>) sheds it, leaving it to the wrapper.
121
+ // "presentation" cascades onto genuinely distinct descendants, so it never marks one entity twice.
125
122
  if (result.role?.length && result.children?.length === 1 && !result.role.includes("presentation")) {
126
123
  const child = result.children[0];
127
- if (child.role?.length && result.role.every((role) => child.role!.includes(role))) {
128
- delete result.role;
129
- }
124
+ const wrapperRoles = result.role;
125
+ child.role = child.role?.filter((role) => !(nonSelfNestingRoles.has(role) && wrapperRoles.includes(role)));
126
+ if (!child.role?.length) delete child.role;
130
127
  }
131
128
 
132
129
  // Hoist a lone child with no role/id/ref into the object itself: its text
package/src/roles.ts CHANGED
@@ -182,6 +182,55 @@ const simpleElementTypeRoles: Record<string, GndRole> = {
182
182
  svg: "image",
183
183
  };
184
184
 
185
+ // Roles that never contain themselves: on both a wrapper and its sole child, they mark one entity.
186
+ export const nonSelfNestingRoles: ReadonlySet<GndRole> = new Set([
187
+ "abbreviation",
188
+ "abstract",
189
+ "acknowledgments",
190
+ "afterword",
191
+ "bibliography",
192
+ "body",
193
+ "caption",
194
+ "cell",
195
+ "chapter",
196
+ "colophon",
197
+ "columnheader",
198
+ "conclusion",
199
+ "cover",
200
+ "credit",
201
+ "credits",
202
+ "dedication",
203
+ "definition",
204
+ "endnotes",
205
+ "epigraph",
206
+ "epilogue",
207
+ "errata",
208
+ "footnote",
209
+ "foreword",
210
+ "glossary",
211
+ ...headingRoles,
212
+ "index",
213
+ "introduction",
214
+ "landmarks",
215
+ "listItem",
216
+ "loa",
217
+ "loi",
218
+ "lot",
219
+ "lov",
220
+ "main",
221
+ "pagelist",
222
+ "paragraph",
223
+ "preface",
224
+ "prologue",
225
+ "pullquote",
226
+ "row",
227
+ "rowheader",
228
+ "subtitle",
229
+ "summary",
230
+ "term",
231
+ "toc",
232
+ ]);
233
+
185
234
  // ARIA presentational inheritance: only a presentational <table>'s own
186
235
  // required owned elements without an explicit role inherit it.
187
236
  const tableOwnedTags = new Set(["tr", "td", "th"]);
@@ -1,9 +1,20 @@
1
1
  import type { GuidedNavigationObject } from "@readium/shared";
2
2
  import type { GndRole } from "./types.ts";
3
- import { type ObjBuilder } from "./object.ts";
3
+ import { type SSMLContext } from "./text.ts";
4
+ import { type ObjBuilder, NavObject } from "./object.ts";
4
5
  import { type GndMediaType } from "./dom.ts";
5
6
  import { type GndGenerationOptions } from "./options.ts";
6
7
  import { IdAllocator } from "./idAllocator.ts";
8
+ type SegmentKind = "text" | "break" | "placeholder";
9
+ export interface Segment {
10
+ kind: SegmentKind;
11
+ text?: string;
12
+ ctx?: SSMLContext;
13
+ tag?: string;
14
+ child?: NavObject;
15
+ candidateID?: string;
16
+ words?: Segment[];
17
+ }
7
18
  /** Walks a DOM subtree, building the Guided Navigation object tree. */
8
19
  export declare class Converter {
9
20
  xmlParsed: boolean;
@@ -35,6 +46,7 @@ export declare class Converter {
35
46
  private flowLastOffset;
36
47
  private lastFlowRange;
37
48
  private lastFlowText;
49
+ words: Segment[] | null;
38
50
  constructor(xmlParsed: boolean);
39
51
  spawnChild(allowNode: Element, depthDelta?: number): Converter;
40
52
  prescan(root: Element): void;
@@ -53,11 +65,17 @@ export declare class Converter {
53
65
  private textContext;
54
66
  private static containsObject;
55
67
  private static isPlaceholder;
68
+ private static abbreviationOver;
69
+ private static punctuationOwner;
70
+ private static ownPunctuation;
56
71
  private static isSoleContent;
57
72
  private updateFlowSpace;
58
73
  private closeSegment;
59
74
  private resetFlow;
60
- placeholder(el: Element, tag: string, object: ObjBuilder, candidateID?: string): void;
75
+ placeholder(el: Element, tag: string, object: ObjBuilder, { candidateID, words }?: {
76
+ candidateID?: string;
77
+ words?: Segment[];
78
+ }): void;
61
79
  private flushText;
62
80
  }
63
81
  /**
@@ -79,3 +97,4 @@ export declare class Converter {
79
97
  * same document.
80
98
  */
81
99
  export declare function parseMarkup(input: string | Element, mediaType?: GndMediaType, options?: GndGenerationOptions): GuidedNavigationObject[];
100
+ export {};
@@ -19,6 +19,7 @@ export declare const substitutedOwnSelectorBuilders: WeakMap<ObjBuilder, string>
19
19
  export declare function isAriaSubstituted(node: GuidedNavigationObject): boolean;
20
20
  export declare function substitutedOwnSelector(node: GuidedNavigationObject): string | undefined;
21
21
  export declare function isEmptyObj(o: ObjBuilder): boolean;
22
+ export declare function isHoistable(o: ObjBuilder): boolean;
22
23
  /** A node being built up during the tree walk, before its final shape is known. */
23
24
  export declare class NavObject {
24
25
  el?: Element;
@@ -1,3 +1,4 @@
1
1
  import type { GndRole } from "./types.ts";
2
+ export declare const nonSelfNestingRoles: ReadonlySet<GndRole>;
2
3
  export declare function hasExplicitRole(el: Element): boolean;
3
4
  export declare function extractNodeRoles(el: Element): GndRole[];