@sub37/webvtt-adapter 1.1.0 → 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";
@@ -85,7 +86,16 @@ export default class WebVTTAdapter extends BaseAdapter {
85
86
  if (isStyle(blockEvaluationResult) && shouldProcessNonCues) {
86
87
  const [blockType, parsedContent] = blockEvaluationResult;
87
88
  latestBlockPhase = blockType;
88
- 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
+ }
89
99
  }
90
100
  if (isCue(blockEvaluationResult)) {
91
101
  const [blockType, parsedContent] = blockEvaluationResult;
@@ -114,7 +124,7 @@ export default class WebVTTAdapter extends BaseAdapter {
114
124
  *
115
125
  * @see https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
116
126
  */
117
- if (!parsedCue.isTimestamp && cueIdsList.has(parsedCue.id)) {
127
+ if (!parsedCue.groupingIdentifier && cueIdsList.has(parsedCue.id)) {
118
128
  failures.push({
119
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}'`),
120
130
  failedChunk: content.substring(block.start, block.cursor),
@@ -243,8 +253,14 @@ function evaluateBlock(content, start, end) {
243
253
  return [BlockType.REGION, payload];
244
254
  }
245
255
  case "STYLE": {
246
- const payload = Parser.parseStyle(blockMatch.groups["payload"]);
247
- 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
+ }
248
264
  }
249
265
  case "NOTE": {
250
266
  return [BlockType.IGNORED, undefined];
@@ -253,6 +269,7 @@ function evaluateBlock(content, start, end) {
253
269
  }
254
270
  const cueMatch = contentSection.match(CUE_MATCH_REGEX);
255
271
  if (!cueMatch) {
272
+ /** Failing gracefully, not critical */
256
273
  return new InvalidFormatError("UNKNOWN_BLOCK_ENTITY", contentSection);
257
274
  }
258
275
  const { attributes, cueid, endtime, starttime, text } = cueMatch.groups;
@@ -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,6 +15,11 @@ export interface CueParsedData {
15
15
  tags: Entities.Tag[];
16
16
  text: string;
17
17
  renderingModifiers: RenderingModifiers;
18
- isTimestamp?: boolean;
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;
19
24
  }
20
25
  export declare function parseCue(data: CueRawData): CueParsedData[];
@@ -55,10 +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.isTimestamp = true;
58
+ currentCue.groupingIdentifier = currentCue.id || "timestamp-group";
59
59
  hsCues.push(currentCue);
60
60
  }
61
- currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.isTimestamp);
61
+ currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.groupingIdentifier);
62
62
  break;
63
63
  }
64
64
  default:
@@ -83,7 +83,7 @@ function addCueEntities(cue, entities) {
83
83
  cue.tags.push(entity);
84
84
  }
85
85
  }
86
- function createCue(startTime, endTime, id, renderingModifiers, isTimestamp = false) {
86
+ function createCue(startTime, endTime, id, renderingModifiers, groupingIdentifier) {
87
87
  return {
88
88
  startTime,
89
89
  endTime,
@@ -91,7 +91,7 @@ function createCue(startTime, endTime, id, renderingModifiers, isTimestamp = fal
91
91
  tags: [],
92
92
  id,
93
93
  renderingModifiers,
94
- isTimestamp,
94
+ groupingIdentifier,
95
95
  };
96
96
  }
97
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.1.0",
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": "f25f16822f3940d1bc26bc0e7e9b65d968913803"
28
+ "gitHead": "45d8a1862acdb71f80c60349eb877dde5af36984"
29
29
  }