@sub37/webvtt-adapter 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Adapter.js CHANGED
@@ -24,7 +24,7 @@ export default class WebVTTAdapter extends BaseAdapter {
24
24
  }
25
25
  parse(rawContent) {
26
26
  if (!rawContent) {
27
- return BaseAdapter.ParseResult(undefined, [
27
+ return BaseAdapter.ParseResult([], [
28
28
  {
29
29
  error: new MissingContentError(),
30
30
  failedChunk: "",
@@ -78,7 +78,7 @@ export default class WebVTTAdapter extends BaseAdapter {
78
78
  const shouldProcessNonCues = latestBlockPhase & (BlockType.REGION | BlockType.STYLE | BlockType.HEADER);
79
79
  if (isRegion(blockEvaluationResult) && shouldProcessNonCues) {
80
80
  const [blockType, parsedContent] = blockEvaluationResult;
81
- if (parsedContent === null || parsedContent === void 0 ? void 0 : parsedContent.id) {
81
+ if (parsedContent?.id) {
82
82
  latestBlockPhase = blockType;
83
83
  regions[parsedContent.id] = parsedContent;
84
84
  }
@@ -146,7 +146,7 @@ export default class WebVTTAdapter extends BaseAdapter {
146
146
  content: parsedCue.text,
147
147
  renderingModifiers: parsedCue.renderingModifiers,
148
148
  });
149
- if (parsedCue.renderingModifiers.regionIdentifier) {
149
+ if (parsedCue.renderingModifiers?.regionIdentifier) {
150
150
  cue.region = regions[parsedCue.renderingModifiers.regionIdentifier];
151
151
  }
152
152
  if (!latestRootCue) {
@@ -224,7 +224,7 @@ export default class WebVTTAdapter extends BaseAdapter {
224
224
  }
225
225
  catch (err) {
226
226
  const error = err instanceof Error ? err : new Error(JSON.stringify(err));
227
- return BaseAdapter.ParseResult(undefined, [
227
+ return BaseAdapter.ParseResult([], [
228
228
  {
229
229
  error,
230
230
  isCritical: true,
@@ -246,15 +246,21 @@ function evaluateBlock(content, start, end) {
246
246
  }
247
247
  const contentSection = content.substring(start, end);
248
248
  const blockMatch = contentSection.match(BLOCK_MATCH_REGEX);
249
- if (blockMatch === null || blockMatch === void 0 ? void 0 : blockMatch.groups["blocktype"]) {
249
+ if (blockMatch?.groups["blocktype"]) {
250
250
  switch (blockMatch.groups["blocktype"]) {
251
251
  case "REGION": {
252
252
  const payload = Parser.parseRegion(blockMatch.groups["payload"]);
253
+ if (!payload) {
254
+ return [BlockType.IGNORED, undefined];
255
+ }
253
256
  return [BlockType.REGION, payload];
254
257
  }
255
258
  case "STYLE": {
256
259
  try {
257
260
  const payload = Parser.parseStyle(blockMatch.groups["payload"]);
261
+ if (!payload) {
262
+ return [BlockType.IGNORED, undefined];
263
+ }
258
264
  return [BlockType.STYLE, payload];
259
265
  }
260
266
  catch (err) {
@@ -3,7 +3,7 @@ export declare class WebVTTRenderingModifiers implements RenderingModifiers {
3
3
  static fromString(source: string | undefined): WebVTTRenderingModifiers;
4
4
  private position?;
5
5
  private positionAlignment?;
6
- private align?;
6
+ private align;
7
7
  private region?;
8
8
  /**
9
9
  * @TODO support vertical in renderer
@@ -14,5 +14,5 @@ export declare class WebVTTRenderingModifiers implements RenderingModifiers {
14
14
  get width(): number;
15
15
  get leftOffset(): number;
16
16
  get textAlignment(): typeof this.align;
17
- get regionIdentifier(): string;
17
+ get regionIdentifier(): string | undefined;
18
18
  }
@@ -12,25 +12,14 @@ const ALIGNMENT_NUMBER_IDENTIFIERS = {
12
12
  end: 5,
13
13
  };
14
14
  export class WebVTTRenderingModifiers {
15
- constructor() {
16
- this.position = 50;
17
- this.positionAlignment = "center";
18
- this.align = "center";
19
- /**
20
- * @TODO support vertical in renderer
21
- * along with a new property "writing mode"
22
- */
23
- // private vertical?: HORIZONTAL | GROWING_LEFT | GROWING_RIGHT = HORIZONTAL;
24
- this.size = 100;
25
- }
26
15
  static fromString(source) {
27
16
  const modifier = new WebVTTRenderingModifiers();
28
- if (!(source === null || source === void 0 ? void 0 : source.length)) {
17
+ if (!source?.length) {
29
18
  return modifier;
30
19
  }
31
20
  const properties = source.split(" ").reduce((acc, curr) => {
32
21
  const [key, value] = curr.split(":");
33
- return (key && value && Object.assign(Object.assign({}, acc), { [key]: value })) || acc;
22
+ return (key && value && { ...acc, [key]: value }) || acc;
34
23
  }, {});
35
24
  let position = "auto";
36
25
  let positionAlignment = "auto";
@@ -42,7 +31,7 @@ export class WebVTTRenderingModifiers {
42
31
  if (isPositionAlignmentStandard(posAlignment)) {
43
32
  positionAlignment = posAlignment;
44
33
  }
45
- if (pos !== "auto") {
34
+ if (pos && pos !== "auto") {
46
35
  const integerPosition = (pos.endsWith("%") && parseInt(pos)) || NaN;
47
36
  if (!Number.isNaN(integerPosition)) {
48
37
  position = Math.min(Math.max(0, integerPosition), 100);
@@ -134,10 +123,24 @@ export class WebVTTRenderingModifiers {
134
123
  }
135
124
  return modifier;
136
125
  }
126
+ position = 50;
127
+ positionAlignment = "center";
128
+ align = "center";
129
+ region;
130
+ /**
131
+ * @TODO support vertical in renderer
132
+ * along with a new property "writing mode"
133
+ */
134
+ // private vertical?: HORIZONTAL | GROWING_LEFT | GROWING_RIGHT = HORIZONTAL;
135
+ size = 100;
137
136
  get id() {
138
137
  return Math.abs(this.width + this.leftOffset - ALIGNMENT_NUMBER_IDENTIFIERS[this.align]);
139
138
  }
140
139
  get width() {
140
+ if (!this.positionAlignment) {
141
+ return 100;
142
+ }
143
+ const position = this.position || 0;
141
144
  /**
142
145
  * Width, and hence cuebox left offset, calculation is
143
146
  * highly influenced by the alignment.
@@ -166,7 +169,7 @@ export class WebVTTRenderingModifiers {
166
169
  * | | |
167
170
  * |----|----|----|----|----|----|----|----|----|----|
168
171
  */
169
- return Math.min(this.size, 100 - this.position);
172
+ return Math.min(this.size, 100 - position);
170
173
  }
171
174
  case "center": {
172
175
  /**
@@ -189,8 +192,8 @@ export class WebVTTRenderingModifiers {
189
192
  * | | |
190
193
  * |----|----|----|----|----|----|----|----|----|----|
191
194
  */
192
- if (this.position <= 50) {
193
- return Math.min(this.size, this.position * 2);
195
+ if (position <= 50) {
196
+ return Math.min(this.size, position * 2);
194
197
  }
195
198
  /**
196
199
  * @example scheme, point > 50%
@@ -205,7 +208,7 @@ export class WebVTTRenderingModifiers {
205
208
  * | | |
206
209
  * |----|----|----|----|----|----|----|----|----|----|
207
210
  */
208
- return Math.min(this.size, (100 - this.position) * 2);
211
+ return Math.min(this.size, (100 - position) * 2);
209
212
  }
210
213
  case "line-right": {
211
214
  /**
@@ -225,11 +228,12 @@ export class WebVTTRenderingModifiers {
225
228
  * | | |
226
229
  * |----|----|----|----|----|----|----|----|----|----|
227
230
  */
228
- return Math.min(this.size, this.position);
231
+ return Math.min(this.size, position);
229
232
  }
230
233
  }
231
234
  }
232
235
  get leftOffset() {
236
+ const position = this.position || 0;
233
237
  /**
234
238
  * Width, and hence cuebox left offset, calculation is
235
239
  * highly influenced by the alignment.
@@ -247,7 +251,7 @@ export class WebVTTRenderingModifiers {
247
251
  *
248
252
  * For schemas, refer to width on "line-left" case.
249
253
  */
250
- return this.position;
254
+ return position;
251
255
  }
252
256
  case "center": {
253
257
  /**
@@ -261,10 +265,10 @@ export class WebVTTRenderingModifiers {
261
265
  *
262
266
  * For schemas, refer to width on "center" case.
263
267
  */
264
- if (this.position <= 50) {
268
+ if (position <= 50) {
265
269
  return 0;
266
270
  }
267
- const width = (100 - this.position) * 2;
271
+ const width = (100 - position) * 2;
268
272
  return 100 - width;
269
273
  }
270
274
  case "line-right": {
@@ -278,6 +282,7 @@ export class WebVTTRenderingModifiers {
278
282
  return 0;
279
283
  }
280
284
  }
285
+ return 0;
281
286
  }
282
287
  get textAlignment() {
283
288
  return this.align;
@@ -292,9 +297,12 @@ function isPositionAlignmentStandard(alignment) {
292
297
  function isTextAlignmentStandard(alignment) {
293
298
  return ["start", "left", "center", "right", "end"].includes(alignment);
294
299
  }
300
+ //
301
+ // type VerticalWritingMode = HORIZONTAL | GROWING_LEFT | GROWING_RIGHT;
302
+ //
295
303
  // function isVerticalStandard(
296
304
  // vertical: string,
297
- // ): vertical is HORIZONTAL | GROWING_LEFT | GROWING_RIGHT {
298
- // return [HORIZONTAL, GROWING_LEFT, GROWING_RIGHT].includes(vertical);
305
+ // ): vertical is VerticalWritingMode {
306
+ // return [HORIZONTAL, GROWING_LEFT, GROWING_RIGHT].includes(vertical as VerticalWritingMode);
299
307
  // }
300
308
  //# sourceMappingURL=RenderingModifiers.js.map
@@ -3,7 +3,7 @@ export default class Node {
3
3
  /** Zero-based position of cue (or timestamp section) content */
4
4
  index: number;
5
5
  token: Token;
6
- parent: Node;
6
+ parent: Node | null;
7
7
  constructor(
8
8
  /** Zero-based position of cue (or timestamp section) content */
9
9
  index: number, token: Token);
@@ -1,10 +1,12 @@
1
1
  export default class Node {
2
+ index;
3
+ token;
4
+ parent = null;
2
5
  constructor(
3
6
  /** Zero-based position of cue (or timestamp section) content */
4
7
  index, token) {
5
8
  this.index = index;
6
9
  this.token = token;
7
- this.parent = null;
8
10
  }
9
11
  }
10
12
  //# sourceMappingURL=Node.js.map
@@ -5,7 +5,7 @@ import type Node from "./Node";
5
5
  */
6
6
  export default class NodeQueue {
7
7
  private root;
8
- get current(): Node;
8
+ get current(): Node | null;
9
9
  get length(): number;
10
10
  push(node: Node): void;
11
11
  pop(): Node | undefined;
@@ -3,9 +3,7 @@
3
3
  * so we can easily pop out and not drill.
4
4
  */
5
5
  export default class NodeQueue {
6
- constructor() {
7
- this.root = null;
8
- }
6
+ root = null;
9
7
  get current() {
10
8
  return this.root;
11
9
  }
@@ -37,10 +37,8 @@ export function createTagEntity(currentCue, tagStart) {
37
37
  * the beginning of the **current cue** and the length should be set to
38
38
  * current cue content.
39
39
  */
40
- var _a;
41
40
  const tagOpenedInCurrentCue = currentCue.text.length - tagStart.index > 0;
42
- const attributes = new Map((_a = tagStart.token.annotations) === null || _a === void 0 ? void 0 : _a.map((annotation) => {
43
- var _a;
41
+ const attributes = new Map(tagStart.token.annotations?.map((annotation) => {
44
42
  if (tagStart.token.content === "lang") {
45
43
  return ["lang", annotation];
46
44
  }
@@ -48,7 +46,7 @@ export function createTagEntity(currentCue, tagStart) {
48
46
  return ["voice", annotation];
49
47
  }
50
48
  const attribute = annotation.split("=");
51
- return [attribute[0], (_a = attribute[1]) === null || _a === void 0 ? void 0 : _a.replace(/["']/g, "")];
49
+ return [attribute[0], attribute[1]?.replace(/["']/g, "")];
52
50
  }));
53
51
  return new Entities.Tag({
54
52
  tagType: EntitiesTokenMap[tagStart.token.content],
@@ -1,10 +1,10 @@
1
1
  const TIME_REGEX = /(?<hours>(\d{2})?):?(?<minutes>(\d{2})):(?<seconds>(\d{2}))(?:\.(?<milliseconds>(\d{0,3})))?/;
2
2
  export function parseMs(timestring) {
3
3
  const timeMatch = timestring.match(TIME_REGEX);
4
- if (!timeMatch) {
4
+ if (!timeMatch?.groups) {
5
5
  throw new Error("Time format is not valid. Ignoring cue.");
6
6
  }
7
- const { groups: { hours, minutes, seconds, milliseconds }, } = timeMatch;
7
+ const { groups: { hours = "", minutes = "", seconds = "", milliseconds = "" }, } = timeMatch;
8
8
  const hoursInSeconds = zeroFallback(parseInt(hours)) * 60 * 60;
9
9
  const minutesInSeconds = zeroFallback(parseInt(minutes)) * 60;
10
10
  const parsedSeconds = zeroFallback(parseInt(seconds));
@@ -14,7 +14,7 @@ export interface CueParsedData {
14
14
  regionName?: string;
15
15
  tags: Entities.Tag[];
16
16
  text: string;
17
- renderingModifiers: RenderingModifiers;
17
+ renderingModifiers?: RenderingModifiers | undefined;
18
18
  /**
19
19
  * Grouping identifier allows us to skip
20
20
  * uniqueness checks for ids for cues nodes
@@ -2,4 +2,4 @@ import type { Region } from "@sub37/server";
2
2
  /**
3
3
  * @param rawRegionData
4
4
  */
5
- export declare function parseRegion(rawRegionData: string): Region;
5
+ export declare function parseRegion(rawRegionData: string | undefined): Region | undefined;
@@ -2,6 +2,9 @@
2
2
  * @param rawRegionData
3
3
  */
4
4
  export function parseRegion(rawRegionData) {
5
+ if (!rawRegionData) {
6
+ return undefined;
7
+ }
5
8
  const region = new WebVTTRegion();
6
9
  const attributes = rawRegionData.split(/[\n\t\s]+/);
7
10
  for (let i = 0; i < attributes.length; i++) {
@@ -37,11 +40,14 @@ export function parseRegion(rawRegionData) {
37
40
  region[key] = value;
38
41
  break;
39
42
  }
40
- case "lines":
41
- case "width": {
43
+ case "lines": {
42
44
  region[key] = parseInt(value);
43
45
  break;
44
46
  }
47
+ case "width": {
48
+ region[key] = `${parseInt(value)}%`;
49
+ break;
50
+ }
45
51
  default:
46
52
  break;
47
53
  }
@@ -66,13 +72,23 @@ export function parseRegion(rawRegionData) {
66
72
  */
67
73
  const VH_LINE_HEIGHT = 5.33;
68
74
  class WebVTTRegion {
69
- constructor() {
70
- /**
71
- * Region width expressed in percentage
72
- */
73
- this.width = 100;
74
- this.lines = 3;
75
- }
75
+ id = "";
76
+ /**
77
+ * Region width expressed in percentage
78
+ */
79
+ width = "100%";
80
+ lines = 3;
81
+ scroll;
82
+ /**
83
+ * Position of region based on video region.
84
+ * Couple of numbers expressed in percentage
85
+ */
86
+ viewportanchor;
87
+ /**
88
+ * Position of region based on viewportAnchor
89
+ * Couple of numbers expressed in percentage
90
+ */
91
+ regionanchor;
76
92
  getOrigin() {
77
93
  const height = VH_LINE_HEIGHT * this.lines;
78
94
  const [regionAnchorWidth = 0, regionAnchorHeight = 0] = this.regionanchor || [];
@@ -81,7 +97,7 @@ class WebVTTRegion {
81
97
  * It is still not very clear to me why we base on current width and height, but
82
98
  * a thing that I know is that we need low numbers.
83
99
  */
84
- const leftOffset = (regionAnchorWidth * this.width) / 100;
100
+ const leftOffset = (regionAnchorWidth * parseFloat(this.width)) / 100;
85
101
  const topOffset = (regionAnchorHeight * height) / 100;
86
102
  const originX = `${viewportAnchorWidth - leftOffset}%`;
87
103
  const originY = `${viewportAnchorHeight - topOffset}%`;
@@ -18,5 +18,5 @@ type SelectorTarget = {
18
18
  export type Style = SelectorTarget & {
19
19
  styleString: string;
20
20
  };
21
- export declare function parseStyle(rawStyleData: string): Style | undefined;
21
+ export declare function parseStyle(rawStyleData: string | undefined): Style | undefined;
22
22
  export {};
@@ -63,7 +63,10 @@ export function parseStyle(rawStyleData) {
63
63
  if (!parsedSelector) {
64
64
  return undefined;
65
65
  }
66
- return Object.assign(Object.assign({}, parsedSelector), { styleString });
66
+ return {
67
+ ...parsedSelector,
68
+ styleString,
69
+ };
67
70
  }
68
71
  function getParsedSelector(selector, classesChain) {
69
72
  if (!selector && !classesChain.length) {
@@ -78,14 +81,19 @@ function getParsedSelector(selector, classesChain) {
78
81
  };
79
82
  }
80
83
  const selectorComponents = getSelectorComponents(selector);
81
- if (!selectorComponents.tagName && !selectorComponents.attributes.size && !classesChain.length) {
84
+ if (!selectorComponents ||
85
+ (!selectorComponents.tagName && !selectorComponents.attributes.size && !classesChain.length)) {
82
86
  /** Invalid */
83
87
  return undefined;
84
88
  }
85
- return Object.assign({ type: 2 /* StyleDomain.TAG */, classes: (classesChain.length && classesChain.split(".")) || [] }, selectorComponents);
89
+ return {
90
+ type: 2 /* StyleDomain.TAG */,
91
+ classes: (classesChain.length && classesChain.split(".")) || [],
92
+ ...selectorComponents,
93
+ };
86
94
  }
87
95
  function getSelectorComponents(rawSelector) {
88
- let selector = undefined;
96
+ let selector = "";
89
97
  const attributes = [];
90
98
  /**
91
99
  * This is too recent and will likely require a polyfill from
package/lib/Token.js CHANGED
@@ -6,6 +6,12 @@ export var TokenType;
6
6
  TokenType[TokenType["TIMESTAMP"] = 3] = "TIMESTAMP";
7
7
  })(TokenType || (TokenType = {}));
8
8
  export class Token {
9
+ annotations = [];
10
+ classes = [];
11
+ offset = 0;
12
+ length = 0;
13
+ type;
14
+ content;
9
15
  constructor(type, content) {
10
16
  this.type = type;
11
17
  this.content = content;
@@ -25,8 +25,8 @@ export declare class Tokenizer {
25
25
  /** Next token index in the content */
26
26
  private startPoint;
27
27
  constructor(rawContent: string);
28
- static isWhitespace(character: string): boolean;
29
- static isNewLine(character: string): boolean;
28
+ static isWhitespace(character: string): character is " " | "\t" | "\f";
29
+ static isNewLine(character: string): character is "\n";
30
30
  /**
31
31
  * Attempts to convert a set of character (a supposed HTML Entity)
32
32
  * to the correct character, by accumulating its char code;
package/lib/Tokenizer.js CHANGED
@@ -32,12 +32,13 @@ var TokenizerState;
32
32
  })(TokenizerState || (TokenizerState = {}));
33
33
  const SHARED_DOM_PARSER = new DOMParser();
34
34
  export class Tokenizer {
35
+ rawContent;
36
+ /** Character index in the content */
37
+ cursor = 0;
38
+ /** Next token index in the content */
39
+ startPoint = 0;
35
40
  constructor(rawContent) {
36
41
  this.rawContent = rawContent;
37
- /** Character index in the content */
38
- this.cursor = 0;
39
- /** Next token index in the content */
40
- this.startPoint = 0;
41
42
  }
42
43
  static isWhitespace(character) {
43
44
  return character == "\x20" || character == "\x09" || character == "\x0C";
@@ -55,7 +56,7 @@ export class Tokenizer {
55
56
  * @returns
56
57
  */
57
58
  static parseHTMLEntity(source, currentCursor, additionalAllowedCharacters = []) {
58
- if (!(source === null || source === void 0 ? void 0 : source.length)) {
59
+ if (!source?.length) {
59
60
  return ["", currentCursor];
60
61
  }
61
62
  let cursor = currentCursor;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@sub37/webvtt-adapter",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "A subtitles adapter for WebVTT subtitles",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
7
7
  "peerDependencies": {
8
- "@sub37/server": "^1.0.0"
8
+ "@sub37/server": "1.1.2"
9
9
  },
10
10
  "scripts": {
11
- "build": "rm -rf lib && pnpm tsc -p tsconfig.build.json",
11
+ "build": "rm -rf lib && pnpm tsc --build --force",
12
12
  "test": "pnpm build && pnpm --prefix ../.. run test",
13
13
  "prepublishOnly": "pnpm build"
14
14
  },
@@ -25,5 +25,5 @@
25
25
  "files": [
26
26
  "lib/**/*.+(js|d.ts)!(*.map)"
27
27
  ],
28
- "gitHead": "45d8a1862acdb71f80c60349eb877dde5af36984"
28
+ "gitHead": "8d290c6b3c3bd11fd97e1574b28e94e14bde72b6"
29
29
  }