From b8c2171c441b61c602eba392926301d7bc616399 Mon Sep 17 00:00:00 2001 From: Casey Occhialini <1508707+littlespex@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:13:48 -0700 Subject: [PATCH 01/21] fix: convert to ES6 modules --- src/main/js/doc.js | 1776 +++++++++++++++++---------------- src/main/js/error.js | 27 + src/main/js/html.js | 2215 ++++++++++++++++++++--------------------- src/main/js/isd.js | 948 +++++++++--------- src/main/js/main.js | 6 +- src/main/js/names.js | 25 +- src/main/js/styles.js | 1851 +++++++++++++++++----------------- src/main/js/utils.js | 505 +++++----- 8 files changed, 3662 insertions(+), 3691 deletions(-) create mode 100644 src/main/js/error.js diff --git a/src/main/js/doc.js b/src/main/js/doc.js index d38f4ce0..d2c76270 100644 --- a/src/main/js/doc.js +++ b/src/main/js/doc.js @@ -24,812 +24,818 @@ * POSSIBILITY OF SUCH DAMAGE. */ +import sax from 'sax'; +import { reportError, reportFatal, reportWarning } from './error.js'; +import { ns_ebutts, ns_ittp, ns_itts, ns_tt, ns_ttp } from './names.js'; +import { byName, byQName } from './styles.js'; +import { ComputedLength, parseLength } from './utils.js'; + /** * @module imscDoc */ -; -(function (imscDoc, sax, imscNames, imscStyles, imscUtils) { - - /** - * Allows a client to provide callbacks to handle children of the element - * @typedef {Object} MetadataHandler - * @property {?OpenTagCallBack} onOpenTag - * @property {?CloseTagCallBack} onCloseTag - * @property {?TextCallBack} onText - */ +/** + * Allows a client to provide callbacks to handle children of the element + * @typedef {Object} MetadataHandler + * @property {?OpenTagCallBack} onOpenTag + * @property {?CloseTagCallBack} onCloseTag + * @property {?TextCallBack} onText + */ - /** - * Called when the opening tag of an element node is encountered. - * @callback OpenTagCallBack - * @param {string} ns Namespace URI of the element - * @param {string} name Local name of the element - * @param {Object[]} attributes List of attributes, each consisting of a - * `uri`, `name` and `value` - */ +/** + * Called when the opening tag of an element node is encountered. + * @callback OpenTagCallBack + * @param {string} ns Namespace URI of the element + * @param {string} name Local name of the element + * @param {Object[]} attributes List of attributes, each consisting of a + * `uri`, `name` and `value` + */ - /** - * Called when the closing tag of an element node is encountered. - * @callback CloseTagCallBack - */ +/** + * Called when the closing tag of an element node is encountered. + * @callback CloseTagCallBack + */ - /** - * Called when a text node is encountered. - * @callback TextCallBack - * @param {string} contents Contents of the text node - */ +/** + * Called when a text node is encountered. + * @callback TextCallBack + * @param {string} contents Contents of the text node + */ - /** - * Parses an IMSC1 document into an opaque in-memory representation that exposes - * a single method
getMediaTimeEvents()
that returns a list of time - * offsets (in seconds) of the ISD, i.e. the points in time where the visual - * representation of the document change. `metadataHandler` allows the caller to - * be called back when nodes are present in elements. - * - * @param {string} xmlstring XML document - * @param {?module:imscUtils.ErrorHandler} errorHandler Error callback - * @param {?MetadataHandler} metadataHandler Callback for elements - * @returns {Object} Opaque in-memory representation of an IMSC1 document - */ +/** + * Parses an IMSC1 document into an opaque in-memory representation that exposes + * a single method
getMediaTimeEvents()
that returns a list of time + * offsets (in seconds) of the ISD, i.e. the points in time where the visual + * representation of the document change. `metadataHandler` allows the caller to + * be called back when nodes are present in elements. + * + * @param {string} xmlstring XML document + * @param {?module:imscUtils.ErrorHandler} errorHandler Error callback + * @param {?MetadataHandler} metadataHandler Callback for elements + * @returns {Object} Opaque in-memory representation of an IMSC1 document + */ - imscDoc.fromXML = function (xmlstring, errorHandler, metadataHandler) { - var p = sax.parser(true, {xmlns: true}); - var estack = []; - var xmllangstack = []; - var xmlspacestack = []; - var metadata_depth = 0; - var doc = null; +export function fromXM(xmlstring, errorHandler, metadataHandler) { + var p = sax.parser(true, { xmlns: true }); + var estack = []; + var xmllangstack = []; + var xmlspacestack = []; + var metadata_depth = 0; + var doc = null; - p.onclosetag = function (node) { + p.onclosetag = function (node) { - - if (estack[0] instanceof Region) { - /* merge referenced styles */ + if (estack[0] instanceof Region) { - if (doc.head !== null && doc.head.styling !== null) { - mergeReferencedStyles(doc.head.styling, estack[0].styleRefs, estack[0].styleAttrs, errorHandler); - } + /* merge referenced styles */ - delete estack[0].styleRefs; + if (doc.head !== null && doc.head.styling !== null) { + mergeReferencedStyles(doc.head.styling, estack[0].styleRefs, estack[0].styleAttrs, errorHandler); + } - } else if (estack[0] instanceof Styling) { + delete estack[0].styleRefs; - /* flatten chained referential styling */ + } else if (estack[0] instanceof Styling) { - for (var sid in estack[0].styles) { + /* flatten chained referential styling */ - if (! estack[0].styles.hasOwnProperty(sid)) continue; + for (var sid in estack[0].styles) { - mergeChainedStyles(estack[0], estack[0].styles[sid], errorHandler); + if (!estack[0].styles.hasOwnProperty(sid)) continue; - } + mergeChainedStyles(estack[0], estack[0].styles[sid], errorHandler); - } else if (estack[0] instanceof P || estack[0] instanceof Span) { + } - /* merge anonymous spans */ + } else if (estack[0] instanceof P || estack[0] instanceof Span) { - if (estack[0].contents.length > 1) { + /* merge anonymous spans */ - var cs = [estack[0].contents[0]]; + if (estack[0].contents.length > 1) { - var c; + var cs = [estack[0].contents[0]]; - for (c = 1; c < estack[0].contents.length; c++) { + var c; - if (estack[0].contents[c] instanceof AnonymousSpan && - cs[cs.length - 1] instanceof AnonymousSpan) { + for (c = 1; c < estack[0].contents.length; c++) { - cs[cs.length - 1].text += estack[0].contents[c].text; + if (estack[0].contents[c] instanceof AnonymousSpan && + cs[cs.length - 1] instanceof AnonymousSpan) { - } else { + cs[cs.length - 1].text += estack[0].contents[c].text; - cs.push(estack[0].contents[c]); + } else { - } + cs.push(estack[0].contents[c]); } - estack[0].contents = cs; - } - // remove redundant nested anonymous spans (9.3.3(1)(c)) + estack[0].contents = cs; - if (estack[0] instanceof Span && - estack[0].contents.length === 1 && - estack[0].contents[0] instanceof AnonymousSpan) { + } - estack[0].text = estack[0].contents[0].text; - delete estack[0].contents; + // remove redundant nested anonymous spans (9.3.3(1)(c)) - } + if (estack[0] instanceof Span && + estack[0].contents.length === 1 && + estack[0].contents[0] instanceof AnonymousSpan) { - } else if (estack[0] instanceof ForeignElement) { + estack[0].text = estack[0].contents[0].text; + delete estack[0].contents; - if (estack[0].node.uri === imscNames.ns_tt && - estack[0].node.local === 'metadata') { + } - /* leave the metadata element */ + } else if (estack[0] instanceof ForeignElement) { - metadata_depth--; + if (estack[0].node.uri === ns_tt && + estack[0].node.local === 'metadata') { - } else if (metadata_depth > 0 && - metadataHandler && - 'onCloseTag' in metadataHandler) { + /* leave the metadata element */ - /* end of child of metadata element */ + metadata_depth--; - metadataHandler.onCloseTag(); + } else if (metadata_depth > 0 && + metadataHandler && + 'onCloseTag' in metadataHandler) { - } + /* end of child of metadata element */ + + metadataHandler.onCloseTag(); } - // TODO: delete stylerefs? + } - // maintain the xml:space stack + // TODO: delete stylerefs? - xmlspacestack.shift(); + // maintain the xml:space stack - // maintain the xml:lang stack + xmlspacestack.shift(); - xmllangstack.shift(); + // maintain the xml:lang stack - // prepare for the next element + xmllangstack.shift(); - estack.shift(); - }; + // prepare for the next element - p.ontext = function (str) { + estack.shift(); + }; - if (estack[0] === undefined) { + p.ontext = function (str) { - /* ignoring text outside of elements */ + if (estack[0] === undefined) { - } else if (estack[0] instanceof Span || estack[0] instanceof P) { + /* ignoring text outside of elements */ - /* ignore children text nodes in ruby container spans */ + } else if (estack[0] instanceof Span || estack[0] instanceof P) { - if (estack[0] instanceof Span) { + /* ignore children text nodes in ruby container spans */ - var ruby = estack[0].styleAttrs[imscStyles.byName.ruby.qname]; + if (estack[0] instanceof Span) { - if (ruby === 'container' || ruby === 'textContainer' || ruby === 'baseContainer') { + var ruby = estack[0].styleAttrs[byName.ruby.qname]; - return; + if (ruby === 'container' || ruby === 'textContainer' || ruby === 'baseContainer') { - } + return; } - /* create an anonymous span */ + } - var s = new AnonymousSpan(); + /* create an anonymous span */ - s.initFromText(doc, estack[0], str, xmllangstack[0], xmlspacestack[0], errorHandler); + var s = new AnonymousSpan(); - estack[0].contents.push(s); + s.initFromText(doc, estack[0], str, xmllangstack[0], xmlspacestack[0], errorHandler); - } else if (estack[0] instanceof ForeignElement && - metadata_depth > 0 && - metadataHandler && - 'onText' in metadataHandler) { + estack[0].contents.push(s); - /* text node within a child of metadata element */ + } else if (estack[0] instanceof ForeignElement && + metadata_depth > 0 && + metadataHandler && + 'onText' in metadataHandler) { - metadataHandler.onText(str); + /* text node within a child of metadata element */ - } + metadataHandler.onText(str); - }; + } + }; - p.onopentag = function (node) { - // maintain the xml:space stack + p.onopentag = function (node) { - var xmlspace = node.attributes["xml:space"]; + // maintain the xml:space stack - if (xmlspace) { + var xmlspace = node.attributes["xml:space"]; - xmlspacestack.unshift(xmlspace.value); + if (xmlspace) { - } else { + xmlspacestack.unshift(xmlspace.value); - if (xmlspacestack.length === 0) { + } else { - xmlspacestack.unshift("default"); + if (xmlspacestack.length === 0) { - } else { + xmlspacestack.unshift("default"); - xmlspacestack.unshift(xmlspacestack[0]); + } else { - } + xmlspacestack.unshift(xmlspacestack[0]); } - /* maintain the xml:lang stack */ + } + /* maintain the xml:lang stack */ - var xmllang = node.attributes["xml:lang"]; - if (xmllang) { + var xmllang = node.attributes["xml:lang"]; - xmllangstack.unshift(xmllang.value); + if (xmllang) { - } else { + xmllangstack.unshift(xmllang.value); - if (xmllangstack.length === 0) { + } else { - xmllangstack.unshift(""); + if (xmllangstack.length === 0) { - } else { + xmllangstack.unshift(""); - xmllangstack.unshift(xmllangstack[0]); + } else { - } + xmllangstack.unshift(xmllangstack[0]); } + } - /* process the element */ - if (node.uri === imscNames.ns_tt) { + /* process the element */ - if (node.local === 'tt') { + if (node.uri === ns_tt) { - if (doc !== null) { + if (node.local === 'tt') { - reportFatal(errorHandler, "Two elements at (" + this.line + "," + this.column + ")"); + if (doc !== null) { - } + reportFatal(errorHandler, "Two elements at (" + this.line + "," + this.column + ")"); - doc = new TT(); + } - doc.initFromNode(node, xmllangstack[0], errorHandler); + doc = new TT(); - estack.unshift(doc); + doc.initFromNode(node, xmllangstack[0], errorHandler); - } else if (node.local === 'head') { + estack.unshift(doc); - if (!(estack[0] instanceof TT)) { - reportFatal(errorHandler, "Parent of element is not at (" + this.line + "," + this.column + ")"); - } + } else if (node.local === 'head') { - estack.unshift(doc.head); + if (!(estack[0] instanceof TT)) { + reportFatal(errorHandler, "Parent of element is not at (" + this.line + "," + this.column + ")"); + } - } else if (node.local === 'styling') { + estack.unshift(doc.head); - if (!(estack[0] instanceof Head)) { - reportFatal(errorHandler, "Parent of element is not at (" + this.line + "," + this.column + ")"); - } + } else if (node.local === 'styling') { - estack.unshift(doc.head.styling); + if (!(estack[0] instanceof Head)) { + reportFatal(errorHandler, "Parent of element is not at (" + this.line + "," + this.column + ")"); + } - } else if (node.local === 'style') { + estack.unshift(doc.head.styling); - var s; + } else if (node.local === 'style') { - if (estack[0] instanceof Styling) { + var s; - s = new Style(); + if (estack[0] instanceof Styling) { - s.initFromNode(node, errorHandler); + s = new Style(); - /* ignore