@sub37/webvtt-adapter 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Adapter.js CHANGED
@@ -172,6 +172,26 @@ export default class WebVTTAdapter extends BaseAdapter {
172
172
  for (const tag of parsedCue.tags) {
173
173
  const originalEntity = Object.getPrototypeOf(tag);
174
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
+ }
175
195
  stylesLoop: for (const style of styles) {
176
196
  if (style.type !== 2 /* Parser.StyleDomain.TAG */) {
177
197
  continue;
@@ -204,11 +224,12 @@ export default class WebVTTAdapter extends BaseAdapter {
204
224
  }
205
225
  }
206
226
  }
207
- if (style.classes.length !== tag.classes.length) {
208
- continue;
209
- }
210
- for (const className of tag.classes) {
211
- if (!style.classes.includes(className)) {
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)) {
212
233
  continue stylesLoop;
213
234
  }
214
235
  }
@@ -18,4 +18,6 @@ export type Style = SelectorTarget & {
18
18
  styleString: string;
19
19
  };
20
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;
21
23
  export {};
@@ -6,6 +6,34 @@ const CSS_RULESET_REGEX = /::cue(?:\(([^.]*?)(?:\.(.+))*\))?\s*\{\s*([\s\S]+)\}/
6
6
  */
7
7
  const CSS_SELECTOR_ATTRIBUTES_REGEX = /([^\[\]]+)|\[(.+?)(?:="(.+?)?")?\]/g;
8
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
+ };
9
37
  /**
10
38
  * @see https://www.w3.org/TR/webvtt1/#the-cue-pseudo-element
11
39
  */
@@ -79,7 +107,13 @@ function getParsedSelector(selector, classesChain) {
79
107
  selector: stripEscapedCodePoint(selector.slice(1)).replace("\\", ""),
80
108
  };
81
109
  }
82
- const selectorComponents = getSelectorComponents(selector);
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() };
83
117
  if (!selectorComponents ||
84
118
  (!selectorComponents.tagName && !selectorComponents.attributes.size && !classesChain.length)) {
85
119
  /** Invalid */
@@ -94,10 +128,6 @@ function getParsedSelector(selector, classesChain) {
94
128
  function getSelectorComponents(rawSelector) {
95
129
  let selector = "";
96
130
  const attributes = [];
97
- /**
98
- * This is too recent and will likely require a polyfill from
99
- * users. But it fulfill a requirement when matching things with regex.
100
- */
101
131
  const matchIterator = rawSelector.matchAll(CSS_SELECTOR_ATTRIBUTES_REGEX);
102
132
  for (const [, tag = selector, attribute, value] of matchIterator) {
103
133
  selector = tag;
@@ -175,4 +205,13 @@ function filterUnsupportedStandardProperties(styleString) {
175
205
  function stripEscapedCodePoint(string) {
176
206
  return string.replace(CODEPOINT_ESCAPE_REPLACE_REGEX, "$1$2");
177
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
+ }
178
217
  //# sourceMappingURL=parseStyle.js.map
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@sub37/webvtt-adapter",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "A subtitles adapter for WebVTT subtitles",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
7
7
  "peerDependencies": {
8
- "@sub37/adapter-utils": "^1.0.0"
8
+ "@sub37/adapter-utils": "^1.0.2"
9
9
  },
10
10
  "scripts": {
11
- "build": "rm -rf lib && pnpm tsc --build --force",
11
+ "build": "rm -rf lib && pnpm tsc --build",
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": "851b6a6f0bed5987c4a716b3b58d833d0569a15f"
31
+ "gitHead": "0b12627570c0737374e9a30c2d9bed7ce0600ad6"
29
32
  }