@sub37/webvtt-adapter 1.1.0 → 1.1.2
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 +32 -9
- package/lib/EmptyStyleDeclarationError.d.ts +3 -0
- package/lib/EmptyStyleDeclarationError.js +8 -0
- package/lib/InvalidStyleDeclarationError.d.ts +3 -0
- package/lib/InvalidStyleDeclarationError.js +8 -0
- package/lib/MalformedStyleBlockError.d.ts +3 -0
- package/lib/MalformedStyleBlockError.js +8 -0
- package/lib/Parser/RenderingModifiers.d.ts +2 -2
- package/lib/Parser/RenderingModifiers.js +32 -24
- package/lib/Parser/Tags/Node.d.ts +1 -1
- package/lib/Parser/Tags/Node.js +3 -1
- package/lib/Parser/Tags/NodeQueue.d.ts +1 -1
- package/lib/Parser/Tags/NodeQueue.js +1 -3
- package/lib/Parser/Tags/utils.js +2 -4
- package/lib/Parser/Timestamps.utils.js +2 -2
- package/lib/Parser/parseCue.d.ts +7 -2
- package/lib/Parser/parseCue.js +4 -4
- package/lib/Parser/parseRegion.d.ts +1 -1
- package/lib/Parser/parseRegion.js +20 -7
- package/lib/Parser/parseStyle.d.ts +1 -1
- package/lib/Parser/parseStyle.js +18 -5
- package/lib/Token.js +6 -0
- package/lib/Tokenizer.d.ts +2 -2
- package/lib/Tokenizer.js +6 -5
- package/package.json +4 -4
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";
|
|
@@ -23,7 +24,7 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
23
24
|
}
|
|
24
25
|
parse(rawContent) {
|
|
25
26
|
if (!rawContent) {
|
|
26
|
-
return BaseAdapter.ParseResult(
|
|
27
|
+
return BaseAdapter.ParseResult([], [
|
|
27
28
|
{
|
|
28
29
|
error: new MissingContentError(),
|
|
29
30
|
failedChunk: "",
|
|
@@ -77,7 +78,7 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
77
78
|
const shouldProcessNonCues = latestBlockPhase & (BlockType.REGION | BlockType.STYLE | BlockType.HEADER);
|
|
78
79
|
if (isRegion(blockEvaluationResult) && shouldProcessNonCues) {
|
|
79
80
|
const [blockType, parsedContent] = blockEvaluationResult;
|
|
80
|
-
if (parsedContent
|
|
81
|
+
if (parsedContent?.id) {
|
|
81
82
|
latestBlockPhase = blockType;
|
|
82
83
|
regions[parsedContent.id] = parsedContent;
|
|
83
84
|
}
|
|
@@ -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
|
-
|
|
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.
|
|
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),
|
|
@@ -136,7 +146,7 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
136
146
|
content: parsedCue.text,
|
|
137
147
|
renderingModifiers: parsedCue.renderingModifiers,
|
|
138
148
|
});
|
|
139
|
-
if (parsedCue.renderingModifiers
|
|
149
|
+
if (parsedCue.renderingModifiers?.regionIdentifier) {
|
|
140
150
|
cue.region = regions[parsedCue.renderingModifiers.regionIdentifier];
|
|
141
151
|
}
|
|
142
152
|
if (!latestRootCue) {
|
|
@@ -214,7 +224,7 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
214
224
|
}
|
|
215
225
|
catch (err) {
|
|
216
226
|
const error = err instanceof Error ? err : new Error(JSON.stringify(err));
|
|
217
|
-
return BaseAdapter.ParseResult(
|
|
227
|
+
return BaseAdapter.ParseResult([], [
|
|
218
228
|
{
|
|
219
229
|
error,
|
|
220
230
|
isCritical: true,
|
|
@@ -236,15 +246,27 @@ function evaluateBlock(content, start, end) {
|
|
|
236
246
|
}
|
|
237
247
|
const contentSection = content.substring(start, end);
|
|
238
248
|
const blockMatch = contentSection.match(BLOCK_MATCH_REGEX);
|
|
239
|
-
if (blockMatch
|
|
249
|
+
if (blockMatch?.groups["blocktype"]) {
|
|
240
250
|
switch (blockMatch.groups["blocktype"]) {
|
|
241
251
|
case "REGION": {
|
|
242
252
|
const payload = Parser.parseRegion(blockMatch.groups["payload"]);
|
|
253
|
+
if (!payload) {
|
|
254
|
+
return [BlockType.IGNORED, undefined];
|
|
255
|
+
}
|
|
243
256
|
return [BlockType.REGION, payload];
|
|
244
257
|
}
|
|
245
258
|
case "STYLE": {
|
|
246
|
-
|
|
247
|
-
|
|
259
|
+
try {
|
|
260
|
+
const payload = Parser.parseStyle(blockMatch.groups["payload"]);
|
|
261
|
+
if (!payload) {
|
|
262
|
+
return [BlockType.IGNORED, undefined];
|
|
263
|
+
}
|
|
264
|
+
return [BlockType.STYLE, payload];
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
/** Failing gracefully, not critical */
|
|
268
|
+
return err;
|
|
269
|
+
}
|
|
248
270
|
}
|
|
249
271
|
case "NOTE": {
|
|
250
272
|
return [BlockType.IGNORED, undefined];
|
|
@@ -253,6 +275,7 @@ function evaluateBlock(content, start, end) {
|
|
|
253
275
|
}
|
|
254
276
|
const cueMatch = contentSection.match(CUE_MATCH_REGEX);
|
|
255
277
|
if (!cueMatch) {
|
|
278
|
+
/** Failing gracefully, not critical */
|
|
256
279
|
return new InvalidFormatError("UNKNOWN_BLOCK_ENTITY", contentSection);
|
|
257
280
|
}
|
|
258
281
|
const { attributes, cueid, endtime, starttime, text } = cueMatch.groups;
|
|
@@ -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,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,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
|
|
@@ -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 (!
|
|
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 &&
|
|
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 -
|
|
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 (
|
|
193
|
-
return Math.min(this.size,
|
|
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 -
|
|
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,
|
|
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
|
|
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 (
|
|
268
|
+
if (position <= 50) {
|
|
265
269
|
return 0;
|
|
266
270
|
}
|
|
267
|
-
const width = (100 -
|
|
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
|
|
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);
|
package/lib/Parser/Tags/Node.js
CHANGED
|
@@ -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
|
package/lib/Parser/Tags/utils.js
CHANGED
|
@@ -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(
|
|
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],
|
|
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));
|
package/lib/Parser/parseCue.d.ts
CHANGED
|
@@ -14,7 +14,12 @@ export interface CueParsedData {
|
|
|
14
14
|
regionName?: string;
|
|
15
15
|
tags: Entities.Tag[];
|
|
16
16
|
text: string;
|
|
17
|
-
renderingModifiers
|
|
18
|
-
|
|
17
|
+
renderingModifiers?: RenderingModifiers | undefined;
|
|
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[];
|
package/lib/Parser/parseCue.js
CHANGED
|
@@ -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.
|
|
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.
|
|
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,
|
|
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
|
-
|
|
94
|
+
groupingIdentifier,
|
|
95
95
|
};
|
|
96
96
|
}
|
|
97
97
|
//# sourceMappingURL=parseCue.js.map
|
|
@@ -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++) {
|
|
@@ -66,13 +69,23 @@ export function parseRegion(rawRegionData) {
|
|
|
66
69
|
*/
|
|
67
70
|
const VH_LINE_HEIGHT = 5.33;
|
|
68
71
|
class WebVTTRegion {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
id = "";
|
|
73
|
+
/**
|
|
74
|
+
* Region width expressed in percentage
|
|
75
|
+
*/
|
|
76
|
+
width = 100;
|
|
77
|
+
lines = 3;
|
|
78
|
+
scroll;
|
|
79
|
+
/**
|
|
80
|
+
* Position of region based on video region.
|
|
81
|
+
* Couple of numbers expressed in percentage
|
|
82
|
+
*/
|
|
83
|
+
viewportanchor;
|
|
84
|
+
/**
|
|
85
|
+
* Position of region based on viewportAnchor
|
|
86
|
+
* Couple of numbers expressed in percentage
|
|
87
|
+
*/
|
|
88
|
+
regionanchor;
|
|
76
89
|
getOrigin() {
|
|
77
90
|
const height = VH_LINE_HEIGHT * this.lines;
|
|
78
91
|
const [regionAnchorWidth = 0, regionAnchorHeight = 0] = this.regionanchor || [];
|
|
@@ -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 {};
|
package/lib/Parser/parseStyle.js
CHANGED
|
@@ -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
|
-
|
|
51
|
+
throw new InvalidStyleDeclarationError();
|
|
47
52
|
}
|
|
48
53
|
const [, selector, classesChain = "", cssData] = styleBlockComponents;
|
|
49
54
|
const normalizedCssData = normalizeCssString(cssData.trim());
|
|
@@ -58,7 +63,10 @@ export function parseStyle(rawStyleData) {
|
|
|
58
63
|
if (!parsedSelector) {
|
|
59
64
|
return undefined;
|
|
60
65
|
}
|
|
61
|
-
return
|
|
66
|
+
return {
|
|
67
|
+
...parsedSelector,
|
|
68
|
+
styleString,
|
|
69
|
+
};
|
|
62
70
|
}
|
|
63
71
|
function getParsedSelector(selector, classesChain) {
|
|
64
72
|
if (!selector && !classesChain.length) {
|
|
@@ -73,14 +81,19 @@ function getParsedSelector(selector, classesChain) {
|
|
|
73
81
|
};
|
|
74
82
|
}
|
|
75
83
|
const selectorComponents = getSelectorComponents(selector);
|
|
76
|
-
if (!selectorComponents
|
|
84
|
+
if (!selectorComponents ||
|
|
85
|
+
(!selectorComponents.tagName && !selectorComponents.attributes.size && !classesChain.length)) {
|
|
77
86
|
/** Invalid */
|
|
78
87
|
return undefined;
|
|
79
88
|
}
|
|
80
|
-
return
|
|
89
|
+
return {
|
|
90
|
+
type: 2 /* StyleDomain.TAG */,
|
|
91
|
+
classes: (classesChain.length && classesChain.split(".")) || [],
|
|
92
|
+
...selectorComponents,
|
|
93
|
+
};
|
|
81
94
|
}
|
|
82
95
|
function getSelectorComponents(rawSelector) {
|
|
83
|
-
let selector =
|
|
96
|
+
let selector = "";
|
|
84
97
|
const attributes = [];
|
|
85
98
|
/**
|
|
86
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;
|
package/lib/Tokenizer.d.ts
CHANGED
|
@@ -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):
|
|
29
|
-
static isNewLine(character: string):
|
|
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 (!
|
|
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.
|
|
3
|
+
"version": "1.1.2",
|
|
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": "
|
|
8
|
+
"@sub37/server": "1.1.1"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "rm -rf lib && pnpm tsc
|
|
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": "
|
|
28
|
+
"gitHead": "0d8b842834856464dde9890feb4c39b5823699ab"
|
|
29
29
|
}
|