UNPKG

r2-navigator-js

Version:

Readium 2 'navigator' for NodeJS (TypeScript)

861 lines (858 loc) 33.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isDocCJK = exports.READIUM2_BASEURL_ID = void 0; exports.isDocVertical = isDocVertical; exports.isDocRTL = isDocRTL; exports.isPaginated = isPaginated; exports.readiumCSSSet = readiumCSSSet; exports.configureFixedLayout = configureFixedLayout; exports.ensureHead = ensureHead; exports.appendCSSInline = appendCSSInline; exports.appendCSS = appendCSS; exports.removeCSS = removeCSS; exports.removeAllCSS = removeAllCSS; exports.injectDefaultCSS = injectDefaultCSS; exports.injectReadPosCSS = injectReadPosCSS; exports.readiumCssTransformHtml = readiumCssTransformHtml; const debug_ = require("debug"); const dom_1 = require("./dom"); const readium_css_settings_1 = require("./readium-css-settings"); const sessions_1 = require("./sessions"); const styles_1 = require("./styles"); exports.READIUM2_BASEURL_ID = "r2_BASEURL_ID"; const IS_DEV = (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "dev"); const debug = debug_("r2:navigator#electron/common/readium-css-inject"); function isDEBUG_VISUALS(documant) { if (!IS_DEV) { return false; } if (documant.defaultView && documant.defaultView.READIUM2 && documant.defaultView.READIUM2.DEBUG_VISUALS) { return true; } return false; } function isDocVertical(documant) { if (!documant || !documant.documentElement) { return false; } return false; } function isDocRTL(documant) { if (!documant || !documant.documentElement) { return false; } let rtl = false; let foundDir = false; let dirAttr = documant.documentElement.getAttribute("dir"); if (dirAttr === "rtl") { foundDir = true; rtl = true; } if (!rtl && documant.body) { dirAttr = documant.body.getAttribute("dir"); if (dirAttr === "rtl") { foundDir = true; rtl = true; } } if (!rtl) { let langAttr = documant.documentElement.getAttribute("lang"); if (!langAttr) { langAttr = documant.documentElement.getAttribute("xml:lang"); } if (!langAttr) { langAttr = documant.documentElement.getAttributeNS("http://www.w3.org/XML/1998/", "lang"); } if (langAttr && (langAttr === "ar" || langAttr.startsWith("ar-") || langAttr === "he" || langAttr.startsWith("he-") || langAttr === "fa" || langAttr.startsWith("fa-"))) { rtl = true; } } if (rtl) { if (!foundDir) { documant.documentElement.setAttribute("dir", "rtl"); } } return rtl; } const isDocJapanese = (documant) => { let isJA = false; let langAttr = documant.documentElement.getAttribute("lang"); if (!langAttr) { langAttr = documant.documentElement.getAttribute("xml:lang"); } if (!langAttr) { langAttr = documant.documentElement.getAttributeNS("http://www.w3.org/XML/1998/", "lang"); } if (langAttr && (langAttr === "ja" || langAttr.startsWith("ja-"))) { isJA = true; } return isJA; }; const isDocCJK = (documant) => { let isCJK = false; let langAttr = documant.documentElement.getAttribute("lang"); if (!langAttr) { langAttr = documant.documentElement.getAttribute("xml:lang"); } if (!langAttr) { langAttr = documant.documentElement.getAttributeNS("http://www.w3.org/XML/1998/", "lang"); } if (langAttr && (langAttr === "ja" || langAttr.startsWith("ja-") || langAttr === "zh" || langAttr.startsWith("zh-") || langAttr === "ko" || langAttr.startsWith("ko-"))) { isCJK = true; } return isCJK; }; exports.isDocCJK = isDocCJK; function isPaginated(documant) { return documant && documant.documentElement && documant.documentElement.classList.contains(styles_1.CLASS_PAGINATED); } function readiumCSSSet(documant, messageJson, isVerticalWritingMode, isRTL) { var _a; if (!messageJson) { return; } if (!documant || !documant.documentElement) { return; } if (!messageJson.urlRoot) { const baseEl = documant.getElementById(exports.READIUM2_BASEURL_ID); if (baseEl) { const baseUrl = baseEl.getAttribute("href"); if (baseUrl) { let u = baseUrl; if (baseUrl.startsWith(sessions_1.READIUM2_ELECTRON_HTTP_PROTOCOL + "://")) { u = (0, sessions_1.convertCustomSchemeToHttpUrl)(baseUrl); } u = u.replace(/\/pub\/.*/, ""); messageJson.urlRoot = u; } } } if (!messageJson.urlRoot) { const elBefore = documant.getElementById("ReadiumCSS-before"); if (elBefore) { const elHref = elBefore.getAttribute("href"); if (elHref) { const iHref = elHref.indexOf("/" + readium_css_settings_1.READIUM_CSS_URL_PATH); if (iHref >= 0) { messageJson.urlRoot = elHref.substring(0, iHref); } } } } if (IS_DEV) { debug("_____ readiumCssJson.urlRoot (readiumCSSSet()): ", messageJson.urlRoot); } const docElement = documant.documentElement; if (messageJson.isFixedLayout) { docElement.classList.add(styles_1.ROOT_CLASS_FIXED_LAYOUT); return; } const setCSS = messageJson.setCSS; if (!setCSS) { docElement.classList.remove(styles_1.ROOT_CLASS_NO_FOOTNOTES); docElement.classList.remove(styles_1.ROOT_CLASS_NO_RUBY); docElement.removeAttribute("data-readiumcss"); removeAllCSS(documant); const toRemove = []; for (let i = 0; i < docElement.style.length; i++) { const item = docElement.style.item(i); if (item.indexOf("--USER__") === 0) { toRemove.push(item); } } toRemove.forEach((item) => { docElement.style.removeProperty(item); }); return; } if (isVerticalWritingMode) { docElement.classList.add(styles_1.CLASS_VWM); setCSS.paged = false; } if (docElement.hasAttribute("data-readiumcss")) { let reset = false; const isVWM = docElement.hasAttribute("data-rss-isVWM"); if (isVWM !== isVerticalWritingMode) { reset = true; if (isVWM) { docElement.removeAttribute("data-rss-isVWM"); } } const isR = docElement.hasAttribute("data-rss-isRTL"); if (isR !== isRTL) { reset = true; if (isR) { docElement.removeAttribute("data-rss-isRTL"); } } if (reset) { docElement.removeAttribute("data-readiumcss"); removeAllCSS(documant); } } if (!docElement.hasAttribute("data-readiumcss")) { docElement.setAttribute("data-readiumcss", "yes"); if (isVerticalWritingMode) { docElement.setAttribute("data-rss-isVWM", "true"); } if (isRTL) { docElement.setAttribute("data-rss-isRTL", "true"); } let needsDefaultCSS = true; if (documant.head && documant.head.childNodes && documant.head.childNodes.length) { for (let i = 0; i < documant.head.childNodes.length; i++) { const child = documant.head.childNodes[i]; if (child.nodeType === 1) { const element = child; if ((element.localName && element.localName.toLowerCase() === "style") || (element.getAttribute && (element.getAttribute("rel") === "stylesheet" || element.getAttribute("type") === "text/css" || (element.getAttribute("src") && /\.css$/i.test(element.getAttribute("src")))))) { needsDefaultCSS = false; break; } } } } if (needsDefaultCSS && documant.body) { const styleAttr = documant.body.getAttribute("style"); if (styleAttr) { needsDefaultCSS = false; } } const isCJK = (0, exports.isDocCJK)(documant); const custom = isVerticalWritingMode && isCJK ? "cjk-vertical/" : (isCJK ? "cjk-horizontal/" : (isRTL ? "rtl/" : "")); const urlRoot = messageJson.urlRoot + "/" + readium_css_settings_1.READIUM_CSS_URL_PATH + "/" + custom; appendCSS(documant, "before", urlRoot); if (needsDefaultCSS) { appendCSS(documant, "default", urlRoot); } appendCSS(documant, "after", urlRoot); } if (isDEBUG_VISUALS(documant)) { debug("---- setCSS -----"); debug(setCSS); debug("-----"); } if (setCSS.noRuby) { docElement.classList.add(styles_1.ROOT_CLASS_NO_RUBY); } else { docElement.classList.remove(styles_1.ROOT_CLASS_NO_RUBY); } if (setCSS.noFootnotes) { docElement.classList.add(styles_1.ROOT_CLASS_NO_FOOTNOTES); } else { docElement.classList.remove(styles_1.ROOT_CLASS_NO_FOOTNOTES); } if (setCSS.noTemporaryNavTargetOutline) { docElement.classList.add(styles_1.DISABLE_TEMPORARY_NAV_TARGET_OUTLINE_CLASS); } else { docElement.classList.remove(styles_1.DISABLE_TEMPORARY_NAV_TARGET_OUTLINE_CLASS); } if (setCSS.mathJax) { docElement.classList.add(styles_1.ROOT_CLASS_MATHJAX); } else { docElement.classList.remove(styles_1.ROOT_CLASS_MATHJAX); } if (setCSS.reduceMotion) { docElement.classList.add(styles_1.ROOT_CLASS_REDUCE_MOTION); } else { docElement.classList.remove(styles_1.ROOT_CLASS_REDUCE_MOTION); } const needsAdvanced = true; docElement.style.setProperty("--USER__advancedSettings", needsAdvanced ? "readium-advanced-on" : "readium-advanced-off"); if (typeof setCSS.darken === "undefined") { docElement.style.removeProperty("--USER__darkenFilter"); } else { docElement.style.setProperty("--USER__darkenFilter", setCSS.darken ? "readium-darken-on" : "readium-darken-off"); } if (typeof setCSS.invert === "undefined") { docElement.style.removeProperty("--USER__invertFilter"); } else { docElement.style.setProperty("--USER__invertFilter", setCSS.invert ? "readium-invert-on" : "readium-invert-off"); } docElement.style.setProperty("--USER__appearance", setCSS.sepia ? "readium-sepia-on" : (setCSS.night ? "readium-night-on" : "readium-default-on")); docElement.style.setProperty("--USER__view", setCSS.paged ? "readium-paged-on" : "readium-scroll-on"); if (setCSS.paged) { docElement.classList.add(styles_1.CLASS_PAGINATED); } else { docElement.classList.remove(styles_1.CLASS_PAGINATED); } const defaultPublisherFont = !setCSS.font || setCSS.font === "DEFAULT"; const a11yNormalize = ((typeof setCSS.a11yNormalize !== "undefined") ? (setCSS.a11yNormalize ? "readium-a11y-on" : "readium-a11y-off") : "readium-a11y-off"); const needsFontOverride = a11yNormalize === "readium-a11y-on" || !defaultPublisherFont; docElement.style.setProperty("--USER__fontOverride", needsFontOverride ? "readium-font-on" : "readium-font-off"); if (typeof setCSS.a11yNormalize === "undefined") { docElement.style.removeProperty("--USER__a11yNormalize"); } else { docElement.style.setProperty("--USER__a11yNormalize", a11yNormalize); } if (defaultPublisherFont) { docElement.style.removeProperty("--USER__fontFamily"); } else { const font = setCSS.font; let fontValue = ""; if (font === "DUO" || font === "IA Writer Duospace") { fontValue = "IA Writer Duospace"; } else if (font === "DYS" || font === "AccessibleDfa" || font === "AccessibleDfA") { fontValue = "AccessibleDfA"; } else if (font === "OLD" || font === "oldStyleTf") { fontValue = "var(--RS__oldStyleTf)"; } else if (font === "MODERN" || font === "modernTf") { fontValue = "var(--RS__modernTf)"; } else if (font === "SANS" || font === "sansTf") { fontValue = "var(--RS__sansTf)"; } else if (font === "HUMAN" || font === "humanistTf") { fontValue = "var(--RS__humanistTf)"; } else if (font === "MONO" || font === "monospaceTf") { fontValue = "var(--RS__monospaceTf)"; } else if (font === "JA" || font === "serif-ja") { fontValue = "var(--RS__serif-ja)"; } else if (font === "JA-SANS" || font === "sans-serif-ja") { fontValue = "var(--RS__sans-serif-ja)"; } else if (font === "JA-V" || font === "serif-ja-v") { fontValue = "var(--RS__serif-ja-v)"; } else if (font === "JA-V-SANS" || font === "sans-serif-ja-v") { fontValue = "var(--RS__sans-serif-ja-v)"; } else if (typeof font === "string") { fontValue = font; } if (fontValue) { docElement.style.setProperty("--USER__fontFamily", fontValue); } else { docElement.style.removeProperty("--USER__fontFamily"); } } const fontSizeTrimmed = (_a = setCSS.fontSize) === null || _a === void 0 ? void 0 : _a.trim(); if (fontSizeTrimmed && fontSizeTrimmed !== "0" && fontSizeTrimmed !== "100%") { docElement.style.setProperty("--USER__fontSize", fontSizeTrimmed); try { docElement.style.setProperty("--USER__fontXSizeX", `${fontSizeTrimmed.endsWith("%") ? (parseFloat(fontSizeTrimmed.replace("%", "")) / 100) : parseFloat(fontSizeTrimmed)}`); } catch (_e) { docElement.style.setProperty("--USER__fontXSizeX", "1.0"); } } else { docElement.style.removeProperty("--USER__fontSize"); docElement.style.setProperty("--USER__fontXSizeX", "1.0"); } if (setCSS.lineHeight && setCSS.lineHeight.trim() !== "0") { docElement.style.setProperty("--USER__lineHeight", setCSS.lineHeight); } else { docElement.style.removeProperty("--USER__lineHeight"); } if (setCSS.typeScale && setCSS.typeScale.trim() !== "0") { docElement.style.setProperty("--USER__typeScale", setCSS.typeScale); } else { docElement.style.removeProperty("--USER__typeScale"); } if (setCSS.paraSpacing && setCSS.paraSpacing.trim() !== "0") { docElement.style.setProperty("--USER__paraSpacing", setCSS.paraSpacing); } else { docElement.style.removeProperty("--USER__paraSpacing"); } const isCJK = (0, exports.isDocCJK)(documant); if (isVerticalWritingMode || (isRTL || isCJK)) { docElement.style.removeProperty("--USER__bodyHyphens"); if (isDocJapanese(documant)) { if (setCSS.wordSpacing && setCSS.wordSpacing.trim() !== "0") { docElement.style.setProperty("--USER__wordSpacing", setCSS.wordSpacing); } else { docElement.style.removeProperty("--USER__wordSpacing"); } if (setCSS.letterSpacing && setCSS.letterSpacing.trim() !== "0") { docElement.style.setProperty("--USER__letterSpacing", setCSS.letterSpacing); } else { docElement.style.removeProperty("--USER__letterSpacing"); } } else { docElement.style.removeProperty("--USER__wordSpacing"); docElement.style.removeProperty("--USER__letterSpacing"); } if (isVerticalWritingMode || isCJK) { if (isVerticalWritingMode) { docElement.style.setProperty("--USER__colCount", "1"); } docElement.style.removeProperty("--USER__paraIndent"); docElement.style.removeProperty("--USER__textAlign"); } else if (isRTL) { if (setCSS.ligatures) { docElement.style.setProperty("--USER__ligatures", setCSS.ligatures); } else { docElement.style.removeProperty("--USER__ligatures"); } } } else { if (setCSS.bodyHyphens) { docElement.style.setProperty("--USER__bodyHyphens", setCSS.bodyHyphens); } else { docElement.style.removeProperty("--USER__bodyHyphens"); } if (setCSS.wordSpacing && setCSS.wordSpacing.trim() !== "0") { docElement.style.setProperty("--USER__wordSpacing", setCSS.wordSpacing); } else { docElement.style.removeProperty("--USER__wordSpacing"); } if (setCSS.letterSpacing && setCSS.letterSpacing.trim() !== "0") { docElement.style.setProperty("--USER__letterSpacing", setCSS.letterSpacing); } else { docElement.style.removeProperty("--USER__letterSpacing"); } } if (!isVerticalWritingMode) { if (setCSS.colCount) { docElement.style.setProperty("--USER__colCount", setCSS.colCount); } else { docElement.style.removeProperty("--USER__colCount"); } if (setCSS.paraIndent && setCSS.paraIndent.trim() !== "0") { docElement.style.setProperty("--USER__paraIndent", setCSS.paraIndent); } else { docElement.style.removeProperty("--USER__paraIndent"); } if (setCSS.textAlign) { docElement.style.setProperty("--USER__textAlign", setCSS.textAlign); } else { docElement.style.removeProperty("--USER__textAlign"); } } else if (!isRTL) { docElement.style.removeProperty("--USER__ligatures"); } if (setCSS.pageMargins && setCSS.pageMargins.trim() !== "0") { docElement.style.setProperty("--USER__pageMargins", setCSS.pageMargins); } else { docElement.style.removeProperty("--USER__pageMargins"); } if (setCSS.backgroundColor) { docElement.style.setProperty(setCSS.sepia || setCSS.night ? "--RS__backgroundColor" : "--USER__backgroundColor", setCSS.backgroundColor); } else { docElement.style.removeProperty("--USER__backgroundColor"); docElement.style.removeProperty("--RS__backgroundColor"); } if (setCSS.textColor) { docElement.style.setProperty(setCSS.sepia || setCSS.night ? "--RS__textColor" : "--USER__textColor", setCSS.textColor); } else { docElement.style.removeProperty("--USER__textColor"); docElement.style.removeProperty("--RS__textColor"); } if (setCSS.selectionBackgroundColor) { docElement.style.setProperty("--RS__selectionBackgroundColor", setCSS.selectionBackgroundColor); } else { docElement.style.removeProperty("--RS__selectionBackgroundColor"); } if (setCSS.selectionTextColor) { docElement.style.setProperty("--RS__selectionTextColor", setCSS.selectionTextColor); } else { docElement.style.removeProperty("--RS__selectionTextColor"); } if (setCSS.linkColor) { docElement.style.setProperty("--RS__linkColor", setCSS.linkColor); } else { docElement.style.removeProperty("--RS__linkColor"); } if (setCSS.linkVisitedColor) { docElement.style.setProperty("--RS__visitedColor", setCSS.linkVisitedColor); } else { docElement.style.removeProperty("--RS__visitedColor"); } } function configureFixedLayout(documant, isFixedLayout, fxlViewportWidth, fxlViewportHeight, innerWidth, innerHeight, wvSlot, zoomPercent) { if (!documant || !documant.head || !documant.body) { return undefined; } debug("configureFixedLayout zoomPercent ", zoomPercent); let wh; let width = fxlViewportWidth; let height = fxlViewportHeight; if (!width || !height) { let metaViewport = null; if (documant.head.querySelector) { metaViewport = documant.head.querySelector("meta[name=viewport]"); } else { if (documant.head.childNodes && documant.head.childNodes.length) { for (let i = 0; i < documant.head.childNodes.length; i++) { const child = documant.head.childNodes[i]; if (child.nodeType === 1) { const element = child; if (element.localName && element.localName.toLowerCase() === "meta") { if (element.getAttribute("name") === "viewport") { metaViewport = element; break; } } } } } } if (!metaViewport) { if (isDEBUG_VISUALS(documant)) { debug("configureFixedLayout NO meta[name=viewport]"); } return undefined; } const attr = metaViewport.getAttribute("content"); if (!attr) { if (isDEBUG_VISUALS(documant)) { debug("configureFixedLayout NO meta[name=viewport && content]"); } return undefined; } const wMatch = attr.match(/\s*width\s*=\s*([0-9]+)(\.([0-9]+))?/); if (wMatch && wMatch.length >= 2) { if (wMatch.length >= 4) { try { width = parseFloat(wMatch[1] + "." + wMatch[3]); } catch (err) { debug(err); } } else { try { width = parseInt(wMatch[1], 10); } catch (err) { debug(err); } } } else { if (isDEBUG_VISUALS(documant)) { debug("configureFixedLayout NO meta[name=viewport && content WIDTH]"); } } const hMatch = attr.match(/\s*height\s*=\s*([0-9]+)(\.([0-9]+))?/); if (hMatch && hMatch.length >= 2) { if (hMatch.length >= 4) { try { height = parseFloat(hMatch[1] + "." + hMatch[3]); } catch (err) { debug(err); } } else { try { height = parseInt(hMatch[1], 10); } catch (err) { debug(err); } } } else { if (isDEBUG_VISUALS(documant)) { debug("configureFixedLayout NO meta[name=viewport && content HEIGHT]"); } } if (width && height) { if (isDEBUG_VISUALS(documant)) { debug("READIUM_FXL_VIEWPORT_WIDTH: " + width); debug("READIUM_FXL_VIEWPORT_HEIGHT: " + height); } wh = { height, scale: 1, tx: 0, ty: 0, width, }; } } else { wh = { height, scale: 1, tx: 0, ty: 0, width, }; } if (innerWidth && innerHeight && width && height && isFixedLayout && documant && documant.documentElement && documant.body) { documant.documentElement.classList.add(styles_1.ROOT_CLASS_FIXED_LAYOUT); documant.body.style.width = width + "px"; documant.body.style.height = height + "px"; if (isDEBUG_VISUALS(documant)) { debug("FXL width: " + width); debug("FXL height: " + height); } const visibleWidth = innerWidth; const visibleHeight = innerHeight; if (isDEBUG_VISUALS(documant)) { debug("FXL visible width: " + visibleWidth); debug("FXL visible height: " + visibleHeight); } const ratioX = visibleWidth / width; const ratioY = visibleHeight / height; const ratio = zoomPercent === 0 ? Math.min(ratioX, ratioY) : (zoomPercent / 100); const tx = (visibleWidth - (width * ratio)) * (wvSlot === styles_1.WebViewSlotEnum.center ? 0.5 : (wvSlot === styles_1.WebViewSlotEnum.right ? 0 : 1)); const ty = (visibleHeight - (height * ratio)) / 2; if (isDEBUG_VISUALS(documant)) { debug("FXL trans X: " + tx); debug("FXL trans Y: " + ty); debug("FXL scale XY: " + ratio); } if (wh) { wh.scale = ratio; wh.tx = tx; wh.ty = ty; } documant.documentElement.style.transformOrigin = "0 0"; documant.documentElement.style.transform = `scale(${ratio})`; } return wh; } function ensureHead(documant) { if (!documant || !documant.documentElement) { return; } const docElement = documant.documentElement; if (!documant.head) { const headElement = documant.createElement("head"); if (documant.body) { docElement.insertBefore(headElement, documant.body); } else { docElement.appendChild(headElement); } } } function appendCSSInline(documant, id, css) { ensureHead(documant); if (!documant || !documant.head) { return; } const idz = "Readium2-" + id; const s = documant.getElementById(idz); if (s) { return; } const styleElement = documant.createElement("style"); styleElement.setAttribute("id", idz); styleElement.setAttribute("type", "text/css"); styleElement.appendChild(documant.createTextNode(css)); documant.head.appendChild(styleElement); } function appendCSS(documant, mod, urlRoot) { ensureHead(documant); if (!documant || !documant.head) { return; } const idz = "ReadiumCSS-" + mod; const s = documant.getElementById(idz); if (s) { return; } const linkElement = documant.createElement("link"); linkElement.setAttribute("id", idz); linkElement.setAttribute("rel", "stylesheet"); linkElement.setAttribute("type", "text/css"); linkElement.setAttribute("href", urlRoot + "ReadiumCSS-" + mod + ".css"); let childElementCount = 0; let firstElementChild = null; if (typeof documant.head.childElementCount !== "undefined") { childElementCount = documant.head.childElementCount; firstElementChild = documant.head.firstElementChild; } else { if (documant.head && documant.head.childNodes && documant.head.childNodes.length) { for (let i = 0; i < documant.head.childNodes.length; i++) { const child = documant.head.childNodes[i]; if (child.nodeType === 1) { childElementCount++; if (!firstElementChild) { firstElementChild = child; } } } } } if (mod === "before" && childElementCount && firstElementChild) { documant.head.insertBefore(linkElement, firstElementChild); const styleElement = documant.createElement("style"); styleElement.setAttribute("id", idz + "-PATCH"); styleElement.setAttribute("type", "text/css"); styleElement.appendChild(documant.createTextNode(` audio[controls] { width: revert !important; height: revert !important; } /* exception for Japanese, ReadiumCSS normally recommends disabling CSS letter/word-spacing for CJK in general */ :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) h1, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) h2, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) h3, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) h4, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) h5, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) h6, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) p, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) li, :root[style*="readium-advanced-on"][style*="--USER__letterSpacing"]:lang(ja) div { letter-spacing: var(--USER__letterSpacing); font-variant: none; } :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) h1, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) h2, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) h3, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) h4, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) h5, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) h6, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) p, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) li, :root[style*="readium-advanced-on"][style*="--USER__wordSpacing"]:lang(ja) div { word-spacing: var(--USER__wordSpacing); } `)); documant.head.insertBefore(styleElement, firstElementChild); } else { documant.head.appendChild(linkElement); } } function removeCSS(documant, mod) { const linkElement = documant.getElementById("ReadiumCSS-" + mod); if (linkElement && linkElement.parentNode) { linkElement.parentNode.removeChild(linkElement); } const styleElement = documant.getElementById("ReadiumCSS-" + mod + "-PATCH"); if (styleElement && styleElement.parentNode) { styleElement.parentNode.removeChild(styleElement); } } function removeAllCSS(documant) { removeCSS(documant, "before"); removeCSS(documant, "after"); removeCSS(documant, "default"); } function injectDefaultCSS(documant) { appendCSSInline(documant, "electron-mo", styles_1.mediaOverlaysCssStyles); appendCSSInline(documant, "electron-tts", styles_1.ttsCssStyles); appendCSSInline(documant, "electron-footnotes", styles_1.footnotesCssStyles); appendCSSInline(documant, "electron-selection", styles_1.selectionCssStyles); appendCSSInline(documant, "electron-focus", styles_1.focusCssStyles); appendCSSInline(documant, "electron-target", styles_1.targetCssStyles); appendCSSInline(documant, "electron-scrollbars", styles_1.scrollBarCssStyles); appendCSSInline(documant, "electron-visibility-mask", styles_1.visibilityMaskCssStyles); appendCSSInline(documant, "electron-audiobook", styles_1.audioCssStyles); } function injectReadPosCSS(documant) { appendCSSInline(documant, "electron-readPos", styles_1.readPosCssStyles); } function readiumCssTransformHtml(htmlStr, readiumcssJson, mediaType) { const iHtmlStart = htmlStr.indexOf("<html"); if (iHtmlStart < 0) { return htmlStr; } const iBodyStart = htmlStr.indexOf("<body"); if (iBodyStart < 0) { return htmlStr; } const iBodyEnd = htmlStr.indexOf(">", iBodyStart); if (iBodyEnd <= 0) { return htmlStr; } const parseableChunk = htmlStr.substr(iHtmlStart, iBodyEnd - iHtmlStart + 1); const htmlStrToParse = `<?xml version="1.0" encoding="utf-8"?>${parseableChunk}TXT</body></html>`; const documantFromXmlDom = (0, dom_1.parseDOM)(htmlStrToParse, mediaType); documantFromXmlDom.documentElement.setAttribute("data-readiumcss-injected", "yes"); if (styles_1.ENABLE_VISIBILITY_MASK) { documantFromXmlDom.documentElement.classList.add(styles_1.ROOT_CLASS_INVISIBLE_MASK); documantFromXmlDom.documentElement.classList.remove(styles_1.ROOT_CLASS_INVISIBLE_MASK_REMOVED); } const rtl = isDocRTL(documantFromXmlDom); const vertical = isDocVertical(documantFromXmlDom); if (readiumcssJson) { if (IS_DEV) { debug("_____ readiumCssJson.urlRoot (readiumCssTransformHtml()): ", readiumcssJson.urlRoot); } readiumCSSSet(documantFromXmlDom, readiumcssJson, vertical, rtl); } injectDefaultCSS(documantFromXmlDom); if (IS_DEV) { injectReadPosCSS(documantFromXmlDom); } const serialized = (0, dom_1.serializeDOM)(documantFromXmlDom); const prefix = htmlStr.substr(0, iHtmlStart); const suffix = htmlStr.substr(iBodyEnd + 1); const iHtmlStart_ = serialized.indexOf("<html"); if (iHtmlStart_ < 0) { return htmlStr; } const iBodyStart_ = serialized.indexOf("<body"); if (iBodyStart_ < 0) { return htmlStr; } const iBodyEnd_ = serialized.indexOf(">", iBodyStart_); if (iBodyEnd_ <= 0) { return htmlStr; } const middle = serialized.substr(iHtmlStart_, iBodyEnd_ - iHtmlStart_ + 1); const newStr = `${prefix}${middle}${suffix}`; return newStr; } //# sourceMappingURL=readium-css-inject.js.map