ui
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
Copyright 2010, 2011, Chris Winberry <chris@winberry.net>. All rights reserved.
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
# htmlparser2
|
||||
|
||||
[](https://npmjs.org/package/htmlparser2)
|
||||
[](https://npmjs.org/package/htmlparser2)
|
||||
[](http://travis-ci.org/fb55/htmlparser2)
|
||||
[](https://coveralls.io/r/fb55/htmlparser2)
|
||||
|
||||
A forgiving HTML/XML/RSS parser.
|
||||
The parser can handle streams and provides a callback interface.
|
||||
|
||||
## Installation
|
||||
|
||||
npm install htmlparser2
|
||||
|
||||
A live demo of htmlparser2 is available [here](https://astexplorer.net/#/2AmVrGuGVJ).
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
const htmlparser2 = require("htmlparser2");
|
||||
const parser = new htmlparser2.Parser(
|
||||
{
|
||||
onopentag(name, attribs) {
|
||||
if (name === "script" && attribs.type === "text/javascript") {
|
||||
console.log("JS! Hooray!");
|
||||
}
|
||||
},
|
||||
ontext(text) {
|
||||
console.log("-->", text);
|
||||
},
|
||||
onclosetag(tagname) {
|
||||
if (tagname === "script") {
|
||||
console.log("That's it?!");
|
||||
}
|
||||
}
|
||||
},
|
||||
{ decodeEntities: true }
|
||||
);
|
||||
parser.write(
|
||||
"Xyz <script type='text/javascript'>var foo = '<<bar>>';</ script>"
|
||||
);
|
||||
parser.end();
|
||||
```
|
||||
|
||||
Output (simplified):
|
||||
|
||||
```
|
||||
--> Xyz
|
||||
JS! Hooray!
|
||||
--> var foo = '<<bar>>';
|
||||
That's it?!
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Read more about the parser and its options in the [wiki](https://github.com/fb55/htmlparser2/wiki/Parser-options).
|
||||
|
||||
## Get a DOM
|
||||
|
||||
The `DomHandler` (known as `DefaultHandler` in the original `htmlparser` module) produces a DOM (document object model) that can be manipulated using the [`DomUtils`](https://github.com/fb55/DomUtils) helper.
|
||||
|
||||
The `DomHandler`, while still bundled with this module, was moved to its [own module](https://github.com/fb55/domhandler). Have a look at it for further information.
|
||||
|
||||
## Parsing RSS/RDF/Atom Feeds
|
||||
|
||||
```javascript
|
||||
const feed = htmlparser2.parseFeed(content, options);
|
||||
```
|
||||
|
||||
Note: While the provided feed handler works for most feeds, you might want to use [danmactough/node-feedparser](https://github.com/danmactough/node-feedparser), which is much better tested and actively maintained.
|
||||
|
||||
## Performance
|
||||
|
||||
After having some artificial benchmarks for some time, **@AndreasMadsen** published his [`htmlparser-benchmark`](https://github.com/AndreasMadsen/htmlparser-benchmark), which benchmarks HTML parses based on real-world websites.
|
||||
|
||||
At the time of writing, the latest versions of all supported parsers show the following performance characteristics on [Travis CI](https://travis-ci.org/AndreasMadsen/htmlparser-benchmark/builds/10805007) (please note that Travis doesn't guarantee equal conditions for all tests):
|
||||
|
||||
```
|
||||
gumbo-parser : 34.9208 ms/file ± 21.4238
|
||||
html-parser : 24.8224 ms/file ± 15.8703
|
||||
html5 : 419.597 ms/file ± 264.265
|
||||
htmlparser : 60.0722 ms/file ± 384.844
|
||||
htmlparser2-dom: 12.0749 ms/file ± 6.49474
|
||||
htmlparser2 : 7.49130 ms/file ± 5.74368
|
||||
hubbub : 30.4980 ms/file ± 16.4682
|
||||
libxmljs : 14.1338 ms/file ± 18.6541
|
||||
parse5 : 22.0439 ms/file ± 15.3743
|
||||
sax : 49.6513 ms/file ± 26.6032
|
||||
```
|
||||
|
||||
## How does this module differ from [node-htmlparser](https://github.com/tautologistics/node-htmlparser)?
|
||||
|
||||
This module started as a fork of the `htmlparser` module.
|
||||
The main difference is that `htmlparser2` is intended to be used only with node (it runs on other platforms using [browserify](https://github.com/substack/node-browserify)).
|
||||
`htmlparser2` was rewritten multiple times and, while it maintains an API that's compatible with `htmlparser` in most cases, the projects don't share any code anymore.
|
||||
|
||||
The parser now provides a callback interface inspired by [sax.js](https://github.com/isaacs/sax-js) (originally targeted at [readabilitySAX](https://github.com/fb55/readabilitysax)).
|
||||
As a result, old handlers won't work anymore.
|
||||
|
||||
The `DefaultHandler` and the `RssHandler` were renamed to clarify their purpose (to `DomHandler` and `FeedHandler`). The old names are still available when requiring `htmlparser2`, your code should work as expected.
|
||||
|
||||
## Security contact information
|
||||
|
||||
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
|
||||
Tidelift will coordinate the fix and disclosure.
|
||||
|
||||
## `htmlparser2` for enterprise
|
||||
|
||||
Available as part of the Tidelift Subscription
|
||||
|
||||
The maintainers of `htmlparser2` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-htmlparser2?utm_source=npm-htmlparser2&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import MultiplexHandler from "./MultiplexHandler";
|
||||
import { Handler } from "./Parser";
|
||||
export declare class CollectingHandler extends MultiplexHandler {
|
||||
_cbs: Partial<Handler>;
|
||||
events: [keyof Handler, ...unknown[]][];
|
||||
constructor(cbs?: Partial<Handler>);
|
||||
onreset(): void;
|
||||
restart(): void;
|
||||
}
|
||||
//# sourceMappingURL=CollectingHandler.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"CollectingHandler.d.ts","sourceRoot":"","sources":["../src/CollectingHandler.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC,qBAAa,iBAAkB,SAAQ,gBAAgB;IACnD,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACvB,MAAM,EAAE,CAAC,MAAM,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,EAAE,CAAC;gBAE5B,GAAG,GAAE,OAAO,CAAC,OAAO,CAAM;IAWtC,OAAO;IAKP,OAAO;CAcV"}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
"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 __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var MultiplexHandler_1 = __importDefault(require("./MultiplexHandler"));
|
||||
var CollectingHandler = /** @class */ (function (_super) {
|
||||
__extends(CollectingHandler, _super);
|
||||
function CollectingHandler(cbs) {
|
||||
if (cbs === void 0) { cbs = {}; }
|
||||
var _this = _super.call(this, function (name) {
|
||||
var _a;
|
||||
var args = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
args[_i - 1] = arguments[_i];
|
||||
}
|
||||
_this.events.push([name].concat(args));
|
||||
// @ts-ignore
|
||||
if (_this._cbs[name])
|
||||
(_a = _this._cbs)[name].apply(_a, args);
|
||||
}) || this;
|
||||
_this._cbs = cbs;
|
||||
_this.events = [];
|
||||
return _this;
|
||||
}
|
||||
CollectingHandler.prototype.onreset = function () {
|
||||
this.events = [];
|
||||
if (this._cbs.onreset)
|
||||
this._cbs.onreset();
|
||||
};
|
||||
CollectingHandler.prototype.restart = function () {
|
||||
var _a;
|
||||
if (this._cbs.onreset)
|
||||
this._cbs.onreset();
|
||||
for (var i = 0; i < this.events.length; i++) {
|
||||
var _b = this.events[i], name_1 = _b[0], args = _b.slice(1);
|
||||
if (!this._cbs[name_1]) {
|
||||
continue;
|
||||
}
|
||||
// @ts-ignore
|
||||
(_a = this._cbs)[name_1].apply(_a, args);
|
||||
}
|
||||
};
|
||||
return CollectingHandler;
|
||||
}(MultiplexHandler_1.default));
|
||||
exports.CollectingHandler = CollectingHandler;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import DomHandler, { DomHandlerOptions } from "domhandler";
|
||||
import { ParserOptions } from "./Parser";
|
||||
interface FeedItem {
|
||||
id?: string;
|
||||
title?: string;
|
||||
link?: string;
|
||||
description?: string;
|
||||
pubDate?: Date;
|
||||
}
|
||||
interface Feed {
|
||||
type?: string;
|
||||
id?: string;
|
||||
title?: string;
|
||||
link?: string;
|
||||
description?: string;
|
||||
updated?: Date;
|
||||
author?: string;
|
||||
items?: FeedItem[];
|
||||
}
|
||||
export declare class FeedHandler extends DomHandler {
|
||||
feed?: Feed;
|
||||
/**
|
||||
*
|
||||
* @param callback
|
||||
* @param options
|
||||
*/
|
||||
constructor(callback?: ((error: Error | null) => void) | DomHandlerOptions, options?: DomHandlerOptions);
|
||||
onend(): void;
|
||||
}
|
||||
/**
|
||||
* Parse a feed.
|
||||
*
|
||||
* @param feed The feed that should be parsed, as a string.
|
||||
* @param options Optionally, options for parsing. When using this option, you probably want to set `xmlMode` to `true`.
|
||||
*/
|
||||
export declare function parseFeed(feed: string, options?: ParserOptions & DomHandlerOptions): Feed | undefined;
|
||||
export {};
|
||||
//# sourceMappingURL=FeedHandler.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"FeedHandler.d.ts","sourceRoot":"","sources":["../src/FeedHandler.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,EAAE,EAAE,iBAAiB,EAAiB,MAAM,YAAY,CAAC;AAE1E,OAAO,EAAU,aAAa,EAAE,MAAM,UAAU,CAAC;AAEjD,UAAU,QAAQ;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,IAAI,CAAC;CAClB;AAED,UAAU,IAAI;IACV,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,IAAI,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC;CACtB;AAGD,qBAAa,WAAY,SAAQ,UAAU;IACvC,IAAI,CAAC,EAAE,IAAI,CAAC;IAEZ;;;;OAIG;gBAEC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,CAAC,GAAG,iBAAiB,EAC9D,OAAO,CAAC,EAAE,iBAAiB;IAS/B,KAAK;CAwGR;AA4CD;;;;;GAKG;AACH,wBAAgB,SAAS,CACrB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,aAAa,GAAG,iBAAkC,GAC5D,IAAI,GAAG,SAAS,CAIlB"}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
"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 __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var domhandler_1 = __importDefault(require("domhandler"));
|
||||
var DomUtils = __importStar(require("domutils"));
|
||||
var Parser_1 = require("./Parser");
|
||||
//TODO: Consume data as it is coming in
|
||||
var FeedHandler = /** @class */ (function (_super) {
|
||||
__extends(FeedHandler, _super);
|
||||
/**
|
||||
*
|
||||
* @param callback
|
||||
* @param options
|
||||
*/
|
||||
function FeedHandler(callback, options) {
|
||||
var _this = this;
|
||||
if (typeof callback === "object" && callback !== null) {
|
||||
callback = undefined;
|
||||
options = callback;
|
||||
}
|
||||
_this = _super.call(this, callback, options) || this;
|
||||
return _this;
|
||||
}
|
||||
FeedHandler.prototype.onend = function () {
|
||||
var feed = {};
|
||||
var feedRoot = getOneElement(isValidFeed, this.dom);
|
||||
if (feedRoot) {
|
||||
if (feedRoot.name === "feed") {
|
||||
var childs = feedRoot.children;
|
||||
feed.type = "atom";
|
||||
addConditionally(feed, "id", "id", childs);
|
||||
addConditionally(feed, "title", "title", childs);
|
||||
var href = getAttribute("href", getOneElement("link", childs));
|
||||
if (href) {
|
||||
feed.link = href;
|
||||
}
|
||||
addConditionally(feed, "description", "subtitle", childs);
|
||||
var updated = fetch("updated", childs);
|
||||
if (updated) {
|
||||
feed.updated = new Date(updated);
|
||||
}
|
||||
addConditionally(feed, "author", "email", childs, true);
|
||||
feed.items = getElements("entry", childs).map(function (item) {
|
||||
var entry = {};
|
||||
var children = item.children;
|
||||
addConditionally(entry, "id", "id", children);
|
||||
addConditionally(entry, "title", "title", children);
|
||||
var href = getAttribute("href", getOneElement("link", children));
|
||||
if (href) {
|
||||
entry.link = href;
|
||||
}
|
||||
var description = fetch("summary", children) ||
|
||||
fetch("content", children);
|
||||
if (description) {
|
||||
entry.description = description;
|
||||
}
|
||||
var pubDate = fetch("updated", children);
|
||||
if (pubDate) {
|
||||
entry.pubDate = new Date(pubDate);
|
||||
}
|
||||
return entry;
|
||||
});
|
||||
}
|
||||
else {
|
||||
var childs = getOneElement("channel", feedRoot.children)
|
||||
.children;
|
||||
feed.type = feedRoot.name.substr(0, 3);
|
||||
feed.id = "";
|
||||
addConditionally(feed, "title", "title", childs);
|
||||
addConditionally(feed, "link", "link", childs);
|
||||
addConditionally(feed, "description", "description", childs);
|
||||
var updated = fetch("lastBuildDate", childs);
|
||||
if (updated) {
|
||||
feed.updated = new Date(updated);
|
||||
}
|
||||
addConditionally(feed, "author", "managingEditor", childs, true);
|
||||
feed.items = getElements("item", feedRoot.children).map(function (item) {
|
||||
var entry = {};
|
||||
var children = item.children;
|
||||
addConditionally(entry, "id", "guid", children);
|
||||
addConditionally(entry, "title", "title", children);
|
||||
addConditionally(entry, "link", "link", children);
|
||||
addConditionally(entry, "description", "description", children);
|
||||
var pubDate = fetch("pubDate", children);
|
||||
if (pubDate)
|
||||
entry.pubDate = new Date(pubDate);
|
||||
return entry;
|
||||
});
|
||||
}
|
||||
}
|
||||
this.feed = feed;
|
||||
this.handleCallback(feedRoot ? null : Error("couldn't find root of feed"));
|
||||
};
|
||||
return FeedHandler;
|
||||
}(domhandler_1.default));
|
||||
exports.FeedHandler = FeedHandler;
|
||||
function getElements(what, where) {
|
||||
return DomUtils.getElementsByTagName(what, where, true);
|
||||
}
|
||||
function getOneElement(what, where) {
|
||||
return DomUtils.getElementsByTagName(what, where, true, 1)[0];
|
||||
}
|
||||
function fetch(what, where, recurse) {
|
||||
if (recurse === void 0) { recurse = false; }
|
||||
return DomUtils.getText(DomUtils.getElementsByTagName(what, where, recurse, 1)).trim();
|
||||
}
|
||||
function getAttribute(name, elem) {
|
||||
if (!elem) {
|
||||
return null;
|
||||
}
|
||||
var attribs = elem.attribs;
|
||||
return attribs[name];
|
||||
}
|
||||
function addConditionally(obj, prop, what, where, recurse) {
|
||||
if (recurse === void 0) { recurse = false; }
|
||||
var tmp = fetch(what, where, recurse);
|
||||
// @ts-ignore
|
||||
if (tmp)
|
||||
obj[prop] = tmp;
|
||||
}
|
||||
function isValidFeed(value) {
|
||||
return value === "rss" || value === "feed" || value === "rdf:RDF";
|
||||
}
|
||||
var defaultOptions = { xmlMode: true };
|
||||
/**
|
||||
* Parse a feed.
|
||||
*
|
||||
* @param feed The feed that should be parsed, as a string.
|
||||
* @param options Optionally, options for parsing. When using this option, you probably want to set `xmlMode` to `true`.
|
||||
*/
|
||||
function parseFeed(feed, options) {
|
||||
if (options === void 0) { options = defaultOptions; }
|
||||
var handler = new FeedHandler(options);
|
||||
new Parser_1.Parser(handler, options).end(feed);
|
||||
return handler.feed;
|
||||
}
|
||||
exports.parseFeed = parseFeed;
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
import { Handler } from "./Parser";
|
||||
/**
|
||||
* Calls a specific handler function for all events that are encountered.
|
||||
*
|
||||
* @param func — The function to multiplex all events to.
|
||||
*/
|
||||
export default class MultiplexHandler implements Handler {
|
||||
_func: (event: keyof Handler, ...args: unknown[]) => void;
|
||||
constructor(func: (event: keyof Handler, ...args: unknown[]) => void);
|
||||
onattribute(name: string, value: string): void;
|
||||
oncdatastart(): void;
|
||||
oncdataend(): void;
|
||||
ontext(text: string): void;
|
||||
onprocessinginstruction(name: string, value: string): void;
|
||||
oncomment(comment: string): void;
|
||||
oncommentend(): void;
|
||||
onclosetag(name: string): void;
|
||||
onopentag(name: string, attribs: {
|
||||
[key: string]: string;
|
||||
}): void;
|
||||
onopentagname(name: string): void;
|
||||
onerror(error: Error): void;
|
||||
onend(): void;
|
||||
onparserinit(parser: {}): void;
|
||||
onreset(): void;
|
||||
}
|
||||
//# sourceMappingURL=MultiplexHandler.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"MultiplexHandler.d.ts","sourceRoot":"","sources":["../src/MultiplexHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,gBAAiB,YAAW,OAAO;IACpD,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;gBAE9C,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI;IAKpE,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAGvC,YAAY;IAGZ,UAAU;IAGV,MAAM,CAAC,IAAI,EAAE,MAAM;IAGnB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAGnD,SAAS,CAAC,OAAO,EAAE,MAAM;IAGzB,YAAY;IAGZ,UAAU,CAAC,IAAI,EAAE,MAAM;IAGvB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE;IAG1D,aAAa,CAAC,IAAI,EAAE,MAAM;IAG1B,OAAO,CAAC,KAAK,EAAE,KAAK;IAGpB,KAAK;IAGL,YAAY,CAAC,MAAM,EAAE,EAAE;IAGvB,OAAO;CAGV"}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
/**
|
||||
* Calls a specific handler function for all events that are encountered.
|
||||
*
|
||||
* @param func — The function to multiplex all events to.
|
||||
*/
|
||||
var MultiplexHandler = /** @class */ (function () {
|
||||
function MultiplexHandler(func) {
|
||||
this._func = func;
|
||||
}
|
||||
/* Format: eventname: number of arguments */
|
||||
MultiplexHandler.prototype.onattribute = function (name, value) {
|
||||
this._func("onattribute", name, value);
|
||||
};
|
||||
MultiplexHandler.prototype.oncdatastart = function () {
|
||||
this._func("oncdatastart");
|
||||
};
|
||||
MultiplexHandler.prototype.oncdataend = function () {
|
||||
this._func("oncdataend");
|
||||
};
|
||||
MultiplexHandler.prototype.ontext = function (text) {
|
||||
this._func("ontext", text);
|
||||
};
|
||||
MultiplexHandler.prototype.onprocessinginstruction = function (name, value) {
|
||||
this._func("onprocessinginstruction", name, value);
|
||||
};
|
||||
MultiplexHandler.prototype.oncomment = function (comment) {
|
||||
this._func("oncomment", comment);
|
||||
};
|
||||
MultiplexHandler.prototype.oncommentend = function () {
|
||||
this._func("oncommentend");
|
||||
};
|
||||
MultiplexHandler.prototype.onclosetag = function (name) {
|
||||
this._func("onclosetag", name);
|
||||
};
|
||||
MultiplexHandler.prototype.onopentag = function (name, attribs) {
|
||||
this._func("onopentag", name, attribs);
|
||||
};
|
||||
MultiplexHandler.prototype.onopentagname = function (name) {
|
||||
this._func("onopentagname", name);
|
||||
};
|
||||
MultiplexHandler.prototype.onerror = function (error) {
|
||||
this._func("onerror", error);
|
||||
};
|
||||
MultiplexHandler.prototype.onend = function () {
|
||||
this._func("onend");
|
||||
};
|
||||
MultiplexHandler.prototype.onparserinit = function (parser) {
|
||||
this._func("onparserinit", parser);
|
||||
};
|
||||
MultiplexHandler.prototype.onreset = function () {
|
||||
this._func("onreset");
|
||||
};
|
||||
return MultiplexHandler;
|
||||
}());
|
||||
exports.default = MultiplexHandler;
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/// <reference types="node" />
|
||||
import Tokenizer from "./Tokenizer";
|
||||
import { EventEmitter } from "events";
|
||||
export interface ParserOptions {
|
||||
/***
|
||||
* Indicates whether special tags (<script> and <style>) should get special treatment
|
||||
* and if "empty" tags (eg. <br>) can have children. If `false`, the content of special tags
|
||||
* will be text only. For feeds and other XML content (documents that don't consist of HTML),
|
||||
* set this to `true`. Default: `false`.
|
||||
*/
|
||||
xmlMode?: boolean;
|
||||
/***
|
||||
* If set to true, entities within the document will be decoded. Defaults to `false`.
|
||||
*/
|
||||
decodeEntities?: boolean;
|
||||
/***
|
||||
* If set to true, all tags will be lowercased. If xmlMode is disabled, this defaults to `true`.
|
||||
*/
|
||||
lowerCaseTags?: boolean;
|
||||
/***
|
||||
* If set to `true`, all attribute names will be lowercased. This has noticeable impact on speed, so it defaults to `false`.
|
||||
*/
|
||||
lowerCaseAttributeNames?: boolean;
|
||||
/***
|
||||
* If set to true, CDATA sections will be recognized as text even if the xmlMode option is not enabled.
|
||||
* NOTE: If xmlMode is set to `true` then CDATA sections will always be recognized as text.
|
||||
*/
|
||||
recognizeCDATA?: boolean;
|
||||
/***
|
||||
* If set to `true`, self-closing tags will trigger the onclosetag event even if xmlMode is not set to `true`.
|
||||
* NOTE: If xmlMode is set to `true` then self-closing tags will always be recognized.
|
||||
*/
|
||||
recognizeSelfClosing?: boolean;
|
||||
/**
|
||||
* Allows the default tokenizer to be overwritten.
|
||||
*/
|
||||
Tokenizer?: typeof Tokenizer;
|
||||
}
|
||||
export interface Handler {
|
||||
onparserinit(parser: Parser): void;
|
||||
/***
|
||||
* Resets the handler back to starting state
|
||||
*/
|
||||
onreset(): void;
|
||||
/***
|
||||
* Signals the handler that parsing is done
|
||||
*/
|
||||
onend(): void;
|
||||
onerror(error: Error): void;
|
||||
onclosetag(name: string): void;
|
||||
onopentagname(name: string): void;
|
||||
onattribute(name: string, value: string): void;
|
||||
onopentag(name: string, attribs: {
|
||||
[s: string]: string;
|
||||
}): void;
|
||||
ontext(data: string): void;
|
||||
oncomment(data: string): void;
|
||||
oncdatastart(): void;
|
||||
oncdataend(): void;
|
||||
oncommentend(): void;
|
||||
onprocessinginstruction(name: string, data: string): void;
|
||||
}
|
||||
export declare class Parser extends EventEmitter {
|
||||
_tagname: string;
|
||||
_attribname: string;
|
||||
_attribvalue: string;
|
||||
_attribs: null | {
|
||||
[key: string]: string;
|
||||
};
|
||||
_stack: string[];
|
||||
_foreignContext: boolean[];
|
||||
startIndex: number;
|
||||
endIndex: number | null;
|
||||
_cbs: Partial<Handler>;
|
||||
_options: ParserOptions;
|
||||
_lowerCaseTagNames: boolean;
|
||||
_lowerCaseAttributeNames: boolean;
|
||||
_tokenizer: Tokenizer;
|
||||
constructor(cbs: Partial<Handler> | null, options?: ParserOptions);
|
||||
_updatePosition(initialOffset: number): void;
|
||||
ontext(data: string): void;
|
||||
onopentagname(name: string): void;
|
||||
onopentagend(): void;
|
||||
onclosetag(name: string): void;
|
||||
onselfclosingtag(): void;
|
||||
_closeCurrentTag(): void;
|
||||
onattribname(name: string): void;
|
||||
onattribdata(value: string): void;
|
||||
onattribend(): void;
|
||||
_getInstructionName(value: string): string;
|
||||
ondeclaration(value: string): void;
|
||||
onprocessinginstruction(value: string): void;
|
||||
oncomment(value: string): void;
|
||||
oncdata(value: string): void;
|
||||
onerror(err: Error): void;
|
||||
onend(): void;
|
||||
reset(): void;
|
||||
parseComplete(data: string): void;
|
||||
write(chunk: string): void;
|
||||
end(chunk?: string): void;
|
||||
pause(): void;
|
||||
resume(): void;
|
||||
parseChunk: (chunk: string) => void;
|
||||
done: (chunk?: string | undefined) => void;
|
||||
}
|
||||
//# sourceMappingURL=Parser.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Parser.d.ts","sourceRoot":"","sources":["../src/Parser.ts"],"names":[],"mappings":"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"}
|
||||
+371
@@ -0,0 +1,371 @@
|
||||
"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 __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Tokenizer_1 = __importDefault(require("./Tokenizer"));
|
||||
var events_1 = require("events");
|
||||
var formTags = new Set([
|
||||
"input",
|
||||
"option",
|
||||
"optgroup",
|
||||
"select",
|
||||
"button",
|
||||
"datalist",
|
||||
"textarea"
|
||||
]);
|
||||
var pTag = new Set(["p"]);
|
||||
var openImpliesClose = {
|
||||
tr: new Set(["tr", "th", "td"]),
|
||||
th: new Set(["th"]),
|
||||
td: new Set(["thead", "th", "td"]),
|
||||
body: new Set(["head", "link", "script"]),
|
||||
li: new Set(["li"]),
|
||||
p: pTag,
|
||||
h1: pTag,
|
||||
h2: pTag,
|
||||
h3: pTag,
|
||||
h4: pTag,
|
||||
h5: pTag,
|
||||
h6: pTag,
|
||||
select: formTags,
|
||||
input: formTags,
|
||||
output: formTags,
|
||||
button: formTags,
|
||||
datalist: formTags,
|
||||
textarea: formTags,
|
||||
option: new Set(["option"]),
|
||||
optgroup: new Set(["optgroup", "option"]),
|
||||
dd: new Set(["dt", "dd"]),
|
||||
dt: new Set(["dt", "dd"]),
|
||||
address: pTag,
|
||||
article: pTag,
|
||||
aside: pTag,
|
||||
blockquote: pTag,
|
||||
details: pTag,
|
||||
div: pTag,
|
||||
dl: pTag,
|
||||
fieldset: pTag,
|
||||
figcaption: pTag,
|
||||
figure: pTag,
|
||||
footer: pTag,
|
||||
form: pTag,
|
||||
header: pTag,
|
||||
hr: pTag,
|
||||
main: pTag,
|
||||
nav: pTag,
|
||||
ol: pTag,
|
||||
pre: pTag,
|
||||
section: pTag,
|
||||
table: pTag,
|
||||
ul: pTag,
|
||||
rt: new Set(["rt", "rp"]),
|
||||
rp: new Set(["rt", "rp"]),
|
||||
tbody: new Set(["thead", "tbody"]),
|
||||
tfoot: new Set(["thead", "tbody"])
|
||||
};
|
||||
var voidElements = new Set([
|
||||
"area",
|
||||
"base",
|
||||
"basefont",
|
||||
"br",
|
||||
"col",
|
||||
"command",
|
||||
"embed",
|
||||
"frame",
|
||||
"hr",
|
||||
"img",
|
||||
"input",
|
||||
"isindex",
|
||||
"keygen",
|
||||
"link",
|
||||
"meta",
|
||||
"param",
|
||||
"source",
|
||||
"track",
|
||||
"wbr"
|
||||
]);
|
||||
var foreignContextElements = new Set(["math", "svg"]);
|
||||
var htmlIntegrationElements = new Set([
|
||||
"mi",
|
||||
"mo",
|
||||
"mn",
|
||||
"ms",
|
||||
"mtext",
|
||||
"annotation-xml",
|
||||
"foreignObject",
|
||||
"desc",
|
||||
"title"
|
||||
]);
|
||||
var reNameEnd = /\s|\//;
|
||||
var Parser = /** @class */ (function (_super) {
|
||||
__extends(Parser, _super);
|
||||
function Parser(cbs, options) {
|
||||
var _this = _super.call(this) || this;
|
||||
_this._tagname = "";
|
||||
_this._attribname = "";
|
||||
_this._attribvalue = "";
|
||||
_this._attribs = null;
|
||||
_this._stack = [];
|
||||
_this._foreignContext = [];
|
||||
_this.startIndex = 0;
|
||||
_this.endIndex = null;
|
||||
// Aliases for backwards compatibility
|
||||
_this.parseChunk = Parser.prototype.write;
|
||||
_this.done = Parser.prototype.end;
|
||||
_this._options = options || {};
|
||||
_this._cbs = cbs || {};
|
||||
_this._tagname = "";
|
||||
_this._attribname = "";
|
||||
_this._attribvalue = "";
|
||||
_this._attribs = null;
|
||||
_this._stack = [];
|
||||
_this._foreignContext = [];
|
||||
_this.startIndex = 0;
|
||||
_this.endIndex = null;
|
||||
_this._lowerCaseTagNames =
|
||||
"lowerCaseTags" in _this._options
|
||||
? !!_this._options.lowerCaseTags
|
||||
: !_this._options.xmlMode;
|
||||
_this._lowerCaseAttributeNames =
|
||||
"lowerCaseAttributeNames" in _this._options
|
||||
? !!_this._options.lowerCaseAttributeNames
|
||||
: !_this._options.xmlMode;
|
||||
_this._tokenizer = new (_this._options.Tokenizer || Tokenizer_1.default)(_this._options, _this);
|
||||
if (_this._cbs.onparserinit)
|
||||
_this._cbs.onparserinit(_this);
|
||||
return _this;
|
||||
}
|
||||
Parser.prototype._updatePosition = function (initialOffset) {
|
||||
if (this.endIndex === null) {
|
||||
if (this._tokenizer._sectionStart <= initialOffset) {
|
||||
this.startIndex = 0;
|
||||
}
|
||||
else {
|
||||
this.startIndex = this._tokenizer._sectionStart - initialOffset;
|
||||
}
|
||||
}
|
||||
else
|
||||
this.startIndex = this.endIndex + 1;
|
||||
this.endIndex = this._tokenizer.getAbsoluteIndex();
|
||||
};
|
||||
//Tokenizer event handlers
|
||||
Parser.prototype.ontext = function (data) {
|
||||
this._updatePosition(1);
|
||||
// @ts-ignore
|
||||
this.endIndex--;
|
||||
if (this._cbs.ontext)
|
||||
this._cbs.ontext(data);
|
||||
};
|
||||
Parser.prototype.onopentagname = function (name) {
|
||||
if (this._lowerCaseTagNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
this._tagname = name;
|
||||
if (!this._options.xmlMode &&
|
||||
Object.prototype.hasOwnProperty.call(openImpliesClose, name)) {
|
||||
for (var el = void 0;
|
||||
// @ts-ignore
|
||||
openImpliesClose[name].has((el = this._stack[this._stack.length - 1])); this.onclosetag(el))
|
||||
;
|
||||
}
|
||||
if (this._options.xmlMode || !voidElements.has(name)) {
|
||||
this._stack.push(name);
|
||||
if (foreignContextElements.has(name)) {
|
||||
this._foreignContext.push(true);
|
||||
}
|
||||
else if (htmlIntegrationElements.has(name)) {
|
||||
this._foreignContext.push(false);
|
||||
}
|
||||
}
|
||||
if (this._cbs.onopentagname)
|
||||
this._cbs.onopentagname(name);
|
||||
if (this._cbs.onopentag)
|
||||
this._attribs = {};
|
||||
};
|
||||
Parser.prototype.onopentagend = function () {
|
||||
this._updatePosition(1);
|
||||
if (this._attribs) {
|
||||
if (this._cbs.onopentag) {
|
||||
this._cbs.onopentag(this._tagname, this._attribs);
|
||||
}
|
||||
this._attribs = null;
|
||||
}
|
||||
if (!this._options.xmlMode &&
|
||||
this._cbs.onclosetag &&
|
||||
voidElements.has(this._tagname)) {
|
||||
this._cbs.onclosetag(this._tagname);
|
||||
}
|
||||
this._tagname = "";
|
||||
};
|
||||
Parser.prototype.onclosetag = function (name) {
|
||||
this._updatePosition(1);
|
||||
if (this._lowerCaseTagNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
if (foreignContextElements.has(name) ||
|
||||
htmlIntegrationElements.has(name)) {
|
||||
this._foreignContext.pop();
|
||||
}
|
||||
if (this._stack.length &&
|
||||
(this._options.xmlMode || !voidElements.has(name))) {
|
||||
var pos = this._stack.lastIndexOf(name);
|
||||
if (pos !== -1) {
|
||||
if (this._cbs.onclosetag) {
|
||||
pos = this._stack.length - pos;
|
||||
// @ts-ignore
|
||||
while (pos--)
|
||||
this._cbs.onclosetag(this._stack.pop());
|
||||
}
|
||||
else
|
||||
this._stack.length = pos;
|
||||
}
|
||||
else if (name === "p" && !this._options.xmlMode) {
|
||||
this.onopentagname(name);
|
||||
this._closeCurrentTag();
|
||||
}
|
||||
}
|
||||
else if (!this._options.xmlMode && (name === "br" || name === "p")) {
|
||||
this.onopentagname(name);
|
||||
this._closeCurrentTag();
|
||||
}
|
||||
};
|
||||
Parser.prototype.onselfclosingtag = function () {
|
||||
if (this._options.xmlMode ||
|
||||
this._options.recognizeSelfClosing ||
|
||||
this._foreignContext[this._foreignContext.length - 1]) {
|
||||
this._closeCurrentTag();
|
||||
}
|
||||
else {
|
||||
this.onopentagend();
|
||||
}
|
||||
};
|
||||
Parser.prototype._closeCurrentTag = function () {
|
||||
var name = this._tagname;
|
||||
this.onopentagend();
|
||||
//self-closing tags will be on the top of the stack
|
||||
//(cheaper check than in onclosetag)
|
||||
if (this._stack[this._stack.length - 1] === name) {
|
||||
if (this._cbs.onclosetag) {
|
||||
this._cbs.onclosetag(name);
|
||||
}
|
||||
this._stack.pop();
|
||||
}
|
||||
};
|
||||
Parser.prototype.onattribname = function (name) {
|
||||
if (this._lowerCaseAttributeNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
this._attribname = name;
|
||||
};
|
||||
Parser.prototype.onattribdata = function (value) {
|
||||
this._attribvalue += value;
|
||||
};
|
||||
Parser.prototype.onattribend = function () {
|
||||
if (this._cbs.onattribute)
|
||||
this._cbs.onattribute(this._attribname, this._attribvalue);
|
||||
if (this._attribs &&
|
||||
!Object.prototype.hasOwnProperty.call(this._attribs, this._attribname)) {
|
||||
this._attribs[this._attribname] = this._attribvalue;
|
||||
}
|
||||
this._attribname = "";
|
||||
this._attribvalue = "";
|
||||
};
|
||||
Parser.prototype._getInstructionName = function (value) {
|
||||
var idx = value.search(reNameEnd);
|
||||
var name = idx < 0 ? value : value.substr(0, idx);
|
||||
if (this._lowerCaseTagNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
return name;
|
||||
};
|
||||
Parser.prototype.ondeclaration = function (value) {
|
||||
if (this._cbs.onprocessinginstruction) {
|
||||
var name_1 = this._getInstructionName(value);
|
||||
this._cbs.onprocessinginstruction("!" + name_1, "!" + value);
|
||||
}
|
||||
};
|
||||
Parser.prototype.onprocessinginstruction = function (value) {
|
||||
if (this._cbs.onprocessinginstruction) {
|
||||
var name_2 = this._getInstructionName(value);
|
||||
this._cbs.onprocessinginstruction("?" + name_2, "?" + value);
|
||||
}
|
||||
};
|
||||
Parser.prototype.oncomment = function (value) {
|
||||
this._updatePosition(4);
|
||||
if (this._cbs.oncomment)
|
||||
this._cbs.oncomment(value);
|
||||
if (this._cbs.oncommentend)
|
||||
this._cbs.oncommentend();
|
||||
};
|
||||
Parser.prototype.oncdata = function (value) {
|
||||
this._updatePosition(1);
|
||||
if (this._options.xmlMode || this._options.recognizeCDATA) {
|
||||
if (this._cbs.oncdatastart)
|
||||
this._cbs.oncdatastart();
|
||||
if (this._cbs.ontext)
|
||||
this._cbs.ontext(value);
|
||||
if (this._cbs.oncdataend)
|
||||
this._cbs.oncdataend();
|
||||
}
|
||||
else {
|
||||
this.oncomment("[CDATA[" + value + "]]");
|
||||
}
|
||||
};
|
||||
Parser.prototype.onerror = function (err) {
|
||||
if (this._cbs.onerror)
|
||||
this._cbs.onerror(err);
|
||||
};
|
||||
Parser.prototype.onend = function () {
|
||||
if (this._cbs.onclosetag) {
|
||||
for (var i = this._stack.length; i > 0; this._cbs.onclosetag(this._stack[--i]))
|
||||
;
|
||||
}
|
||||
if (this._cbs.onend)
|
||||
this._cbs.onend();
|
||||
};
|
||||
//Resets the parser to a blank state, ready to parse a new HTML document
|
||||
Parser.prototype.reset = function () {
|
||||
if (this._cbs.onreset)
|
||||
this._cbs.onreset();
|
||||
this._tokenizer.reset();
|
||||
this._tagname = "";
|
||||
this._attribname = "";
|
||||
this._attribs = null;
|
||||
this._stack = [];
|
||||
if (this._cbs.onparserinit)
|
||||
this._cbs.onparserinit(this);
|
||||
};
|
||||
//Parses a complete HTML document and pushes it to the handler
|
||||
Parser.prototype.parseComplete = function (data) {
|
||||
this.reset();
|
||||
this.end(data);
|
||||
};
|
||||
Parser.prototype.write = function (chunk) {
|
||||
this._tokenizer.write(chunk);
|
||||
};
|
||||
Parser.prototype.end = function (chunk) {
|
||||
this._tokenizer.end(chunk);
|
||||
};
|
||||
Parser.prototype.pause = function () {
|
||||
this._tokenizer.pause();
|
||||
};
|
||||
Parser.prototype.resume = function () {
|
||||
this._tokenizer.resume();
|
||||
};
|
||||
return Parser;
|
||||
}(events_1.EventEmitter));
|
||||
exports.Parser = Parser;
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
/** All the states the tokenizer can be in. */
|
||||
declare const enum State {
|
||||
Text = 1,
|
||||
BeforeTagName = 2,
|
||||
InTagName = 3,
|
||||
InSelfClosingTag = 4,
|
||||
BeforeClosingTagName = 5,
|
||||
InClosingTagName = 6,
|
||||
AfterClosingTagName = 7,
|
||||
BeforeAttributeName = 8,
|
||||
InAttributeName = 9,
|
||||
AfterAttributeName = 10,
|
||||
BeforeAttributeValue = 11,
|
||||
InAttributeValueDq = 12,
|
||||
InAttributeValueSq = 13,
|
||||
InAttributeValueNq = 14,
|
||||
BeforeDeclaration = 15,
|
||||
InDeclaration = 16,
|
||||
InProcessingInstruction = 17,
|
||||
BeforeComment = 18,
|
||||
InComment = 19,
|
||||
AfterComment1 = 20,
|
||||
AfterComment2 = 21,
|
||||
BeforeCdata1 = 22,
|
||||
BeforeCdata2 = 23,
|
||||
BeforeCdata3 = 24,
|
||||
BeforeCdata4 = 25,
|
||||
BeforeCdata5 = 26,
|
||||
BeforeCdata6 = 27,
|
||||
InCdata = 28,
|
||||
AfterCdata1 = 29,
|
||||
AfterCdata2 = 30,
|
||||
BeforeSpecial = 31,
|
||||
BeforeSpecialEnd = 32,
|
||||
BeforeScript1 = 33,
|
||||
BeforeScript2 = 34,
|
||||
BeforeScript3 = 35,
|
||||
BeforeScript4 = 36,
|
||||
BeforeScript5 = 37,
|
||||
AfterScript1 = 38,
|
||||
AfterScript2 = 39,
|
||||
AfterScript3 = 40,
|
||||
AfterScript4 = 41,
|
||||
AfterScript5 = 42,
|
||||
BeforeStyle1 = 43,
|
||||
BeforeStyle2 = 44,
|
||||
BeforeStyle3 = 45,
|
||||
BeforeStyle4 = 46,
|
||||
AfterStyle1 = 47,
|
||||
AfterStyle2 = 48,
|
||||
AfterStyle3 = 49,
|
||||
AfterStyle4 = 50,
|
||||
BeforeEntity = 51,
|
||||
BeforeNumericEntity = 52,
|
||||
InNamedEntity = 53,
|
||||
InNumericEntity = 54,
|
||||
InHexEntity = 55
|
||||
}
|
||||
declare const enum Special {
|
||||
None = 1,
|
||||
Script = 2,
|
||||
Style = 3
|
||||
}
|
||||
interface Callbacks {
|
||||
onattribdata(value: string): void;
|
||||
onattribend(): void;
|
||||
onattribname(name: string): void;
|
||||
oncdata(data: string): void;
|
||||
onclosetag(name: string): void;
|
||||
oncomment(data: string): void;
|
||||
ondeclaration(content: string): void;
|
||||
onend(): void;
|
||||
onerror(error: Error, state?: State): void;
|
||||
onopentagend(): void;
|
||||
onopentagname(name: string): void;
|
||||
onprocessinginstruction(instruction: string): void;
|
||||
onselfclosingtag(): void;
|
||||
ontext(value: string): void;
|
||||
}
|
||||
export default class Tokenizer {
|
||||
/** The current state the tokenizer is in. */
|
||||
_state: State;
|
||||
/** The read buffer. */
|
||||
_buffer: string;
|
||||
/** The beginning of the section that is currently being read. */
|
||||
_sectionStart: number;
|
||||
/** The index within the buffer that we are currently looking at. */
|
||||
_index: number;
|
||||
/**
|
||||
* Data that has already been processed will be removed from the buffer occasionally.
|
||||
* `_bufferOffset` keeps track of how many characters have been removed, to make sure position information is accurate.
|
||||
*/
|
||||
_bufferOffset: number;
|
||||
/** Some behavior, eg. when decoding entities, is done while we are in another state. This keeps track of the other state type. */
|
||||
_baseState: State;
|
||||
/** For special parsing behavior inside of script and style tags. */
|
||||
_special: Special;
|
||||
/** Indicates whether the tokenizer has been paused. */
|
||||
_running: boolean;
|
||||
/** Indicates whether the tokenizer has finished running / `.end` has been called. */
|
||||
_ended: boolean;
|
||||
_cbs: Callbacks;
|
||||
_xmlMode: boolean;
|
||||
_decodeEntities: boolean;
|
||||
constructor(options: {
|
||||
xmlMode?: boolean;
|
||||
decodeEntities?: boolean;
|
||||
} | null, cbs: Callbacks);
|
||||
reset(): void;
|
||||
_stateText(c: string): void;
|
||||
_stateBeforeTagName(c: string): void;
|
||||
_stateInTagName(c: string): void;
|
||||
_stateBeforeClosingTagName(c: string): void;
|
||||
_stateInClosingTagName(c: string): void;
|
||||
_stateAfterClosingTagName(c: string): void;
|
||||
_stateBeforeAttributeName(c: string): void;
|
||||
_stateInSelfClosingTag(c: string): void;
|
||||
_stateInAttributeName(c: string): void;
|
||||
_stateAfterAttributeName(c: string): void;
|
||||
_stateBeforeAttributeValue(c: string): void;
|
||||
_stateInAttributeValueDoubleQuotes(c: string): void;
|
||||
_stateInAttributeValueSingleQuotes(c: string): void;
|
||||
_stateInAttributeValueNoQuotes(c: string): void;
|
||||
_stateBeforeDeclaration(c: string): void;
|
||||
_stateInDeclaration(c: string): void;
|
||||
_stateInProcessingInstruction(c: string): void;
|
||||
_stateBeforeComment(c: string): void;
|
||||
_stateInComment(c: string): void;
|
||||
_stateAfterComment1(c: string): void;
|
||||
_stateAfterComment2(c: string): void;
|
||||
_stateBeforeCdata6(c: string): void;
|
||||
_stateInCdata(c: string): void;
|
||||
_stateAfterCdata1(c: string): void;
|
||||
_stateAfterCdata2(c: string): void;
|
||||
_stateBeforeSpecial(c: string): void;
|
||||
_stateBeforeSpecialEnd(c: string): void;
|
||||
_stateBeforeScript5(c: string): void;
|
||||
_stateAfterScript5(c: string): void;
|
||||
_stateBeforeStyle4(c: string): void;
|
||||
_stateAfterStyle4(c: string): void;
|
||||
_parseNamedEntityStrict(): void;
|
||||
_parseLegacyEntity(): void;
|
||||
_stateInNamedEntity(c: string): void;
|
||||
_decodeNumericEntity(offset: number, base: number): void;
|
||||
_stateInNumericEntity(c: string): void;
|
||||
_stateInHexEntity(c: string): void;
|
||||
_cleanup(): void;
|
||||
write(chunk: string): void;
|
||||
_parse(): void;
|
||||
pause(): void;
|
||||
resume(): void;
|
||||
end(chunk?: string): void;
|
||||
_finish(): void;
|
||||
_handleTrailingData(): void;
|
||||
getAbsoluteIndex(): number;
|
||||
_getSection(): string;
|
||||
_emitToken(name: "onopentagname" | "onclosetag" | "onattribdata"): void;
|
||||
_emitPartial(value: string): void;
|
||||
}
|
||||
export {};
|
||||
//# sourceMappingURL=Tokenizer.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Tokenizer.d.ts","sourceRoot":"","sources":["../src/Tokenizer.ts"],"names":[],"mappings":"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"}
|
||||
+859
@@ -0,0 +1,859 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var decode_codepoint_1 = __importDefault(require("entities/lib/decode_codepoint"));
|
||||
var entities_json_1 = __importDefault(require("entities/lib/maps/entities.json"));
|
||||
var legacy_json_1 = __importDefault(require("entities/lib/maps/legacy.json"));
|
||||
var xml_json_1 = __importDefault(require("entities/lib/maps/xml.json"));
|
||||
function whitespace(c) {
|
||||
return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
|
||||
}
|
||||
function ifElseState(upper, SUCCESS, FAILURE) {
|
||||
var lower = upper.toLowerCase();
|
||||
if (upper === lower) {
|
||||
return function (t, c) {
|
||||
if (c === lower) {
|
||||
t._state = SUCCESS;
|
||||
}
|
||||
else {
|
||||
t._state = FAILURE;
|
||||
t._index--;
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
return function (t, c) {
|
||||
if (c === lower || c === upper) {
|
||||
t._state = SUCCESS;
|
||||
}
|
||||
else {
|
||||
t._state = FAILURE;
|
||||
t._index--;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
function consumeSpecialNameChar(upper, NEXT_STATE) {
|
||||
var lower = upper.toLowerCase();
|
||||
return function (t, c) {
|
||||
if (c === lower || c === upper) {
|
||||
t._state = NEXT_STATE;
|
||||
}
|
||||
else {
|
||||
t._state = 3 /* InTagName */;
|
||||
t._index--; //consume the token again
|
||||
}
|
||||
};
|
||||
}
|
||||
var stateBeforeCdata1 = ifElseState("C", 23 /* BeforeCdata2 */, 16 /* InDeclaration */);
|
||||
var stateBeforeCdata2 = ifElseState("D", 24 /* BeforeCdata3 */, 16 /* InDeclaration */);
|
||||
var stateBeforeCdata3 = ifElseState("A", 25 /* BeforeCdata4 */, 16 /* InDeclaration */);
|
||||
var stateBeforeCdata4 = ifElseState("T", 26 /* BeforeCdata5 */, 16 /* InDeclaration */);
|
||||
var stateBeforeCdata5 = ifElseState("A", 27 /* BeforeCdata6 */, 16 /* InDeclaration */);
|
||||
var stateBeforeScript1 = consumeSpecialNameChar("R", 34 /* BeforeScript2 */);
|
||||
var stateBeforeScript2 = consumeSpecialNameChar("I", 35 /* BeforeScript3 */);
|
||||
var stateBeforeScript3 = consumeSpecialNameChar("P", 36 /* BeforeScript4 */);
|
||||
var stateBeforeScript4 = consumeSpecialNameChar("T", 37 /* BeforeScript5 */);
|
||||
var stateAfterScript1 = ifElseState("R", 39 /* AfterScript2 */, 1 /* Text */);
|
||||
var stateAfterScript2 = ifElseState("I", 40 /* AfterScript3 */, 1 /* Text */);
|
||||
var stateAfterScript3 = ifElseState("P", 41 /* AfterScript4 */, 1 /* Text */);
|
||||
var stateAfterScript4 = ifElseState("T", 42 /* AfterScript5 */, 1 /* Text */);
|
||||
var stateBeforeStyle1 = consumeSpecialNameChar("Y", 44 /* BeforeStyle2 */);
|
||||
var stateBeforeStyle2 = consumeSpecialNameChar("L", 45 /* BeforeStyle3 */);
|
||||
var stateBeforeStyle3 = consumeSpecialNameChar("E", 46 /* BeforeStyle4 */);
|
||||
var stateAfterStyle1 = ifElseState("Y", 48 /* AfterStyle2 */, 1 /* Text */);
|
||||
var stateAfterStyle2 = ifElseState("L", 49 /* AfterStyle3 */, 1 /* Text */);
|
||||
var stateAfterStyle3 = ifElseState("E", 50 /* AfterStyle4 */, 1 /* Text */);
|
||||
var stateBeforeEntity = ifElseState("#", 52 /* BeforeNumericEntity */, 53 /* InNamedEntity */);
|
||||
var stateBeforeNumericEntity = ifElseState("X", 55 /* InHexEntity */, 54 /* InNumericEntity */);
|
||||
var Tokenizer = /** @class */ (function () {
|
||||
function Tokenizer(options, cbs) {
|
||||
/** The current state the tokenizer is in. */
|
||||
this._state = 1 /* Text */;
|
||||
/** The read buffer. */
|
||||
this._buffer = "";
|
||||
/** The beginning of the section that is currently being read. */
|
||||
this._sectionStart = 0;
|
||||
/** The index within the buffer that we are currently looking at. */
|
||||
this._index = 0;
|
||||
/**
|
||||
* Data that has already been processed will be removed from the buffer occasionally.
|
||||
* `_bufferOffset` keeps track of how many characters have been removed, to make sure position information is accurate.
|
||||
*/
|
||||
this._bufferOffset = 0;
|
||||
/** Some behavior, eg. when decoding entities, is done while we are in another state. This keeps track of the other state type. */
|
||||
this._baseState = 1 /* Text */;
|
||||
/** For special parsing behavior inside of script and style tags. */
|
||||
this._special = 1 /* None */;
|
||||
/** Indicates whether the tokenizer has been paused. */
|
||||
this._running = true;
|
||||
/** Indicates whether the tokenizer has finished running / `.end` has been called. */
|
||||
this._ended = false;
|
||||
this._cbs = cbs;
|
||||
this._xmlMode = !!(options && options.xmlMode);
|
||||
this._decodeEntities = !!(options && options.decodeEntities);
|
||||
}
|
||||
Tokenizer.prototype.reset = function () {
|
||||
this._state = 1 /* Text */;
|
||||
this._buffer = "";
|
||||
this._sectionStart = 0;
|
||||
this._index = 0;
|
||||
this._bufferOffset = 0;
|
||||
this._baseState = 1 /* Text */;
|
||||
this._special = 1 /* None */;
|
||||
this._running = true;
|
||||
this._ended = false;
|
||||
};
|
||||
Tokenizer.prototype._stateText = function (c) {
|
||||
if (c === "<") {
|
||||
if (this._index > this._sectionStart) {
|
||||
this._cbs.ontext(this._getSection());
|
||||
}
|
||||
this._state = 2 /* BeforeTagName */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
else if (this._decodeEntities &&
|
||||
this._special === 1 /* None */ &&
|
||||
c === "&") {
|
||||
if (this._index > this._sectionStart) {
|
||||
this._cbs.ontext(this._getSection());
|
||||
}
|
||||
this._baseState = 1 /* Text */;
|
||||
this._state = 51 /* BeforeEntity */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeTagName = function (c) {
|
||||
if (c === "/") {
|
||||
this._state = 5 /* BeforeClosingTagName */;
|
||||
}
|
||||
else if (c === "<") {
|
||||
this._cbs.ontext(this._getSection());
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
else if (c === ">" ||
|
||||
this._special !== 1 /* None */ ||
|
||||
whitespace(c)) {
|
||||
this._state = 1 /* Text */;
|
||||
}
|
||||
else if (c === "!") {
|
||||
this._state = 15 /* BeforeDeclaration */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (c === "?") {
|
||||
this._state = 17 /* InProcessingInstruction */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else {
|
||||
this._state =
|
||||
!this._xmlMode && (c === "s" || c === "S")
|
||||
? 31 /* BeforeSpecial */
|
||||
: 3 /* InTagName */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInTagName = function (c) {
|
||||
if (c === "/" || c === ">" || whitespace(c)) {
|
||||
this._emitToken("onopentagname");
|
||||
this._state = 8 /* BeforeAttributeName */;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeClosingTagName = function (c) {
|
||||
if (whitespace(c)) {
|
||||
// ignore
|
||||
}
|
||||
else if (c === ">") {
|
||||
this._state = 1 /* Text */;
|
||||
}
|
||||
else if (this._special !== 1 /* None */) {
|
||||
if (c === "s" || c === "S") {
|
||||
this._state = 32 /* BeforeSpecialEnd */;
|
||||
}
|
||||
else {
|
||||
this._state = 1 /* Text */;
|
||||
this._index--;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._state = 6 /* InClosingTagName */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInClosingTagName = function (c) {
|
||||
if (c === ">" || whitespace(c)) {
|
||||
this._emitToken("onclosetag");
|
||||
this._state = 7 /* AfterClosingTagName */;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateAfterClosingTagName = function (c) {
|
||||
//skip everything until ">"
|
||||
if (c === ">") {
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeAttributeName = function (c) {
|
||||
if (c === ">") {
|
||||
this._cbs.onopentagend();
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (c === "/") {
|
||||
this._state = 4 /* InSelfClosingTag */;
|
||||
}
|
||||
else if (!whitespace(c)) {
|
||||
this._state = 9 /* InAttributeName */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInSelfClosingTag = function (c) {
|
||||
if (c === ">") {
|
||||
this._cbs.onselfclosingtag();
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (!whitespace(c)) {
|
||||
this._state = 8 /* BeforeAttributeName */;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInAttributeName = function (c) {
|
||||
if (c === "=" || c === "/" || c === ">" || whitespace(c)) {
|
||||
this._cbs.onattribname(this._getSection());
|
||||
this._sectionStart = -1;
|
||||
this._state = 10 /* AfterAttributeName */;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateAfterAttributeName = function (c) {
|
||||
if (c === "=") {
|
||||
this._state = 11 /* BeforeAttributeValue */;
|
||||
}
|
||||
else if (c === "/" || c === ">") {
|
||||
this._cbs.onattribend();
|
||||
this._state = 8 /* BeforeAttributeName */;
|
||||
this._index--;
|
||||
}
|
||||
else if (!whitespace(c)) {
|
||||
this._cbs.onattribend();
|
||||
this._state = 9 /* InAttributeName */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeAttributeValue = function (c) {
|
||||
if (c === '"') {
|
||||
this._state = 12 /* InAttributeValueDq */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (c === "'") {
|
||||
this._state = 13 /* InAttributeValueSq */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (!whitespace(c)) {
|
||||
this._state = 14 /* InAttributeValueNq */;
|
||||
this._sectionStart = this._index;
|
||||
this._index--; //reconsume token
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInAttributeValueDoubleQuotes = function (c) {
|
||||
if (c === '"') {
|
||||
this._emitToken("onattribdata");
|
||||
this._cbs.onattribend();
|
||||
this._state = 8 /* BeforeAttributeName */;
|
||||
}
|
||||
else if (this._decodeEntities && c === "&") {
|
||||
this._emitToken("onattribdata");
|
||||
this._baseState = this._state;
|
||||
this._state = 51 /* BeforeEntity */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInAttributeValueSingleQuotes = function (c) {
|
||||
if (c === "'") {
|
||||
this._emitToken("onattribdata");
|
||||
this._cbs.onattribend();
|
||||
this._state = 8 /* BeforeAttributeName */;
|
||||
}
|
||||
else if (this._decodeEntities && c === "&") {
|
||||
this._emitToken("onattribdata");
|
||||
this._baseState = this._state;
|
||||
this._state = 51 /* BeforeEntity */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInAttributeValueNoQuotes = function (c) {
|
||||
if (whitespace(c) || c === ">") {
|
||||
this._emitToken("onattribdata");
|
||||
this._cbs.onattribend();
|
||||
this._state = 8 /* BeforeAttributeName */;
|
||||
this._index--;
|
||||
}
|
||||
else if (this._decodeEntities && c === "&") {
|
||||
this._emitToken("onattribdata");
|
||||
this._baseState = this._state;
|
||||
this._state = 51 /* BeforeEntity */;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeDeclaration = function (c) {
|
||||
this._state =
|
||||
c === "["
|
||||
? 22 /* BeforeCdata1 */
|
||||
: c === "-"
|
||||
? 18 /* BeforeComment */
|
||||
: 16 /* InDeclaration */;
|
||||
};
|
||||
Tokenizer.prototype._stateInDeclaration = function (c) {
|
||||
if (c === ">") {
|
||||
this._cbs.ondeclaration(this._getSection());
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInProcessingInstruction = function (c) {
|
||||
if (c === ">") {
|
||||
this._cbs.onprocessinginstruction(this._getSection());
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeComment = function (c) {
|
||||
if (c === "-") {
|
||||
this._state = 19 /* InComment */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else {
|
||||
this._state = 16 /* InDeclaration */;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInComment = function (c) {
|
||||
if (c === "-")
|
||||
this._state = 20 /* AfterComment1 */;
|
||||
};
|
||||
Tokenizer.prototype._stateAfterComment1 = function (c) {
|
||||
if (c === "-") {
|
||||
this._state = 21 /* AfterComment2 */;
|
||||
}
|
||||
else {
|
||||
this._state = 19 /* InComment */;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateAfterComment2 = function (c) {
|
||||
if (c === ">") {
|
||||
//remove 2 trailing chars
|
||||
this._cbs.oncomment(this._buffer.substring(this._sectionStart, this._index - 2));
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (c !== "-") {
|
||||
this._state = 19 /* InComment */;
|
||||
}
|
||||
// else: stay in AFTER_COMMENT_2 (`--->`)
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeCdata6 = function (c) {
|
||||
if (c === "[") {
|
||||
this._state = 28 /* InCdata */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else {
|
||||
this._state = 16 /* InDeclaration */;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInCdata = function (c) {
|
||||
if (c === "]")
|
||||
this._state = 29 /* AfterCdata1 */;
|
||||
};
|
||||
Tokenizer.prototype._stateAfterCdata1 = function (c) {
|
||||
if (c === "]")
|
||||
this._state = 30 /* AfterCdata2 */;
|
||||
else
|
||||
this._state = 28 /* InCdata */;
|
||||
};
|
||||
Tokenizer.prototype._stateAfterCdata2 = function (c) {
|
||||
if (c === ">") {
|
||||
//remove 2 trailing chars
|
||||
this._cbs.oncdata(this._buffer.substring(this._sectionStart, this._index - 2));
|
||||
this._state = 1 /* Text */;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
else if (c !== "]") {
|
||||
this._state = 28 /* InCdata */;
|
||||
}
|
||||
//else: stay in AFTER_CDATA_2 (`]]]>`)
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeSpecial = function (c) {
|
||||
if (c === "c" || c === "C") {
|
||||
this._state = 33 /* BeforeScript1 */;
|
||||
}
|
||||
else if (c === "t" || c === "T") {
|
||||
this._state = 43 /* BeforeStyle1 */;
|
||||
}
|
||||
else {
|
||||
this._state = 3 /* InTagName */;
|
||||
this._index--; //consume the token again
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeSpecialEnd = function (c) {
|
||||
if (this._special === 2 /* Script */ && (c === "c" || c === "C")) {
|
||||
this._state = 38 /* AfterScript1 */;
|
||||
}
|
||||
else if (this._special === 3 /* Style */ &&
|
||||
(c === "t" || c === "T")) {
|
||||
this._state = 47 /* AfterStyle1 */;
|
||||
}
|
||||
else
|
||||
this._state = 1 /* Text */;
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeScript5 = function (c) {
|
||||
if (c === "/" || c === ">" || whitespace(c)) {
|
||||
this._special = 2 /* Script */;
|
||||
}
|
||||
this._state = 3 /* InTagName */;
|
||||
this._index--; //consume the token again
|
||||
};
|
||||
Tokenizer.prototype._stateAfterScript5 = function (c) {
|
||||
if (c === ">" || whitespace(c)) {
|
||||
this._special = 1 /* None */;
|
||||
this._state = 6 /* InClosingTagName */;
|
||||
this._sectionStart = this._index - 6;
|
||||
this._index--; //reconsume the token
|
||||
}
|
||||
else
|
||||
this._state = 1 /* Text */;
|
||||
};
|
||||
Tokenizer.prototype._stateBeforeStyle4 = function (c) {
|
||||
if (c === "/" || c === ">" || whitespace(c)) {
|
||||
this._special = 3 /* Style */;
|
||||
}
|
||||
this._state = 3 /* InTagName */;
|
||||
this._index--; //consume the token again
|
||||
};
|
||||
Tokenizer.prototype._stateAfterStyle4 = function (c) {
|
||||
if (c === ">" || whitespace(c)) {
|
||||
this._special = 1 /* None */;
|
||||
this._state = 6 /* InClosingTagName */;
|
||||
this._sectionStart = this._index - 5;
|
||||
this._index--; //reconsume the token
|
||||
}
|
||||
else
|
||||
this._state = 1 /* Text */;
|
||||
};
|
||||
//for entities terminated with a semicolon
|
||||
Tokenizer.prototype._parseNamedEntityStrict = function () {
|
||||
//offset = 1
|
||||
if (this._sectionStart + 1 < this._index) {
|
||||
var entity = this._buffer.substring(this._sectionStart + 1, this._index), map = this._xmlMode ? xml_json_1.default : entities_json_1.default;
|
||||
if (Object.prototype.hasOwnProperty.call(map, entity)) {
|
||||
// @ts-ignore
|
||||
this._emitPartial(map[entity]);
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
//parses legacy entities (without trailing semicolon)
|
||||
Tokenizer.prototype._parseLegacyEntity = function () {
|
||||
var start = this._sectionStart + 1;
|
||||
var limit = this._index - start;
|
||||
if (limit > 6)
|
||||
limit = 6; // The max length of legacy entities is 6
|
||||
while (limit >= 2) {
|
||||
// The min length of legacy entities is 2
|
||||
var entity = this._buffer.substr(start, limit);
|
||||
if (Object.prototype.hasOwnProperty.call(legacy_json_1.default, entity)) {
|
||||
// @ts-ignore
|
||||
this._emitPartial(legacy_json_1.default[entity]);
|
||||
this._sectionStart += limit + 1;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
limit--;
|
||||
}
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInNamedEntity = function (c) {
|
||||
if (c === ";") {
|
||||
this._parseNamedEntityStrict();
|
||||
if (this._sectionStart + 1 < this._index && !this._xmlMode) {
|
||||
this._parseLegacyEntity();
|
||||
}
|
||||
this._state = this._baseState;
|
||||
}
|
||||
else if ((c < "a" || c > "z") &&
|
||||
(c < "A" || c > "Z") &&
|
||||
(c < "0" || c > "9")) {
|
||||
if (this._xmlMode || this._sectionStart + 1 === this._index) {
|
||||
// ignore
|
||||
}
|
||||
else if (this._baseState !== 1 /* Text */) {
|
||||
if (c !== "=") {
|
||||
this._parseNamedEntityStrict();
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._parseLegacyEntity();
|
||||
}
|
||||
this._state = this._baseState;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._decodeNumericEntity = function (offset, base) {
|
||||
var sectionStart = this._sectionStart + offset;
|
||||
if (sectionStart !== this._index) {
|
||||
//parse entity
|
||||
var entity = this._buffer.substring(sectionStart, this._index);
|
||||
var parsed = parseInt(entity, base);
|
||||
this._emitPartial(decode_codepoint_1.default(parsed));
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
else {
|
||||
this._sectionStart--;
|
||||
}
|
||||
this._state = this._baseState;
|
||||
};
|
||||
Tokenizer.prototype._stateInNumericEntity = function (c) {
|
||||
if (c === ";") {
|
||||
this._decodeNumericEntity(2, 10);
|
||||
this._sectionStart++;
|
||||
}
|
||||
else if (c < "0" || c > "9") {
|
||||
if (!this._xmlMode) {
|
||||
this._decodeNumericEntity(2, 10);
|
||||
}
|
||||
else {
|
||||
this._state = this._baseState;
|
||||
}
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._stateInHexEntity = function (c) {
|
||||
if (c === ";") {
|
||||
this._decodeNumericEntity(3, 16);
|
||||
this._sectionStart++;
|
||||
}
|
||||
else if ((c < "a" || c > "f") &&
|
||||
(c < "A" || c > "F") &&
|
||||
(c < "0" || c > "9")) {
|
||||
if (!this._xmlMode) {
|
||||
this._decodeNumericEntity(3, 16);
|
||||
}
|
||||
else {
|
||||
this._state = this._baseState;
|
||||
}
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype._cleanup = function () {
|
||||
if (this._sectionStart < 0) {
|
||||
this._buffer = "";
|
||||
this._bufferOffset += this._index;
|
||||
this._index = 0;
|
||||
}
|
||||
else if (this._running) {
|
||||
if (this._state === 1 /* Text */) {
|
||||
if (this._sectionStart !== this._index) {
|
||||
this._cbs.ontext(this._buffer.substr(this._sectionStart));
|
||||
}
|
||||
this._buffer = "";
|
||||
this._bufferOffset += this._index;
|
||||
this._index = 0;
|
||||
}
|
||||
else if (this._sectionStart === this._index) {
|
||||
//the section just started
|
||||
this._buffer = "";
|
||||
this._bufferOffset += this._index;
|
||||
this._index = 0;
|
||||
}
|
||||
else {
|
||||
//remove everything unnecessary
|
||||
this._buffer = this._buffer.substr(this._sectionStart);
|
||||
this._index -= this._sectionStart;
|
||||
this._bufferOffset += this._sectionStart;
|
||||
}
|
||||
this._sectionStart = 0;
|
||||
}
|
||||
};
|
||||
//TODO make events conditional
|
||||
Tokenizer.prototype.write = function (chunk) {
|
||||
if (this._ended)
|
||||
this._cbs.onerror(Error(".write() after done!"));
|
||||
this._buffer += chunk;
|
||||
this._parse();
|
||||
};
|
||||
// Iterates through the buffer, calling the function corresponding to the current state.
|
||||
// States that are more likely to be hit are higher up, as a performance improvement.
|
||||
Tokenizer.prototype._parse = function () {
|
||||
while (this._index < this._buffer.length && this._running) {
|
||||
var c = this._buffer.charAt(this._index);
|
||||
if (this._state === 1 /* Text */) {
|
||||
this._stateText(c);
|
||||
}
|
||||
else if (this._state === 12 /* InAttributeValueDq */) {
|
||||
this._stateInAttributeValueDoubleQuotes(c);
|
||||
}
|
||||
else if (this._state === 9 /* InAttributeName */) {
|
||||
this._stateInAttributeName(c);
|
||||
}
|
||||
else if (this._state === 19 /* InComment */) {
|
||||
this._stateInComment(c);
|
||||
}
|
||||
else if (this._state === 8 /* BeforeAttributeName */) {
|
||||
this._stateBeforeAttributeName(c);
|
||||
}
|
||||
else if (this._state === 3 /* InTagName */) {
|
||||
this._stateInTagName(c);
|
||||
}
|
||||
else if (this._state === 6 /* InClosingTagName */) {
|
||||
this._stateInClosingTagName(c);
|
||||
}
|
||||
else if (this._state === 2 /* BeforeTagName */) {
|
||||
this._stateBeforeTagName(c);
|
||||
}
|
||||
else if (this._state === 10 /* AfterAttributeName */) {
|
||||
this._stateAfterAttributeName(c);
|
||||
}
|
||||
else if (this._state === 13 /* InAttributeValueSq */) {
|
||||
this._stateInAttributeValueSingleQuotes(c);
|
||||
}
|
||||
else if (this._state === 11 /* BeforeAttributeValue */) {
|
||||
this._stateBeforeAttributeValue(c);
|
||||
}
|
||||
else if (this._state === 5 /* BeforeClosingTagName */) {
|
||||
this._stateBeforeClosingTagName(c);
|
||||
}
|
||||
else if (this._state === 7 /* AfterClosingTagName */) {
|
||||
this._stateAfterClosingTagName(c);
|
||||
}
|
||||
else if (this._state === 31 /* BeforeSpecial */) {
|
||||
this._stateBeforeSpecial(c);
|
||||
}
|
||||
else if (this._state === 20 /* AfterComment1 */) {
|
||||
this._stateAfterComment1(c);
|
||||
}
|
||||
else if (this._state === 14 /* InAttributeValueNq */) {
|
||||
this._stateInAttributeValueNoQuotes(c);
|
||||
}
|
||||
else if (this._state === 4 /* InSelfClosingTag */) {
|
||||
this._stateInSelfClosingTag(c);
|
||||
}
|
||||
else if (this._state === 16 /* InDeclaration */) {
|
||||
this._stateInDeclaration(c);
|
||||
}
|
||||
else if (this._state === 15 /* BeforeDeclaration */) {
|
||||
this._stateBeforeDeclaration(c);
|
||||
}
|
||||
else if (this._state === 21 /* AfterComment2 */) {
|
||||
this._stateAfterComment2(c);
|
||||
}
|
||||
else if (this._state === 18 /* BeforeComment */) {
|
||||
this._stateBeforeComment(c);
|
||||
}
|
||||
else if (this._state === 32 /* BeforeSpecialEnd */) {
|
||||
this._stateBeforeSpecialEnd(c);
|
||||
}
|
||||
else if (this._state === 38 /* AfterScript1 */) {
|
||||
stateAfterScript1(this, c);
|
||||
}
|
||||
else if (this._state === 39 /* AfterScript2 */) {
|
||||
stateAfterScript2(this, c);
|
||||
}
|
||||
else if (this._state === 40 /* AfterScript3 */) {
|
||||
stateAfterScript3(this, c);
|
||||
}
|
||||
else if (this._state === 33 /* BeforeScript1 */) {
|
||||
stateBeforeScript1(this, c);
|
||||
}
|
||||
else if (this._state === 34 /* BeforeScript2 */) {
|
||||
stateBeforeScript2(this, c);
|
||||
}
|
||||
else if (this._state === 35 /* BeforeScript3 */) {
|
||||
stateBeforeScript3(this, c);
|
||||
}
|
||||
else if (this._state === 36 /* BeforeScript4 */) {
|
||||
stateBeforeScript4(this, c);
|
||||
}
|
||||
else if (this._state === 37 /* BeforeScript5 */) {
|
||||
this._stateBeforeScript5(c);
|
||||
}
|
||||
else if (this._state === 41 /* AfterScript4 */) {
|
||||
stateAfterScript4(this, c);
|
||||
}
|
||||
else if (this._state === 42 /* AfterScript5 */) {
|
||||
this._stateAfterScript5(c);
|
||||
}
|
||||
else if (this._state === 43 /* BeforeStyle1 */) {
|
||||
stateBeforeStyle1(this, c);
|
||||
}
|
||||
else if (this._state === 28 /* InCdata */) {
|
||||
this._stateInCdata(c);
|
||||
}
|
||||
else if (this._state === 44 /* BeforeStyle2 */) {
|
||||
stateBeforeStyle2(this, c);
|
||||
}
|
||||
else if (this._state === 45 /* BeforeStyle3 */) {
|
||||
stateBeforeStyle3(this, c);
|
||||
}
|
||||
else if (this._state === 46 /* BeforeStyle4 */) {
|
||||
this._stateBeforeStyle4(c);
|
||||
}
|
||||
else if (this._state === 47 /* AfterStyle1 */) {
|
||||
stateAfterStyle1(this, c);
|
||||
}
|
||||
else if (this._state === 48 /* AfterStyle2 */) {
|
||||
stateAfterStyle2(this, c);
|
||||
}
|
||||
else if (this._state === 49 /* AfterStyle3 */) {
|
||||
stateAfterStyle3(this, c);
|
||||
}
|
||||
else if (this._state === 50 /* AfterStyle4 */) {
|
||||
this._stateAfterStyle4(c);
|
||||
}
|
||||
else if (this._state === 17 /* InProcessingInstruction */) {
|
||||
this._stateInProcessingInstruction(c);
|
||||
}
|
||||
else if (this._state === 53 /* InNamedEntity */) {
|
||||
this._stateInNamedEntity(c);
|
||||
}
|
||||
else if (this._state === 22 /* BeforeCdata1 */) {
|
||||
stateBeforeCdata1(this, c);
|
||||
}
|
||||
else if (this._state === 51 /* BeforeEntity */) {
|
||||
stateBeforeEntity(this, c);
|
||||
}
|
||||
else if (this._state === 23 /* BeforeCdata2 */) {
|
||||
stateBeforeCdata2(this, c);
|
||||
}
|
||||
else if (this._state === 24 /* BeforeCdata3 */) {
|
||||
stateBeforeCdata3(this, c);
|
||||
}
|
||||
else if (this._state === 29 /* AfterCdata1 */) {
|
||||
this._stateAfterCdata1(c);
|
||||
}
|
||||
else if (this._state === 30 /* AfterCdata2 */) {
|
||||
this._stateAfterCdata2(c);
|
||||
}
|
||||
else if (this._state === 25 /* BeforeCdata4 */) {
|
||||
stateBeforeCdata4(this, c);
|
||||
}
|
||||
else if (this._state === 26 /* BeforeCdata5 */) {
|
||||
stateBeforeCdata5(this, c);
|
||||
}
|
||||
else if (this._state === 27 /* BeforeCdata6 */) {
|
||||
this._stateBeforeCdata6(c);
|
||||
}
|
||||
else if (this._state === 55 /* InHexEntity */) {
|
||||
this._stateInHexEntity(c);
|
||||
}
|
||||
else if (this._state === 54 /* InNumericEntity */) {
|
||||
this._stateInNumericEntity(c);
|
||||
}
|
||||
else if (this._state === 52 /* BeforeNumericEntity */) {
|
||||
stateBeforeNumericEntity(this, c);
|
||||
}
|
||||
else {
|
||||
this._cbs.onerror(Error("unknown _state"), this._state);
|
||||
}
|
||||
this._index++;
|
||||
}
|
||||
this._cleanup();
|
||||
};
|
||||
Tokenizer.prototype.pause = function () {
|
||||
this._running = false;
|
||||
};
|
||||
Tokenizer.prototype.resume = function () {
|
||||
this._running = true;
|
||||
if (this._index < this._buffer.length) {
|
||||
this._parse();
|
||||
}
|
||||
if (this._ended) {
|
||||
this._finish();
|
||||
}
|
||||
};
|
||||
Tokenizer.prototype.end = function (chunk) {
|
||||
if (this._ended)
|
||||
this._cbs.onerror(Error(".end() after done!"));
|
||||
if (chunk)
|
||||
this.write(chunk);
|
||||
this._ended = true;
|
||||
if (this._running)
|
||||
this._finish();
|
||||
};
|
||||
Tokenizer.prototype._finish = function () {
|
||||
//if there is remaining data, emit it in a reasonable way
|
||||
if (this._sectionStart < this._index) {
|
||||
this._handleTrailingData();
|
||||
}
|
||||
this._cbs.onend();
|
||||
};
|
||||
Tokenizer.prototype._handleTrailingData = function () {
|
||||
var data = this._buffer.substr(this._sectionStart);
|
||||
if (this._state === 28 /* InCdata */ ||
|
||||
this._state === 29 /* AfterCdata1 */ ||
|
||||
this._state === 30 /* AfterCdata2 */) {
|
||||
this._cbs.oncdata(data);
|
||||
}
|
||||
else if (this._state === 19 /* InComment */ ||
|
||||
this._state === 20 /* AfterComment1 */ ||
|
||||
this._state === 21 /* AfterComment2 */) {
|
||||
this._cbs.oncomment(data);
|
||||
}
|
||||
else if (this._state === 53 /* InNamedEntity */ && !this._xmlMode) {
|
||||
this._parseLegacyEntity();
|
||||
if (this._sectionStart < this._index) {
|
||||
this._state = this._baseState;
|
||||
this._handleTrailingData();
|
||||
}
|
||||
}
|
||||
else if (this._state === 54 /* InNumericEntity */ && !this._xmlMode) {
|
||||
this._decodeNumericEntity(2, 10);
|
||||
if (this._sectionStart < this._index) {
|
||||
this._state = this._baseState;
|
||||
this._handleTrailingData();
|
||||
}
|
||||
}
|
||||
else if (this._state === 55 /* InHexEntity */ && !this._xmlMode) {
|
||||
this._decodeNumericEntity(3, 16);
|
||||
if (this._sectionStart < this._index) {
|
||||
this._state = this._baseState;
|
||||
this._handleTrailingData();
|
||||
}
|
||||
}
|
||||
else if (this._state !== 3 /* InTagName */ &&
|
||||
this._state !== 8 /* BeforeAttributeName */ &&
|
||||
this._state !== 11 /* BeforeAttributeValue */ &&
|
||||
this._state !== 10 /* AfterAttributeName */ &&
|
||||
this._state !== 9 /* InAttributeName */ &&
|
||||
this._state !== 13 /* InAttributeValueSq */ &&
|
||||
this._state !== 12 /* InAttributeValueDq */ &&
|
||||
this._state !== 14 /* InAttributeValueNq */ &&
|
||||
this._state !== 6 /* InClosingTagName */) {
|
||||
this._cbs.ontext(data);
|
||||
}
|
||||
//else, ignore remaining data
|
||||
//TODO add a way to remove current tag
|
||||
};
|
||||
Tokenizer.prototype.getAbsoluteIndex = function () {
|
||||
return this._bufferOffset + this._index;
|
||||
};
|
||||
Tokenizer.prototype._getSection = function () {
|
||||
return this._buffer.substring(this._sectionStart, this._index);
|
||||
};
|
||||
Tokenizer.prototype._emitToken = function (name) {
|
||||
this._cbs[name](this._getSection());
|
||||
this._sectionStart = -1;
|
||||
};
|
||||
Tokenizer.prototype._emitPartial = function (value) {
|
||||
if (this._baseState !== 1 /* Text */) {
|
||||
this._cbs.onattribdata(value); //TODO implement the new event
|
||||
}
|
||||
else {
|
||||
this._cbs.ontext(value);
|
||||
}
|
||||
};
|
||||
return Tokenizer;
|
||||
}());
|
||||
exports.default = Tokenizer;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
/// <reference types="node" />
|
||||
import { Parser, Handler, ParserOptions } from "./Parser";
|
||||
import { Writable } from "stream";
|
||||
import { StringDecoder } from "string_decoder";
|
||||
/**
|
||||
* WritableStream makes the `Parser` interface available as a NodeJS stream.
|
||||
*
|
||||
* @see Parser
|
||||
*/
|
||||
export declare class WritableStream extends Writable {
|
||||
_parser: Parser;
|
||||
_decoder: StringDecoder;
|
||||
constructor(cbs: Partial<Handler>, options?: ParserOptions);
|
||||
_write(chunk: string | Buffer, encoding: string, cb: () => void): void;
|
||||
_final(cb: () => void): void;
|
||||
}
|
||||
//# sourceMappingURL=WritableStream.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"WritableStream.d.ts","sourceRoot":"","sources":["../src/WritableStream.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAO/C;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,QAAQ;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,gBAAuB;gBAEnB,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa;IAK1D,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,IAAI;IAM/D,MAAM,CAAC,EAAE,EAAE,MAAM,IAAI;CAIxB"}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
"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 __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Parser_1 = require("./Parser");
|
||||
var stream_1 = require("stream");
|
||||
var string_decoder_1 = require("string_decoder");
|
||||
// Following the example in https://nodejs.org/api/stream.html#stream_decoding_buffers_in_a_writable_stream
|
||||
function isBuffer(_chunk, encoding) {
|
||||
return encoding === "buffer";
|
||||
}
|
||||
/**
|
||||
* WritableStream makes the `Parser` interface available as a NodeJS stream.
|
||||
*
|
||||
* @see Parser
|
||||
*/
|
||||
var WritableStream = /** @class */ (function (_super) {
|
||||
__extends(WritableStream, _super);
|
||||
function WritableStream(cbs, options) {
|
||||
var _this = _super.call(this, { decodeStrings: false }) || this;
|
||||
_this._decoder = new string_decoder_1.StringDecoder();
|
||||
_this._parser = new Parser_1.Parser(cbs, options);
|
||||
return _this;
|
||||
}
|
||||
WritableStream.prototype._write = function (chunk, encoding, cb) {
|
||||
if (isBuffer(chunk, encoding))
|
||||
chunk = this._decoder.write(chunk);
|
||||
this._parser.write(chunk);
|
||||
cb();
|
||||
};
|
||||
WritableStream.prototype._final = function (cb) {
|
||||
this._parser.end(this._decoder.end());
|
||||
cb();
|
||||
};
|
||||
return WritableStream;
|
||||
}(stream_1.Writable));
|
||||
exports.WritableStream = WritableStream;
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import { Parser, ParserOptions } from "./Parser";
|
||||
export { Parser, ParserOptions };
|
||||
import { DomHandler, DomHandlerOptions, Node, Element } from "domhandler";
|
||||
export { DomHandler, DomHandlerOptions };
|
||||
declare type Options = ParserOptions & DomHandlerOptions;
|
||||
/**
|
||||
* Parses data, returns the resulting DOM.
|
||||
*
|
||||
* @param data The data that should be parsed.
|
||||
* @param options Optional options for the parser and DOM builder.
|
||||
*/
|
||||
export declare function parseDOM(data: string, options?: Options): Node[];
|
||||
/**
|
||||
* Creates a parser instance, with an attached DOM handler.
|
||||
*
|
||||
* @param cb A callback that will be called once parsing has been completed.
|
||||
* @param options Optional options for the parser and DOM builder.
|
||||
* @param elementCb An optional callback that will be called every time a tag has been completed inside of the DOM.
|
||||
*/
|
||||
export declare function createDomStream(cb: (error: Error | null, dom: Node[]) => void, options?: Options, elementCb?: (element: Element) => void): Parser;
|
||||
export { default as Tokenizer } from "./Tokenizer";
|
||||
import * as ElementType from "domelementtype";
|
||||
export { ElementType };
|
||||
/**
|
||||
* List of all events that the parser emits.
|
||||
*
|
||||
* Format: eventname: number of arguments.
|
||||
*/
|
||||
export declare const EVENTS: {
|
||||
attribute: number;
|
||||
cdatastart: number;
|
||||
cdataend: number;
|
||||
text: number;
|
||||
processinginstruction: number;
|
||||
comment: number;
|
||||
commentend: number;
|
||||
closetag: number;
|
||||
opentag: number;
|
||||
opentagname: number;
|
||||
error: number;
|
||||
end: number;
|
||||
};
|
||||
export * from "./FeedHandler";
|
||||
export * from "./WritableStream";
|
||||
export * from "./CollectingHandler";
|
||||
import * as DomUtils from "domutils";
|
||||
export { DomUtils };
|
||||
export { DomHandler as DefaultHandler };
|
||||
export { FeedHandler as RssHandler } from "./FeedHandler";
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC;AAEjC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1E,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,CAAC;AAEzC,aAAK,OAAO,GAAG,aAAa,GAAG,iBAAiB,CAAC;AAIjD;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,EAAE,CAIhE;AACD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC3B,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,IAAI,EAC9C,OAAO,CAAC,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,UAIzC;AAED,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,CAAC;AAEvB;;;;GAIG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;CAalB,CAAC;AAOF,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AAEpC,OAAO,KAAK,QAAQ,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,CAAC;AAGpB,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,CAAC;AACxC,OAAO,EAAE,WAAW,IAAI,UAAU,EAAE,MAAM,eAAe,CAAC"}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Parser_1 = require("./Parser");
|
||||
exports.Parser = Parser_1.Parser;
|
||||
var domhandler_1 = require("domhandler");
|
||||
exports.DomHandler = domhandler_1.DomHandler;
|
||||
exports.DefaultHandler = domhandler_1.DomHandler;
|
||||
// Helper methods
|
||||
/**
|
||||
* Parses data, returns the resulting DOM.
|
||||
*
|
||||
* @param data The data that should be parsed.
|
||||
* @param options Optional options for the parser and DOM builder.
|
||||
*/
|
||||
function parseDOM(data, options) {
|
||||
var handler = new domhandler_1.DomHandler(void 0, options);
|
||||
new Parser_1.Parser(handler, options).end(data);
|
||||
return handler.dom;
|
||||
}
|
||||
exports.parseDOM = parseDOM;
|
||||
/**
|
||||
* Creates a parser instance, with an attached DOM handler.
|
||||
*
|
||||
* @param cb A callback that will be called once parsing has been completed.
|
||||
* @param options Optional options for the parser and DOM builder.
|
||||
* @param elementCb An optional callback that will be called every time a tag has been completed inside of the DOM.
|
||||
*/
|
||||
function createDomStream(cb, options, elementCb) {
|
||||
var handler = new domhandler_1.DomHandler(cb, options, elementCb);
|
||||
return new Parser_1.Parser(handler, options);
|
||||
}
|
||||
exports.createDomStream = createDomStream;
|
||||
var Tokenizer_1 = require("./Tokenizer");
|
||||
exports.Tokenizer = Tokenizer_1.default;
|
||||
var ElementType = __importStar(require("domelementtype"));
|
||||
exports.ElementType = ElementType;
|
||||
/**
|
||||
* List of all events that the parser emits.
|
||||
*
|
||||
* Format: eventname: number of arguments.
|
||||
*/
|
||||
exports.EVENTS = {
|
||||
attribute: 2,
|
||||
cdatastart: 0,
|
||||
cdataend: 0,
|
||||
text: 1,
|
||||
processinginstruction: 2,
|
||||
comment: 1,
|
||||
commentend: 0,
|
||||
closetag: 1,
|
||||
opentag: 2,
|
||||
opentagname: 1,
|
||||
error: 1,
|
||||
end: 0
|
||||
};
|
||||
/*
|
||||
All of the following exports exist for backwards-compatibility.
|
||||
They should probably be removed eventually.
|
||||
*/
|
||||
__export(require("./FeedHandler"));
|
||||
__export(require("./WritableStream"));
|
||||
__export(require("./CollectingHandler"));
|
||||
var DomUtils = __importStar(require("domutils"));
|
||||
exports.DomUtils = DomUtils;
|
||||
var FeedHandler_1 = require("./FeedHandler");
|
||||
exports.RssHandler = FeedHandler_1.FeedHandler;
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "htmlparser2",
|
||||
"description": "Fast & forgiving HTML/XML/RSS parser",
|
||||
"version": "4.1.0",
|
||||
"author": "Felix Boehm <me@feedic.com>",
|
||||
"keywords": [
|
||||
"html",
|
||||
"parser",
|
||||
"streams",
|
||||
"xml",
|
||||
"dom",
|
||||
"rss",
|
||||
"feed",
|
||||
"atom"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/fb55/htmlparser2.git"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "lib/"
|
||||
},
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"files": [
|
||||
"lib/**/*"
|
||||
],
|
||||
"browser": {
|
||||
"./lib/WritableStream.js": false
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest --coverage -u && npm run lint",
|
||||
"coverage": "cat coverage/lcov.info | coveralls",
|
||||
"lint": "eslint --ext=js,ts src",
|
||||
"format": "prettier --write '**/*.{ts,md,json}'",
|
||||
"build": "tsc",
|
||||
"prepare": "npm run build"
|
||||
},
|
||||
"dependencies": {
|
||||
"domelementtype": "^2.0.1",
|
||||
"domhandler": "^3.0.0",
|
||||
"domutils": "^2.0.0",
|
||||
"entities": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^25.1.3",
|
||||
"@types/node": "^13.1.1",
|
||||
"@typescript-eslint/eslint-plugin": "^1.13.0",
|
||||
"@typescript-eslint/parser": "^1.13.0",
|
||||
"coveralls": "^3.0.1",
|
||||
"eslint": "^6.0.0",
|
||||
"eslint-config-prettier": "^6.0.0",
|
||||
"jest": "^24.8.0",
|
||||
"prettier": "^1.18.2",
|
||||
"ts-jest": "^24.0.2",
|
||||
"typescript": "^3.5.3"
|
||||
},
|
||||
"jest": {
|
||||
"preset": "ts-jest",
|
||||
"testEnvironment": "node"
|
||||
},
|
||||
"license": "MIT",
|
||||
"prettier": {
|
||||
"tabWidth": 4
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user