@sub37/webvtt-adapter 1.1.3 → 2.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.d.ts +3 -2
- package/lib/Adapter.js +88 -79
- package/lib/Parser/RenderingModifiers.d.ts +1 -1
- package/lib/Parser/Tags/Entities/Tag.d.ts +9 -0
- package/lib/Parser/Tags/Entities/Tag.js +47 -0
- package/lib/Parser/Tags/utils.d.ts +3 -4
- package/lib/Parser/Tags/utils.js +6 -29
- package/lib/Parser/parseCue.d.ts +2 -2
- package/lib/Parser/parseCue.js +27 -9
- package/lib/Parser/parseRegion.d.ts +1 -1
- package/lib/Parser/parseRegion.js +1 -0
- package/lib/Parser/parseStyle.d.ts +3 -2
- package/lib/Parser/parseStyle.js +52 -7
- package/lib/Token.d.ts +2 -2
- package/lib/Token.js +2 -2
- package/lib/Tokenizer.d.ts +1 -1
- package/lib/Tokenizer.js +1 -1
- package/package.json +6 -3
- package/lib/MissingContentError.d.ts +0 -3
- package/lib/MissingContentError.js +0 -8
- package/lib/Parser/Tags/tokenEntities.d.ts +0 -4
- package/lib/Parser/Tags/tokenEntities.js +0 -12
package/lib/Adapter.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BaseAdapter } from "@sub37/
|
|
1
|
+
import { BaseAdapter } from "@sub37/adapter-utils";
|
|
2
|
+
import type { ParseGenerator } from "@sub37/adapter-utils";
|
|
2
3
|
export default class WebVTTAdapter extends BaseAdapter {
|
|
3
4
|
static get supportedType(): string;
|
|
4
|
-
parse(rawContent: string):
|
|
5
|
+
parse(rawContent: string): ParseGenerator;
|
|
5
6
|
}
|
package/lib/Adapter.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BaseAdapter, CueNode, Entities } from "@sub37/
|
|
1
|
+
import { BaseAdapter, CueNode, Entities } from "@sub37/adapter-utils";
|
|
2
2
|
import { EmptyStyleDeclarationError } from "./EmptyStyleDeclarationError.js";
|
|
3
3
|
import { InvalidFormatError } from "./InvalidFormatError.js";
|
|
4
|
-
import { MissingContentError } from "
|
|
4
|
+
import { MissingContentError } from "@sub37/adapter-utils/MissingContentError";
|
|
5
5
|
import * as Parser from "./Parser/index.js";
|
|
6
6
|
const WEBVTT_HEADER_SECTION = /^(?:[\uFEFF\n\s]*)?WEBVTT(?:\n(.+))?/;
|
|
7
7
|
const BLOCK_MATCH_REGEX = /(?<blocktype>(?:REGION|STYLE|NOTE))[\s\r\n]*(?<payload>[\w\W]*)/;
|
|
@@ -22,15 +22,15 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
22
22
|
static get supportedType() {
|
|
23
23
|
return "text/vtt";
|
|
24
24
|
}
|
|
25
|
-
parse(rawContent) {
|
|
25
|
+
*parse(rawContent) {
|
|
26
26
|
if (!rawContent) {
|
|
27
|
-
return
|
|
27
|
+
return [
|
|
28
28
|
{
|
|
29
29
|
error: new MissingContentError(),
|
|
30
30
|
failedChunk: "",
|
|
31
31
|
isCritical: true,
|
|
32
32
|
},
|
|
33
|
-
]
|
|
33
|
+
];
|
|
34
34
|
}
|
|
35
35
|
const cueIdsList = new Set();
|
|
36
36
|
const cues = [];
|
|
@@ -41,7 +41,6 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
41
41
|
};
|
|
42
42
|
const regions = Object.create(null);
|
|
43
43
|
const styles = [];
|
|
44
|
-
const failures = [];
|
|
45
44
|
/**
|
|
46
45
|
* Phase indicator to ignore unordered blocks.
|
|
47
46
|
* Standard expects header (WEBVTT, STYLE, REGION, COMMENTS)
|
|
@@ -61,11 +60,13 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
61
60
|
try {
|
|
62
61
|
const blockEvaluationResult = evaluateBlock(content, block.start, block.cursor);
|
|
63
62
|
if (isError(blockEvaluationResult)) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
yield [
|
|
64
|
+
{
|
|
65
|
+
error: blockEvaluationResult,
|
|
66
|
+
failedChunk: content.substring(block.start, block.cursor),
|
|
67
|
+
isCritical: false,
|
|
68
|
+
},
|
|
69
|
+
];
|
|
69
70
|
/** Skipping \n\n and going to the next character */
|
|
70
71
|
block.cursor += 3;
|
|
71
72
|
block.start = block.cursor - 1;
|
|
@@ -87,11 +88,13 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
87
88
|
const [blockType, parsedContent] = blockEvaluationResult;
|
|
88
89
|
latestBlockPhase = blockType;
|
|
89
90
|
if (!parsedContent) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
yield [
|
|
92
|
+
{
|
|
93
|
+
error: new EmptyStyleDeclarationError(),
|
|
94
|
+
failedChunk: content.substring(block.start, block.cursor),
|
|
95
|
+
isCritical: false,
|
|
96
|
+
},
|
|
97
|
+
];
|
|
95
98
|
}
|
|
96
99
|
else {
|
|
97
100
|
styles.push(parsedContent);
|
|
@@ -107,13 +110,21 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
107
110
|
* origin from.
|
|
108
111
|
*/
|
|
109
112
|
let latestRootCue = undefined;
|
|
113
|
+
/**
|
|
114
|
+
* @TODO performing a filter and a remap for each parsed cue might be useless
|
|
115
|
+
*/
|
|
116
|
+
const globalStylesEntities = styles
|
|
117
|
+
.filter((style) => style.type === 0 /* Parser.StyleDomain.GLOBAL */)
|
|
118
|
+
.map((style) => Entities.createLineStyleEntity(style.styleString));
|
|
110
119
|
for (const parsedCue of parsedContent) {
|
|
111
120
|
if (parsedCue.startTime >= parsedCue.endTime) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
121
|
+
yield [
|
|
122
|
+
{
|
|
123
|
+
error: new Error(`A cue cannot start (${parsedCue.startTime}) after its end time (${parsedCue.endTime})`),
|
|
124
|
+
failedChunk: content.substring(block.start, block.cursor),
|
|
125
|
+
isCritical: false,
|
|
126
|
+
},
|
|
127
|
+
];
|
|
117
128
|
continue;
|
|
118
129
|
}
|
|
119
130
|
if (parsedCue.id) {
|
|
@@ -125,11 +136,13 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
125
136
|
* @see https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
|
|
126
137
|
*/
|
|
127
138
|
if (!parsedCue.groupingIdentifier && cueIdsList.has(parsedCue.id)) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
139
|
+
yield [
|
|
140
|
+
{
|
|
141
|
+
error: new Error(`A WebVTT cue identifier must be unique amongst all the cue identifiers of a WebVTT file. Double id found: '${parsedCue.id}'`),
|
|
142
|
+
failedChunk: content.substring(block.start, block.cursor),
|
|
143
|
+
isCritical: false,
|
|
144
|
+
},
|
|
145
|
+
];
|
|
133
146
|
continue;
|
|
134
147
|
}
|
|
135
148
|
/**
|
|
@@ -152,38 +165,46 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
152
165
|
if (!latestRootCue) {
|
|
153
166
|
latestRootCue = cue;
|
|
154
167
|
}
|
|
155
|
-
const
|
|
156
|
-
.filter((style) =>
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
})
|
|
160
|
-
.sort(styleSpecificitySorter);
|
|
161
|
-
const entities = [];
|
|
162
|
-
if (stylesEntities.length) {
|
|
163
|
-
/**
|
|
164
|
-
* Having the same length of the style entities here allows
|
|
165
|
-
* us to prevent having several elements with the same styles
|
|
166
|
-
* in renderer.
|
|
167
|
-
*/
|
|
168
|
-
const superCue = Object.getPrototypeOf(cue);
|
|
169
|
-
const length = superCue instanceof CueNode ? superCue.content.length : parsedCue.text.length;
|
|
170
|
-
entities.push(new Entities.Tag({
|
|
171
|
-
offset: 0,
|
|
172
|
-
length,
|
|
173
|
-
tagType: Entities.TagType.SPAN,
|
|
174
|
-
attributes: new Map(),
|
|
175
|
-
classes: [],
|
|
176
|
-
}));
|
|
177
|
-
for (let style of stylesEntities) {
|
|
178
|
-
entities[0].setStyles(style.styleString);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
168
|
+
const stylesById = styles
|
|
169
|
+
.filter((style) => style.type === 1 /* Parser.StyleDomain.ID */ && style.selector === parsedCue.id)
|
|
170
|
+
.map((style) => Entities.createLineStyleEntity(style.styleString));
|
|
171
|
+
const entities = [...globalStylesEntities, ...stylesById];
|
|
181
172
|
for (const tag of parsedCue.tags) {
|
|
182
|
-
|
|
173
|
+
const originalEntity = Object.getPrototypeOf(tag);
|
|
174
|
+
entities.push(originalEntity);
|
|
175
|
+
if (tag.classes) {
|
|
176
|
+
for (const className of tag.classes) {
|
|
177
|
+
const knownTextColor = Parser.resolveClassDefaultTextColor(className);
|
|
178
|
+
if (knownTextColor) {
|
|
179
|
+
entities.push(Entities.createLocalStyleEntity({
|
|
180
|
+
color: knownTextColor,
|
|
181
|
+
"text-decoration-color": knownTextColor,
|
|
182
|
+
"text-decoration-line": "inherit",
|
|
183
|
+
}));
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const knownBackgroundColor = Parser.resolveClassDefaultBackgroundColor(className);
|
|
187
|
+
if (knownBackgroundColor) {
|
|
188
|
+
entities.push(Entities.createLocalStyleEntity({
|
|
189
|
+
"background-color": knownBackgroundColor,
|
|
190
|
+
}));
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
183
195
|
stylesLoop: for (const style of styles) {
|
|
184
196
|
if (style.type !== 2 /* Parser.StyleDomain.TAG */) {
|
|
185
197
|
continue;
|
|
186
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Looking for a matching tag that has the same:
|
|
201
|
+
* - tag name
|
|
202
|
+
* - number of attributes
|
|
203
|
+
* - exact matching attributes
|
|
204
|
+
* - classes amount
|
|
205
|
+
* - exact matching classes
|
|
206
|
+
* - A nice smile :)
|
|
207
|
+
*/
|
|
187
208
|
if (style.tagName && style.tagName !== tag.tagType) {
|
|
188
209
|
continue;
|
|
189
210
|
}
|
|
@@ -203,19 +224,24 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
203
224
|
}
|
|
204
225
|
}
|
|
205
226
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
227
|
+
/*
|
|
228
|
+
* CSS class selectors are subset matches. `::cue(.yellow)` must
|
|
229
|
+
* match <c.yellow.bg_blue>.
|
|
230
|
+
*/
|
|
231
|
+
for (const className of style.classes) {
|
|
232
|
+
if (!tag.classes.includes(className)) {
|
|
211
233
|
continue stylesLoop;
|
|
212
234
|
}
|
|
213
235
|
}
|
|
214
|
-
|
|
236
|
+
/**
|
|
237
|
+
* YAY 🎉 We found a matching tag for a style!
|
|
238
|
+
*/
|
|
239
|
+
entities.push(Entities.createLocalStyleEntity(style.styleString));
|
|
215
240
|
}
|
|
216
241
|
}
|
|
217
242
|
cue.entities = entities;
|
|
218
243
|
cues.push(cue);
|
|
244
|
+
yield [cue];
|
|
219
245
|
}
|
|
220
246
|
}
|
|
221
247
|
/** Skipping \n\n and going to the next character */
|
|
@@ -224,16 +250,16 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
224
250
|
}
|
|
225
251
|
catch (err) {
|
|
226
252
|
const error = err instanceof Error ? err : new Error(JSON.stringify(err));
|
|
227
|
-
return
|
|
253
|
+
return [
|
|
228
254
|
{
|
|
229
255
|
error,
|
|
230
256
|
isCritical: true,
|
|
231
257
|
failedChunk: undefined,
|
|
232
258
|
},
|
|
233
|
-
]
|
|
259
|
+
];
|
|
234
260
|
}
|
|
235
261
|
} while (block.cursor <= content.length);
|
|
236
|
-
return
|
|
262
|
+
return;
|
|
237
263
|
}
|
|
238
264
|
}
|
|
239
265
|
function evaluateBlock(content, start, end) {
|
|
@@ -307,21 +333,4 @@ function isCue(evaluation) {
|
|
|
307
333
|
function isError(evaluation) {
|
|
308
334
|
return !Array.isArray(evaluation) && evaluation instanceof Error;
|
|
309
335
|
}
|
|
310
|
-
/**
|
|
311
|
-
* Reorders styles so that Global styles are placed
|
|
312
|
-
* before id styles
|
|
313
|
-
*
|
|
314
|
-
* @param s1
|
|
315
|
-
* @param s2
|
|
316
|
-
* @returns
|
|
317
|
-
*/
|
|
318
|
-
function styleSpecificitySorter(s1, s2) {
|
|
319
|
-
if (s1.type === s2.type) {
|
|
320
|
-
return 0;
|
|
321
|
-
}
|
|
322
|
-
if (s1.type === 1 /* Parser.StyleDomain.ID */ && s2.type === 0 /* Parser.StyleDomain.GLOBAL */) {
|
|
323
|
-
return 1;
|
|
324
|
-
}
|
|
325
|
-
return -1;
|
|
326
|
-
}
|
|
327
336
|
//# sourceMappingURL=Adapter.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RenderingModifiers } from "@sub37/
|
|
1
|
+
import type { RenderingModifiers } from "@sub37/adapter-utils/RenderingModifiers";
|
|
2
2
|
export declare class WebVTTRenderingModifiers implements RenderingModifiers {
|
|
3
3
|
static fromString(source: string | undefined): WebVTTRenderingModifiers;
|
|
4
4
|
private position?;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This object overrides and contains
|
|
3
|
+
* the original "@sub37/server" tag entity in order
|
|
4
|
+
* to add properties without altering the original
|
|
5
|
+
* object. This is needed for a matter of tag matching
|
|
6
|
+
* with styles
|
|
7
|
+
*/
|
|
8
|
+
import * as Entities from "@sub37/adapter-utils/Entities";
|
|
9
|
+
export declare function createTagEntity(tagType: string, attributes: Map<string, string | undefined>, classes?: string[]): Entities.TagEntity;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This object overrides and contains
|
|
3
|
+
* the original "@sub37/server" tag entity in order
|
|
4
|
+
* to add properties without altering the original
|
|
5
|
+
* object. This is needed for a matter of tag matching
|
|
6
|
+
* with styles
|
|
7
|
+
*/
|
|
8
|
+
import * as Entities from "@sub37/adapter-utils/Entities";
|
|
9
|
+
const WebVTTEntities = {
|
|
10
|
+
b: Entities.TagType.BOLD,
|
|
11
|
+
i: Entities.TagType.ITALIC,
|
|
12
|
+
span: Entities.TagType.SPAN,
|
|
13
|
+
rt: Entities.TagType.RT,
|
|
14
|
+
ruby: Entities.TagType.RUBY,
|
|
15
|
+
u: Entities.TagType.UNDERLINE,
|
|
16
|
+
/**
|
|
17
|
+
* Custom for WebVTT. They are remapped to
|
|
18
|
+
* span for the renderer
|
|
19
|
+
*/
|
|
20
|
+
lang: Entities.TagType.SPAN,
|
|
21
|
+
v: Entities.TagType.SPAN,
|
|
22
|
+
c: Entities.TagType.SPAN,
|
|
23
|
+
};
|
|
24
|
+
function isWebVTTEntityRecognized(tagType) {
|
|
25
|
+
return WebVTTEntities.hasOwnProperty(tagType);
|
|
26
|
+
}
|
|
27
|
+
export function createTagEntity(tagType, attributes, classes = []) {
|
|
28
|
+
const originalTagType = isWebVTTEntityRecognized(tagType)
|
|
29
|
+
? WebVTTEntities[tagType]
|
|
30
|
+
: Entities.TagType.SPAN;
|
|
31
|
+
const originalEntity = Entities.createTagEntity(originalTagType, attributes, classes);
|
|
32
|
+
return Object.create(originalEntity, {
|
|
33
|
+
type: {
|
|
34
|
+
value: 2 /* Entities.Type.TAG */,
|
|
35
|
+
},
|
|
36
|
+
tagType: {
|
|
37
|
+
value: tagType,
|
|
38
|
+
},
|
|
39
|
+
attributes: {
|
|
40
|
+
value: attributes,
|
|
41
|
+
},
|
|
42
|
+
classes: {
|
|
43
|
+
value: classes,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=Tag.js.map
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Entities } from "@sub37/
|
|
1
|
+
import { Entities } from "@sub37/adapter-utils";
|
|
2
2
|
import type { CueParsedData } from "../parseCue.js";
|
|
3
3
|
import type Node from "./Node.js";
|
|
4
4
|
import type NodeQueue from "./NodeQueue.js";
|
|
5
|
-
export declare function isSupported(content: string): boolean;
|
|
6
5
|
/**
|
|
7
6
|
* Creates entities from tree entities that have not been popped
|
|
8
7
|
* out yet, without removing them from the tree
|
|
@@ -11,5 +10,5 @@ export declare function isSupported(content: string): boolean;
|
|
|
11
10
|
* @param currentCue
|
|
12
11
|
* @returns
|
|
13
12
|
*/
|
|
14
|
-
export declare function createTagEntitiesFromUnpaired(openTagsQueue: NodeQueue, currentCue: CueParsedData): Entities.
|
|
15
|
-
export declare function
|
|
13
|
+
export declare function createTagEntitiesFromUnpaired(openTagsQueue: NodeQueue, currentCue: CueParsedData): Entities.TagEntity[];
|
|
14
|
+
export declare function createTagEntityByNode(tagStart: Node): Entities.TagEntity;
|
package/lib/Parser/Tags/utils.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EntitiesTokenMap } from "./tokenEntities.js";
|
|
3
|
-
export function isSupported(content) {
|
|
4
|
-
return EntitiesTokenMap.hasOwnProperty(content);
|
|
5
|
-
}
|
|
1
|
+
import { createTagEntity } from "./Entities/Tag.js";
|
|
6
2
|
/**
|
|
7
3
|
* Creates entities from tree entities that have not been popped
|
|
8
4
|
* out yet, without removing them from the tree
|
|
@@ -18,26 +14,15 @@ export function createTagEntitiesFromUnpaired(openTagsQueue, currentCue) {
|
|
|
18
14
|
}
|
|
19
15
|
const entities = [];
|
|
20
16
|
while (nodeCursor !== null) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* the closing timestamp should not have the new tag associated.
|
|
25
|
-
* tag.index is zero-based.
|
|
26
|
-
*/
|
|
27
|
-
entities.push(createTagEntity(currentCue, nodeCursor));
|
|
17
|
+
const entity = createTagEntityByNode(nodeCursor);
|
|
18
|
+
if (!currentCue.tags.length || !currentCue.tags.find((e) => e.tagType === entity.tagType)) {
|
|
19
|
+
entities.unshift(entity);
|
|
28
20
|
}
|
|
29
21
|
nodeCursor = nodeCursor.parent;
|
|
30
22
|
}
|
|
31
23
|
return entities;
|
|
32
24
|
}
|
|
33
|
-
export function
|
|
34
|
-
/**
|
|
35
|
-
* If length is negative, that means that the tag was opened before
|
|
36
|
-
* the beginning of the current Cue. Therefore, offset should represent
|
|
37
|
-
* the beginning of the **current cue** and the length should be set to
|
|
38
|
-
* current cue content.
|
|
39
|
-
*/
|
|
40
|
-
const tagOpenedInCurrentCue = currentCue.text.length - tagStart.index > 0;
|
|
25
|
+
export function createTagEntityByNode(tagStart) {
|
|
41
26
|
const attributes = new Map(tagStart.token.annotations?.map((annotation) => {
|
|
42
27
|
if (tagStart.token.content === "lang") {
|
|
43
28
|
return ["lang", annotation];
|
|
@@ -48,14 +33,6 @@ export function createTagEntity(currentCue, tagStart) {
|
|
|
48
33
|
const attribute = annotation.split("=");
|
|
49
34
|
return [attribute[0], attribute[1]?.replace(/["']/g, "")];
|
|
50
35
|
}));
|
|
51
|
-
return
|
|
52
|
-
tagType: EntitiesTokenMap[tagStart.token.content],
|
|
53
|
-
offset: tagOpenedInCurrentCue ? tagStart.index : 0,
|
|
54
|
-
length: tagOpenedInCurrentCue
|
|
55
|
-
? currentCue.text.length - tagStart.index
|
|
56
|
-
: currentCue.text.length,
|
|
57
|
-
attributes,
|
|
58
|
-
classes: tagStart.token.classes,
|
|
59
|
-
});
|
|
36
|
+
return createTagEntity(tagStart.token.content, attributes, tagStart.token.classes);
|
|
60
37
|
}
|
|
61
38
|
//# sourceMappingURL=utils.js.map
|
package/lib/Parser/parseCue.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Entities, RenderingModifiers } from "@sub37/
|
|
1
|
+
import type { Entities, RenderingModifiers } from "@sub37/adapter-utils";
|
|
2
2
|
/** This structure is compliant with the resulting one from Regex groups property */
|
|
3
3
|
export interface CueRawData {
|
|
4
4
|
cueid: string;
|
|
@@ -12,7 +12,7 @@ export interface CueParsedData {
|
|
|
12
12
|
startTime: number;
|
|
13
13
|
endTime: number;
|
|
14
14
|
regionName?: string;
|
|
15
|
-
tags: Entities.
|
|
15
|
+
tags: Entities.TagEntity[];
|
|
16
16
|
text: string;
|
|
17
17
|
renderingModifiers?: RenderingModifiers | undefined;
|
|
18
18
|
/**
|
package/lib/Parser/parseCue.js
CHANGED
|
@@ -13,13 +13,17 @@ export function parseCue(data) {
|
|
|
13
13
|
while ((token = tokenizer.nextToken())) {
|
|
14
14
|
switch (token.type) {
|
|
15
15
|
case TokenType.START_TAG: {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
openTagsQueue.push(new Tags.Node(currentCue.text.length, token));
|
|
17
|
+
if (!isCueDataTextEmpty(currentCue)) {
|
|
18
|
+
hsCues.push(currentCue);
|
|
19
|
+
currentCue = createCue(currentCue.startTime, currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.groupingIdentifier);
|
|
18
20
|
}
|
|
21
|
+
currentCue.groupingIdentifier = currentCue.id || "tag-group";
|
|
22
|
+
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
19
23
|
break;
|
|
20
24
|
}
|
|
21
25
|
case TokenType.END_TAG: {
|
|
22
|
-
if (
|
|
26
|
+
if (openTagsQueue.length) {
|
|
23
27
|
if (!openTagsQueue.current) {
|
|
24
28
|
break;
|
|
25
29
|
}
|
|
@@ -29,16 +33,23 @@ export function parseCue(data) {
|
|
|
29
33
|
*/
|
|
30
34
|
if (token.content === "ruby" && openTagsQueue.current.token.content === "rt") {
|
|
31
35
|
const out = openTagsQueue.pop();
|
|
32
|
-
addCueEntities(currentCue, [Tags.
|
|
36
|
+
addCueEntities(currentCue, [Tags.createTagEntityByNode(out)]);
|
|
33
37
|
}
|
|
34
38
|
if (openTagsQueue.current.token.content === token.content) {
|
|
35
|
-
|
|
36
|
-
addCueEntities(currentCue, [Tags.createTagEntity(currentCue, out)]);
|
|
39
|
+
openTagsQueue.pop();
|
|
37
40
|
}
|
|
38
41
|
}
|
|
42
|
+
if (!isCueDataTextEmpty(currentCue)) {
|
|
43
|
+
hsCues.push(currentCue);
|
|
44
|
+
currentCue = createCue(currentCue.startTime, currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.groupingIdentifier);
|
|
45
|
+
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
46
|
+
}
|
|
39
47
|
break;
|
|
40
48
|
}
|
|
41
49
|
case TokenType.STRING: {
|
|
50
|
+
if (!currentCue.text.length && Tokenizer.isWhitespace(token.content)) {
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
42
53
|
currentCue.text += token.content;
|
|
43
54
|
break;
|
|
44
55
|
}
|
|
@@ -47,7 +58,7 @@ export function parseCue(data) {
|
|
|
47
58
|
* If current cue has no content, we can safely ignore it.
|
|
48
59
|
* Next cues will be the timestamped ones.
|
|
49
60
|
*/
|
|
50
|
-
if (currentCue
|
|
61
|
+
if (!isCueDataTextEmpty(currentCue)) {
|
|
51
62
|
/**
|
|
52
63
|
* Closing the current entities for the previous cue,
|
|
53
64
|
* still without resetting open tags, because timestamps
|
|
@@ -59,6 +70,7 @@ export function parseCue(data) {
|
|
|
59
70
|
hsCues.push(currentCue);
|
|
60
71
|
}
|
|
61
72
|
currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.groupingIdentifier);
|
|
73
|
+
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
62
74
|
break;
|
|
63
75
|
}
|
|
64
76
|
default:
|
|
@@ -73,14 +85,16 @@ export function parseCue(data) {
|
|
|
73
85
|
* tags and create entities for them.
|
|
74
86
|
*/
|
|
75
87
|
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
76
|
-
if (currentCue
|
|
88
|
+
if (!isCueDataTextEmpty(currentCue)) {
|
|
77
89
|
hsCues.push(currentCue);
|
|
78
90
|
}
|
|
79
91
|
return hsCues;
|
|
80
92
|
}
|
|
81
93
|
function addCueEntities(cue, entities) {
|
|
82
94
|
for (const entity of entities) {
|
|
83
|
-
cue.tags.
|
|
95
|
+
if (!cue.tags.length || !cue.tags.find((t) => t.tagType === entity.tagType)) {
|
|
96
|
+
cue.tags.push(entity);
|
|
97
|
+
}
|
|
84
98
|
}
|
|
85
99
|
}
|
|
86
100
|
function createCue(startTime, endTime, id, renderingModifiers, groupingIdentifier) {
|
|
@@ -94,4 +108,8 @@ function createCue(startTime, endTime, id, renderingModifiers, groupingIdentifie
|
|
|
94
108
|
groupingIdentifier,
|
|
95
109
|
};
|
|
96
110
|
}
|
|
111
|
+
const EMPTY_STRING_REGEX = /\x0A|\x09|\x20|\x0C|/;
|
|
112
|
+
function isCueDataTextEmpty(cue) {
|
|
113
|
+
return !cue.text.length || !cue.text.replace(EMPTY_STRING_REGEX, "").length;
|
|
114
|
+
}
|
|
97
115
|
//# sourceMappingURL=parseCue.js.map
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Entities } from "@sub37/server";
|
|
2
1
|
export declare const enum StyleDomain {
|
|
3
2
|
GLOBAL = 0,
|
|
4
3
|
ID = 1,
|
|
@@ -11,7 +10,7 @@ type SelectorTarget = {
|
|
|
11
10
|
selector: string;
|
|
12
11
|
} | {
|
|
13
12
|
type: StyleDomain.TAG;
|
|
14
|
-
tagName:
|
|
13
|
+
tagName: string;
|
|
15
14
|
classes: string[];
|
|
16
15
|
attributes: Map<string, string>;
|
|
17
16
|
};
|
|
@@ -19,4 +18,6 @@ export type Style = SelectorTarget & {
|
|
|
19
18
|
styleString: string;
|
|
20
19
|
};
|
|
21
20
|
export declare function parseStyle(rawStyleData: string | undefined): Style | undefined;
|
|
21
|
+
export declare function resolveClassDefaultTextColor(className: string): string | undefined;
|
|
22
|
+
export declare function resolveClassDefaultBackgroundColor(className: string): string | undefined;
|
|
22
23
|
export {};
|
package/lib/Parser/parseStyle.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { EntitiesTokenMap } from "./Tags/tokenEntities.js";
|
|
2
1
|
import { MalformedStyleBlockError } from "../MalformedStyleBlockError.js";
|
|
3
2
|
import { InvalidStyleDeclarationError } from "../InvalidStyleDeclarationError.js";
|
|
4
3
|
const CSS_RULESET_REGEX = /::cue(?:\(([^.]*?)(?:\.(.+))*\))?\s*\{\s*([\s\S]+)\}/;
|
|
@@ -7,6 +6,34 @@ const CSS_RULESET_REGEX = /::cue(?:\(([^.]*?)(?:\.(.+))*\))?\s*\{\s*([\s\S]+)\}/
|
|
|
7
6
|
*/
|
|
8
7
|
const CSS_SELECTOR_ATTRIBUTES_REGEX = /([^\[\]]+)|\[(.+?)(?:="(.+?)?")?\]/g;
|
|
9
8
|
const CODEPOINT_ESCAPE_REPLACE_REGEX = /\\3(\d)\s+(\d+)/;
|
|
9
|
+
/**
|
|
10
|
+
* § 5.1. Default text colors
|
|
11
|
+
* @see https://www.w3.org/TR/webvtt1/#default-text-color
|
|
12
|
+
*/
|
|
13
|
+
const DEFAULT_TEXT_COLOR_CLASSES_MAP = {
|
|
14
|
+
white: "rgba(255,255,255,1)",
|
|
15
|
+
lime: "rgba(0,255,0,1)",
|
|
16
|
+
cyan: "rgba(0,255,255,1)",
|
|
17
|
+
red: "rgba(255,0,0,1)",
|
|
18
|
+
yellow: "rgba(255,255,0,1)",
|
|
19
|
+
magenta: "rgba(255,0,255,1)",
|
|
20
|
+
blue: "rgba(0,0,255,1)",
|
|
21
|
+
black: "rgba(0,0,0,1)",
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* § 5.2. Default text background colors
|
|
25
|
+
* @see https://www.w3.org/TR/webvtt1/#default-text-background
|
|
26
|
+
*/
|
|
27
|
+
const DEFAULT_BACKGROUND_COLOR_CLASSES_MAP = {
|
|
28
|
+
bg_white: "rgba(255,255,255,1)",
|
|
29
|
+
bg_lime: "rgba(0,255,0,1)",
|
|
30
|
+
bg_cyan: "rgba(0,255,255,1)",
|
|
31
|
+
bg_red: "rgba(255,0,0,1)",
|
|
32
|
+
bg_yellow: "rgba(255,255,0,1)",
|
|
33
|
+
bg_magenta: "rgba(255,0,255,1)",
|
|
34
|
+
bg_blue: "rgba(0,0,255,1)",
|
|
35
|
+
bg_black: "rgba(0,0,0,1)",
|
|
36
|
+
};
|
|
10
37
|
/**
|
|
11
38
|
* @see https://www.w3.org/TR/webvtt1/#the-cue-pseudo-element
|
|
12
39
|
*/
|
|
@@ -80,7 +107,13 @@ function getParsedSelector(selector, classesChain) {
|
|
|
80
107
|
selector: stripEscapedCodePoint(selector.slice(1)).replace("\\", ""),
|
|
81
108
|
};
|
|
82
109
|
}
|
|
83
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Class-only selectors, like `::cue(.yellow)`, are valid but the tag is not specified (empty):
|
|
112
|
+
* we only have classes.
|
|
113
|
+
*/
|
|
114
|
+
const selectorComponents = selector.length
|
|
115
|
+
? getSelectorComponents(selector)
|
|
116
|
+
: { tagName: "", attributes: new Map() };
|
|
84
117
|
if (!selectorComponents ||
|
|
85
118
|
(!selectorComponents.tagName && !selectorComponents.attributes.size && !classesChain.length)) {
|
|
86
119
|
/** Invalid */
|
|
@@ -95,10 +128,6 @@ function getParsedSelector(selector, classesChain) {
|
|
|
95
128
|
function getSelectorComponents(rawSelector) {
|
|
96
129
|
let selector = "";
|
|
97
130
|
const attributes = [];
|
|
98
|
-
/**
|
|
99
|
-
* This is too recent and will likely require a polyfill from
|
|
100
|
-
* users. But it fulfill a requirement when matching things with regex.
|
|
101
|
-
*/
|
|
102
131
|
const matchIterator = rawSelector.matchAll(CSS_SELECTOR_ATTRIBUTES_REGEX);
|
|
103
132
|
for (const [, tag = selector, attribute, value] of matchIterator) {
|
|
104
133
|
selector = tag;
|
|
@@ -106,11 +135,18 @@ function getSelectorComponents(rawSelector) {
|
|
|
106
135
|
attributes.push([attribute, value || "*"]);
|
|
107
136
|
}
|
|
108
137
|
}
|
|
138
|
+
if (!isSelectorSupported(selector)) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
109
141
|
return {
|
|
110
|
-
tagName:
|
|
142
|
+
tagName: selector,
|
|
111
143
|
attributes: new Map(attributes),
|
|
112
144
|
};
|
|
113
145
|
}
|
|
146
|
+
const SUPPORTED_STYLABLE_TAG_SELECTORS = ["b", "i", "u", "v", "lang", "c", "ruby", "rt"];
|
|
147
|
+
function isSelectorSupported(selector) {
|
|
148
|
+
return SUPPORTED_STYLABLE_TAG_SELECTORS.includes(selector);
|
|
149
|
+
}
|
|
114
150
|
function normalizeCssString(cssData = "") {
|
|
115
151
|
return cssData
|
|
116
152
|
.replace(/\/\*.+\*\//, "") /** CSS inline Comments */
|
|
@@ -169,4 +205,13 @@ function filterUnsupportedStandardProperties(styleString) {
|
|
|
169
205
|
function stripEscapedCodePoint(string) {
|
|
170
206
|
return string.replace(CODEPOINT_ESCAPE_REPLACE_REGEX, "$1$2");
|
|
171
207
|
}
|
|
208
|
+
export function resolveClassDefaultTextColor(className) {
|
|
209
|
+
return DEFAULT_TEXT_COLOR_CLASSES_MAP[className];
|
|
210
|
+
}
|
|
211
|
+
export function resolveClassDefaultBackgroundColor(className) {
|
|
212
|
+
if (!className.startsWith("bg_")) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
return DEFAULT_BACKGROUND_COLOR_CLASSES_MAP[className];
|
|
216
|
+
}
|
|
172
217
|
//# sourceMappingURL=parseStyle.js.map
|
package/lib/Token.d.ts
CHANGED
package/lib/Token.js
CHANGED
package/lib/Tokenizer.d.ts
CHANGED
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
*/
|
|
20
20
|
import { Token } from "./Token.js";
|
|
21
21
|
export declare class Tokenizer {
|
|
22
|
-
private rawContent;
|
|
23
22
|
/** Character index in the content */
|
|
24
23
|
private cursor;
|
|
25
24
|
/** Next token index in the content */
|
|
26
25
|
private startPoint;
|
|
26
|
+
private rawContent;
|
|
27
27
|
constructor(rawContent: string);
|
|
28
28
|
static isWhitespace(character: string): character is " " | "\t" | "\f";
|
|
29
29
|
static isNewLine(character: string): character is "\n";
|
package/lib/Tokenizer.js
CHANGED
|
@@ -32,11 +32,11 @@ var TokenizerState;
|
|
|
32
32
|
})(TokenizerState || (TokenizerState = {}));
|
|
33
33
|
const SHARED_DOM_PARSER = new DOMParser();
|
|
34
34
|
export class Tokenizer {
|
|
35
|
-
rawContent;
|
|
36
35
|
/** Character index in the content */
|
|
37
36
|
cursor = 0;
|
|
38
37
|
/** Next token index in the content */
|
|
39
38
|
startPoint = 0;
|
|
39
|
+
rawContent;
|
|
40
40
|
constructor(rawContent) {
|
|
41
41
|
this.rawContent = rawContent;
|
|
42
42
|
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sub37/webvtt-adapter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A subtitles adapter for WebVTT subtitles",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@sub37/
|
|
8
|
+
"@sub37/adapter-utils": "^1.0.1"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
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
|
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
15
18
|
"repository": {
|
|
16
19
|
"type": "git",
|
|
17
20
|
"url": "git+https://github.com/alexandercerutti/sub37.git"
|
|
@@ -25,5 +28,5 @@
|
|
|
25
28
|
"files": [
|
|
26
29
|
"lib/**/*.+(js|d.ts)!(*.map)"
|
|
27
30
|
],
|
|
28
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "b37421ec187692e55ca0c8071b28bab5b9e331f7"
|
|
29
32
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Entities } from "@sub37/server";
|
|
2
|
-
export const EntitiesTokenMap = {
|
|
3
|
-
b: Entities.TagType.BOLD,
|
|
4
|
-
c: Entities.TagType.CLASS,
|
|
5
|
-
i: Entities.TagType.ITALIC,
|
|
6
|
-
lang: Entities.TagType.LANG,
|
|
7
|
-
rt: Entities.TagType.RT,
|
|
8
|
-
ruby: Entities.TagType.RUBY,
|
|
9
|
-
u: Entities.TagType.UNDERLINE,
|
|
10
|
-
v: Entities.TagType.VOICE,
|
|
11
|
-
};
|
|
12
|
-
//# sourceMappingURL=tokenEntities.js.map
|