From 453710e6475dc19aeef727a123065baa9aed5658 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Thu, 17 Feb 2022 11:41:38 +0000 Subject: [PATCH 1/5] enable running in nodeJS without 'document' (cherry picked from commit 5fc26d89f1743483af3fce201e6d274cc38bc1ad) --- src/main/js/html.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/js/html.js b/src/main/js/html.js index e1425414..9ffddb04 100755 --- a/src/main/js/html.js +++ b/src/main/js/html.js @@ -1821,15 +1821,20 @@ } /* CSS property names */ + try { // try these, but if we're not in the browser, window does not exist. + var RUBYPOSITION_ISWK = "webkitRubyPosition" in window.getComputedStyle(document.documentElement); - var RUBYPOSITION_ISWK = "webkitRubyPosition" in window.getComputedStyle(document.documentElement); + var RUBYPOSITION_PROP = RUBYPOSITION_ISWK ? "webkitRubyPosition" : "rubyPosition"; - var RUBYPOSITION_PROP = RUBYPOSITION_ISWK ? "webkitRubyPosition" : "rubyPosition"; - - var TEXTEMPHASISSTYLE_PROP = "webkitTextEmphasisStyle" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisStyle" : "textEmphasisStyle"; - - var TEXTEMPHASISPOSITION_PROP = "webkitTextEmphasisPosition" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisPosition" : "textEmphasisPosition"; + var TEXTEMPHASISSTYLE_PROP = "webkitTextEmphasisStyle" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisStyle" : "textEmphasisStyle"; + var TEXTEMPHASISPOSITION_PROP = "webkitTextEmphasisPosition" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisPosition" : "textEmphasisPosition"; + } catch (e){ + var RUBYPOSITION_ISWK = false; + var RUBYPOSITION_PROP = "rubyPosition"; + var TEXTEMPHASISSTYLE_PROP = "textEmphasisStyle"; + var TEXTEMPHASISPOSITION_PROP = "textEmphasisPosition"; + } /* error utilities */ function reportError(errorHandler, msg) { From c1f1df115bb55edaaa0f4db375b409734f247492 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Thu, 17 Feb 2022 11:42:47 +0000 Subject: [PATCH 2/5] enable external DOM for HTML render (cherry picked from commit c6a80fbbf88a33a6cdc4f2066b9aefaf0349d20b) --- src/main/js/html.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/js/html.js b/src/main/js/html.js index 9ffddb04..21d89a15 100755 --- a/src/main/js/html.js +++ b/src/main/js/html.js @@ -78,9 +78,20 @@ displayForcedOnlyMode, errorHandler, previousISDState, - enableRollUp + enableRollUp, + dom_document ) { + // if alternative dom document functions wanted, use it + if (dom_document){ + imscHTML.document = dom_document; + imscHTML.Node = dom_document.Node; + } else { + imscHTML.document = document; + imscHTML.Node = Node; + } + var document = imscHTML.document; + /* maintain aspect ratio if specified */ var height = eheight || element.clientHeight; @@ -155,6 +166,7 @@ function processElement(context, dom_parent, isd_element, isd_parent) { var e; + var document = imscHTML.document; if (isd_element.kind === 'region') { @@ -762,6 +774,7 @@ } function applyMultiRowAlign(lineList) { + var document = imscHTML.document; /* apply an explicit br to all but the last line */ @@ -871,6 +884,7 @@ } function applyRubyReserve(lineList, context) { + var document = imscHTML.document; for (var i = 0; i < lineList.length; i++) { @@ -1102,6 +1116,8 @@ return; } + + var Node = imscHTML.Node; var curbgcolor = element.style.backgroundColor || bgcolor; From 56059b88dd4b8680c0247c2e7d3ecb573d46d65f Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Thu, 17 Feb 2022 11:43:28 +0000 Subject: [PATCH 3/5] expose styles through main.js - allow for external ISD processing. (cherry picked from commit 7cf9f1bbeded1df948f329e198b4d69207d080b1) --- src/main/js/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/js/main.js b/src/main/js/main.js index 14d07be9..0d6625a8 100644 --- a/src/main/js/main.js +++ b/src/main/js/main.js @@ -26,4 +26,5 @@ exports.generateISD = require('./isd').generateISD; exports.fromXML = require('./doc').fromXML; -exports.renderHTML = require('./html').render; \ No newline at end of file +exports.renderHTML = require('./html').render; +exports.styles = require('./styles'); From a172a2995c8b38fa9f1439b357d0aa98c6044c0a Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Thu, 17 Feb 2022 12:13:48 +0000 Subject: [PATCH 4/5] put document in context, as suggested, modify input param to be 'options', so to set a differend document, use { document: domprocessor } --- src/main/js/html.js | 104 +++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/src/main/js/html.js b/src/main/js/html.js index 21d89a15..5c3e93a9 100755 --- a/src/main/js/html.js +++ b/src/main/js/html.js @@ -67,6 +67,7 @@ * @param {?module:imscUtils.ErrorHandler} errorHandler Error callback * @param {Object} previousISDState State saved during processing of the previous ISD, or null if initial call * @param {?boolean} enableRollUp Enables roll-up animations (see CEA 708) + * @param {?Object} options Additional options, e.g. { document: jsdom.window.document } * @return {Object} ISD state to be provided when this funtion is called for the next ISD */ @@ -79,19 +80,9 @@ errorHandler, previousISDState, enableRollUp, - dom_document + options ) { - // if alternative dom document functions wanted, use it - if (dom_document){ - imscHTML.document = dom_document; - imscHTML.Node = dom_document.Node; - } else { - imscHTML.document = document; - imscHTML.Node = Node; - } - var document = imscHTML.document; - /* maintain aspect ratio if specified */ var height = eheight || element.clientHeight; @@ -113,17 +104,18 @@ } - var rootcontainer = document.createElement("div"); - - rootcontainer.style.position = "relative"; - rootcontainer.style.width = width + "px"; - rootcontainer.style.height = height + "px"; - rootcontainer.style.margin = "auto"; - rootcontainer.style.top = 0; - rootcontainer.style.bottom = 0; - rootcontainer.style.left = 0; - rootcontainer.style.right = 0; - rootcontainer.style.zIndex = 0; + options = options || {}; + + // if no alternative dom document, use browser's one + if (!options.document){ + try{ + options.document = document; + } catch (e) { + // no document object - + reportError(errorHandler, "Error no document object"); + return; + } + } var context = { h: height, @@ -131,6 +123,7 @@ regionH: null, regionW: null, imgResolver: imgResolver, + document: options.document, displayForcedOnlyMode: displayForcedOnlyMode || false, isd: isd, errorHandler: errorHandler, @@ -146,6 +139,18 @@ textEmphasis: null, /* is textEmphasis present in a

*/ rubyReserve: null /* is rubyReserve applicable to a

*/ }; + + var rootcontainer = context.document.createElement("div"); + + rootcontainer.style.position = "relative"; + rootcontainer.style.width = width + "px"; + rootcontainer.style.height = height + "px"; + rootcontainer.style.margin = "auto"; + rootcontainer.style.top = 0; + rootcontainer.style.bottom = 0; + rootcontainer.style.left = 0; + rootcontainer.style.right = 0; + rootcontainer.style.zIndex = 0; element.appendChild(rootcontainer); @@ -166,24 +171,23 @@ function processElement(context, dom_parent, isd_element, isd_parent) { var e; - var document = imscHTML.document; if (isd_element.kind === 'region') { - e = document.createElement("div"); + e = context.document.createElement("div"); e.style.position = "absolute"; } else if (isd_element.kind === 'body') { - e = document.createElement("div"); + e = context.document.createElement("div"); } else if (isd_element.kind === 'div') { - e = document.createElement("div"); + e = context.document.createElement("div"); } else if (isd_element.kind === 'image') { - e = document.createElement("img"); + e = context.document.createElement("img"); if (context.imgResolver !== null && isd_element.src !== null) { @@ -199,33 +203,33 @@ } else if (isd_element.kind === 'p') { - e = document.createElement("p"); + e = context.document.createElement("p"); } else if (isd_element.kind === 'span') { if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "container") { - e = document.createElement("ruby"); + e = context.document.createElement("ruby"); context.ruby = true; } else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "base") { - e = document.createElement("rb"); + e = context.document.createElement("rb"); } else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "text") { - e = document.createElement("rt"); + e = context.document.createElement("rt"); } else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "baseContainer") { - e = document.createElement("rbc"); + e = context.document.createElement("rbc"); } else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "textContainer") { - e = document.createElement("rtc"); + e = context.document.createElement("rtc"); } else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "delimiter") { @@ -236,7 +240,7 @@ } else { - e = document.createElement("span"); + e = context.document.createElement("span"); } @@ -244,7 +248,7 @@ } else if (isd_element.kind === 'br') { - e = document.createElement("br"); + e = context.document.createElement("br"); } @@ -368,7 +372,7 @@ /* create inline block to handle multirowAlign */ - var s = document.createElement("span"); + var s = context.document.createElement("span"); s.style.display = "inline-block"; @@ -436,7 +440,7 @@ /* wrap the character(s) in a span unless it is a high surrogate */ - var span = document.createElement("span"); + var span = context.document.createElement("span"); span.textContent = cbuf; @@ -519,7 +523,7 @@ if (context.mra) { - applyMultiRowAlign(linelist); + applyMultiRowAlign(linelist, context); context.mra = null; @@ -773,8 +777,7 @@ } - function applyMultiRowAlign(lineList) { - var document = imscHTML.document; + function applyMultiRowAlign(lineList, context) { /* apply an explicit br to all but the last line */ @@ -783,7 +786,7 @@ var l = lineList[i].elements.length; if (l !== 0 && lineList[i].br === false) { - var br = document.createElement("br"); + var br = context.document.createElement("br"); var lastnode = lineList[i].elements[l - 1].node; @@ -884,13 +887,12 @@ } function applyRubyReserve(lineList, context) { - var document = imscHTML.document; for (var i = 0; i < lineList.length; i++) { - var ruby = document.createElement("ruby"); + var ruby = context.document.createElement("ruby"); - var rb = document.createElement("rb"); + var rb = context.document.createElement("rb"); rb.textContent = "\u200B"; ruby.appendChild(rb); @@ -902,12 +904,12 @@ if (context.rubyReserve[0] === "both" || (context.rubyReserve[0] === "outside" && lineList.length == 1)) { - rt1 = document.createElement("rtc"); + rt1 = context.document.createElement("rtc"); rt1.style[RUBYPOSITION_PROP] = RUBYPOSITION_ISWK ? "after" : "under"; rt1.textContent = "\u200B"; rt1.style.fontSize = fs; - rt2 = document.createElement("rtc"); + rt2 = context.document.createElement("rtc"); rt2.style[RUBYPOSITION_PROP] = RUBYPOSITION_ISWK ? "before" : "over"; rt2.textContent = "\u200B"; rt2.style.fontSize = fs; @@ -917,7 +919,7 @@ } else { - rt1 = document.createElement("rtc"); + rt1 = context.document.createElement("rtc"); rt1.textContent = "\u200B"; rt1.style.fontSize = fs; @@ -1199,7 +1201,7 @@ while (child) { - if (child.nodeType === Node.ELEMENT_NODE) { + if (child.nodeType === 1) { /* was Node.ELEMENT_NODE - but only in browser */ constructLineList(context, child, llist, curbgcolor); @@ -1837,7 +1839,7 @@ } /* CSS property names */ - try { // try these, but if we're not in the browser, window does not exist. + try { var RUBYPOSITION_ISWK = "webkitRubyPosition" in window.getComputedStyle(document.documentElement); var RUBYPOSITION_PROP = RUBYPOSITION_ISWK ? "webkitRubyPosition" : "rubyPosition"; @@ -1846,9 +1848,13 @@ var TEXTEMPHASISPOSITION_PROP = "webkitTextEmphasisPosition" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisPosition" : "textEmphasisPosition"; } catch (e){ + // if no browser window/document, then default, not break; var RUBYPOSITION_ISWK = false; + var RUBYPOSITION_PROP = "rubyPosition"; + var TEXTEMPHASISSTYLE_PROP = "textEmphasisStyle"; + var TEXTEMPHASISPOSITION_PROP = "textEmphasisPosition"; } /* error utilities */ From 85a9e267bf97a6517ea2603e296cff825e556e08 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Thu, 17 Feb 2022 12:39:19 +0000 Subject: [PATCH 5/5] fix warnings --- src/main/js/html.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/main/js/html.js b/src/main/js/html.js index 5c3e93a9..5973a973 100755 --- a/src/main/js/html.js +++ b/src/main/js/html.js @@ -1838,24 +1838,21 @@ STYLMAP_BY_QNAME[STYLING_MAP_DEFS[i].qname] = STYLING_MAP_DEFS[i]; } - /* CSS property names */ + // if no browser window/document, then default, not break; + var RUBYPOSITION_ISWK = false; + var RUBYPOSITION_PROP = "rubyPosition"; + var TEXTEMPHASISSTYLE_PROP = "textEmphasisStyle"; + var TEXTEMPHASISPOSITION_PROP = "textEmphasisPosition"; + try { - var RUBYPOSITION_ISWK = "webkitRubyPosition" in window.getComputedStyle(document.documentElement); + RUBYPOSITION_ISWK = "webkitRubyPosition" in window.getComputedStyle(document.documentElement); - var RUBYPOSITION_PROP = RUBYPOSITION_ISWK ? "webkitRubyPosition" : "rubyPosition"; + RUBYPOSITION_PROP = RUBYPOSITION_ISWK ? "webkitRubyPosition" : "rubyPosition"; - var TEXTEMPHASISSTYLE_PROP = "webkitTextEmphasisStyle" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisStyle" : "textEmphasisStyle"; + TEXTEMPHASISSTYLE_PROP = "webkitTextEmphasisStyle" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisStyle" : "textEmphasisStyle"; - var TEXTEMPHASISPOSITION_PROP = "webkitTextEmphasisPosition" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisPosition" : "textEmphasisPosition"; + TEXTEMPHASISPOSITION_PROP = "webkitTextEmphasisPosition" in window.getComputedStyle(document.documentElement) ? "webkitTextEmphasisPosition" : "textEmphasisPosition"; } catch (e){ - // if no browser window/document, then default, not break; - var RUBYPOSITION_ISWK = false; - - var RUBYPOSITION_PROP = "rubyPosition"; - - var TEXTEMPHASISSTYLE_PROP = "textEmphasisStyle"; - - var TEXTEMPHASISPOSITION_PROP = "textEmphasisPosition"; } /* error utilities */