@js-toolkit/web-utils 1.61.1 → 1.62.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.
@@ -14,4 +14,10 @@ declare global {
14
14
  ManagedMediaSource?: ManagedMediaSource & typeof MediaSource;
15
15
  }
16
16
  }
17
- export declare function getMediaSource(managedMediaSource?: 'prefer' | 'maybe'): typeof MediaSource | undefined;
17
+ export declare function getMediaSource(
18
+ /**
19
+ * `prefer` - If has support.
20
+ * `maybe` - If has support but mse is unsupported.
21
+ * Otherwise returns mse MediaSource.
22
+ */
23
+ managedMediaSource?: 'prefer' | 'maybe'): typeof MediaSource | undefined;
@@ -0,0 +1,17 @@
1
+ import type { getMediaSource } from './getMediaSource';
2
+ declare global {
3
+ interface ManagedSourceBufferEventMap extends SourceBufferEventMap {
4
+ bufferedchange: Event;
5
+ }
6
+ interface ManagedSourceBuffer extends SourceBuffer {
7
+ onbufferedchange: ((this: ManagedSourceBuffer, ev: Event) => any) | null;
8
+ addEventListener<K extends keyof ManagedSourceBufferEventMap>(type: K, listener: (this: ManagedSourceBuffer, ev: ManagedSourceBufferEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
9
+ removeEventListener<K extends keyof ManagedSourceBufferEventMap>(type: K, listener: (this: ManagedSourceBuffer, ev: ManagedSourceBufferEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
10
+ }
11
+ interface Window {
12
+ ManagedSourceBuffer?: ManagedSourceBuffer & typeof SourceBuffer;
13
+ }
14
+ }
15
+ type ManagedMediaSourceOption = Parameters<typeof getMediaSource>[0];
16
+ export declare function getSourceBuffer(managedSourceBuffer?: ManagedMediaSourceOption): typeof SourceBuffer | undefined;
17
+ export {};
@@ -0,0 +1 @@
1
+ export function getSourceBuffer(e){return"prefer"===e&&window.ManagedSourceBuffer||"maybe"===e&&window.ManagedSourceBuffer&&!window.SourceBuffer?window.ManagedSourceBuffer:window.SourceBuffer}
@@ -7,11 +7,11 @@ export interface CueSegment {
7
7
  declare const TAG_NAME: Record<string, string>;
8
8
  export interface ParseCueTextOptions {
9
9
  /** Defaults to `0`. */
10
- readonly preferLength?: number | undefined;
10
+ readonly preferLineLength?: number | undefined;
11
11
  }
12
12
  export interface ParseCueTextResult<P> {
13
13
  readonly segments: P[][];
14
14
  readonly rawText: string[];
15
15
  }
16
- export declare function parseCueText<P = CueSegment>(input0: string, map?: (segment: CueSegment, prevSegment: P | undefined) => P, { preferLength }?: ParseCueTextOptions): ParseCueTextResult<P>;
16
+ export declare function parseCueText<P = CueSegment>(input0: string, map?: (segment: CueSegment, prevSegment: P | undefined) => P, { preferLineLength }?: ParseCueTextOptions): ParseCueTextResult<P>;
17
17
  export {};
@@ -1 +1 @@
1
- import{splitRows}from"./TextTracksController/utils";const ESCAPE={"&amp;":"&","&lt;":"<","&gt;":">","&lrm;":"‎","&rlm;":"‏","&nbsp;":" "},TAG_NAME={c:"span",i:"i",b:"b",u:"u",ruby:"ruby",rt:"rt",lang:"span"},TAG_ANNOTATION={v:"title",lang:"lang"},NEEDS_PARENT={rt:"ruby"};function computeSeconds(e,t,n,o){return 3600*(+e||0)+60*(+t||0)+(+n||0)+(+o||0)/1e3}function parseTimeStamp(e){const t=e.match(/^(\d+):(\d{2})(:\d{2})?\.(\d{3})/);if(!t)return;const[,n,o,r,l]=t;return l?computeSeconds(n,o,r.replace(":",""),l):+n>59?computeSeconds(n,o,"",l):computeSeconds("",n,o,l)}function unescape(e){let t,n=e;for(;t=n.match(/&(amp|lt|gt|lrm|rlm|nbsp);/);)n=n.replace(t[0],(e=>ESCAPE[e]));return n}function nextToken(e){if(!e)return[e,void 0];const t=e.match(/^([^<]*)(<[^>]*>?)?/);if(!t)return[e,void 0];const n=t[1]?t[1]:t[2];return null==n?[e,n]:[e.substring(n.length),n]}function shouldAdd(e,t){return!NEEDS_PARENT[t.localName]||NEEDS_PARENT[t.localName]===e.localName}function createHtmlNode(e,t){const n=TAG_NAME[e];if(!n)return;const o=window.document.createElement(n),r=TAG_ANNOTATION[e];return r&&t&&(o[r]=t.trim()),o}export function parseCueText(e,t,{preferLength:n=0}={}){let o,r,l=e,s=-1;const c=[],a=[],u=[],i=e=>{0===a.length&&a.push([]),a.at(-1).push(t?t(e,(a.at(-1)??a.at(-2))?.at(-1)):e)},p=e=>{const t=c.pop()??"",n=o;if(o=n.parentElement||void 0,null==o){i({id:e,startTime:s>=0?s:void 0,tag:t,node:n})}},d=()=>`${a.length}-${(a.at(-1)?.length??0)+1}`,m=(e,t)=>{t?u.push(e):u[u.length-1]+=e,t&&a.push([]),null==o?(e=>{const t=createHtmlNode("c","");t.appendChild(window.document.createTextNode(unescape(e)));const n={id:d(),node:t,tag:"c",startTime:s>=0?s:void 0};i(n)})(e):o.appendChild(window.document.createTextNode(unescape(e)))};for(;null!=([l,r]=nextToken(l),r);)if("<"===r[0])if("/"===r[1])c.at(-1)===r.substring(2).replace(">","")&&p(d());else{const e=parseTimeStamp(r.substring(1,r.length-1));if(e)s=e;else{const e=r.match(/^<([^.\s/0-9>]+)(\.[^\s\\>]+)?([^>\\]+)?(\\?)>?$/),t=e&&createHtmlNode(e[1],e[3]);!t||o&&!shouldAdd(o,t)||(e[2]&&(t.className=e[2].substring(1).replace("."," ")),c.push(e[1]),o?.appendChild(t),o=t)}}else if(0===u.length||n>0&&u.at(-1).length+r.length>n){const e=splitRows(r,n);for(let t=0;t<e.length;t+=1)m(e[t],!0)}else m(r,!1);return{segments:a,rawText:u}}
1
+ import{splitRows}from"./TextTracksController/utils";const ESCAPE={"&amp;":"&","&lt;":"<","&gt;":">","&lrm;":"‎","&rlm;":"‏","&nbsp;":" "},TAG_NAME={c:"span",i:"i",b:"b",u:"u",ruby:"ruby",rt:"rt",lang:"span"},TAG_ANNOTATION={v:"title",lang:"lang"},NEEDS_PARENT={rt:"ruby"};function computeSeconds(e,t,n,o){return 3600*(+e||0)+60*(+t||0)+(+n||0)+(+o||0)/1e3}function parseTimeStamp(e){const t=e.match(/^(\d+):(\d{2})(:\d{2})?\.(\d{3})/);if(!t)return;const[,n,o,r,l]=t;return l?computeSeconds(n,o,r.replace(":",""),l):+n>59?computeSeconds(n,o,"",l):computeSeconds("",n,o,l)}function unescape(e){let t,n=e;for(;t=n.match(/&(amp|lt|gt|lrm|rlm|nbsp);/);)n=n.replace(t[0],(e=>ESCAPE[e]));return n}function nextToken(e){if(!e)return[e,void 0];const t=e.match(/^([^<]*)(<[^>]*>?)?/);if(!t)return[e,void 0];const n=t[1]?t[1]:t[2];return null==n?[e,n]:[e.substring(n.length),n]}function shouldAdd(e,t){return!NEEDS_PARENT[t.localName]||NEEDS_PARENT[t.localName]===e.localName}function createHtmlNode(e,t){const n=TAG_NAME[e];if(!n)return;const o=window.document.createElement(n),r=TAG_ANNOTATION[e];return r&&t&&(o[r]=t.trim()),o}export function parseCueText(e,t,{preferLineLength:n=0}={}){let o,r,l,s=e,c=-1;const a=[],u=[],i=[],d=e=>{0===u.length&&u.push([]),u.at(-1).push(t?t(e,(u.at(-1)??u.at(-2))?.at(-1)):e)},p=e=>{const t=a.pop()??"",n=o;if(o=n.parentElement||void 0,null==o){d({id:e,startTime:c>=0?c:void 0,tag:t,node:n}),r=n}},m=()=>`${u.length}-${(u.at(-1)?.length??0)+1}`,h=(e,t)=>{t?i.push(e):i[i.length-1]+=e,t&&u.push([]),null==o?0===e.trim().length&&!t&&r?r.appendChild(window.document.createTextNode(unescape(e))):(e=>{const t=createHtmlNode("c","");t.appendChild(window.document.createTextNode(unescape(e)));const n={id:m(),node:t,tag:"c",startTime:c>=0?c:void 0};d(n)})(e):o.appendChild(window.document.createTextNode(unescape(e)))};for(;null!=([s,l]=nextToken(s),l);)if("<"===l[0])if("/"===l[1])a.at(-1)===l.substring(2).replace(">","")&&p(m());else{const e=parseTimeStamp(l.substring(1,l.length-1));if(e)c=e;else{const e=l.match(/^<([^.\s/0-9>]+)(\.[^\s\\>]+)?([^>\\]+)?(\\?)>?$/),t=e&&createHtmlNode(e[1],e[3]);!t||o&&!shouldAdd(o,t)||(e[2]&&(t.className=e[2].substring(1).replace("."," ")),a.push(e[1]),o?.appendChild(t),o=t)}}else if(0===i.length||n>0&&i.at(-1).length+l.length>n){const e=splitRows(l,n);for(let t=0;t<e.length;t+=1)h(e[t],!0)}else h(l,!1);return{segments:u,rawText:i}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-toolkit/web-utils",
3
- "version": "1.61.1",
3
+ "version": "1.62.0",
4
4
  "description": "Web utils",
5
5
  "author": "VZH",
6
6
  "license": "MIT",
@@ -1,2 +1,5 @@
1
+ import { getMediaSource } from '../media/getMediaSource';
2
+ type ManagedMediaSourceOption = Parameters<typeof getMediaSource>[0];
1
3
  /** Media Source Extensions */
2
- export declare function isMSESupported(): boolean;
4
+ export declare function isMSESupported(managedMediaSource?: ManagedMediaSourceOption): boolean;
5
+ export {};
@@ -1 +1 @@
1
- import{getMediaSource}from"../media/getMediaSource";function getSourceBuffer(){return window.SourceBuffer}export function isMSESupported(){if(!getMediaSource()?.isTypeSupported)return!1;const e=getSourceBuffer();return!e||e.prototype&&"function"==typeof e.prototype.appendBuffer&&"function"==typeof e.prototype.remove}
1
+ import{getMediaSource}from"../media/getMediaSource";import{getSourceBuffer}from"../media/getSourceBuffer";export function isMSESupported(e){if(!getMediaSource(e)?.isTypeSupported)return!1;const o=getSourceBuffer(e);return!o||o.prototype&&"function"==typeof o.prototype.appendBuffer&&"function"==typeof o.prototype.remove}