@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
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 {};
@@ -1,3 +1,5 @@
1
1
  export declare const DROPPED = "\0";
2
2
  export declare function bindsToPrevious(text: string, lang: string | undefined): boolean;
3
+ /** Takes each `DROPPED` out of plain text in `lang`, with any bracket or quote pair left enclosing only fillers (spaces, commas, other `DROPPED`).
4
+ * 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. */
3
5
  export declare function removeDropped(plain: string, lang: string | undefined): string;
@@ -6,3 +6,4 @@ export type { GndGenerationOptions, TextrefOptions } from "./options.ts";
6
6
  export { encodeCssSelectorFragment, decodeCssSelectorFragment, encodeDomRangeFragment, decodeDomRangeFragment, decodeTextref, combineDomRangeTextrefs, } from "./textrefFragment.ts";
7
7
  export type { DomRangeJSON, DecodedTextref } from "./textrefFragment.ts";
8
8
  export { isAriaSubstituted, substitutedOwnSelector } from "./object.ts";
9
+ export { DROPPED, removeDropped } from "./emptyPairs.ts";
@@ -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[];