@sub37/webvtt-adapter 1.1.3 → 2.0.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 +62 -74
- 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 +1 -2
- package/lib/Parser/parseStyle.js +8 -2
- 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 +3 -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,26 @@ 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);
|
|
183
175
|
stylesLoop: for (const style of styles) {
|
|
184
176
|
if (style.type !== 2 /* Parser.StyleDomain.TAG */) {
|
|
185
177
|
continue;
|
|
186
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Looking for a matching tag that has the same:
|
|
181
|
+
* - tag name
|
|
182
|
+
* - number of attributes
|
|
183
|
+
* - exact matching attributes
|
|
184
|
+
* - classes amount
|
|
185
|
+
* - exact matching classes
|
|
186
|
+
* - A nice smile :)
|
|
187
|
+
*/
|
|
187
188
|
if (style.tagName && style.tagName !== tag.tagType) {
|
|
188
189
|
continue;
|
|
189
190
|
}
|
|
@@ -211,11 +212,15 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
211
212
|
continue stylesLoop;
|
|
212
213
|
}
|
|
213
214
|
}
|
|
214
|
-
|
|
215
|
+
/**
|
|
216
|
+
* YAY 🎉 We found a matching tag for a style!
|
|
217
|
+
*/
|
|
218
|
+
entities.push(Entities.createLocalStyleEntity(style.styleString));
|
|
215
219
|
}
|
|
216
220
|
}
|
|
217
221
|
cue.entities = entities;
|
|
218
222
|
cues.push(cue);
|
|
223
|
+
yield [cue];
|
|
219
224
|
}
|
|
220
225
|
}
|
|
221
226
|
/** Skipping \n\n and going to the next character */
|
|
@@ -224,16 +229,16 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
224
229
|
}
|
|
225
230
|
catch (err) {
|
|
226
231
|
const error = err instanceof Error ? err : new Error(JSON.stringify(err));
|
|
227
|
-
return
|
|
232
|
+
return [
|
|
228
233
|
{
|
|
229
234
|
error,
|
|
230
235
|
isCritical: true,
|
|
231
236
|
failedChunk: undefined,
|
|
232
237
|
},
|
|
233
|
-
]
|
|
238
|
+
];
|
|
234
239
|
}
|
|
235
240
|
} while (block.cursor <= content.length);
|
|
236
|
-
return
|
|
241
|
+
return;
|
|
237
242
|
}
|
|
238
243
|
}
|
|
239
244
|
function evaluateBlock(content, start, end) {
|
|
@@ -307,21 +312,4 @@ function isCue(evaluation) {
|
|
|
307
312
|
function isError(evaluation) {
|
|
308
313
|
return !Array.isArray(evaluation) && evaluation instanceof Error;
|
|
309
314
|
}
|
|
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
315
|
//# 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
|
};
|
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]+)\}/;
|
|
@@ -106,11 +105,18 @@ function getSelectorComponents(rawSelector) {
|
|
|
106
105
|
attributes.push([attribute, value || "*"]);
|
|
107
106
|
}
|
|
108
107
|
}
|
|
108
|
+
if (!isSelectorSupported(selector)) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
109
111
|
return {
|
|
110
|
-
tagName:
|
|
112
|
+
tagName: selector,
|
|
111
113
|
attributes: new Map(attributes),
|
|
112
114
|
};
|
|
113
115
|
}
|
|
116
|
+
const SUPPORTED_STYLABLE_TAG_SELECTORS = ["b", "i", "u", "v", "lang", "c", "ruby", "rt"];
|
|
117
|
+
function isSelectorSupported(selector) {
|
|
118
|
+
return SUPPORTED_STYLABLE_TAG_SELECTORS.includes(selector);
|
|
119
|
+
}
|
|
114
120
|
function normalizeCssString(cssData = "") {
|
|
115
121
|
return cssData
|
|
116
122
|
.replace(/\/\*.+\*\//, "") /** CSS inline Comments */
|
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,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sub37/webvtt-adapter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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.0"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "rm -rf lib && pnpm tsc --build --force",
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"files": [
|
|
26
26
|
"lib/**/*.+(js|d.ts)!(*.map)"
|
|
27
27
|
],
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "851b6a6f0bed5987c4a716b3b58d833d0569a15f"
|
|
29
29
|
}
|
|
@@ -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
|