@sub37/webvtt-adapter 1.0.3 → 1.1.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.
package/lib/Adapter.js CHANGED
@@ -31,6 +31,7 @@ export default class WebVTTAdapter extends BaseAdapter {
31
31
  },
32
32
  ]);
33
33
  }
34
+ const cueIdsList = new Set();
34
35
  const cues = [];
35
36
  const content = String(rawContent).replace(/\r?\n/g, "\n");
36
37
  const block = {
@@ -98,10 +99,38 @@ export default class WebVTTAdapter extends BaseAdapter {
98
99
  let latestRootCue = undefined;
99
100
  for (const parsedCue of parsedContent) {
100
101
  if (parsedCue.startTime >= parsedCue.endTime) {
102
+ failures.push({
103
+ error: new Error(`A cue cannot start (${parsedCue.startTime}) after its end time (${parsedCue.endTime})`),
104
+ failedChunk: content.substring(block.start, block.cursor),
105
+ isCritical: false,
106
+ });
101
107
  continue;
102
108
  }
109
+ if (parsedCue.id) {
110
+ /**
111
+ * "A WebVTT cue identifier must be unique amongst
112
+ * all the WebVTT cue identifiers of all WebVTT
113
+ * cues of a WebVTT file."
114
+ *
115
+ * @see https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
116
+ */
117
+ if (!parsedCue.isTimestamp && cueIdsList.has(parsedCue.id)) {
118
+ failures.push({
119
+ 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
+ failedChunk: content.substring(block.start, block.cursor),
121
+ isCritical: false,
122
+ });
123
+ continue;
124
+ }
125
+ /**
126
+ * ... however, when we generate a custom identifier
127
+ * for the cue, we re-use the same for the timestamps
128
+ * because they must appear on the same line.
129
+ */
130
+ cueIdsList.add(parsedCue.id);
131
+ }
103
132
  const cue = CueNode.from(latestRootCue, {
104
- id: parsedCue.id,
133
+ id: parsedCue.id || `cue-${block.start}-${block.cursor}`,
105
134
  startTime: parsedCue.startTime,
106
135
  endTime: parsedCue.endTime,
107
136
  content: parsedCue.text,
@@ -229,7 +258,7 @@ function evaluateBlock(content, start, end) {
229
258
  const { attributes, cueid, endtime, starttime, text } = cueMatch.groups;
230
259
  const cueParsingResult = Parser.parseCue({
231
260
  attributes,
232
- cueid: cueid || `cue-${start}-${end}`,
261
+ cueid: cueid,
233
262
  endtime,
234
263
  starttime,
235
264
  text: text.replace(TABS_REGEX, ""),
@@ -15,5 +15,6 @@ export interface CueParsedData {
15
15
  tags: Entities.Tag[];
16
16
  text: string;
17
17
  renderingModifiers: RenderingModifiers;
18
+ isTimestamp?: boolean;
18
19
  }
19
20
  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.isTimestamp = true;
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.isTimestamp);
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, isTimestamp = false) {
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
+ isTimestamp,
93
95
  };
94
96
  }
95
97
  //# sourceMappingURL=parseCue.js.map
package/lib/Token.d.ts CHANGED
@@ -9,12 +9,12 @@ type Boundaries = {
9
9
  end: number;
10
10
  };
11
11
  export declare class Token {
12
- readonly type: TokenType;
13
- readonly content: string;
14
12
  annotations: string[];
15
13
  classes: string[];
16
14
  offset: number;
17
15
  length: number;
16
+ readonly type: TokenType;
17
+ readonly content: string;
18
18
  private constructor();
19
19
  static String(content: string, boundaries: Boundaries): Token;
20
20
  static StartTag(tagName: string, boundaries: Boundaries, classes?: string[], annotations?: string[]): Token;
package/lib/Tokenizer.js CHANGED
@@ -40,7 +40,7 @@ export class Tokenizer {
40
40
  this.startPoint = 0;
41
41
  }
42
42
  static isWhitespace(character) {
43
- return character == " " || character == "\x09" || character == "\x0C";
43
+ return character == "\x20" || character == "\x09" || character == "\x0C";
44
44
  }
45
45
  static isNewLine(character) {
46
46
  return character == "\x0A";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sub37/webvtt-adapter",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
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": "e4225d306701a1248847d856d4493c8bbee092bf"
28
+ "gitHead": "f25f16822f3940d1bc26bc0e7e9b65d968913803"
29
29
  }