tag-soup-ng 0.0.1-security → 1.1.11
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.
Potentially problematic release.
This version of tag-soup-ng might be problematic. Click here for more details.
- package/LICENSE.txt +21 -0
- package/README.md +283 -3
- package/lib/createDomParser.d.ts +12 -0
- package/lib/createDomParser.js +84 -0
- package/lib/createHtmlDomParser.d.ts +21 -0
- package/lib/createHtmlDomParser.js +7 -0
- package/lib/createHtmlSaxParser.d.ts +29 -0
- package/lib/createHtmlSaxParser.js +120 -0
- package/lib/createSaxParser.d.ts +8 -0
- package/lib/createSaxParser.js +124 -0
- package/lib/createXmlDomParser.d.ts +21 -0
- package/lib/createXmlDomParser.js +58 -0
- package/lib/createXmlSaxParser.d.ts +18 -0
- package/lib/createXmlSaxParser.js +28 -0
- package/lib/dom-types.d.ts +116 -0
- package/lib/dom-types.js +14 -0
- package/lib/index-cjs.js +1 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +8 -0
- package/lib/parser-types.d.ts +425 -0
- package/lib/parser-types.js +14 -0
- package/lib/tokenize.d.ts +34 -0
- package/lib/tokenize.js +409 -0
- package/lib/tokens.d.ts +9 -0
- package/lib/tokens.js +69 -0
- package/package.json +77 -3
@@ -0,0 +1,124 @@
|
|
1
|
+
import { __assign } from "tslib";
|
2
|
+
import { tokenize } from './tokenize';
|
3
|
+
import { ObjectPool } from '@smikhalevski/object-pool';
|
4
|
+
import { createAttributeToken, createDataToken, createEndTagToken, createStartTagToken } from './tokens';
|
5
|
+
/**
|
6
|
+
* Creates a new stateful SAX parser.
|
7
|
+
*
|
8
|
+
* @param handler The parsing handler.
|
9
|
+
* @param options Parsing options.
|
10
|
+
*/
|
11
|
+
export function createSaxParser(handler, options) {
|
12
|
+
var opts = __assign({}, options);
|
13
|
+
var buffer = '';
|
14
|
+
var chunkOffset = 0;
|
15
|
+
var tokenizerOptions = {
|
16
|
+
startTagTokenPool: new ObjectPool(createStartTagToken),
|
17
|
+
attributeTokenPool: new ObjectPool(createAttributeToken),
|
18
|
+
endTagToken: createEndTagToken(),
|
19
|
+
dataToken: createDataToken(),
|
20
|
+
};
|
21
|
+
var forgivingHandler = createForgivingHandler(handler, tokenizerOptions, opts);
|
22
|
+
var write = function (sourceChunk) {
|
23
|
+
sourceChunk || (sourceChunk = '');
|
24
|
+
buffer += sourceChunk;
|
25
|
+
var index = tokenize(buffer, true, chunkOffset, tokenizerOptions, opts, forgivingHandler);
|
26
|
+
buffer = buffer.substr(index);
|
27
|
+
chunkOffset += index;
|
28
|
+
};
|
29
|
+
var parse = function (source) {
|
30
|
+
var _a;
|
31
|
+
source || (source = '');
|
32
|
+
buffer += source;
|
33
|
+
var index = tokenize(buffer, false, chunkOffset, tokenizerOptions, opts, forgivingHandler);
|
34
|
+
(_a = forgivingHandler.sourceEnd) === null || _a === void 0 ? void 0 : _a.call(forgivingHandler, chunkOffset + index);
|
35
|
+
reset();
|
36
|
+
};
|
37
|
+
var reset = function () {
|
38
|
+
var _a;
|
39
|
+
buffer = '';
|
40
|
+
chunkOffset = 0;
|
41
|
+
(_a = forgivingHandler.reset) === null || _a === void 0 ? void 0 : _a.call(forgivingHandler);
|
42
|
+
};
|
43
|
+
return {
|
44
|
+
write: write,
|
45
|
+
parse: parse,
|
46
|
+
reset: reset,
|
47
|
+
};
|
48
|
+
}
|
49
|
+
function createForgivingHandler(handler, tokenizerOptions, options) {
|
50
|
+
var startTagCallback = handler.startTag, endTagCallback = handler.endTag, resetCallback = handler.reset, sourceEndCallback = handler.sourceEnd;
|
51
|
+
var startTagTokenPool = tokenizerOptions.startTagTokenPool, attributeTokenPool = tokenizerOptions.attributeTokenPool;
|
52
|
+
var checkVoidTag = options.checkVoidTag, endsAncestorAt = options.endsAncestorAt;
|
53
|
+
var endTagToken = createEndTagToken();
|
54
|
+
var forgivingHandler = __assign({}, handler);
|
55
|
+
var ancestors = { length: 0 };
|
56
|
+
var releaseStartTag = function (token) {
|
57
|
+
startTagTokenPool.release(token);
|
58
|
+
for (var i = 0; i < token.attributes.length; ++i) {
|
59
|
+
attributeTokenPool.release(token.attributes[i]);
|
60
|
+
}
|
61
|
+
};
|
62
|
+
if (!startTagCallback && !endTagCallback) {
|
63
|
+
forgivingHandler.startTag = releaseStartTag;
|
64
|
+
return forgivingHandler;
|
65
|
+
}
|
66
|
+
var releaseAncestors = function (ancestorIndex) {
|
67
|
+
for (var i = ancestorIndex; i < ancestors.length; ++i) {
|
68
|
+
releaseStartTag(ancestors[i]);
|
69
|
+
ancestors[i] = undefined;
|
70
|
+
}
|
71
|
+
ancestors.length = ancestorIndex;
|
72
|
+
};
|
73
|
+
var triggerImplicitEnd = function (ancestorIndex, end) {
|
74
|
+
if (ancestorIndex % 1 !== 0 || ancestorIndex < 0 || ancestorIndex >= ancestors.length) {
|
75
|
+
return;
|
76
|
+
}
|
77
|
+
if (!endTagCallback) {
|
78
|
+
releaseAncestors(ancestorIndex);
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
for (var i = ancestors.length - 1; i >= ancestorIndex; --i) {
|
82
|
+
endTagToken.rawName = ancestors[i].rawName;
|
83
|
+
endTagToken.name = ancestors[i].name;
|
84
|
+
endTagToken.start = endTagToken.end = end;
|
85
|
+
endTagToken.nameStart = endTagToken.nameEnd = -1;
|
86
|
+
endTagCallback(endTagToken);
|
87
|
+
}
|
88
|
+
releaseAncestors(ancestorIndex);
|
89
|
+
};
|
90
|
+
forgivingHandler.startTag = function (token) {
|
91
|
+
token.selfClosing || (token.selfClosing = (checkVoidTag === null || checkVoidTag === void 0 ? void 0 : checkVoidTag(token)) || false);
|
92
|
+
if (endsAncestorAt != null && ancestors.length !== 0) {
|
93
|
+
triggerImplicitEnd(endsAncestorAt(ancestors, token), token.start);
|
94
|
+
}
|
95
|
+
startTagCallback === null || startTagCallback === void 0 ? void 0 : startTagCallback(token);
|
96
|
+
if (token.selfClosing) {
|
97
|
+
releaseStartTag(token);
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
ancestors[ancestors.length++] = token;
|
101
|
+
}
|
102
|
+
};
|
103
|
+
forgivingHandler.endTag = function (token) {
|
104
|
+
for (var i = ancestors.length - 1; i >= 0; --i) {
|
105
|
+
if (ancestors[i].name !== token.name) {
|
106
|
+
continue;
|
107
|
+
}
|
108
|
+
triggerImplicitEnd(i + 1, token.start);
|
109
|
+
endTagCallback === null || endTagCallback === void 0 ? void 0 : endTagCallback(token);
|
110
|
+
releaseStartTag(ancestors[i]);
|
111
|
+
ancestors.length = i;
|
112
|
+
break;
|
113
|
+
}
|
114
|
+
};
|
115
|
+
forgivingHandler.sourceEnd = function (sourceLength) {
|
116
|
+
triggerImplicitEnd(0, sourceLength);
|
117
|
+
sourceEndCallback === null || sourceEndCallback === void 0 ? void 0 : sourceEndCallback(sourceLength);
|
118
|
+
};
|
119
|
+
forgivingHandler.reset = function () {
|
120
|
+
releaseAncestors(0);
|
121
|
+
resetCallback === null || resetCallback === void 0 ? void 0 : resetCallback();
|
122
|
+
};
|
123
|
+
return forgivingHandler;
|
124
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { IDomHandler, IParser, IParserOptions } from './parser-types';
|
2
|
+
import { ContainerNode, Node } from './dom-types';
|
3
|
+
/**
|
4
|
+
* Creates a pre-configured XML DOM parser that uses {@link domHandler}.
|
5
|
+
*
|
6
|
+
* @see {@link domHandler}
|
7
|
+
*/
|
8
|
+
export declare function createXmlDomParser(): IParser<Array<Node>>;
|
9
|
+
/**
|
10
|
+
* Creates a pre-configured XML DOM parser.
|
11
|
+
*
|
12
|
+
* @param handler The parsing handler.
|
13
|
+
* @param options Options that override the defaults.
|
14
|
+
*
|
15
|
+
* @see {@link domHandler}
|
16
|
+
*/
|
17
|
+
export declare function createXmlDomParser<Node, ContainerNode extends Node>(handler: IDomHandler<Node, ContainerNode>, options?: IParserOptions): IParser<Array<Node>>;
|
18
|
+
/**
|
19
|
+
* The default DOM handler.
|
20
|
+
*/
|
21
|
+
export declare const domHandler: IDomHandler<Node, ContainerNode>;
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { __assign } from "tslib";
|
2
|
+
import { createDomParser } from './createDomParser';
|
3
|
+
import { xmlParserOptions } from './createXmlSaxParser';
|
4
|
+
export function createXmlDomParser(handler, options) {
|
5
|
+
return createDomParser(handler || domHandler, __assign(__assign({}, xmlParserOptions), options));
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* The default DOM handler.
|
9
|
+
*/
|
10
|
+
export var domHandler = {
|
11
|
+
element: function (token) {
|
12
|
+
var attributes = Object.create(null);
|
13
|
+
for (var i = 0; i < token.attributes.length; i++) {
|
14
|
+
var attribute = token.attributes[i];
|
15
|
+
attributes[attribute.name] = attribute.value;
|
16
|
+
}
|
17
|
+
return {
|
18
|
+
nodeType: 1 /* ELEMENT */,
|
19
|
+
parent: null,
|
20
|
+
tagName: token.name,
|
21
|
+
attributes: attributes,
|
22
|
+
selfClosing: token.selfClosing,
|
23
|
+
children: [],
|
24
|
+
start: token.start,
|
25
|
+
end: token.end,
|
26
|
+
};
|
27
|
+
},
|
28
|
+
appendChild: function (parentNode, node) {
|
29
|
+
node.parent = parentNode;
|
30
|
+
parentNode.children.push(node);
|
31
|
+
},
|
32
|
+
containerEnd: function (node, token) {
|
33
|
+
node.end = token.end;
|
34
|
+
},
|
35
|
+
document: function (token) {
|
36
|
+
return {
|
37
|
+
nodeType: 9 /* DOCUMENT */,
|
38
|
+
parent: null,
|
39
|
+
doctype: token.data,
|
40
|
+
children: [],
|
41
|
+
start: token.start,
|
42
|
+
end: token.end,
|
43
|
+
};
|
44
|
+
},
|
45
|
+
text: function (token) { return createDataNode(3 /* TEXT */, token); },
|
46
|
+
processingInstruction: function (token) { return createDataNode(7 /* PROCESSING_INSTRUCTION */, token); },
|
47
|
+
cdata: function (token) { return createDataNode(4 /* CDATA_SECTION */, token); },
|
48
|
+
comment: function (token) { return createDataNode(8 /* COMMENT */, token); },
|
49
|
+
};
|
50
|
+
function createDataNode(nodeType, token) {
|
51
|
+
return {
|
52
|
+
nodeType: nodeType,
|
53
|
+
data: token.data,
|
54
|
+
parent: null,
|
55
|
+
start: token.start,
|
56
|
+
end: token.end,
|
57
|
+
};
|
58
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { IParser, IParserOptions, ISaxHandler } from './parser-types';
|
2
|
+
/**
|
3
|
+
* Creates a pre-configured XML SAX parser.
|
4
|
+
*
|
5
|
+
* @param handler The parsing handler.
|
6
|
+
* @param options Options that override the defaults.
|
7
|
+
* @see {@link xmlParserOptions}
|
8
|
+
*/
|
9
|
+
export declare function createXmlSaxParser(handler: ISaxHandler, options?: IParserOptions): IParser<void>;
|
10
|
+
/**
|
11
|
+
* The default XML parser options:
|
12
|
+
* - CDATA sections, processing instructions and self-closing tags are recognized;
|
13
|
+
* - XML entities are decoded in text and attribute values;
|
14
|
+
* - Tag and attribute names are preserved as is;
|
15
|
+
*
|
16
|
+
* @see {@link https://github.com/smikhalevski/speedy-entities decodeXml}
|
17
|
+
*/
|
18
|
+
export declare const xmlParserOptions: IParserOptions;
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { __assign } from "tslib";
|
2
|
+
import { createSaxParser } from './createSaxParser';
|
3
|
+
import { decodeXml } from 'speedy-entities';
|
4
|
+
/**
|
5
|
+
* Creates a pre-configured XML SAX parser.
|
6
|
+
*
|
7
|
+
* @param handler The parsing handler.
|
8
|
+
* @param options Options that override the defaults.
|
9
|
+
* @see {@link xmlParserOptions}
|
10
|
+
*/
|
11
|
+
export function createXmlSaxParser(handler, options) {
|
12
|
+
return createSaxParser(handler, __assign(__assign({}, xmlParserOptions), options));
|
13
|
+
}
|
14
|
+
/**
|
15
|
+
* The default XML parser options:
|
16
|
+
* - CDATA sections, processing instructions and self-closing tags are recognized;
|
17
|
+
* - XML entities are decoded in text and attribute values;
|
18
|
+
* - Tag and attribute names are preserved as is;
|
19
|
+
*
|
20
|
+
* @see {@link https://github.com/smikhalevski/speedy-entities decodeXml}
|
21
|
+
*/
|
22
|
+
export var xmlParserOptions = {
|
23
|
+
cdataEnabled: true,
|
24
|
+
processingInstructionsEnabled: true,
|
25
|
+
selfClosingEnabled: true,
|
26
|
+
decodeText: decodeXml,
|
27
|
+
decodeAttribute: decodeXml,
|
28
|
+
};
|
@@ -0,0 +1,116 @@
|
|
1
|
+
/**
|
2
|
+
* Type of nodes in the DOM tree.
|
3
|
+
*
|
4
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants Node type constants on MDN}
|
5
|
+
*/
|
6
|
+
export declare const enum NodeType {
|
7
|
+
ELEMENT = 1,
|
8
|
+
TEXT = 3,
|
9
|
+
PROCESSING_INSTRUCTION = 7,
|
10
|
+
CDATA_SECTION = 4,
|
11
|
+
DOCUMENT = 9,
|
12
|
+
COMMENT = 8
|
13
|
+
}
|
14
|
+
/**
|
15
|
+
* A node of the DOM tree.
|
16
|
+
*/
|
17
|
+
export declare type Node = DataNode | ContainerNode;
|
18
|
+
export declare type ContainerNode = IDocumentNode | IElementNode;
|
19
|
+
/**
|
20
|
+
* The data node of the DOM tree.
|
21
|
+
*/
|
22
|
+
export declare type DataNode = ITextNode | ICdataSectionNode | IProcessingInstructionNode | ICommentNode;
|
23
|
+
/**
|
24
|
+
* The DOM node.
|
25
|
+
*/
|
26
|
+
export interface INode {
|
27
|
+
/**
|
28
|
+
* The type of the node.
|
29
|
+
*/
|
30
|
+
nodeType: NodeType;
|
31
|
+
/**
|
32
|
+
* The parent of the node.
|
33
|
+
*/
|
34
|
+
parent: IContainerNode | null;
|
35
|
+
/**
|
36
|
+
* The index where the node starts.
|
37
|
+
*/
|
38
|
+
start: number;
|
39
|
+
/**
|
40
|
+
* The index where the node ends.
|
41
|
+
*/
|
42
|
+
end: number;
|
43
|
+
}
|
44
|
+
/**
|
45
|
+
* The DOM node that can have children.
|
46
|
+
*/
|
47
|
+
export interface IContainerNode extends INode {
|
48
|
+
/**
|
49
|
+
* The list of node children.
|
50
|
+
*/
|
51
|
+
children: Array<Node>;
|
52
|
+
}
|
53
|
+
/**
|
54
|
+
* The DOM node that contains textual data.
|
55
|
+
*/
|
56
|
+
export interface IDataNode extends INode {
|
57
|
+
/**
|
58
|
+
* The text contained the the node.
|
59
|
+
*/
|
60
|
+
data: string;
|
61
|
+
}
|
62
|
+
/**
|
63
|
+
* The root node of the document.
|
64
|
+
*/
|
65
|
+
export interface IDocumentNode extends IContainerNode {
|
66
|
+
nodeType: NodeType.DOCUMENT;
|
67
|
+
/**
|
68
|
+
* The doctype string `<!DOCTYPE … >`.
|
69
|
+
*/
|
70
|
+
doctype: string;
|
71
|
+
}
|
72
|
+
export interface IElementNode extends IContainerNode {
|
73
|
+
nodeType: NodeType.ELEMENT;
|
74
|
+
/**
|
75
|
+
* The name of element tag.
|
76
|
+
*
|
77
|
+
* @see {@link IStartTagToken.name}
|
78
|
+
*/
|
79
|
+
tagName: string;
|
80
|
+
/**
|
81
|
+
* The mapping from the attribute name to its value. If value of the attribute was omitted and name is followed by
|
82
|
+
* "=" char like `foo=` then `null`. If value is omitted and name isn't followed by a "=" char like `foo` then
|
83
|
+
* `undefined`.
|
84
|
+
*/
|
85
|
+
attributes: Record<string, string | null | undefined>;
|
86
|
+
/**
|
87
|
+
* If `true` then the element was represented as a self-closing tag in the source.
|
88
|
+
*
|
89
|
+
* @see {@link IStartTagToken.selfClosing}
|
90
|
+
*/
|
91
|
+
selfClosing: boolean;
|
92
|
+
}
|
93
|
+
/**
|
94
|
+
* The text node.
|
95
|
+
*/
|
96
|
+
export interface ITextNode extends IDataNode {
|
97
|
+
nodeType: NodeType.TEXT;
|
98
|
+
}
|
99
|
+
/**
|
100
|
+
* The CDATA section node `<![CDATA[ … ]]>`.
|
101
|
+
*/
|
102
|
+
export interface ICdataSectionNode extends IDataNode {
|
103
|
+
nodeType: NodeType.CDATA_SECTION;
|
104
|
+
}
|
105
|
+
/**
|
106
|
+
* The processing instruction node `<?xml-stylesheet … ?>`.
|
107
|
+
*/
|
108
|
+
export interface IProcessingInstructionNode extends IDataNode {
|
109
|
+
nodeType: NodeType.PROCESSING_INSTRUCTION;
|
110
|
+
}
|
111
|
+
/**
|
112
|
+
* The comment node `<!-- … -->`.
|
113
|
+
*/
|
114
|
+
export interface ICommentNode extends IDataNode {
|
115
|
+
nodeType: NodeType.COMMENT;
|
116
|
+
}
|
package/lib/dom-types.js
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
/**
|
2
|
+
* Type of nodes in the DOM tree.
|
3
|
+
*
|
4
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants Node type constants on MDN}
|
5
|
+
*/
|
6
|
+
export var NodeType;
|
7
|
+
(function (NodeType) {
|
8
|
+
NodeType[NodeType["ELEMENT"] = 1] = "ELEMENT";
|
9
|
+
NodeType[NodeType["TEXT"] = 3] = "TEXT";
|
10
|
+
NodeType[NodeType["PROCESSING_INSTRUCTION"] = 7] = "PROCESSING_INSTRUCTION";
|
11
|
+
NodeType[NodeType["CDATA_SECTION"] = 4] = "CDATA_SECTION";
|
12
|
+
NodeType[NodeType["DOCUMENT"] = 9] = "DOCUMENT";
|
13
|
+
NodeType[NodeType["COMMENT"] = 8] = "COMMENT";
|
14
|
+
})(NodeType || (NodeType = {}));
|
package/lib/index-cjs.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("tokenizer-dsl"),n=require("@smikhalevski/object-pool"),r=require("speedy-entities"),a=function(e){return 32===e||9===e||13===e||10===e},o=function(e){return e>=97&&e<=122||e>=65&&e<=90||95===e||58===e||e>=192&&e<=214||e>=216&&e<=246||e>=248&&e<=767||e>=880&&e<=893||e>=895&&e<=8191||e>=8204&&e<=8205||e>=8304&&e<=8591||e>=11264&&e<=12271||e>=12289&&e<=55295||e>=63744&&e<=64975||e>=65008&&e<=65533||e>=65536&&e<=983039},i=function(e){return 32===e||9===e||13===e||10===e||47===e},u=function(e){return 32===e||9===e||13===e||10===e||47===e||62===e},s=function(e){return 32===e||9===e||13===e||10===e||47===e||62===e||61===e},l=function(e){return 32===e||9===e||13===e||10===e||62===e},d=t.until(t.text("<")),c=t.until(t.text(">"),{inclusive:!0}),f=t.char(o),T=t.until(t.char(u),{openEnded:!0,endOffset:1}),p=t.seq(t.text("<"),f,T),g=t.seq(t.text("</"),f,T),m=t.until(t.char(s),{openEnded:!0}),h=t.all(t.char(i)),v=t.all(t.char(a)),E=t.seq(v,t.text("="),v),b=t.seq(t.text('"'),t.until(t.text('"'),{inclusive:!0,openEnded:!0,endOffset:1})),x=t.seq(t.text("'"),t.until(t.text("'"),{inclusive:!0,openEnded:!0,endOffset:1})),C=t.until(t.char(l),{openEnded:!0}),_=t.seq(t.text("\x3c!--"),t.until(t.text("--\x3e"),{inclusive:!0,openEnded:!0,endOffset:3})),k=t.seq(t.text("<!"),t.until(t.text(">"),{inclusive:!0,openEnded:!0,endOffset:1})),N=t.seq(t.text("<?"),t.until(t.text("?>"),{inclusive:!0,openEnded:!0,endOffset:2})),O=t.seq(t.text("<![CDATA["),t.until(t.text("]]>"),{inclusive:!0,openEnded:!0,endOffset:3})),S=t.seq(t.text("<!DOCTYPE",{caseInsensitive:!0}),t.until(t.text(">"),{inclusive:!0,openEnded:!0,endOffset:1}));function y(e,t,n,r,a,o){for(var i=a.attributeTokenPool,u=o.decodeAttribute,s=o.renameAttribute,l=e.length,d=0;t<l;){var c=h(e,t),f=m(e,c);if(f===c)break;var T=r[d]=i.take(),p=e.substring(c,f);T.rawName=p,T.name=null!=s?s(p):p,T.nameStart=T.start=n+c,T.nameEnd=n+f;var g=void 0,v=void 0,_=-1,k=-1,N=!1;-1!==(f=E(e,c=f))&&(g=v=null,-1===(f=b(e,c=f))&&(f=x(e,c)),-1!==f?(_=c+1,k=f-1,N=!0,c=Math.min(f,l)):(f=C(e,c))!==c&&(_=c,k=f,c=f),-1!==_&&(g=e.substring(_,k),v=null!=u?u(g):g,_+=n,k+=n)),T.rawValue=g,T.value=v,T.valueStart=_,T.valueEnd=k,T.quoted=N,T.end=n+c,++d,t=c}for(var O=d;O<r.length;++O)r[O]=void 0;return r.length=d,t}function A(e,t,n,r,a,o){for(var i,u,s=r.startTagTokenPool,l=r.endTagToken,f=r.dataToken,T=a.cdataEnabled,m=a.processingInstructionsEnabled,h=a.selfClosingEnabled,v=a.decodeText,E=a.renameTag,b=a.checkCdataTag,x=o.startTag,C=o.endTag,A=o.text,I=o.comment,P=o.processingInstruction,M=o.cdata,q=o.doctype,D=-1,R=0,G=!0,U=e.length,X=0;X<U;){if(-1===D){if(-1===(F=d(e,X))&&(F=U)&&t)break;if(F!==X){D=X,R=X=F;continue}}if(G&&-1!==(u=p(e,X))){var j=s.take(),H=j.attributes,V=X+1,L=u,Y=e.substring(V,L),B=null!=E?E(Y):Y;if(u=y(e,u,n,H,r,a),-1===(F=c(e,u)))return X;var z=h&&F-u>=2&&47===e.charCodeAt(F-2)||!1;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),j.rawName=Y,j.name=B,j.selfClosing=z,j.start=n+X,j.end=n+F,j.nameStart=n+V,j.nameEnd=n+L,z||(i=B,G=!(null==b?void 0:b(j))),X=F,null==x||x(j)}else{if(-1!==(u=g(e,X))){V=X+2,L=u,Y=e.substring(V,L),B=null!=E?E(Y):Y;if(G||i===B){if(G=!0,-1===(F=c(e,u)))return X;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),C&&(l.rawName=Y,l.name=B,l.start=n+X,l.end=n+F,l.nameStart=n+V,l.nameEnd=n+L,C(l)),X=F;continue}}if(G){var F=void 0;if(F=u=_(e,X),-1!==u){if(u>U&&t)return X;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),X=w(e,n,8,f,I,X,u,4,3,v);continue}if(F=u=S(e,X),-1!==u){if(u>U&&t)return X;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),X=w(e,n,10,f,q,X,u,9,1);continue}if(-1!==(u=O(e,X))){if(u>U&&t)return X;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),X=T?w(e,n,4,f,M,X,u,9,3):w(e,n,8,f,I,X,u,2,1);continue}if(-1!==(u=N(e,X))){if(u>U&&t)return X;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),X=m?w(e,n,7,f,P,X,u,2,2):w(e,n,8,f,I,X,u,1,1);continue}if(-1!==(u=k(e,X))){if(u>U&&t)return X;-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),X=T?Math.min(u,U):w(e,n,8,f,I,X,u,2,1,v);continue}}if(-1===D&&(D=X),-1===(R=d(e,X+1))){R=U;break}X=R}}return t?-1!==D?D:X:(-1!==D&&(w(e,n,3,f,A,D,R,0,0,v),D=-1),X)}function w(e,t,n,r,a,o,i,u,s,l){var d=e.length,c=Math.min(i,d);if(!a)return c;var f=o+u,T=Math.min(i-s,d),p=e.substring(f,T);return r.tokenType=n,r.rawData=p,r.data=null!=l?l(p):p,r.start=t+o,r.end=t+c,r.dataStart=t+f,r.dataEnd=t+T,a(r),c}function I(){var t=e.__assign({},this);if(1===t.tokenType)for(var n=t.attributes=e.__assign({},t.attributes),r=0;r<n.length;++r)n[r]=e.__assign({},n[r]);return t}function P(){return{tokenType:1,name:"",rawName:"",attributes:{length:0},selfClosing:!1,start:0,end:0,nameStart:0,nameEnd:0,clone:I}}function M(){return{tokenType:101,name:"",rawName:"",start:0,end:0,nameStart:0,nameEnd:0,clone:I}}function q(){return{tokenType:3,data:"",rawData:"",start:0,end:0,dataStart:0,dataEnd:0,clone:I}}function D(){return{tokenType:2,name:"",rawName:"",value:"",rawValue:"",quoted:!1,start:0,end:0,nameStart:0,nameEnd:0,valueStart:0,valueEnd:0,clone:I}}function R(t,r){var a=e.__assign({},r),o="",i=0,u={startTagTokenPool:new n.ObjectPool(P),attributeTokenPool:new n.ObjectPool(D),endTagToken:M(),dataToken:q()},s=G(t,u,a),l=function(){var e;o="",i=0,null===(e=s.reset)||void 0===e||e.call(s)};return{write:function(e){e||(e="");var t=A(o+=e,!0,i,u,a,s);o=o.substr(t),i+=t},parse:function(e){var t;e||(e="");var n=A(o+=e,!1,i,u,a,s);null===(t=s.sourceEnd)||void 0===t||t.call(s,i+n),l()},reset:l}}function G(t,n,r){var a=t.startTag,o=t.endTag,i=t.reset,u=t.sourceEnd,s=n.startTagTokenPool,l=n.attributeTokenPool,d=r.checkVoidTag,c=r.endsAncestorAt,f=M(),T=e.__assign({},t),p={length:0},g=function(e){s.release(e);for(var t=0;t<e.attributes.length;++t)l.release(e.attributes[t])};if(!a&&!o)return T.startTag=g,T;var m=function(e){for(var t=e;t<p.length;++t)g(p[t]),p[t]=void 0;p.length=e},h=function(e,t){if(!(e%1!=0||e<0||e>=p.length))if(o){for(var n=p.length-1;n>=e;--n)f.rawName=p[n].rawName,f.name=p[n].name,f.start=f.end=t,f.nameStart=f.nameEnd=-1,o(f);m(e)}else m(e)};return T.startTag=function(e){e.selfClosing||(e.selfClosing=(null==d?void 0:d(e))||!1),null!=c&&0!==p.length&&h(c(p,e),e.start),null==a||a(e),e.selfClosing?g(e):p[p.length++]=e},T.endTag=function(e){for(var t=p.length-1;t>=0;--t)if(p[t].name===e.name){h(t+1,e.start),null==o||o(e),g(p[t]),p.length=t;break}},T.sourceEnd=function(e){h(0,e),null==u||u(e)},T.reset=function(){m(0),null==i||i()},T}function U(e,t){var n=[],r=R(X(n,e,(function(e){return n.push(e)})),t),a=function(){r.reset(),n=[]};return{write:function(e){return r.write(e),n},parse:function(e){r.parse(e);var t=n;return a(),t},reset:a}}function X(e,t,n){var r=t.element,a=t.containerEnd,o=t.appendChild,i=t.text,u=t.document,s=t.comment,l=t.processingInstruction,d=t.cdata,c=t.sourceEnd,f=t.reset,T={length:0};if("function"!=typeof r)throw new Error("Missing element factory");if("function"!=typeof o)throw new Error("Missing appendChild callback");var p=function(e){0!==T.length?o(T[T.length-1],e):n(e)},g=function(e){return null!=e?function(t){return p(e(t))}:void 0};return{startTag:function(e){var t=r(e);p(t),e.selfClosing||(T[T.length++]=t)},endTag:function(e){--T.length,null==a||a(T[T.length],e)},doctype:function(t){if(u&&0===e.length){var n=u(t);p(n),T[T.length++]=n}},text:g(i),processingInstruction:g(l),cdata:g(d),comment:g(s),sourceEnd:c,reset:function(){T.length=0,null==f||f()}}}function j(t,n){return R(t,e.__assign(e.__assign({},H),n))}var H={cdataEnabled:!0,processingInstructionsEnabled:!0,selfClosingEnabled:!0,decodeText:r.decodeXml,decodeAttribute:r.decodeXml};function V(t,n){return U(t||L,e.__assign(e.__assign({},H),n))}var L={element:function(e){for(var t=Object.create(null),n=0;n<e.attributes.length;n++){var r=e.attributes[n];t[r.name]=r.value}return{nodeType:1,parent:null,tagName:e.name,attributes:t,selfClosing:e.selfClosing,children:[],start:e.start,end:e.end}},appendChild:function(e,t){t.parent=e,e.children.push(t)},containerEnd:function(e,t){e.end=t.end},document:function(e){return{nodeType:9,parent:null,doctype:e.data,children:[],start:e.start,end:e.end}},text:function(e){return Y(3,e)},processingInstruction:function(e){return Y(7,e)},cdata:function(e){return Y(4,e)},comment:function(e){return Y(8,e)}};function Y(e,t){return{nodeType:e,data:t.data,parent:null,start:t.start,end:t.end}}function B(t,n){return R(t,e.__assign(e.__assign({},z),n))}var z={decodeText:r.decodeHtml,decodeAttribute:r.decodeHtml,renameTag:F,renameAttribute:F,checkCdataTag:J,checkVoidTag:K,endsAncestorAt:Q};function F(e){return e.toLowerCase()}function J(e){return Z.has(e.name)}function K(e){return W.has(e.name)}function Q(e,t){var n=te.get(t.name);if(n)for(var r=e.length-1;r>=0;--r)if(n.has(e[r].name))return r;return-1}var W=ne("area base basefont br col command embed frame hr img input isindex keygen link meta param source track wbr"),Z=ne("script style textarea"),$=ne("input option optgroup select button datalist textarea"),ee=ne("p"),te=re({tr:ne("tr th td"),th:ne("th"),td:ne("thead th td"),body:ne("head link script"),li:ne("li"),option:ne("option"),optgroup:ne("optgroup option"),dd:ne("dt dd"),dt:ne("dt dd"),select:$,input:$,output:$,button:$,datalist:$,textarea:$,p:ee,h1:ee,h2:ee,h3:ee,h4:ee,h5:ee,h6:ee,address:ee,article:ee,aside:ee,blockquote:ee,details:ee,div:ee,dl:ee,fieldset:ee,figcaption:ee,figure:ee,footer:ee,form:ee,header:ee,hr:ee,main:ee,nav:ee,ol:ee,pre:ee,section:ee,table:ee,ul:ee,rt:ne("rt rp"),rp:ne("rt rp"),tbody:ne("thead tbody"),tfoot:ne("thead tbody")});function ne(e){return new Set(e.split(" "))}function re(e){return new Map(Object.entries(e))}function ae(t,n){return U(t||L,e.__assign(e.__assign({},z),n))}exports.NodeType=void 0,function(e){e[e.ELEMENT=1]="ELEMENT",e[e.TEXT=3]="TEXT",e[e.PROCESSING_INSTRUCTION=7]="PROCESSING_INSTRUCTION",e[e.CDATA_SECTION=4]="CDATA_SECTION",e[e.DOCUMENT=9]="DOCUMENT",e[e.COMMENT=8]="COMMENT"}(exports.NodeType||(exports.NodeType={})),exports.TokenType=void 0,function(e){e[e.START_TAG=1]="START_TAG",e[e.END_TAG=101]="END_TAG",e[e.ATTRIBUTE=2]="ATTRIBUTE",e[e.DOCTYPE=10]="DOCTYPE",e[e.TEXT=3]="TEXT",e[e.CDATA_SECTION=4]="CDATA_SECTION",e[e.PROCESSING_INSTRUCTION=7]="PROCESSING_INSTRUCTION",e[e.COMMENT=8]="COMMENT"}(exports.TokenType||(exports.TokenType={})),exports.createDomParser=U,exports.createHtmlDomParser=ae,exports.createHtmlSaxParser=B,exports.createSaxParser=R,exports.createXmlDomParser=V,exports.createXmlSaxParser=j,exports.domHandler=L,exports.htmlParserOptions=z,exports.xmlParserOptions=H;
|
package/lib/index.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
export * from './createSaxParser';
|
2
|
+
export * from './createDomParser';
|
3
|
+
export * from './createXmlSaxParser';
|
4
|
+
export * from './createXmlDomParser';
|
5
|
+
export * from './createHtmlSaxParser';
|
6
|
+
export * from './createHtmlDomParser';
|
7
|
+
export * from './dom-types';
|
8
|
+
export * from './parser-types';
|
package/lib/index.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
export * from './createSaxParser';
|
2
|
+
export * from './createDomParser';
|
3
|
+
export * from './createXmlSaxParser';
|
4
|
+
export * from './createXmlDomParser';
|
5
|
+
export * from './createHtmlSaxParser';
|
6
|
+
export * from './createHtmlDomParser';
|
7
|
+
export * from './dom-types';
|
8
|
+
export * from './parser-types';
|