@sub37/webvtt-adapter 1.0.4 → 1.1.1

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
@@ -1,4 +1,5 @@
1
1
  import { BaseAdapter, CueNode, Entities } from "@sub37/server";
2
+ import { EmptyStyleDeclarationError } from "./EmptyStyleDeclarationError.js";
2
3
  import { InvalidFormatError } from "./InvalidFormatError.js";
3
4
  import { MissingContentError } from "./MissingContentError.js";
4
5
  import * as Parser from "./Parser/index.js";
@@ -31,6 +32,7 @@ export default class WebVTTAdapter extends BaseAdapter {
31
32
  },
32
33
  ]);
33
34
  }
35
+ const cueIdsList = new Set();
34
36
  const cues = [];
35
37
  const content = String(rawContent).replace(/\r?\n/g, "\n");
36
38
  const block = {
@@ -84,7 +86,16 @@ export default class WebVTTAdapter extends BaseAdapter {
84
86
  if (isStyle(blockEvaluationResult) && shouldProcessNonCues) {
85
87
  const [blockType, parsedContent] = blockEvaluationResult;
86
88
  latestBlockPhase = blockType;
87
- styles.push(parsedContent);
89
+ if (!parsedContent) {
90
+ failures.push({
91
+ error: new EmptyStyleDeclarationError(),
92
+ failedChunk: content.substring(block.start, block.cursor),
93
+ isCritical: false,
94
+ });
95
+ }
96
+ else {
97
+ styles.push(parsedContent);
98
+ }
88
99
  }
89
100
  if (isCue(blockEvaluationResult)) {
90
101
  const [blockType, parsedContent] = blockEvaluationResult;
@@ -98,10 +109,38 @@ export default class WebVTTAdapter extends BaseAdapter {
98
109
  let latestRootCue = undefined;
99
110
  for (const parsedCue of parsedContent) {
100
111
  if (parsedCue.startTime >= parsedCue.endTime) {
112
+ failures.push({
113
+ error: new Error(`A cue cannot start (${parsedCue.startTime}) after its end time (${parsedCue.endTime})`),
114
+ failedChunk: content.substring(block.start, block.cursor),
115
+ isCritical: false,
116
+ });
101
117
  continue;
102
118
  }
119
+ if (parsedCue.id) {
120
+ /**
121
+ * "A WebVTT cue identifier must be unique amongst
122
+ * all the WebVTT cue identifiers of all WebVTT
123
+ * cues of a WebVTT file."
124
+ *
125
+ * @see https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
126
+ */
127
+ if (!parsedCue.groupingIdentifier && cueIdsList.has(parsedCue.id)) {
128
+ failures.push({
129
+ error: new Error(`A WebVTT cue identifier must be unique amongst all the cue identifiers of a WebVTT file. Double id found: '${parsedCue.id}'`),
130
+ failedChunk: content.substring(block.start, block.cursor),
131
+ isCritical: false,
132
+ });
133
+ continue;
134
+ }
135
+ /**
136
+ * ... however, when we generate a custom identifier
137
+ * for the cue, we re-use the same for the timestamps
138
+ * because they must appear on the same line.
139
+ */
140
+ cueIdsList.add(parsedCue.id);
141
+ }
103
142
  const cue = CueNode.from(latestRootCue, {
104
- id: parsedCue.id,
143
+ id: parsedCue.id || `cue-${block.start}-${block.cursor}`,
105
144
  startTime: parsedCue.startTime,
106
145
  endTime: parsedCue.endTime,
107
146
  content: parsedCue.text,
@@ -214,8 +253,14 @@ function evaluateBlock(content, start, end) {
214
253
  return [BlockType.REGION, payload];
215
254
  }
216
255
  case "STYLE": {
217
- const payload = Parser.parseStyle(blockMatch.groups["payload"]);
218
- return [BlockType.STYLE, payload];
256
+ try {
257
+ const payload = Parser.parseStyle(blockMatch.groups["payload"]);
258
+ return [BlockType.STYLE, payload];
259
+ }
260
+ catch (err) {
261
+ /** Failing gracefully, not critical */
262
+ return err;
263
+ }
219
264
  }
220
265
  case "NOTE": {
221
266
  return [BlockType.IGNORED, undefined];
@@ -224,12 +269,13 @@ function evaluateBlock(content, start, end) {
224
269
  }
225
270
  const cueMatch = contentSection.match(CUE_MATCH_REGEX);
226
271
  if (!cueMatch) {
272
+ /** Failing gracefully, not critical */
227
273
  return new InvalidFormatError("UNKNOWN_BLOCK_ENTITY", contentSection);
228
274
  }
229
275
  const { attributes, cueid, endtime, starttime, text } = cueMatch.groups;
230
276
  const cueParsingResult = Parser.parseCue({
231
277
  attributes,
232
- cueid: cueid || `cue-${start}-${end}`,
278
+ cueid: cueid,
233
279
  endtime,
234
280
  starttime,
235
281
  text: text.replace(TABS_REGEX, ""),
@@ -0,0 +1,3 @@
1
+ export declare class EmptyStyleDeclarationError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,8 @@
1
+ export class EmptyStyleDeclarationError extends Error {
2
+ constructor() {
3
+ super();
4
+ this.name = "EmptyStyleDeclarationError";
5
+ this.message = `The provided style block resulted in an empty css ruleset.`;
6
+ }
7
+ }
8
+ //# sourceMappingURL=EmptyStyleDeclarationError.js.map
@@ -0,0 +1,3 @@
1
+ export declare class InvalidStyleDeclarationError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,8 @@
1
+ export class InvalidStyleDeclarationError extends Error {
2
+ constructor() {
3
+ super();
4
+ this.name = "InvalidStyleDeclarationError";
5
+ this.message = `The provided style block is invalid and will be ignored. The block must not contain empty lines and must have either a valid or no selector.`;
6
+ }
7
+ }
8
+ //# sourceMappingURL=InvalidStyleDeclarationError.js.map
@@ -0,0 +1,3 @@
1
+ export declare class MalformedStyleBlockError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,8 @@
1
+ export class MalformedStyleBlockError extends Error {
2
+ constructor() {
3
+ super();
4
+ this.name = "MalformedStyleBlockError";
5
+ this.message = `The provided style block is malformed. Will be ignored. STYLE declaration must be immediately followed by the '::cue' declaration (no empty lines allowed).`;
6
+ }
7
+ }
8
+ //# sourceMappingURL=MalformedStyleBlockError.js.map
@@ -15,5 +15,11 @@ export interface CueParsedData {
15
15
  tags: Entities.Tag[];
16
16
  text: string;
17
17
  renderingModifiers: RenderingModifiers;
18
+ /**
19
+ * Grouping identifier allows us to skip
20
+ * uniqueness checks for ids for cues nodes
21
+ * coming from the same source cues
22
+ */
23
+ groupingIdentifier?: string;
18
24
  }
19
25
  export declare function parseCue(data: CueRawData): CueParsedData[];
@@ -55,9 +55,10 @@ export function parseCue(data) {
55
55
  * have some tags still open
56
56
  */
57
57
  addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
58
+ currentCue.groupingIdentifier = currentCue.id || "timestamp-group";
58
59
  hsCues.push(currentCue);
59
60
  }
60
- currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers);
61
+ currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.groupingIdentifier);
61
62
  break;
62
63
  }
63
64
  default:
@@ -82,7 +83,7 @@ function addCueEntities(cue, entities) {
82
83
  cue.tags.push(entity);
83
84
  }
84
85
  }
85
- function createCue(startTime, endTime, id, renderingModifiers) {
86
+ function createCue(startTime, endTime, id, renderingModifiers, groupingIdentifier) {
86
87
  return {
87
88
  startTime,
88
89
  endTime,
@@ -90,6 +91,7 @@ function createCue(startTime, endTime, id, renderingModifiers) {
90
91
  tags: [],
91
92
  id,
92
93
  renderingModifiers,
94
+ groupingIdentifier,
93
95
  };
94
96
  }
95
97
  //# sourceMappingURL=parseCue.js.map
@@ -1,4 +1,6 @@
1
1
  import { EntitiesTokenMap } from "./Tags/tokenEntities.js";
2
+ import { MalformedStyleBlockError } from "../MalformedStyleBlockError.js";
3
+ import { InvalidStyleDeclarationError } from "../InvalidStyleDeclarationError.js";
2
4
  const CSS_RULESET_REGEX = /::cue(?:\(([^.]*?)(?:\.(.+))*\))?\s*\{\s*([\s\S]+)\}/;
3
5
  /**
4
6
  * Matching of `lang[voice="Esme"]` both 'lang' or 'voice' + 'Esme'
@@ -41,9 +43,12 @@ const WEBVTT_CSS_SUPPORTED_PROPERTIES = [
41
43
  // "line-height",
42
44
  ];
43
45
  export function parseStyle(rawStyleData) {
46
+ if (!rawStyleData) {
47
+ throw new MalformedStyleBlockError();
48
+ }
44
49
  const styleBlockComponents = rawStyleData.match(CSS_RULESET_REGEX);
45
50
  if (!styleBlockComponents) {
46
- return undefined;
51
+ throw new InvalidStyleDeclarationError();
47
52
  }
48
53
  const [, selector, classesChain = "", cssData] = styleBlockComponents;
49
54
  const normalizedCssData = normalizeCssString(cssData.trim());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sub37/webvtt-adapter",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "description": "A subtitles adapter for WebVTT subtitles",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -25,5 +25,5 @@
25
25
  "files": [
26
26
  "lib/**/*.+(js|d.ts)!(*.map)"
27
27
  ],
28
- "gitHead": "eddbc140e9228ac38ca5858550697545423db203"
28
+ "gitHead": "45d8a1862acdb71f80c60349eb877dde5af36984"
29
29
  }