ztechno_core 0.0.63 → 0.0.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/translate_service.js +2 -2
- package/lib/vendor/dom-parser/dist/index.d.ts +3 -0
- package/lib/vendor/dom-parser/dist/index.js +36 -0
- package/lib/vendor/dom-parser/dist/lib/Dom.d.ts +12 -0
- package/lib/vendor/dom-parser/dist/lib/Dom.js +139 -0
- package/lib/vendor/dom-parser/dist/lib/Node.d.ts +45 -0
- package/lib/vendor/dom-parser/dist/lib/Node.js +107 -0
- package/lib/vendor/dom-parser/dist/lib/NodeAttribute.d.ts +6 -0
- package/lib/vendor/dom-parser/dist/lib/NodeAttribute.js +10 -0
- package/package.json +1 -1
package/lib/translate_service.js
CHANGED
|
@@ -6,7 +6,7 @@ var __importDefault =
|
|
|
6
6
|
};
|
|
7
7
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
8
8
|
exports.ZTranslateService = void 0;
|
|
9
|
-
const
|
|
9
|
+
const dist_1 = require('./vendor/dom-parser/dist');
|
|
10
10
|
const translate_1 = __importDefault(require('translate'));
|
|
11
11
|
class ZTranslateService {
|
|
12
12
|
get sql() {
|
|
@@ -98,7 +98,7 @@ class ZTranslateService {
|
|
|
98
98
|
async translateHtml(html, cookies) {
|
|
99
99
|
const lang = this.getLang(cookies);
|
|
100
100
|
const srcLang = this.getSourceLang();
|
|
101
|
-
const dom = (0,
|
|
101
|
+
const dom = (0, dist_1.parseFromString)(html);
|
|
102
102
|
const htmlNodes = dom.getElementsByTagName('html');
|
|
103
103
|
const mainNodes = dom.getElementsByTagName('main');
|
|
104
104
|
const isView = htmlNodes.length === 0;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return m[k];
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
17
|
+
}
|
|
18
|
+
: function (o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
o[k2] = m[k];
|
|
21
|
+
});
|
|
22
|
+
var __exportStar =
|
|
23
|
+
(this && this.__exportStar) ||
|
|
24
|
+
function (m, exports) {
|
|
25
|
+
for (var p in m)
|
|
26
|
+
if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
29
|
+
exports.parseFromString = void 0;
|
|
30
|
+
const Dom_1 = require('./lib/Dom');
|
|
31
|
+
function parseFromString(html) {
|
|
32
|
+
return new Dom_1.Dom(html);
|
|
33
|
+
}
|
|
34
|
+
exports.parseFromString = parseFromString;
|
|
35
|
+
__exportStar(require('./lib/Dom'), exports);
|
|
36
|
+
__exportStar(require('./lib/Node'), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const __esModule: boolean;
|
|
2
|
+
export class Dom {
|
|
3
|
+
constructor(rawHTML: any);
|
|
4
|
+
rawHTML: any;
|
|
5
|
+
find(conditionFn: any, findFirst: any): Node_1.Node | Node_1.Node[];
|
|
6
|
+
getElementsByClassName(className: any): Node_1.Node | Node_1.Node[];
|
|
7
|
+
getElementsByTagName(tagName: any): Node_1.Node | Node_1.Node[];
|
|
8
|
+
getElementById(id: any): Node_1.Node | Node_1.Node[];
|
|
9
|
+
getElementsByName(name: any): Node_1.Node | Node_1.Node[];
|
|
10
|
+
getElementsByAttribute(attributeName: any, attributeValue: any): Node_1.Node | Node_1.Node[];
|
|
11
|
+
}
|
|
12
|
+
import Node_1 = require('./Node');
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.Dom = void 0;
|
|
4
|
+
const Node_1 = require('./Node');
|
|
5
|
+
const NodeAttribute_1 = require('./NodeAttribute');
|
|
6
|
+
const tagRegExp =
|
|
7
|
+
/(<\/?(?:[a-z][a-z0-9]*:)?[a-z][a-z0-9-_.]*?[a-z0-9]*\s*(?:\s+[a-z0-9-_:]+(?:=(?:(?:'[\s\S]*?')|(?:"[\s\S]*?")))?)*\s*\/?>)|([^<]|<(?![a-z/]))*/gi;
|
|
8
|
+
const attrRegExp = /\s[a-z0-9-_:]+\b(\s*=\s*('|")[\s\S]*?\2)?/gi;
|
|
9
|
+
const splitAttrRegExp = /(\s[a-z0-9-_:]+\b\s*)(?:=(\s*('|")[\s\S]*?\3))?/gi;
|
|
10
|
+
const startTagExp = /^<[a-z]/;
|
|
11
|
+
const selfCloseTagExp = /\/>$/;
|
|
12
|
+
const closeTagExp = /^<\//;
|
|
13
|
+
const textNodeExp = /^[^<]/;
|
|
14
|
+
const nodeNameExp = /<\/?((?:([a-z][a-z0-9]*):)?(?:[a-z](?:[a-z0-9-_.]*[a-z0-9])?))/i;
|
|
15
|
+
const attributeQuotesExp = /^('|")|('|")$/g;
|
|
16
|
+
const noClosingTagsExp = /^(?:area|base|br|col|command|embed|hr|img|input|link|meta|param|source)/i;
|
|
17
|
+
class Dom {
|
|
18
|
+
constructor(rawHTML) {
|
|
19
|
+
this.rawHTML = rawHTML;
|
|
20
|
+
}
|
|
21
|
+
find(conditionFn, findFirst) {
|
|
22
|
+
const result = find(this.rawHTML, conditionFn, findFirst);
|
|
23
|
+
return findFirst ? result[0] || null : result;
|
|
24
|
+
}
|
|
25
|
+
getElementsByClassName(className) {
|
|
26
|
+
const expr = new RegExp(`^(.*?\\s)?${className}(\\s.*?)?$`);
|
|
27
|
+
return this.find((node) => Boolean(node.attributes.length && expr.test(node.getAttribute('class') || '')));
|
|
28
|
+
}
|
|
29
|
+
getElementsByTagName(tagName) {
|
|
30
|
+
return this.find((node) => node.nodeName.toUpperCase() === tagName.toUpperCase());
|
|
31
|
+
}
|
|
32
|
+
getElementById(id) {
|
|
33
|
+
return this.find((node) => node.getAttribute('id') === id, true);
|
|
34
|
+
}
|
|
35
|
+
getElementsByName(name) {
|
|
36
|
+
return this.find((node) => node.getAttribute('name') === name);
|
|
37
|
+
}
|
|
38
|
+
getElementsByAttribute(attributeName, attributeValue) {
|
|
39
|
+
return this.find((node) => node.getAttribute(attributeName) === attributeValue);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.Dom = Dom;
|
|
43
|
+
// private
|
|
44
|
+
function find(html, conditionFn, onlyFirst = false) {
|
|
45
|
+
const generator = domGenerator(html);
|
|
46
|
+
const result = [];
|
|
47
|
+
for (const node of generator) {
|
|
48
|
+
if (node && conditionFn(node)) {
|
|
49
|
+
result.push(node);
|
|
50
|
+
if (onlyFirst) {
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
function* domGenerator(html) {
|
|
58
|
+
const tags = getAllTags(html);
|
|
59
|
+
let cursor = null;
|
|
60
|
+
for (let i = 0, l = tags.length; i < l; i++) {
|
|
61
|
+
const tag = tags[i];
|
|
62
|
+
const node = createNode(tag, cursor);
|
|
63
|
+
cursor = node || cursor;
|
|
64
|
+
if (isElementComposed(cursor, tag)) {
|
|
65
|
+
yield cursor;
|
|
66
|
+
cursor = cursor.parentNode;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
while (cursor) {
|
|
70
|
+
yield cursor;
|
|
71
|
+
cursor = cursor.parentNode;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function isElementComposed(element, tag) {
|
|
75
|
+
if (!tag) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const isCloseTag = closeTagExp.test(tag);
|
|
79
|
+
const [, nodeName] = tag.match(nodeNameExp) || [];
|
|
80
|
+
const isElementClosedByTag = isCloseTag && element.nodeName === nodeName;
|
|
81
|
+
return isElementClosedByTag || element?.isSelfCloseTag || element?.nodeType === Node_1.NodeType.text;
|
|
82
|
+
}
|
|
83
|
+
function getAllTags(html) {
|
|
84
|
+
return html.match(tagRegExp) || [];
|
|
85
|
+
}
|
|
86
|
+
function createNode(tag, parentNode) {
|
|
87
|
+
const isTextNode = textNodeExp.test(tag);
|
|
88
|
+
const isStartTag = startTagExp.test(tag);
|
|
89
|
+
if (isTextNode) {
|
|
90
|
+
return createTextNode(tag, parentNode);
|
|
91
|
+
}
|
|
92
|
+
if (isStartTag) {
|
|
93
|
+
return createElementNode(tag, parentNode);
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
function createElementNode(tag, parentNode) {
|
|
98
|
+
var _a;
|
|
99
|
+
const [, nodeName, namespace] = tag.match(nodeNameExp) || [];
|
|
100
|
+
const selfCloseTag = selfCloseTagExp.test(tag) || noClosingTagsExp.test(nodeName);
|
|
101
|
+
const attributes = parseAttributes(tag);
|
|
102
|
+
const elementNode = new Node_1.Node({
|
|
103
|
+
nodeType: Node_1.NodeType.element,
|
|
104
|
+
nodeName,
|
|
105
|
+
namespace,
|
|
106
|
+
attributes,
|
|
107
|
+
childNodes: [],
|
|
108
|
+
parentNode,
|
|
109
|
+
selfCloseTag,
|
|
110
|
+
});
|
|
111
|
+
(_a = parentNode === null || parentNode === void 0 ? void 0 : parentNode.childNodes) === null || _a === void 0
|
|
112
|
+
? void 0
|
|
113
|
+
: _a.push(elementNode);
|
|
114
|
+
return elementNode;
|
|
115
|
+
}
|
|
116
|
+
function parseAttributes(tag) {
|
|
117
|
+
return (tag.match(attrRegExp) || []).map((attributeString) => {
|
|
118
|
+
splitAttrRegExp.lastIndex = 0;
|
|
119
|
+
const exec = splitAttrRegExp.exec(attributeString) || [];
|
|
120
|
+
const [, name = '', value = ''] = exec;
|
|
121
|
+
return new NodeAttribute_1.NodeAttribute({
|
|
122
|
+
name: name.trim(),
|
|
123
|
+
value: value.trim().replace(attributeQuotesExp, ''),
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function createTextNode(text, parentNode) {
|
|
128
|
+
var _a;
|
|
129
|
+
const textNode = new Node_1.Node({
|
|
130
|
+
nodeType: Node_1.NodeType.text,
|
|
131
|
+
nodeName: '#text',
|
|
132
|
+
text,
|
|
133
|
+
parentNode,
|
|
134
|
+
});
|
|
135
|
+
(_a = parentNode === null || parentNode === void 0 ? void 0 : parentNode.childNodes) === null || _a === void 0
|
|
136
|
+
? void 0
|
|
137
|
+
: _a.push(textNode);
|
|
138
|
+
return textNode;
|
|
139
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export const __esModule: boolean;
|
|
2
|
+
export const NodeType: {};
|
|
3
|
+
export class Node {
|
|
4
|
+
constructor({
|
|
5
|
+
nodeType,
|
|
6
|
+
namespace,
|
|
7
|
+
selfCloseTag,
|
|
8
|
+
text,
|
|
9
|
+
nodeName,
|
|
10
|
+
childNodes,
|
|
11
|
+
parentNode,
|
|
12
|
+
attributes,
|
|
13
|
+
}: {
|
|
14
|
+
nodeType: any;
|
|
15
|
+
namespace: any;
|
|
16
|
+
selfCloseTag: any;
|
|
17
|
+
text: any;
|
|
18
|
+
nodeName: any;
|
|
19
|
+
childNodes: any;
|
|
20
|
+
parentNode: any;
|
|
21
|
+
attributes: any;
|
|
22
|
+
});
|
|
23
|
+
namespace: any;
|
|
24
|
+
nodeType: any;
|
|
25
|
+
isSelfCloseTag: boolean;
|
|
26
|
+
text: any;
|
|
27
|
+
nodeName: any;
|
|
28
|
+
childNodes: any;
|
|
29
|
+
parentNode: any;
|
|
30
|
+
attributes: any;
|
|
31
|
+
get firstChild(): any;
|
|
32
|
+
get lastChild(): any;
|
|
33
|
+
get innerHTML(): any;
|
|
34
|
+
get outerHTML(): any;
|
|
35
|
+
get textContent(): any;
|
|
36
|
+
getAttribute(name: any): any;
|
|
37
|
+
getElementsByTagName(tagName: any): any;
|
|
38
|
+
getElementsByClassName(className: any): any;
|
|
39
|
+
getElementsByName(name: any): any;
|
|
40
|
+
getElementById(id: any): any;
|
|
41
|
+
}
|
|
42
|
+
export namespace Node {
|
|
43
|
+
const ELEMENT_NODE: any;
|
|
44
|
+
const TEXT_NODE: any;
|
|
45
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Node
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
exports.Node = exports.NodeType = void 0;
|
|
5
|
+
var NodeType;
|
|
6
|
+
(function (NodeType) {
|
|
7
|
+
NodeType[(NodeType['element'] = 1)] = 'element';
|
|
8
|
+
NodeType[(NodeType['text'] = 3)] = 'text';
|
|
9
|
+
})(NodeType || (exports.NodeType = NodeType = {}));
|
|
10
|
+
class Node {
|
|
11
|
+
constructor({ nodeType, namespace, selfCloseTag, text, nodeName, childNodes, parentNode, attributes }) {
|
|
12
|
+
this.namespace = namespace || null;
|
|
13
|
+
this.nodeType = nodeType;
|
|
14
|
+
this.isSelfCloseTag = Boolean(selfCloseTag);
|
|
15
|
+
this.text = text || null;
|
|
16
|
+
this.nodeName = nodeType === NodeType.element ? nodeName : '#text';
|
|
17
|
+
this.childNodes = childNodes || [];
|
|
18
|
+
this.parentNode = parentNode;
|
|
19
|
+
this.attributes = attributes || [];
|
|
20
|
+
}
|
|
21
|
+
get firstChild() {
|
|
22
|
+
return this.childNodes[0] || null;
|
|
23
|
+
}
|
|
24
|
+
get lastChild() {
|
|
25
|
+
return this.childNodes[this.childNodes.length - 1] || null;
|
|
26
|
+
}
|
|
27
|
+
get innerHTML() {
|
|
28
|
+
return this.childNodes.reduce(
|
|
29
|
+
(html, node) => html + (node.nodeType === NodeType.text ? node.text : node.outerHTML),
|
|
30
|
+
'',
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
get outerHTML() {
|
|
34
|
+
if (this.nodeType === NodeType.text) {
|
|
35
|
+
return this.textContent;
|
|
36
|
+
}
|
|
37
|
+
const attributesString = stringifyAttributes(this.attributes);
|
|
38
|
+
const openTag = `<${this.nodeName}${attributesString.length ? ' ' : ''}${attributesString}${
|
|
39
|
+
this.isSelfCloseTag ? '/' : ''
|
|
40
|
+
}>`;
|
|
41
|
+
if (this.isSelfCloseTag) {
|
|
42
|
+
return openTag;
|
|
43
|
+
}
|
|
44
|
+
const childs = this.childNodes.map((child) => child.outerHTML).join('');
|
|
45
|
+
const closeTag = `</${this.nodeName}>`;
|
|
46
|
+
return [openTag, childs, closeTag].join('');
|
|
47
|
+
}
|
|
48
|
+
get textContent() {
|
|
49
|
+
if (this.nodeType === NodeType.text) {
|
|
50
|
+
return this.text;
|
|
51
|
+
}
|
|
52
|
+
return this.childNodes
|
|
53
|
+
.map((node) => node.textContent)
|
|
54
|
+
.join('')
|
|
55
|
+
.replace(/\x20+/g, ' ');
|
|
56
|
+
}
|
|
57
|
+
getAttribute(name) {
|
|
58
|
+
const attribute = this.attributes.find((a) => a.name === name);
|
|
59
|
+
return attribute ? attribute.value : null;
|
|
60
|
+
}
|
|
61
|
+
getElementsByTagName(tagName) {
|
|
62
|
+
return searchElements(this, (elem) => elem.nodeName.toUpperCase() === tagName.toUpperCase());
|
|
63
|
+
}
|
|
64
|
+
getElementsByClassName(className) {
|
|
65
|
+
const expr = new RegExp(`^(.*?\\s)?${className}(\\s.*?)?$`);
|
|
66
|
+
return searchElements(this, (node) =>
|
|
67
|
+
Boolean(node.attributes.length && expr.test(node.getAttribute('class') || '')),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
getElementsByName(name) {
|
|
71
|
+
return searchElements(this, (node) => Boolean(node.attributes.length && node.getAttribute('name') === name));
|
|
72
|
+
}
|
|
73
|
+
getElementById(id) {
|
|
74
|
+
return searchElement(this, (node) => Boolean(node.attributes.length && node.getAttribute('id') === id));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.Node = Node;
|
|
78
|
+
Node.ELEMENT_NODE = NodeType.element;
|
|
79
|
+
Node.TEXT_NODE = NodeType.text;
|
|
80
|
+
// private
|
|
81
|
+
function searchElements(root, conditionFn) {
|
|
82
|
+
if (root.nodeType === NodeType.text) {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
return root.childNodes.reduce((accumulator, childNode) => {
|
|
86
|
+
if (childNode.nodeType !== NodeType.text && conditionFn(childNode)) {
|
|
87
|
+
return [...accumulator, childNode, ...searchElements(childNode, conditionFn)];
|
|
88
|
+
}
|
|
89
|
+
return [...accumulator, ...searchElements(childNode, conditionFn)];
|
|
90
|
+
}, []);
|
|
91
|
+
}
|
|
92
|
+
function searchElement(root, conditionFn) {
|
|
93
|
+
for (let i = 0, l = root.childNodes.length; i < l; i++) {
|
|
94
|
+
const childNode = root.childNodes[i];
|
|
95
|
+
if (conditionFn(childNode)) {
|
|
96
|
+
return childNode;
|
|
97
|
+
}
|
|
98
|
+
const node = searchElement(childNode, conditionFn);
|
|
99
|
+
if (node) {
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
function stringifyAttributes(attributes) {
|
|
106
|
+
return attributes.map((elem) => elem.name + (elem.value ? `="${elem.value}"` : '')).join(' ');
|
|
107
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.NodeAttribute = void 0;
|
|
4
|
+
class NodeAttribute {
|
|
5
|
+
constructor({ name, value }) {
|
|
6
|
+
this.name = name;
|
|
7
|
+
this.value = value;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.NodeAttribute = NodeAttribute;
|