@sub37/webvtt-adapter 1.0.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/README.md +23 -0
- package/lib/Adapter.d.ts +7 -0
- package/lib/Adapter.js +279 -0
- package/lib/InvalidFormatError.d.ts +8 -0
- package/lib/InvalidFormatError.js +28 -0
- package/lib/MissingContentError.d.ts +3 -0
- package/lib/MissingContentError.js +8 -0
- package/lib/Parser/RenderingModifiers.d.ts +18 -0
- package/lib/Parser/RenderingModifiers.js +300 -0
- package/lib/Parser/Tags/Node.d.ts +10 -0
- package/lib/Parser/Tags/Node.js +10 -0
- package/lib/Parser/Tags/NodeQueue.d.ts +12 -0
- package/lib/Parser/Tags/NodeQueue.js +41 -0
- package/lib/Parser/Tags/index.d.ts +3 -0
- package/lib/Parser/Tags/index.js +4 -0
- package/lib/Parser/Tags/tokenEntities.d.ts +4 -0
- package/lib/Parser/Tags/tokenEntities.js +12 -0
- package/lib/Parser/Tags/utils.d.ts +15 -0
- package/lib/Parser/Tags/utils.js +63 -0
- package/lib/Parser/Timestamps.utils.d.ts +1 -0
- package/lib/Parser/Timestamps.utils.js +17 -0
- package/lib/Parser/index.d.ts +3 -0
- package/lib/Parser/index.js +4 -0
- package/lib/Parser/parseCue.d.ts +19 -0
- package/lib/Parser/parseCue.js +95 -0
- package/lib/Parser/parseRegion.d.ts +5 -0
- package/lib/Parser/parseRegion.js +81 -0
- package/lib/Parser/parseStyle.d.ts +22 -0
- package/lib/Parser/parseStyle.js +159 -0
- package/lib/Token.d.ts +24 -0
- package/lib/Token.js +40 -0
- package/lib/Tokenizer.d.ts +41 -0
- package/lib/Tokenizer.js +280 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- package/package.json +28 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Token } from "../../Token";
|
|
2
|
+
export default class Node {
|
|
3
|
+
/** Zero-based position of cue (or timestamp section) content */
|
|
4
|
+
index: number;
|
|
5
|
+
token: Token;
|
|
6
|
+
parent: Node;
|
|
7
|
+
constructor(
|
|
8
|
+
/** Zero-based position of cue (or timestamp section) content */
|
|
9
|
+
index: number, token: Token);
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type Node from "./Node";
|
|
2
|
+
/**
|
|
3
|
+
* LIFO queue, where root is always the first element,
|
|
4
|
+
* so we can easily pop out and not drill.
|
|
5
|
+
*/
|
|
6
|
+
export default class NodeQueue {
|
|
7
|
+
private root;
|
|
8
|
+
get current(): Node;
|
|
9
|
+
get length(): number;
|
|
10
|
+
push(node: Node): void;
|
|
11
|
+
pop(): Node | undefined;
|
|
12
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LIFO queue, where root is always the first element,
|
|
3
|
+
* so we can easily pop out and not drill.
|
|
4
|
+
*/
|
|
5
|
+
export default class NodeQueue {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.root = null;
|
|
8
|
+
}
|
|
9
|
+
get current() {
|
|
10
|
+
return this.root;
|
|
11
|
+
}
|
|
12
|
+
get length() {
|
|
13
|
+
if (!this.root) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
let thisNode = this.root;
|
|
17
|
+
let length = 1;
|
|
18
|
+
while (thisNode.parent !== null) {
|
|
19
|
+
length++;
|
|
20
|
+
thisNode = thisNode.parent;
|
|
21
|
+
}
|
|
22
|
+
return length;
|
|
23
|
+
}
|
|
24
|
+
push(node) {
|
|
25
|
+
if (!this.root) {
|
|
26
|
+
this.root = node;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
node.parent = this.root;
|
|
30
|
+
this.root = node;
|
|
31
|
+
}
|
|
32
|
+
pop() {
|
|
33
|
+
if (!this.root) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const out = this.root;
|
|
37
|
+
this.root = this.root.parent;
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=NodeQueue.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Entities } from "@sub37/server";
|
|
2
|
+
import type { CueParsedData } from "../parseCue.js";
|
|
3
|
+
import type Node from "./Node.js";
|
|
4
|
+
import type NodeQueue from "./NodeQueue.js";
|
|
5
|
+
export declare function isSupported(content: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Creates entities from tree entities that have not been popped
|
|
8
|
+
* out yet, without removing them from the tree
|
|
9
|
+
*
|
|
10
|
+
* @param openTagsQueue
|
|
11
|
+
* @param currentCue
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare function createTagEntitiesFromUnpaired(openTagsQueue: NodeQueue, currentCue: CueParsedData): Entities.Tag[];
|
|
15
|
+
export declare function createTagEntity(currentCue: CueParsedData, tagStart: Node): Entities.Tag;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Entities } from "@sub37/server";
|
|
2
|
+
import { EntitiesTokenMap } from "./tokenEntities.js";
|
|
3
|
+
export function isSupported(content) {
|
|
4
|
+
return EntitiesTokenMap.hasOwnProperty(content);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Creates entities from tree entities that have not been popped
|
|
8
|
+
* out yet, without removing them from the tree
|
|
9
|
+
*
|
|
10
|
+
* @param openTagsQueue
|
|
11
|
+
* @param currentCue
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export function createTagEntitiesFromUnpaired(openTagsQueue, currentCue) {
|
|
15
|
+
let nodeCursor = openTagsQueue.current;
|
|
16
|
+
if (!nodeCursor) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
const entities = [];
|
|
20
|
+
while (nodeCursor !== null) {
|
|
21
|
+
if (currentCue.text.length - nodeCursor.index !== 0) {
|
|
22
|
+
/**
|
|
23
|
+
* If an entity startTag is placed between two timestamps
|
|
24
|
+
* the closing timestamp should not have the new tag associated.
|
|
25
|
+
* tag.index is zero-based.
|
|
26
|
+
*/
|
|
27
|
+
entities.push(createTagEntity(currentCue, nodeCursor));
|
|
28
|
+
}
|
|
29
|
+
nodeCursor = nodeCursor.parent;
|
|
30
|
+
}
|
|
31
|
+
return entities;
|
|
32
|
+
}
|
|
33
|
+
export function createTagEntity(currentCue, tagStart) {
|
|
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
|
+
var _a;
|
|
41
|
+
const tagOpenedInCurrentCue = currentCue.text.length - tagStart.index > 0;
|
|
42
|
+
const attributes = new Map((_a = tagStart.token.annotations) === null || _a === void 0 ? void 0 : _a.map((annotation) => {
|
|
43
|
+
var _a;
|
|
44
|
+
if (tagStart.token.content === "lang") {
|
|
45
|
+
return ["lang", annotation];
|
|
46
|
+
}
|
|
47
|
+
if (tagStart.token.content === "v") {
|
|
48
|
+
return ["voice", annotation];
|
|
49
|
+
}
|
|
50
|
+
const attribute = annotation.split("=");
|
|
51
|
+
return [attribute[0], (_a = attribute[1]) === null || _a === void 0 ? void 0 : _a.replace(/["']/g, "")];
|
|
52
|
+
}));
|
|
53
|
+
return new Entities.Tag({
|
|
54
|
+
tagType: EntitiesTokenMap[tagStart.token.content],
|
|
55
|
+
offset: tagOpenedInCurrentCue ? tagStart.index : 0,
|
|
56
|
+
length: tagOpenedInCurrentCue
|
|
57
|
+
? currentCue.text.length - tagStart.index
|
|
58
|
+
: currentCue.text.length,
|
|
59
|
+
attributes,
|
|
60
|
+
classes: tagStart.token.classes,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseMs(timestring: string): number;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const TIME_REGEX = /(?<hours>(\d{2})?):?(?<minutes>(\d{2})):(?<seconds>(\d{2}))(?:\.(?<milliseconds>(\d{0,3})))?/;
|
|
2
|
+
export function parseMs(timestring) {
|
|
3
|
+
const timeMatch = timestring.match(TIME_REGEX);
|
|
4
|
+
if (!timeMatch) {
|
|
5
|
+
throw new Error("Time format is not valid. Ignoring cue.");
|
|
6
|
+
}
|
|
7
|
+
const { groups: { hours, minutes, seconds, milliseconds }, } = timeMatch;
|
|
8
|
+
const hoursInSeconds = zeroFallback(parseInt(hours)) * 60 * 60;
|
|
9
|
+
const minutesInSeconds = zeroFallback(parseInt(minutes)) * 60;
|
|
10
|
+
const parsedSeconds = zeroFallback(parseInt(seconds));
|
|
11
|
+
const parsedMs = zeroFallback(parseInt(milliseconds)) / 1000;
|
|
12
|
+
return (hoursInSeconds + minutesInSeconds + parsedSeconds + parsedMs) * 1000;
|
|
13
|
+
}
|
|
14
|
+
function zeroFallback(value) {
|
|
15
|
+
return value || 0;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=Timestamps.utils.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Entities, RenderingModifiers } from "@sub37/server";
|
|
2
|
+
/** This structure is compliant with the resulting one from Regex groups property */
|
|
3
|
+
export interface CueRawData {
|
|
4
|
+
cueid: string;
|
|
5
|
+
starttime: string;
|
|
6
|
+
endtime: string;
|
|
7
|
+
attributes: string;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
export interface CueParsedData {
|
|
11
|
+
id?: string;
|
|
12
|
+
startTime: number;
|
|
13
|
+
endTime: number;
|
|
14
|
+
regionName?: string;
|
|
15
|
+
tags: Entities.Tag[];
|
|
16
|
+
text: string;
|
|
17
|
+
renderingModifiers: RenderingModifiers;
|
|
18
|
+
}
|
|
19
|
+
export declare function parseCue(data: CueRawData): CueParsedData[];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Tokenizer } from "../Tokenizer.js";
|
|
2
|
+
import { TokenType } from "../Token.js";
|
|
3
|
+
import * as Tags from "./Tags/index.js";
|
|
4
|
+
import * as Timestamps from "./Timestamps.utils.js";
|
|
5
|
+
import { WebVTTRenderingModifiers } from "./RenderingModifiers.js";
|
|
6
|
+
export function parseCue(data) {
|
|
7
|
+
const { starttime, endtime, text } = data;
|
|
8
|
+
const hsCues = [];
|
|
9
|
+
const tokenizer = new Tokenizer(text);
|
|
10
|
+
let token = null;
|
|
11
|
+
let currentCue = createCue(Timestamps.parseMs(starttime), Timestamps.parseMs(endtime), data.cueid, WebVTTRenderingModifiers.fromString(data.attributes));
|
|
12
|
+
const openTagsQueue = new Tags.NodeQueue();
|
|
13
|
+
while ((token = tokenizer.nextToken())) {
|
|
14
|
+
switch (token.type) {
|
|
15
|
+
case TokenType.START_TAG: {
|
|
16
|
+
if (Tags.isSupported(token.content)) {
|
|
17
|
+
openTagsQueue.push(new Tags.Node(currentCue.text.length, token));
|
|
18
|
+
}
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
case TokenType.END_TAG: {
|
|
22
|
+
if (Tags.isSupported(token.content) && openTagsQueue.length) {
|
|
23
|
+
if (!openTagsQueue.current) {
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* <ruby> is expected to contain nothing but text and <rt>.
|
|
28
|
+
* Can we be safe about popping twice, one for rt and one for ruby later?
|
|
29
|
+
*/
|
|
30
|
+
if (token.content === "ruby" && openTagsQueue.current.token.content === "rt") {
|
|
31
|
+
const out = openTagsQueue.pop();
|
|
32
|
+
addCueEntities(currentCue, [Tags.createTagEntity(currentCue, out)]);
|
|
33
|
+
}
|
|
34
|
+
if (openTagsQueue.current.token.content === token.content) {
|
|
35
|
+
const out = openTagsQueue.pop();
|
|
36
|
+
addCueEntities(currentCue, [Tags.createTagEntity(currentCue, out)]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case TokenType.STRING: {
|
|
42
|
+
currentCue.text += token.content;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case TokenType.TIMESTAMP: {
|
|
46
|
+
/**
|
|
47
|
+
* If current cue has no content, we can safely ignore it.
|
|
48
|
+
* Next cues will be the timestamped ones.
|
|
49
|
+
*/
|
|
50
|
+
if (currentCue.text.length) {
|
|
51
|
+
/**
|
|
52
|
+
* Closing the current entities for the previous cue,
|
|
53
|
+
* still without resetting open tags, because timestamps
|
|
54
|
+
* actually belong to the same "logic" cue, so we might
|
|
55
|
+
* have some tags still open
|
|
56
|
+
*/
|
|
57
|
+
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
58
|
+
hsCues.push(currentCue);
|
|
59
|
+
}
|
|
60
|
+
currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
default:
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
// Resetting the token for the next one
|
|
67
|
+
token = null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* For the last token... hip hip, hooray!
|
|
71
|
+
* Jk, we need to close the yet-opened
|
|
72
|
+
* tags and create entities for them.
|
|
73
|
+
*/
|
|
74
|
+
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
75
|
+
if (currentCue.text.length) {
|
|
76
|
+
hsCues.push(currentCue);
|
|
77
|
+
}
|
|
78
|
+
return hsCues;
|
|
79
|
+
}
|
|
80
|
+
function addCueEntities(cue, entities) {
|
|
81
|
+
for (const entity of entities) {
|
|
82
|
+
cue.tags.push(entity);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function createCue(startTime, endTime, id, renderingModifiers) {
|
|
86
|
+
return {
|
|
87
|
+
startTime,
|
|
88
|
+
endTime,
|
|
89
|
+
text: "",
|
|
90
|
+
tags: [],
|
|
91
|
+
id,
|
|
92
|
+
renderingModifiers,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=parseCue.js.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param rawRegionData
|
|
3
|
+
*/
|
|
4
|
+
export function parseRegion(rawRegionData) {
|
|
5
|
+
const region = new WebVTTRegion();
|
|
6
|
+
const attributes = rawRegionData.split(/[\n\t\s]+/);
|
|
7
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
8
|
+
const [key, value] = attributes[i].split(":");
|
|
9
|
+
if (!value || !key) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
switch (key) {
|
|
13
|
+
case "regionanchor":
|
|
14
|
+
case "viewportanchor": {
|
|
15
|
+
const [x = "0%", y = "0%"] = value.split(",");
|
|
16
|
+
region[key] = [parseInt(x), parseInt(y)];
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
case "scroll": {
|
|
20
|
+
if (value !== "up" && value !== "none") {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
region[key] = value;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case "id": {
|
|
27
|
+
region[key] = value;
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case "lines":
|
|
31
|
+
case "width": {
|
|
32
|
+
region[key] = parseInt(value);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
default:
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!region.id) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
return region;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* One line's height in VH units.
|
|
46
|
+
* This probably assumes that each line in renderer is
|
|
47
|
+
* of the same height. So this might lead to some issues
|
|
48
|
+
* in the future.
|
|
49
|
+
*
|
|
50
|
+
* I still don't have clear why Chrome does have this
|
|
51
|
+
* constant while all the standard version of VTT standard
|
|
52
|
+
* says "6vh".
|
|
53
|
+
*
|
|
54
|
+
* @see https://github.com/chromium/chromium/blob/c4d3c31083a2e1481253ff2d24298a1dfe19c754/third_party/blink/renderer/core/html/track/vtt/vtt_region.cc#L70
|
|
55
|
+
* @see https://www.w3.org/TR/webvtt1/#processing-model
|
|
56
|
+
*/
|
|
57
|
+
const VH_LINE_HEIGHT = 5.33;
|
|
58
|
+
class WebVTTRegion {
|
|
59
|
+
constructor() {
|
|
60
|
+
/**
|
|
61
|
+
* Region width expressed in percentage
|
|
62
|
+
*/
|
|
63
|
+
this.width = 100;
|
|
64
|
+
this.lines = 3;
|
|
65
|
+
}
|
|
66
|
+
getOrigin() {
|
|
67
|
+
const height = VH_LINE_HEIGHT * this.lines;
|
|
68
|
+
const [regionAnchorWidth = 0, regionAnchorHeight = 0] = this.regionanchor || [];
|
|
69
|
+
const [viewportAnchorWidth = 0, viewportAnchorHeight = 0] = this.viewportanchor || [];
|
|
70
|
+
/**
|
|
71
|
+
* It is still not very clear to me why we base on current width and height, but
|
|
72
|
+
* a thing that I know is that we need low numbers.
|
|
73
|
+
*/
|
|
74
|
+
const leftOffset = (regionAnchorWidth * this.width) / 100;
|
|
75
|
+
const topOffset = (regionAnchorHeight * height) / 100;
|
|
76
|
+
const originX = viewportAnchorWidth - leftOffset;
|
|
77
|
+
const originY = viewportAnchorHeight - topOffset;
|
|
78
|
+
return [originX, originY];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=parseRegion.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Entities } from "@sub37/server";
|
|
2
|
+
export declare const enum StyleDomain {
|
|
3
|
+
GLOBAL = 0,
|
|
4
|
+
ID = 1,
|
|
5
|
+
TAG = 2
|
|
6
|
+
}
|
|
7
|
+
type SelectorTarget = {
|
|
8
|
+
type: StyleDomain.GLOBAL;
|
|
9
|
+
} | {
|
|
10
|
+
type: StyleDomain.ID;
|
|
11
|
+
selector: string;
|
|
12
|
+
} | {
|
|
13
|
+
type: StyleDomain.TAG;
|
|
14
|
+
tagName: Entities.TagType;
|
|
15
|
+
classes: string[];
|
|
16
|
+
attributes: Map<string, string>;
|
|
17
|
+
};
|
|
18
|
+
export type Style = SelectorTarget & {
|
|
19
|
+
styleString: string;
|
|
20
|
+
};
|
|
21
|
+
export declare function parseStyle(rawStyleData: string): Style | undefined;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { EntitiesTokenMap } from "./Tags/tokenEntities.js";
|
|
2
|
+
const CSS_RULESET_REGEX = /::cue(?:\(([^.]*?)(?:\.(.+))*\))?\s*\{\s*([\s\S]+)\}/;
|
|
3
|
+
/**
|
|
4
|
+
* Matching of `lang[voice="Esme"]` both 'lang' or 'voice' + 'Esme'
|
|
5
|
+
*/
|
|
6
|
+
const CSS_SELECTOR_ATTRIBUTES_REGEX = /([^\[\]]+)|\[(.+?)(?:="(.+?)?")?\]/g;
|
|
7
|
+
const CODEPOINT_ESCAPE_REPLACE_REGEX = /\\3(\d)\s+(\d+)/;
|
|
8
|
+
/**
|
|
9
|
+
* @see https://www.w3.org/TR/webvtt1/#the-cue-pseudo-element
|
|
10
|
+
*/
|
|
11
|
+
const WEBVTT_CSS_SUPPORTED_PROPERTIES = [
|
|
12
|
+
"color",
|
|
13
|
+
"opacity",
|
|
14
|
+
"visibility",
|
|
15
|
+
"text-shadow",
|
|
16
|
+
"white-space",
|
|
17
|
+
"text-combine-upright",
|
|
18
|
+
"ruby-position",
|
|
19
|
+
"text-decoration",
|
|
20
|
+
"text-decoration-color",
|
|
21
|
+
"text-decoration-line",
|
|
22
|
+
"text-decoration-style",
|
|
23
|
+
"text-decoration-thickness",
|
|
24
|
+
"background",
|
|
25
|
+
"background-color",
|
|
26
|
+
"background-image",
|
|
27
|
+
"outline",
|
|
28
|
+
"outline-color",
|
|
29
|
+
"outline-style",
|
|
30
|
+
"outline-width",
|
|
31
|
+
"font-family",
|
|
32
|
+
"font-size",
|
|
33
|
+
"font-stretch",
|
|
34
|
+
"font-style",
|
|
35
|
+
"font-variant",
|
|
36
|
+
"font-weight",
|
|
37
|
+
/**
|
|
38
|
+
* Line-height have been excluded because it might cause issues with
|
|
39
|
+
* the renderer as it has its own line-height property
|
|
40
|
+
*/
|
|
41
|
+
// "line-height",
|
|
42
|
+
];
|
|
43
|
+
export function parseStyle(rawStyleData) {
|
|
44
|
+
const styleBlockComponents = rawStyleData.match(CSS_RULESET_REGEX);
|
|
45
|
+
if (!styleBlockComponents) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
const [, selector, classesChain = "", cssData] = styleBlockComponents;
|
|
49
|
+
const normalizedCssData = normalizeCssString(cssData.trim());
|
|
50
|
+
if (!normalizedCssData.length) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const styleString = filterUnsupportedStandardProperties(normalizedCssData);
|
|
54
|
+
if (!styleString.length) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const parsedSelector = getParsedSelector(selector, classesChain);
|
|
58
|
+
if (!parsedSelector) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
return Object.assign(Object.assign({}, parsedSelector), { styleString });
|
|
62
|
+
}
|
|
63
|
+
function getParsedSelector(selector, classesChain) {
|
|
64
|
+
if (!selector && !classesChain.length) {
|
|
65
|
+
return {
|
|
66
|
+
type: 0 /* StyleDomain.GLOBAL */,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (selector.startsWith("#")) {
|
|
70
|
+
return {
|
|
71
|
+
type: 1 /* StyleDomain.ID */,
|
|
72
|
+
selector: stripEscapedCodePoint(selector.slice(1)).replace("\\", ""),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const selectorComponents = getSelectorComponents(selector);
|
|
76
|
+
if (!selectorComponents.tagName && !selectorComponents.attributes.size && !classesChain.length) {
|
|
77
|
+
/** Invalid */
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
return Object.assign({ type: 2 /* StyleDomain.TAG */, classes: (classesChain.length && classesChain.split(".")) || [] }, selectorComponents);
|
|
81
|
+
}
|
|
82
|
+
function getSelectorComponents(rawSelector) {
|
|
83
|
+
let selector = undefined;
|
|
84
|
+
const attributes = [];
|
|
85
|
+
/**
|
|
86
|
+
* This is too recent and will likely require a polyfill from
|
|
87
|
+
* users. But it fulfill a requirement when matching things with regex.
|
|
88
|
+
*/
|
|
89
|
+
const matchIterator = rawSelector.matchAll(CSS_SELECTOR_ATTRIBUTES_REGEX);
|
|
90
|
+
for (const [, tag = selector, attribute, value] of matchIterator) {
|
|
91
|
+
selector = tag;
|
|
92
|
+
if (attribute) {
|
|
93
|
+
attributes.push([attribute, value]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
tagName: EntitiesTokenMap[selector],
|
|
98
|
+
attributes: new Map(attributes),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function normalizeCssString(cssData = "") {
|
|
102
|
+
return cssData
|
|
103
|
+
.replace(/\/\*.+\*\//, "") /** CSS inline Comments */
|
|
104
|
+
.replace(/\s+/g, "\x20") /** Multiple whitespaces */
|
|
105
|
+
.trim();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Recomposes the style string by filtering out the unsupported
|
|
109
|
+
* WebVTT properties
|
|
110
|
+
*
|
|
111
|
+
* @param styleString
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
function filterUnsupportedStandardProperties(styleString) {
|
|
115
|
+
let finalStyleString = "";
|
|
116
|
+
let startCursor = 0;
|
|
117
|
+
let endCursor = 0;
|
|
118
|
+
while (endCursor <= styleString.length) {
|
|
119
|
+
if (styleString[endCursor] === ";" || endCursor === styleString.length) {
|
|
120
|
+
const parsed = styleString.slice(startCursor, endCursor + 1).split(/\s*:\s*/);
|
|
121
|
+
const property = parsed[0].trim();
|
|
122
|
+
const value = parsed[1].trim();
|
|
123
|
+
if (property.length && value.length && WEBVTT_CSS_SUPPORTED_PROPERTIES.includes(property)) {
|
|
124
|
+
finalStyleString += `${property}:${value}`;
|
|
125
|
+
}
|
|
126
|
+
/** Clearning up an restarting */
|
|
127
|
+
startCursor = endCursor + 1;
|
|
128
|
+
endCursor++;
|
|
129
|
+
}
|
|
130
|
+
endCursor++;
|
|
131
|
+
}
|
|
132
|
+
return finalStyleString;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* CSS specs require selectors to start with a character in
|
|
136
|
+
* ['ASCII upper alpha' - 'ASCII lower alpha'] code points range or an
|
|
137
|
+
* escaped character.
|
|
138
|
+
*
|
|
139
|
+
* If we have a CSS ID which is '123', it is written like "\31 23",
|
|
140
|
+
* where the space is needed to represent the entity and to not select
|
|
141
|
+
* a different character (\3123 would be a different character).
|
|
142
|
+
*
|
|
143
|
+
* Since ASCII Digits starts go from U+0030 (0) to U+0039 (9), we can
|
|
144
|
+
* safely strip it.
|
|
145
|
+
*
|
|
146
|
+
* Right now we are supporting only numbers but other character might
|
|
147
|
+
* get supported in the future.
|
|
148
|
+
*
|
|
149
|
+
*
|
|
150
|
+
* @param string
|
|
151
|
+
* @see https://w3c.github.io/webvtt/#introduction-other-features
|
|
152
|
+
* @see https://infra.spec.whatwg.org/#code-points
|
|
153
|
+
* @see https://www.w3.org/International/questions/qa-escapes#css_identifiers
|
|
154
|
+
* @see https://github.com/chromium/chromium/blob/924ec189cdfd33c8cee15d918f927afcb88d06db/third_party/blink/renderer/core/css/parser/css_parser_idioms.cc#L24-L52
|
|
155
|
+
*/
|
|
156
|
+
function stripEscapedCodePoint(string) {
|
|
157
|
+
return string.replace(CODEPOINT_ESCAPE_REPLACE_REGEX, "$1$2");
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=parseStyle.js.map
|
package/lib/Token.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare enum TokenType {
|
|
2
|
+
STRING = 0,
|
|
3
|
+
START_TAG = 1,
|
|
4
|
+
END_TAG = 2,
|
|
5
|
+
TIMESTAMP = 3
|
|
6
|
+
}
|
|
7
|
+
type Boundaries = {
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
};
|
|
11
|
+
export declare class Token {
|
|
12
|
+
readonly type: TokenType;
|
|
13
|
+
readonly content: string;
|
|
14
|
+
annotations: string[];
|
|
15
|
+
classes: string[];
|
|
16
|
+
offset: number;
|
|
17
|
+
length: number;
|
|
18
|
+
private constructor();
|
|
19
|
+
static String(content: string, boundaries: Boundaries): Token;
|
|
20
|
+
static StartTag(tagName: string, boundaries: Boundaries, classes?: string[], annotations?: string[]): Token;
|
|
21
|
+
static EndTag(tagName: string, boundaries: Boundaries): Token;
|
|
22
|
+
static TimestampTag(timestampRaw: string, boundaries: Boundaries): Token;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
package/lib/Token.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export var TokenType;
|
|
2
|
+
(function (TokenType) {
|
|
3
|
+
TokenType[TokenType["STRING"] = 0] = "STRING";
|
|
4
|
+
TokenType[TokenType["START_TAG"] = 1] = "START_TAG";
|
|
5
|
+
TokenType[TokenType["END_TAG"] = 2] = "END_TAG";
|
|
6
|
+
TokenType[TokenType["TIMESTAMP"] = 3] = "TIMESTAMP";
|
|
7
|
+
})(TokenType || (TokenType = {}));
|
|
8
|
+
export class Token {
|
|
9
|
+
constructor(type, content) {
|
|
10
|
+
this.type = type;
|
|
11
|
+
this.content = content;
|
|
12
|
+
}
|
|
13
|
+
static String(content, boundaries) {
|
|
14
|
+
const token = new Token(TokenType.STRING, content);
|
|
15
|
+
token.length = boundaries.end - boundaries.start;
|
|
16
|
+
token.offset = boundaries.start;
|
|
17
|
+
return token;
|
|
18
|
+
}
|
|
19
|
+
static StartTag(tagName, boundaries, classes = [], annotations = []) {
|
|
20
|
+
const token = new Token(TokenType.START_TAG, tagName);
|
|
21
|
+
token.length = boundaries.end - boundaries.start;
|
|
22
|
+
token.offset = boundaries.start;
|
|
23
|
+
token.classes = classes;
|
|
24
|
+
token.annotations = annotations;
|
|
25
|
+
return token;
|
|
26
|
+
}
|
|
27
|
+
static EndTag(tagName, boundaries) {
|
|
28
|
+
const token = new Token(TokenType.END_TAG, tagName);
|
|
29
|
+
token.length = boundaries.end - boundaries.start;
|
|
30
|
+
token.offset = boundaries.start;
|
|
31
|
+
return token;
|
|
32
|
+
}
|
|
33
|
+
static TimestampTag(timestampRaw, boundaries) {
|
|
34
|
+
const token = new Token(TokenType.TIMESTAMP, timestampRaw);
|
|
35
|
+
token.length = boundaries.end - boundaries.start;
|
|
36
|
+
token.offset = boundaries.start;
|
|
37
|
+
return token;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=Token.js.map
|