mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 23:51:20 -08:00
fix(multiple-subtitles): [PKG-MULTI-SUB-08] preserve entity text and semantic cue wrappers
This commit is contained in:
1 parent
4acf85ea69
commit
675b095b27
22 files changed
+744
-59
No files matched your search
@@ -799,19 +799,36 @@ var WebVTTSerializer = function() {
|
||||
return result;
|
||||
};
|
||||
};
|
||||
const cueEntities = {
|
||||
"&": "&",
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
">": ">",
|
||||
"&lrm": "",
|
||||
"‎": "",
|
||||
"&rlm": "",
|
||||
"‏": "",
|
||||
" ": " ",
|
||||
" ": " "
|
||||
};
|
||||
function escapeCueText(value) {
|
||||
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||
}
|
||||
const TIMESTAMP_CLASS = "artplayer-multiple-subtitles-timestamp";
|
||||
function markTimestamp(node) {
|
||||
return { type: "object", name: "c", classes: [TIMESTAMP_CLASS], children: [node] };
|
||||
}
|
||||
function markNestedTimestamps(node) {
|
||||
function prepareCueNode(node) {
|
||||
if (node.type === "timestamp")
|
||||
return markTimestamp(node);
|
||||
if (node.type === "object")
|
||||
return { ...node, children: node.children.map(markNestedTimestamps) };
|
||||
return node;
|
||||
return { ...node, children: node.children.map(prepareCueNode) };
|
||||
return { ...node, value: escapeCueText(node.value) };
|
||||
}
|
||||
function parseTracks(vtts, subtitles) {
|
||||
const parser = new WebVTTParser();
|
||||
const parser = new WebVTTParser(cueEntities);
|
||||
return vtts.map((vtt, index2) => {
|
||||
const tree = parser.parse(vtt, "metadata");
|
||||
return { ...tree, url: subtitles[index2].url, name: subtitles[index2].name };
|
||||
@@ -822,16 +839,21 @@ function serializeTracks(trees) {
|
||||
for (const selected of trees) {
|
||||
const tree = selected;
|
||||
for (const cue of tree.cues) {
|
||||
const children = [];
|
||||
for (const child of cue.tree.children) {
|
||||
if (child.type === "timestamp")
|
||||
children.push(markTimestamp(child));
|
||||
else children.push(
|
||||
{ type: "text", value: `<div class="art-subtitle-${tree.name}">` },
|
||||
prepareCueNode(child),
|
||||
{ type: "text", value: "</div>" }
|
||||
);
|
||||
}
|
||||
cues.push({
|
||||
...cue,
|
||||
tree: {
|
||||
...cue.tree,
|
||||
children: cue.tree.children.map((child) => {
|
||||
if (child.type === "timestamp")
|
||||
return markTimestamp(child);
|
||||
const value = `<div class="art-subtitle-${tree.name}">${child.value}</div>`;
|
||||
return child.type === "object" ? { ...child, value, children: child.children.map(markNestedTimestamps) } : { ...child, value };
|
||||
})
|
||||
children
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user