function CollectingHandler(cbs){
this._cbs = cbs || {};
this.events = [];
}n/a
function DomHandler(callback, options, elementCB){
if(typeof callback === "object"){
elementCB = options;
options = callback;
callback = null;
} else if(typeof options === "function"){
elementCB = options;
options = defaultOpts;
}
this._callback = callback;
this._options = options || defaultOpts;
this._elementCB = elementCB;
this.dom = [];
this._done = false;
this._tagStack = [];
this._parser = this._parser || null;
}n/a
function DomHandler(callback, options, elementCB){
if(typeof callback === "object"){
elementCB = options;
options = callback;
callback = null;
} else if(typeof options === "function"){
elementCB = options;
options = defaultOpts;
}
this._callback = callback;
this._options = options || defaultOpts;
this._elementCB = elementCB;
this.dom = [];
this._done = false;
this._tagStack = [];
this._parser = this._parser || null;
}n/a
function FeedHandler(callback, options){
this.init(callback, options);
}...
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
new htmlparser.FeedHandler(function(<error> error, <object> feed){
...
});
```
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
...function Parser(cbs, options){
this._options = options || {};
this._cbs = cbs || {};
this._tagname = "";
this._attribname = "";
this._attribvalue = "";
this._attribs = null;
this._stack = [];
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;
if(this._options.Tokenizer) {
Tokenizer = this._options.Tokenizer;
}
this._tokenizer = new Tokenizer(this._options, this);
if(this._cbs.onparserinit) this._cbs.onparserinit(this);
}...
A live demo of htmlparser2 is available [here](http://demos.forbeslindesay.co.uk/htmlparser2/).
## Usage
```javascript
var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
console.log("JS! Hooray!");
}
},
ontext: function(text){
console.log("-->", text);
...function ProxyHandler(cbs){
this._cbs = cbs || {};
}n/a
function FeedHandler(callback, options){
this.init(callback, options);
}n/a
function Stream(options){
Parser.call(this, new Cbs(this), options);
}n/a
function Tokenizer(options, cbs){
this._state = TEXT;
this._buffer = "";
this._sectionStart = 0;
this._index = 0;
this._bufferOffset = 0; //chars removed from _buffer
this._baseState = TEXT;
this._special = SPECIAL_NONE;
this._cbs = cbs;
this._running = true;
this._ended = false;
this._xmlMode = !!(options && options.xmlMode);
this._decodeEntities = !!(options && options.decodeEntities);
}n/a
function Stream(cbs, options){
var parser = this._parser = new Parser(cbs, options);
var decoder = this._decoder = new StringDecoder();
WritableStream.call(this, {decodeStrings: false});
this.once("finish", function(){
parser.end(decoder.end());
});
}n/a
createDomStream = function (cb, options, elementCb){
var handler = new DomHandler(cb, options, elementCb);
return new Parser(handler, options);
}n/a
parseDOM = function (data, options){
var handler = new DomHandler(options);
new Parser(handler, options).end(data);
return handler.dom;
}n/a
parseFeed = function (feed, options){
var handler = new module.exports.FeedHandler(options);
new Parser(handler, options).end(feed);
return handler.dom;
}n/a
function CollectingHandler(cbs){
this._cbs = cbs || {};
this.events = [];
}n/a
onattribute = function (a, b){
this.events.push([name, a, b]);
if(this._cbs[name]) this._cbs[name](a, b);
}n/a
oncdataend = function (){
this.events.push([name]);
if(this._cbs[name]) this._cbs[name]();
}n/a
oncdatastart = function (){
this.events.push([name]);
if(this._cbs[name]) this._cbs[name]();
}n/a
onclosetag = function (a){
this.events.push([name, a]);
if(this._cbs[name]) this._cbs[name](a);
}n/a
oncomment = function (a){
this.events.push([name, a]);
if(this._cbs[name]) this._cbs[name](a);
}n/a
oncommentend = function (){
this.events.push([name]);
if(this._cbs[name]) this._cbs[name]();
}n/a
onend = function (){
this.events.push([name]);
if(this._cbs[name]) this._cbs[name]();
}n/a
onerror = function (a){
this.events.push([name, a]);
if(this._cbs[name]) this._cbs[name](a);
}n/a
onopentag = function (a, b){
this.events.push([name, a, b]);
if(this._cbs[name]) this._cbs[name](a, b);
}n/a
onopentagname = function (a){
this.events.push([name, a]);
if(this._cbs[name]) this._cbs[name](a);
}n/a
onprocessinginstruction = function (a, b){
this.events.push([name, a, b]);
if(this._cbs[name]) this._cbs[name](a, b);
}n/a
onreset = function (){
this.events = [];
if(this._cbs.onreset) this._cbs.onreset();
}n/a
ontext = function (a){
this.events.push([name, a]);
if(this._cbs[name]) this._cbs[name](a);
}n/a
restart = function (){
if(this._cbs.onreset) this._cbs.onreset();
for(var i = 0, len = this.events.length; i < len; i++){
if(this._cbs[this.events[i][0]]){
var num = this.events[i].length;
if(num === 1){
this._cbs[this.events[i][0]]();
} else if(num === 2){
this._cbs[this.events[i][0]](this.events[i][1]);
} else {
this._cbs[this.events[i][0]](this.events[i][1], this.events[i][2]);
}
}
}
}n/a
function DomHandler(callback, options, elementCB){
if(typeof callback === "object"){
elementCB = options;
options = callback;
callback = null;
} else if(typeof options === "function"){
elementCB = options;
options = defaultOpts;
}
this._callback = callback;
this._options = options || defaultOpts;
this._elementCB = elementCB;
this.dom = [];
this._done = false;
this._tagStack = [];
this._parser = this._parser || null;
}n/a
_addDomElement = function (element){
var parent = this._tagStack[this._tagStack.length - 1];
var siblings = parent ? parent.children : this.dom;
var previousSibling = siblings[siblings.length - 1];
element.next = null;
if(this._options.withStartIndices){
element.startIndex = this._parser.startIndex;
}
if (this._options.withDomLvl1) {
element.__proto__ = element.type === "tag" ? ElementPrototype : NodePrototype;
}
if(previousSibling){
element.prev = previousSibling;
previousSibling.next = element;
} else {
element.prev = null;
}
siblings.push(element);
element.parent = parent || null;
}n/a
_handleCallback = function (error){
if(typeof this._callback === "function"){
this._callback(error, this.dom);
} else {
if(error) throw error;
}
}n/a
oncdataend = function (){
this._tagStack.pop();
}n/a
oncdatastart = function (){
var element = {
children: [{
data: "",
type: ElementType.Text
}],
type: ElementType.CDATA
};
this._addDomElement(element);
this._tagStack.push(element);
}n/a
onclosetag = function (){
//if(this._tagStack.pop().name !== name) this._handleCallback(Error("Tagname didn't match!"));
var elem = this._tagStack.pop();
if(this._elementCB) this._elementCB(elem);
}n/a
oncomment = function (data){
var lastTag = this._tagStack[this._tagStack.length - 1];
if(lastTag && lastTag.type === ElementType.Comment){
lastTag.data += data;
return;
}
var element = {
data: data,
type: ElementType.Comment
};
this._addDomElement(element);
this._tagStack.push(element);
}n/a
oncommentend = function (){
this._tagStack.pop();
}n/a
onend = function (){
if(this._done) return;
this._done = true;
this._parser = null;
this._handleCallback(null);
}n/a
onerror = function (error){
if(typeof this._callback === "function"){
this._callback(error, this.dom);
} else {
if(error) throw error;
}
}n/a
onopentag = function (name, attribs){
var element = {
type: name === "script" ? ElementType.Script : name === "style" ? ElementType.Style : ElementType.Tag,
name: name,
attribs: attribs,
children: []
};
this._addDomElement(element);
this._tagStack.push(element);
}n/a
onparserinit = function (parser){
this._parser = parser;
}n/a
onprocessinginstruction = function (name, data){
this._addDomElement({
name: name,
data: data,
type: ElementType.Directive
});
}n/a
onreset = function (){
DomHandler.call(this, this._callback, this._options, this._elementCB);
}n/a
ontext = function (data){
//the ignoreWhitespace is officially dropped, but for now,
//it's an alias for normalizeWhitespace
var normalize = this._options.normalizeWhitespace || this._options.ignoreWhitespace;
var lastTag;
if(!this._tagStack.length && this.dom.length && (lastTag = this.dom[this.dom.length-1]).type === ElementType.Text){
if(normalize){
lastTag.data = (lastTag.data + data).replace(re_whitespace, " ");
} else {
lastTag.data += data;
}
} else {
if(
this._tagStack.length &&
(lastTag = this._tagStack[this._tagStack.length - 1]) &&
(lastTag = lastTag.children[lastTag.children.length - 1]) &&
lastTag.type === ElementType.Text
){
if(normalize){
lastTag.data = (lastTag.data + data).replace(re_whitespace, " ");
} else {
lastTag.data += data;
}
} else {
if(normalize){
data = data.replace(re_whitespace, " ");
}
this._addDomElement({
data: data,
type: ElementType.Text
});
}
}
}n/a
append = function () { [native code] }n/a
appendChild = function () { [native code] }n/a
compareDocumentPosition = function () { [native code] }n/a
existsOne = function () { [native code] }n/a
filter = function () { [native code] }n/a
find = function () { [native code] }n/a
findAll = function () { [native code] }n/a
findOne = function () { [native code] }n/a
findOneChild = function () { [native code] }n/a
getAttributeValue = function () { [native code] }n/a
getChildren = function () { [native code] }n/a
getElementById = function () { [native code] }n/a
getElements = function () { [native code] }n/a
getElementsByTagName = function () { [native code] }n/a
getElementsByTagType = function () { [native code] }n/a
getInnerHTML = function () { [native code] }n/a
getName = function () { [native code] }n/a
getOuterHTML = function () { [native code] }n/a
getParent = function () { [native code] }n/a
getSiblings = function () { [native code] }n/a
getText = function () { [native code] }n/a
hasAttrib = function () { [native code] }n/a
isTag = function () { [native code] }n/a
prepend = function () { [native code] }n/a
removeElement = function () { [native code] }n/a
removeSubsets = function () { [native code] }n/a
replaceElement = function () { [native code] }n/a
testElement = function () { [native code] }n/a
uniqueSort = function () { [native code] }n/a
isTag = function (elem){
return elem.type === "tag" || elem.type === "script" || elem.type === "style";
}n/a
function FeedHandler(callback, options){
this.init(callback, options);
}...
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
new htmlparser.FeedHandler(function(<error> error, <object> feed){
...
});
```
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
...function DomHandler(callback, options, elementCB){
if(typeof callback === "object"){
elementCB = options;
options = callback;
callback = null;
} else if(typeof options === "function"){
elementCB = options;
options = defaultOpts;
}
this._callback = callback;
this._options = options || defaultOpts;
this._elementCB = elementCB;
this.dom = [];
this._done = false;
this._tagStack = [];
this._parser = this._parser || null;
}n/a
function DomHandler(callback, options, elementCB){
if(typeof callback === "object"){
elementCB = options;
options = callback;
callback = null;
} else if(typeof options === "function"){
elementCB = options;
options = defaultOpts;
}
this._callback = callback;
this._options = options || defaultOpts;
this._elementCB = elementCB;
this.dom = [];
this._done = false;
this._tagStack = [];
this._parser = this._parser || null;
}n/a
onend = function (){
var feed = {},
feedRoot = getOneElement(isValidFeed, this.dom),
tmp, childs;
if(feedRoot){
if(feedRoot.name === "feed"){
childs = feedRoot.children;
feed.type = "atom";
addConditionally(feed, "id", "id", childs);
addConditionally(feed, "title", "title", childs);
if((tmp = getOneElement("link", childs)) && (tmp = tmp.attribs) && (tmp = tmp.href)) feed.link = tmp;
addConditionally(feed, "description", "subtitle", childs);
if((tmp = fetch("updated", childs))) feed.updated = new Date(tmp);
addConditionally(feed, "author", "email", childs, true);
feed.items = getElements("entry", childs).map(function(item){
var entry = {}, tmp;
item = item.children;
addConditionally(entry, "id", "id", item);
addConditionally(entry, "title", "title", item);
if((tmp = getOneElement("link", item)) && (tmp = tmp.attribs) && (tmp = tmp.href)) entry.link = tmp;
if((tmp = fetch("summary", item) || fetch("content", item))) entry.description = tmp;
if((tmp = fetch("updated", item))) entry.pubDate = new Date(tmp);
return entry;
});
} else {
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);
if((tmp = fetch("lastBuildDate", childs))) feed.updated = new Date(tmp);
addConditionally(feed, "author", "managingEditor", childs, true);
feed.items = getElements("item", feedRoot.children).map(function(item){
var entry = {}, tmp;
item = item.children;
addConditionally(entry, "id", "guid", item);
addConditionally(entry, "title", "title", item);
addConditionally(entry, "link", "link", item);
addConditionally(entry, "description", "description", item);
if((tmp = fetch("pubDate", item))) entry.pubDate = new Date(tmp);
return entry;
});
}
}
this.dom = feed;
DomHandler.prototype._handleCallback.call(
this, feedRoot ? null : Error("couldn't find root of feed")
);
}n/a
function Parser(cbs, options){
this._options = options || {};
this._cbs = cbs || {};
this._tagname = "";
this._attribname = "";
this._attribvalue = "";
this._attribs = null;
this._stack = [];
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;
if(this._options.Tokenizer) {
Tokenizer = this._options.Tokenizer;
}
this._tokenizer = new Tokenizer(this._options, this);
if(this._cbs.onparserinit) this._cbs.onparserinit(this);
}...
A live demo of htmlparser2 is available [here](http://demos.forbeslindesay.co.uk/htmlparser2/).
## Usage
```javascript
var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
console.log("JS! Hooray!");
}
},
ontext: function(text){
console.log("-->", text);
...function EventEmitter() {
EventEmitter.init.call(this);
}n/a
_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();
}
}n/a
_getInstructionName = function (value){
var idx = value.search(re_nameEnd),
name = idx < 0 ? value : value.substr(0, idx);
if(this._lowerCaseTagNames){
name = name.toLowerCase();
}
return name;
}n/a
_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();
}n/a
done = function (chunk){
this._tokenizer.end(chunk);
}n/a
end = function (chunk){
this._tokenizer.end(chunk);
}...
onclosetag: function(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!
...onattribdata = function (value){
this._attribvalue += value;
}n/a
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 = "";
}n/a
onattribname = function (name){
if(this._lowerCaseAttributeNames){
name = name.toLowerCase();
}
this._attribname = name;
}n/a
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 + "]]");
}
}n/a
onclosetag = function (name){
this._updatePosition(1);
if(this._lowerCaseTagNames){
name = name.toLowerCase();
}
if(this._stack.length && (!(name in voidElements) || this._options.xmlMode)){
var pos = this._stack.lastIndexOf(name);
if(pos !== -1){
if(this._cbs.onclosetag){
pos = this._stack.length - pos;
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();
}
}n/a
oncomment = function (value){
this._updatePosition(4);
if(this._cbs.oncomment) this._cbs.oncomment(value);
if(this._cbs.oncommentend) this._cbs.oncommentend();
}n/a
ondeclaration = function (value){
if(this._cbs.onprocessinginstruction){
var name = this._getInstructionName(value);
this._cbs.onprocessinginstruction("!" + name, "!" + value);
}
}n/a
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();
}n/a
onerror = function (err){
if(this._cbs.onerror) this._cbs.onerror(err);
}n/a
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 && this._tagname in voidElements){
this._cbs.onclosetag(this._tagname);
}
this._tagname = "";
}n/a
onopentagname = function (name){
if(this._lowerCaseTagNames){
name = name.toLowerCase();
}
this._tagname = name;
if(!this._options.xmlMode && name in openImpliesClose) {
for(
var el;
(el = this._stack[this._stack.length - 1]) in openImpliesClose[name];
this.onclosetag(el)
);
}
if(this._options.xmlMode || !(name in voidElements)){
this._stack.push(name);
}
if(this._cbs.onopentagname) this._cbs.onopentagname(name);
if(this._cbs.onopentag) this._attribs = {};
}n/a
onprocessinginstruction = function (value){
if(this._cbs.onprocessinginstruction){
var name = this._getInstructionName(value);
this._cbs.onprocessinginstruction("?" + name, "?" + value);
}
}n/a
onselfclosingtag = function (){
if(this._options.xmlMode || this._options.recognizeSelfClosing){
this._closeCurrentTag();
} else {
this.onopentagend();
}
}n/a
ontext = function (data){
this._updatePosition(1);
this.endIndex--;
if(this._cbs.ontext) this._cbs.ontext(data);
}n/a
parseChunk = function (chunk){
this._tokenizer.write(chunk);
}n/a
parseComplete = function (data){
this.reset();
this.end(data);
}n/a
pause = function (){
this._tokenizer.pause();
}n/a
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);
}n/a
resume = function (){
this._tokenizer.resume();
}n/a
write = function (chunk){
this._tokenizer.write(chunk);
}...
},
onclosetag: function(tagname){
if(tagname === "script"){
console.log("That's it?!");
}
}
}, {decodeEntities: true});
parser.write("Xyz <script type='text/javascript'>var foo =
x27;<<bar>>';</ script>");
parser.end();
```
Output (simplified):
```
--> Xyz
...function ProxyHandler(cbs){
this._cbs = cbs || {};
}n/a
onattribute = function (a, b){
if(this._cbs[name]) this._cbs[name](a, b);
}n/a
oncdataend = function (){
if(this._cbs[name]) this._cbs[name]();
}n/a
oncdatastart = function (){
if(this._cbs[name]) this._cbs[name]();
}n/a
onclosetag = function (a){
if(this._cbs[name]) this._cbs[name](a);
}n/a
oncomment = function (a){
if(this._cbs[name]) this._cbs[name](a);
}n/a
oncommentend = function (){
if(this._cbs[name]) this._cbs[name]();
}n/a
onend = function (){
if(this._cbs[name]) this._cbs[name]();
}n/a
onerror = function (a){
if(this._cbs[name]) this._cbs[name](a);
}n/a
onopentag = function (a, b){
if(this._cbs[name]) this._cbs[name](a, b);
}n/a
onopentagname = function (a){
if(this._cbs[name]) this._cbs[name](a);
}n/a
onprocessinginstruction = function (a, b){
if(this._cbs[name]) this._cbs[name](a, b);
}n/a
ontext = function (a){
if(this._cbs[name]) this._cbs[name](a);
}n/a
function Stream(options){
Parser.call(this, new Cbs(this), options);
}n/a
function Stream(cbs, options){
var parser = this._parser = new Parser(cbs, options);
var decoder = this._decoder = new StringDecoder();
WritableStream.call(this, {decodeStrings: false});
this.once("finish", function(){
parser.end(decoder.end());
});
}n/a
function Tokenizer(options, cbs){
this._state = TEXT;
this._buffer = "";
this._sectionStart = 0;
this._index = 0;
this._bufferOffset = 0; //chars removed from _buffer
this._baseState = TEXT;
this._special = SPECIAL_NONE;
this._cbs = cbs;
this._running = true;
this._ended = false;
this._xmlMode = !!(options && options.xmlMode);
this._decodeEntities = !!(options && options.decodeEntities);
}n/a
_cleanup = function (){
if(this._sectionStart < 0){
this._buffer = "";
this._bufferOffset += this._index;
this._index = 0;
} else if(this._running){
if(this._state === 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;
}
}n/a
_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(decodeCodePoint(parsed));
this._sectionStart = this._index;
} else {
this._sectionStart--;
}
this._state = this._baseState;
}n/a
_emitPartial = function (value){
if(this._baseState !== TEXT){
this._cbs.onattribdata(value); //TODO implement the new event
} else {
this._cbs.ontext(value);
}
}n/a
_emitToken = function (name){
this._cbs[name](this._getSection());
this._sectionStart = -1;
}n/a
_finish = function (){
//if there is remaining data, emit it in a reasonable way
if(this._sectionStart < this._index){
this._handleTrailingData();
}
this._cbs.onend();
}n/a
_getSection = function (){
return this._buffer.substring(this._sectionStart, this._index);
}n/a
_handleTrailingData = function (){
var data = this._buffer.substr(this._sectionStart);
if(this._state === IN_CDATA || this._state === AFTER_CDATA_1 || this._state === AFTER_CDATA_2){
this._cbs.oncdata(data);
} else if(this._state === IN_COMMENT || this._state === AFTER_COMMENT_1 || this._state === AFTER_COMMENT_2){
this._cbs.oncomment(data);
} else if(this._state === IN_NAMED_ENTITY && !this._xmlMode){
this._parseLegacyEntity();
if(this._sectionStart < this._index){
this._state = this._baseState;
this._handleTrailingData();
}
} else if(this._state === IN_NUMERIC_ENTITY && !this._xmlMode){
this._decodeNumericEntity(2, 10);
if(this._sectionStart < this._index){
this._state = this._baseState;
this._handleTrailingData();
}
} else if(this._state === IN_HEX_ENTITY && !this._xmlMode){
this._decodeNumericEntity(3, 16);
if(this._sectionStart < this._index){
this._state = this._baseState;
this._handleTrailingData();
}
} else if(
this._state !== IN_TAG_NAME &&
this._state !== BEFORE_ATTRIBUTE_NAME &&
this._state !== BEFORE_ATTRIBUTE_VALUE &&
this._state !== AFTER_ATTRIBUTE_NAME &&
this._state !== IN_ATTRIBUTE_NAME &&
this._state !== IN_ATTRIBUTE_VALUE_SQ &&
this._state !== IN_ATTRIBUTE_VALUE_DQ &&
this._state !== IN_ATTRIBUTE_VALUE_NQ &&
this._state !== IN_CLOSING_TAG_NAME
){
this._cbs.ontext(data);
}
//else, ignore remaining data
//TODO add a way to remove current tag
}n/a
_parse = function (){
while(this._index < this._buffer.length && this._running){
var c = this._buffer.charAt(this._index);
if(this._state === TEXT) {
this._stateText(c);
} else if(this._state === BEFORE_TAG_NAME){
this._stateBeforeTagName(c);
} else if(this._state === IN_TAG_NAME) {
this._stateInTagName(c);
} else if(this._state === BEFORE_CLOSING_TAG_NAME){
this._stateBeforeCloseingTagName(c);
} else if(this._state === IN_CLOSING_TAG_NAME){
this._stateInCloseingTagName(c);
} else if(this._state === AFTER_CLOSING_TAG_NAME){
this._stateAfterCloseingTagName(c);
} else if(this._state === IN_SELF_CLOSING_TAG){
this._stateInSelfClosingTag(c);
}
/*
* attributes
*/
else if(this._state === BEFORE_ATTRIBUTE_NAME){
this._stateBeforeAttributeName(c);
} else if(this._state === IN_ATTRIBUTE_NAME){
this._stateInAttributeName(c);
} else if(this._state === AFTER_ATTRIBUTE_NAME){
this._stateAfterAttributeName(c);
} else if(this._state === BEFORE_ATTRIBUTE_VALUE){
this._stateBeforeAttributeValue(c);
} else if(this._state === IN_ATTRIBUTE_VALUE_DQ){
this._stateInAttributeValueDoubleQuotes(c);
} else if(this._state === IN_ATTRIBUTE_VALUE_SQ){
this._stateInAttributeValueSingleQuotes(c);
} else if(this._state === IN_ATTRIBUTE_VALUE_NQ){
this._stateInAttributeValueNoQuotes(c);
}
/*
* declarations
*/
else if(this._state === BEFORE_DECLARATION){
this._stateBeforeDeclaration(c);
} else if(this._state === IN_DECLARATION){
this._stateInDeclaration(c);
}
/*
* processing instructions
*/
else if(this._state === IN_PROCESSING_INSTRUCTION){
this._stateInProcessingInstruction(c);
}
/*
* comments
*/
else if(this._state === BEFORE_COMMENT){
this._stateBeforeComment(c);
} else if(this._state === IN_COMMENT){
this._stateInComment(c);
} else if(this._state === AFTER_COMMENT_1){
this._stateAfterComment1(c);
} else if(this._state === AFTER_COMMENT_2){
this._stateAfterComment2(c);
}
/*
* cdata
*/
else if(this._state === BEFORE_CDATA_1){
this._stateBeforeCdata1(c);
} else if(this._state === BEFORE_CDATA_2){
this._stateBeforeCdata2(c);
} else if(this._state === BEFORE_CDATA_3){
this._stateBeforeCdata3(c);
} else if(this._state === BEFORE_CDATA_4){
this._stateBeforeCdata4(c);
} else if(this._state === BEFORE_CDATA_5){
this._stateBeforeCdata5(c);
} else if(this._state === BEFORE_CDATA_6){
this._stateBeforeCdata6(c);
} else if(this._state === IN_CDATA){
this._stateInCdata(c);
} else if(this._state === AFTER_CDATA_1){
this._stateAfterCdata1(c);
} else if(this._state === AFTER_CDATA_2){
this._stateAfterCdata2(c);
}
/*
* special tags
*/
else if(this._state === BEFORE_SPECIAL){
this._stateBeforeSpecial(c);
} else if(this._state === BEFORE_SPECIAL_END){
this._stateBeforeSpecialEnd(c);
}
/*
* script
*/
else if(this._state === BEFORE_SCRIPT_1){
this._stateBeforeScript1(c);
} else if(this._state === BEFORE_SCRIPT_2){
this._stateBeforeScript2(c);
} else if(this._state === BEFORE_SCRIPT_3){
this._stateBeforeScript3(c);
} else if(this._state === BEFORE_SCRIPT_4){
this._stateBeforeScript4(c);
} else if(this._state === BEFORE_SCRIPT_5){
this._stateBeforeScript5(c);
}
else if(this._state === AFTER_SCRIPT_1){
this._stateAfterScript1(c);
} else if(this._state === AFTER_SCRIPT_2){
this._stateAfterScript2(c);
} else if(this._state === AFTER_SCRIPT_3){
this._stateAfterScript3(c);
} else if(this._state === AFTER_SCRIPT_4){
this._stateAfterScript4(c);
} else if(this._state === AFTER_SCRIPT_5){
this._stateAfterScript5(c);
}
/*
* style
*/
else if(this._state === BEFORE_STYLE_1){
this._stateBeforeStyle1(c);
} else if(this._state === BEFORE_STYLE_2){
this._stateBeforeStyle2(c);
} else if(this._state === BEFORE_STYLE_3){
this._stateBeforeStyle3(c);
} else if(this._state === BEFORE_STYLE_4){
this._stateBeforeStyle4(c);
}
else if(this._state === AFTER_STYLE_1){
this._stateAfterStyle1(c);
} e ...n/a
_parseLegacyEntity = function (){
var start = this._sectionStart + 1,
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(legacyMap.hasOwnProperty(entity)){
this._emitPartial(legacyMap[entity]);
this._sectionStart += limit + 1;
return;
} else {
limit--;
}
}
}n/a
_parseNamedEntityStrict = function (){
//offset = 1
if(this._sectionStart + 1 < this._index){
var entity = this._buffer.substring(this._sectionStart + 1, this._index),
map = this._xmlMode ? xmlMap : entityMap;
if(map.hasOwnProperty(entity)){
this._emitPartial(map[entity]);
this._sectionStart = this._index + 1;
}
}
}n/a
_stateAfterAttributeName = function (c){
if(c === "="){
this._state = BEFORE_ATTRIBUTE_VALUE;
} else if(c === "/" || c === ">"){
this._cbs.onattribend();
this._state = BEFORE_ATTRIBUTE_NAME;
this._index--;
} else if(!whitespace(c)){
this._cbs.onattribend();
this._state = IN_ATTRIBUTE_NAME;
this._sectionStart = this._index;
}
}n/a
_stateAfterCdata1 = function (c){
if(c === char) this._state = SUCCESS;
}n/a
_stateAfterCdata2 = function (c){
if(c === ">"){
//remove 2 trailing chars
this._cbs.oncdata(this._buffer.substring(this._sectionStart, this._index - 2));
this._state = TEXT;
this._sectionStart = this._index + 1;
} else if(c !== "]") {
this._state = IN_CDATA;
}
//else: stay in AFTER_CDATA_2 (`]]]>`)
}n/a
_stateAfterCloseingTagName = function (c){
//skip everything until ">"
if(c === ">"){
this._state = TEXT;
this._sectionStart = this._index + 1;
}
}n/a
_stateAfterComment1 = function (c){
if(c === "-"){
this._state = AFTER_COMMENT_2;
} else {
this._state = IN_COMMENT;
}
}n/a
_stateAfterComment2 = function (c){
if(c === ">"){
//remove 2 trailing chars
this._cbs.oncomment(this._buffer.substring(this._sectionStart, this._index - 2));
this._state = TEXT;
this._sectionStart = this._index + 1;
} else if(c !== "-"){
this._state = IN_COMMENT;
}
// else: stay in AFTER_COMMENT_2 (`--->`)
}n/a
_stateAfterScript1 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterScript2 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterScript3 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterScript4 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterScript5 = function (c){
if(c === ">" || whitespace(c)){
this._special = SPECIAL_NONE;
this._state = IN_CLOSING_TAG_NAME;
this._sectionStart = this._index - 6;
this._index--; //reconsume the token
}
else this._state = TEXT;
}n/a
_stateAfterStyle1 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterStyle2 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterStyle3 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateAfterStyle4 = function (c){
if(c === ">" || whitespace(c)){
this._special = SPECIAL_NONE;
this._state = IN_CLOSING_TAG_NAME;
this._sectionStart = this._index - 5;
this._index--; //reconsume the token
}
else this._state = TEXT;
}n/a
_stateBeforeAttributeName = function (c){
if(c === ">"){
this._cbs.onopentagend();
this._state = TEXT;
this._sectionStart = this._index + 1;
} else if(c === "/"){
this._state = IN_SELF_CLOSING_TAG;
} else if(!whitespace(c)){
this._state = IN_ATTRIBUTE_NAME;
this._sectionStart = this._index;
}
}n/a
_stateBeforeAttributeValue = function (c){
if(c === "\""){
this._state = IN_ATTRIBUTE_VALUE_DQ;
this._sectionStart = this._index + 1;
} else if(c === "'"){
this._state = IN_ATTRIBUTE_VALUE_SQ;
this._sectionStart = this._index + 1;
} else if(!whitespace(c)){
this._state = IN_ATTRIBUTE_VALUE_NQ;
this._sectionStart = this._index;
this._index--; //reconsume token
}
}n/a
_stateBeforeCdata1 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeCdata2 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeCdata3 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeCdata4 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeCdata5 = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeCdata6 = function (c){
if(c === "["){
this._state = IN_CDATA;
this._sectionStart = this._index + 1;
} else {
this._state = IN_DECLARATION;
this._index--;
}
}n/a
_stateBeforeCloseingTagName = function (c){
if(whitespace(c));
else if(c === ">"){
this._state = TEXT;
} else if(this._special !== SPECIAL_NONE){
if(c === "s" || c === "S"){
this._state = BEFORE_SPECIAL_END;
} else {
this._state = TEXT;
this._index--;
}
} else {
this._state = IN_CLOSING_TAG_NAME;
this._sectionStart = this._index;
}
}n/a
_stateBeforeComment = function (c){
if(c === "-"){
this._state = IN_COMMENT;
this._sectionStart = this._index + 1;
} else {
this._state = IN_DECLARATION;
}
}n/a
_stateBeforeDeclaration = function (c){
this._state = c === "[" ? BEFORE_CDATA_1 :
c === "-" ? BEFORE_COMMENT :
IN_DECLARATION;
}n/a
_stateBeforeEntity = function (c){
if(c === lower){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeNumericEntity = function (c){
if(c === lower || c === upper){
this._state = SUCCESS;
} else {
this._state = FAILURE;
this._index--;
}
}n/a
_stateBeforeScript1 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeScript2 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeScript3 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeScript4 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeScript5 = function (c){
if(c === "/" || c === ">" || whitespace(c)){
this._special = SPECIAL_SCRIPT;
}
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}n/a
_stateBeforeSpecial = function (c){
if(c === "c" || c === "C"){
this._state = BEFORE_SCRIPT_1;
} else if(c === "t" || c === "T"){
this._state = BEFORE_STYLE_1;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeSpecialEnd = function (c){
if(this._special === SPECIAL_SCRIPT && (c === "c" || c === "C")){
this._state = AFTER_SCRIPT_1;
} else if(this._special === SPECIAL_STYLE && (c === "t" || c === "T")){
this._state = AFTER_STYLE_1;
}
else this._state = TEXT;
}n/a
_stateBeforeStyle1 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeStyle2 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeStyle3 = function (c){
if(c === lower || c === upper){
this._state = NEXT_STATE;
} else {
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}
}n/a
_stateBeforeStyle4 = function (c){
if(c === "/" || c === ">" || whitespace(c)){
this._special = SPECIAL_STYLE;
}
this._state = IN_TAG_NAME;
this._index--; //consume the token again
}n/a
_stateBeforeTagName = function (c){
if(c === "/"){
this._state = BEFORE_CLOSING_TAG_NAME;
} else if(c === "<"){
this._cbs.ontext(this._getSection());
this._sectionStart = this._index;
} else if(c === ">" || this._special !== SPECIAL_NONE || whitespace(c)) {
this._state = TEXT;
} else if(c === "!"){
this._state = BEFORE_DECLARATION;
this._sectionStart = this._index + 1;
} else if(c === "?"){
this._state = IN_PROCESSING_INSTRUCTION;
this._sectionStart = this._index + 1;
} else {
this._state = (!this._xmlMode && (c === "s" || c === "S")) ?
BEFORE_SPECIAL : IN_TAG_NAME;
this._sectionStart = this._index;
}
}n/a
_stateInAttributeName = function (c){
if(c === "=" || c === "/" || c === ">" || whitespace(c)){
this._cbs.onattribname(this._getSection());
this._sectionStart = -1;
this._state = AFTER_ATTRIBUTE_NAME;
this._index--;
}
}n/a
_stateInAttributeValueDoubleQuotes = function (c){
if(c === "\""){
this._emitToken("onattribdata");
this._cbs.onattribend();
this._state = BEFORE_ATTRIBUTE_NAME;
} else if(this._decodeEntities && c === "&"){
this._emitToken("onattribdata");
this._baseState = this._state;
this._state = BEFORE_ENTITY;
this._sectionStart = this._index;
}
}n/a
_stateInAttributeValueNoQuotes = function (c){
if(whitespace(c) || c === ">"){
this._emitToken("onattribdata");
this._cbs.onattribend();
this._state = BEFORE_ATTRIBUTE_NAME;
this._index--;
} else if(this._decodeEntities && c === "&"){
this._emitToken("onattribdata");
this._baseState = this._state;
this._state = BEFORE_ENTITY;
this._sectionStart = this._index;
}
}n/a
_stateInAttributeValueSingleQuotes = function (c){
if(c === "'"){
this._emitToken("onattribdata");
this._cbs.onattribend();
this._state = BEFORE_ATTRIBUTE_NAME;
} else if(this._decodeEntities && c === "&"){
this._emitToken("onattribdata");
this._baseState = this._state;
this._state = BEFORE_ENTITY;
this._sectionStart = this._index;
}
}n/a
_stateInCdata = function (c){
if(c === "]") this._state = AFTER_CDATA_1;
}n/a
_stateInCloseingTagName = function (c){
if(c === ">" || whitespace(c)){
this._emitToken("onclosetag");
this._state = AFTER_CLOSING_TAG_NAME;
this._index--;
}
}n/a
_stateInComment = function (c){
if(c === "-") this._state = AFTER_COMMENT_1;
}n/a
_stateInDeclaration = function (c){
if(c === ">"){
this._cbs.ondeclaration(this._getSection());
this._state = TEXT;
this._sectionStart = this._index + 1;
}
}n/a
_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--;
}
}n/a
_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);
else if(this._sectionStart + 1 === this._index);
else if(this._baseState !== TEXT){
if(c !== "="){
this._parseNamedEntityStrict();
}
} else {
this._parseLegacyEntity();
}
this._state = this._baseState;
this._index--;
}
}n/a
_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--;
}
}n/a
_stateInProcessingInstruction = function (c){
if(c === ">"){
this._cbs.onprocessinginstruction(this._getSection());
this._state = TEXT;
this._sectionStart = this._index + 1;
}
}n/a
_stateInSelfClosingTag = function (c){
if(c === ">"){
this._cbs.onselfclosingtag();
this._state = TEXT;
this._sectionStart = this._index + 1;
} else if(!whitespace(c)){
this._state = BEFORE_ATTRIBUTE_NAME;
this._index--;
}
}n/a
_stateInTagName = function (c){
if(c === "/" || c === ">" || whitespace(c)){
this._emitToken("onopentagname");
this._state = BEFORE_ATTRIBUTE_NAME;
this._index--;
}
}n/a
_stateText = function (c){
if(c === "<"){
if(this._index > this._sectionStart){
this._cbs.ontext(this._getSection());
}
this._state = BEFORE_TAG_NAME;
this._sectionStart = this._index;
} else if(this._decodeEntities && this._special === SPECIAL_NONE && c === "&"){
if(this._index > this._sectionStart){
this._cbs.ontext(this._getSection());
}
this._baseState = TEXT;
this._state = BEFORE_ENTITY;
this._sectionStart = this._index;
}
}n/a
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();
}...
onclosetag: function(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!
...getAbsoluteIndex = function (){
return this._bufferOffset + this._index;
}n/a
pause = function (){
this._running = false;
}n/a
reset = function (){
Tokenizer.call(this, {xmlMode: this._xmlMode, decodeEntities: this._decodeEntities}, this._cbs);
}n/a
resume = function (){
this._running = true;
if(this._index < this._buffer.length){
this._parse();
}
if(this._ended){
this._finish();
}
}n/a
write = function (chunk){
if(this._ended) this._cbs.onerror(Error(".write() after done!"));
this._buffer += chunk;
this._parse();
}...
},
onclosetag: function(tagname){
if(tagname === "script"){
console.log("That's it?!");
}
}
}, {decodeEntities: true});
parser.write("Xyz <script type='text/javascript'>var foo =
x27;<<bar>>';</ script>");
parser.end();
```
Output (simplified):
```
--> Xyz
...function Stream(cbs, options){
var parser = this._parser = new Parser(cbs, options);
var decoder = this._decoder = new StringDecoder();
WritableStream.call(this, {decodeStrings: false});
this.once("finish", function(){
parser.end(decoder.end());
});
}n/a
function Writable(options) {
// Writable ctor is applied to Duplexes, too.
// `realHasInstance` is necessary because using plain `instanceof`
// would return false, as no `_writableState` property is attached.
// Trying to use the custom `instanceof` for Writable here will also break the
// Node.js LazyTransform implementation, which has a non-trivial getter for
// `_writableState` that would lead to infinite recursion.
if (!(realHasInstance.call(Writable, this)) &&
!(this instanceof Stream.Duplex)) {
return new Writable(options);
}
this._writableState = new WritableState(options, this);
// legacy.
this.writable = true;
if (options) {
if (typeof options.write === 'function')
this._write = options.write;
if (typeof options.writev === 'function')
this._writev = options.writev;
}
Stream.call(this);
}n/a