UNPKG

@awesome-fe/translate

Version:
100 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Injector = void 0; var fs_1 = require("fs"); var dom_models_1 = require("../tiny-dom/dom-models"); var Injector = /** @class */ (function () { function Injector(styleUrls, scriptUrls, urlMap, textMap) { this.styleUrls = styleUrls; this.scriptUrls = scriptUrls; this.urlMap = urlMap; this.textMap = textMap; } Injector.prototype.injectFile = function (filename) { var content = fs_1.readFileSync(filename, 'utf8'); var result = this.inject(content); fs_1.writeFileSync(filename, result, 'utf8'); }; Injector.prototype.inject = function (content) { var doc = dom_models_1.DomDocument.parse(content); // 对于文档片段,不需要注入 if (doc.isFragment()) { return content; } this.injectTranslationKitToDoc(doc); return replaceText(doc.toHtml(), this.textMap); }; Injector.prototype.injectTranslationKitToDoc = function (doc) { this.injectTranslators(doc); this.replaceResourceUrls(doc); }; Injector.prototype.injectTranslators = function (doc) { this.styleUrls.forEach(function (styleUrl) { if (styleSheetExists(styleSheetsOf(doc), styleUrl)) { return; } var link = new dom_models_1.DomElement('link', [{ name: 'href', value: styleUrl }, { name: 'rel', value: 'stylesheet' }]); doc.head.appendChild(link); }); this.scriptUrls.forEach(function (scriptUrl) { if (scriptExists(scriptsOf(doc), scriptUrl)) { return; } var script = new dom_models_1.DomElement('script', [{ name: 'src', value: scriptUrl }]); doc.body.appendChild(script); }); }; Injector.prototype.replaceResourceUrls = function (doc) { var _this = this; styleSheetsOf(doc).forEach(function (styleSheet) { var newValue = _this.urlMap[styleSheet.getAttribute('href')]; if (newValue) { styleSheet.setAttribute('href', newValue); } }); scriptsOf(doc).forEach(function (script) { var newValue = _this.urlMap[script.getAttribute('src')]; if (newValue) { script.setAttribute('src', newValue); } }); doc.querySelectorAll(function (it) { return it.isTagOf('img') && it.hasAttribute('src'); }).forEach(function (image) { var newValue = _this.urlMap[image.getAttribute('src')]; if (newValue) { image.setAttribute('src', newValue); } }); }; return Injector; }()); exports.Injector = Injector; function replaceText(text, textMap) { Object.entries(textMap).forEach(function (_a) { var key = _a[0], value = _a[1]; text = text.replace(new RegExp(key, 'gi'), value); }); return text; } function styleSheetExists(styleSheets, styleSheetUrl) { for (var i = 0; i < styleSheets.length; ++i) { if (styleSheets[i].getAttribute('href') === styleSheetUrl) { return true; } } return false; } function scriptExists(scripts, scriptUrl) { for (var i = 0; i < scripts.length; ++i) { if (scripts[i].getAttribute('src') === scriptUrl) { return true; } } return false; } function styleSheetsOf(doc) { return doc.querySelectorAll(function (it) { return it.isTagOf('link') && it.getAttribute('rel') === 'stylesheet'; }); } function scriptsOf(doc) { return doc.querySelectorAll(function (it) { return it.isTagOf('script') && it.hasAttribute('src'); }); } //# sourceMappingURL=injector.js.map