@awesome-fe/translate
Version:
Translation utils
448 lines • 16.5 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultSelectors = exports.DomTableRowElement = exports.DomTableCellElement = exports.DomTableElement = exports.DomText = exports.DomElement = exports.DomDocumentType = exports.DomDocumentFragment = exports.DomDocument = exports.DomComment = exports.DomParentNode = exports.DomChildNode = exports.DomAttr = exports.DomNode = void 0;
var parse5_1 = require("parse5");
var dom_tree_adapter_1 = require("./dom-tree-adapter");
var DomNode = /** @class */ (function () {
function DomNode() {
this.childNodes = [];
}
Object.defineProperty(DomNode.prototype, "index", {
get: function () {
var _a;
if (!(this instanceof DomChildNode)) {
return -1;
}
return (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.childNodes.indexOf(this);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomNode.prototype, "indexOfElement", {
get: function () {
var _a;
if (!(this instanceof DomElement)) {
return -1;
}
return (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.children.indexOf(this);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomNode.prototype, "firstChild", {
get: function () {
var _a;
return (_a = this.childNodes) === null || _a === void 0 ? void 0 : _a[0];
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomNode.prototype, "textContent", {
get: function () {
var result = '';
if (this instanceof DomText) {
result += this.value;
}
else if (this instanceof DomParentNode) {
result += this.childNodes.map(function (it) { return it.textContent; }).join('');
}
return result;
},
set: function (value) {
this.childNodes = [new DomText(value)];
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomNode.prototype, "parentElement", {
get: function () {
var parent = this.parentNode;
while (parent) {
if (parent instanceof DomElement) {
return parent;
}
parent = parent.parentNode;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomNode.prototype, "previousElementSibling", {
get: function () {
var node = this.previousSibling();
while (node) {
if (node instanceof DomElement) {
return node;
}
node = node.previousSibling();
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomNode.prototype, "nextElementSibling", {
get: function () {
var node = this.nextSibling();
while (node) {
if (node instanceof DomElement) {
return node;
}
node = node.nextSibling();
}
},
enumerable: false,
configurable: true
});
DomNode.prototype.previousSibling = function () {
var _a;
if (this.index === -1) {
return;
}
return (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.childNodes[this.index - 1];
};
DomNode.prototype.nextSibling = function () {
var _a;
if (this.index === -1) {
return;
}
return (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.childNodes[this.index + 1];
};
DomNode.prototype.appendChild = function (child) {
if (this instanceof DomParentNode) {
child.parentNode = this;
this.childNodes.push(child);
}
};
DomNode.prototype.insertBefore = function (newNode, referenceNode) {
newNode.remove();
this.childNodes.splice(referenceNode.index, 0, newNode);
};
DomNode.treeAdapter = dom_tree_adapter_1.DomTreeAdapter.create();
return DomNode;
}());
exports.DomNode = DomNode;
var DomAttr = /** @class */ (function () {
function DomAttr() {
}
return DomAttr;
}());
exports.DomAttr = DomAttr;
var DomChildNode = /** @class */ (function (_super) {
__extends(DomChildNode, _super);
function DomChildNode() {
return _super !== null && _super.apply(this, arguments) || this;
}
DomChildNode.prototype.remove = function () {
if (!this.parentNode) {
return;
}
this.parentNode.childNodes.splice(this.index, 1);
};
return DomChildNode;
}(DomNode));
exports.DomChildNode = DomChildNode;
var DomParentNode = /** @class */ (function (_super) {
__extends(DomParentNode, _super);
function DomParentNode() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(DomParentNode.prototype, "children", {
get: function () {
return this.childNodes.filter(function (it) { return it instanceof DomElement; });
},
enumerable: false,
configurable: true
});
DomParentNode.prototype.prepend = function (node) {
this.childNodes.unshift(node);
};
DomParentNode.prototype.querySelectorAll = function (selector) {
var result = [];
for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
var child = _a[_i];
if (selector(child)) {
result.push(child);
}
result.push.apply(result, child.querySelectorAll(selector));
}
return result;
};
DomParentNode.prototype.querySelector = function (selector) {
for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
var child = _a[_i];
if (selector(child)) {
return child;
}
var subNode = child.querySelector(selector);
if (subNode) {
return subNode;
}
}
};
return DomParentNode;
}(DomChildNode));
exports.DomParentNode = DomParentNode;
var DomComment = /** @class */ (function (_super) {
__extends(DomComment, _super);
function DomComment(data) {
var _this = _super.call(this) || this;
_this.data = data;
_this.nodeName = '#comment';
return _this;
}
return DomComment;
}(DomChildNode));
exports.DomComment = DomComment;
var DomDocument = /** @class */ (function (_super) {
__extends(DomDocument, _super);
function DomDocument() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.nodeName = '#document';
return _this;
}
Object.defineProperty(DomDocument.prototype, "head", {
get: function () {
return this.querySelector(function (it) { return it.isTagOf('head'); });
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomDocument.prototype, "body", {
get: function () {
return this.querySelector(function (it) { return it.isTagOf('body'); });
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomDocument.prototype, "title", {
get: function () {
var _a;
return (_a = this.titleElement) === null || _a === void 0 ? void 0 : _a.textContent;
},
set: function (value) {
var element = this.titleElement;
if (element) {
element.textContent = value;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomDocument.prototype, "titleElement", {
get: function () {
return this.querySelector(function (it) { return it.isTagOf('title'); });
},
enumerable: false,
configurable: true
});
DomDocument.prototype.toHtml = function () {
// 如果是文档片段,则只序列化 body,否则序列化整个对象
return parse5_1.serialize(this.isFragment() ? this.body : this, { treeAdapter: DomNode.treeAdapter });
};
DomDocument.prototype.isFragment = function () {
// 目前看来,片段和非片段的主要差别是 mode 为 'quirks',并且head 中没有任何东西
return this.mode === 'quirks' && this.head.childNodes.length === 0;
};
DomDocument.parse = function (content) {
return parse5_1.parse(content, { treeAdapter: DomNode.treeAdapter });
};
return DomDocument;
}(DomParentNode));
exports.DomDocument = DomDocument;
var DomDocumentFragment = /** @class */ (function (_super) {
__extends(DomDocumentFragment, _super);
function DomDocumentFragment() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.nodeName = '#document-fragment';
return _this;
}
DomDocumentFragment.prototype.toHtml = function () {
return parse5_1.serialize(this, { treeAdapter: DomNode.treeAdapter });
};
DomDocumentFragment.parse = function (content) {
return parse5_1.parseFragment(content, { treeAdapter: DomNode.treeAdapter });
};
return DomDocumentFragment;
}(DomParentNode));
exports.DomDocumentFragment = DomDocumentFragment;
var DomDocumentType = /** @class */ (function (_super) {
__extends(DomDocumentType, _super);
function DomDocumentType(name, publicId, systemId) {
var _this = _super.call(this) || this;
_this.name = name;
_this.publicId = publicId;
_this.systemId = systemId;
_this.nodeName = '#documentType';
return _this;
}
return DomDocumentType;
}(DomChildNode));
exports.DomDocumentType = DomDocumentType;
function isSameName(name1, name2) {
return (name1 === null || name1 === void 0 ? void 0 : name1.localeCompare(name2)) === 0;
}
var DomElement = /** @class */ (function (_super) {
__extends(DomElement, _super);
function DomElement(tagName, attrs, namespaceURI) {
if (attrs === void 0) { attrs = []; }
if (namespaceURI === void 0) { namespaceURI = 'http://www.w3.org/1999/xhtml'; }
var _this = _super.call(this) || this;
_this.tagName = tagName;
_this.attrs = attrs;
_this.namespaceURI = namespaceURI;
_this.nodeName = tagName;
return _this;
}
Object.defineProperty(DomElement.prototype, "innerHTML", {
get: function () {
return parse5_1.serialize(this, { treeAdapter: DomNode.treeAdapter });
},
set: function (html) {
this.childNodes = parse5_1.parseFragment(html, { treeAdapter: DomNode.treeAdapter }).childNodes;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomElement.prototype, "outerHTML", {
get: function () {
var node = new DomElement('div');
node.childNodes = [this];
return node.innerHTML;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomElement.prototype, "className", {
get: function () {
var _a;
return (_a = this.getAttributeNode('class')) === null || _a === void 0 ? void 0 : _a.value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DomElement.prototype, "templateContent", {
get: function () {
var parent = this.parentNode;
while (parent) {
if (parent instanceof DomDocumentFragment) {
return parent;
}
parent = parent.parentNode;
}
},
enumerable: false,
configurable: true
});
DomElement.prototype.getAttributeNode = function (name) {
return this.attrs.find(function (it) { return isSameName(it.name, name); });
};
DomElement.prototype.getAttribute = function (name) {
var _a;
return (_a = this.getAttributeNode(name)) === null || _a === void 0 ? void 0 : _a.value;
};
DomElement.prototype.hasAttribute = function (name) {
return !!this.getAttributeNode(name);
};
DomElement.prototype.setAttribute = function (name, value) {
var attr = this.getAttributeNode(name);
if (!attr) {
this.attrs.push({ name: name, value: value });
}
else {
attr.value = value;
}
};
DomElement.prototype.setAttributes = function () {
var _a;
var domAttrs = [];
for (var _i = 0; _i < arguments.length; _i++) {
domAttrs[_i] = arguments[_i];
}
(_a = this.attrs).push.apply(_a, domAttrs);
};
DomElement.prototype.removeAttribute = function (name) {
var index = this.attrs.findIndex(function (it) { return isSameName(it.name, name); });
if (index !== -1) {
this.attrs.splice(index, 1);
}
};
DomElement.prototype.isSameTag = function (element) {
return this.isTagOf(element.tagName);
};
DomElement.prototype.isTagOf = function () {
var _this = this;
var names = [];
for (var _i = 0; _i < arguments.length; _i++) {
names[_i] = arguments[_i];
}
return !!names.find(function (name) { return isSameName(_this.tagName, name); });
};
return DomElement;
}(DomParentNode));
exports.DomElement = DomElement;
var DomText = /** @class */ (function (_super) {
__extends(DomText, _super);
function DomText(value) {
var _this = _super.call(this) || this;
_this.value = value;
_this.nodeName = '#text';
return _this;
}
return DomText;
}(DomChildNode));
exports.DomText = DomText;
var DomTableElement = /** @class */ (function (_super) {
__extends(DomTableElement, _super);
function DomTableElement() {
return _super !== null && _super.apply(this, arguments) || this;
}
return DomTableElement;
}(DomElement));
exports.DomTableElement = DomTableElement;
var DomTableCellElement = /** @class */ (function (_super) {
__extends(DomTableCellElement, _super);
function DomTableCellElement() {
return _super !== null && _super.apply(this, arguments) || this;
}
return DomTableCellElement;
}(DomElement));
exports.DomTableCellElement = DomTableCellElement;
var DomTableRowElement = /** @class */ (function (_super) {
__extends(DomTableRowElement, _super);
function DomTableRowElement() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(DomTableRowElement.prototype, "cells", {
get: function () {
return this.children.filter(function (it) { return it.isTagOf('td') || it.isTagOf('th'); });
},
enumerable: false,
configurable: true
});
return DomTableRowElement;
}(DomElement));
exports.DomTableRowElement = DomTableRowElement;
var elementSelectors = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 't'].map(function (it) { return function (node) { return node.isTagOf(it); }; });
var attributeSelector = function (node) { return node.hasAttribute('ng-should-translate'); };
exports.defaultSelectors = __spreadArrays(elementSelectors, [attributeSelector]);
//# sourceMappingURL=dom-models.js.map