react-native-parse-html
Version:
WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.js
50 lines (43 loc) • 1.52 kB
JavaScript
;
var ReadableStream = require('react-native-stream').Readable,
setPrototypeOf = require('../common/setPrototypeOf'),
Serializer = require('./index');
/**
* Streaming AST node to an HTML serializer.
* A [readable stream]{@link https://nodejs.org/api/stream.html#stream_class_stream_readable}.
* @class SerializerStream
* @memberof parse5
* @instance
* @extends stream.Readable
* @param {ASTNode} node - Node to serialize.
* @param {SerializerOptions} [options] - Serialization options.
* @example
* var parse5 = require('parse5');
* var fs = require('fs');
*
* var file = fs.createWriteStream('/home/index.html');
*
* // Serializes the parsed document to HTML and writes it to the file.
* var document = parse5.parse('<body>Who is John Galt?</body>');
* var serializer = new parse5.SerializerStream(document);
*
* serializer.pipe(file);
*/
var SerializerStream = module.exports = function (node, options) {
ReadableStream.call(this);
this.serializer = new Serializer(node, options);
Object.defineProperty(this.serializer, 'html', {
//NOTE: To make `+=` concat operator work properly we define
//getter which always returns empty string
get: function () {
return '';
},
set: this.push.bind(this)
});
};
setPrototypeOf(SerializerStream.prototype, ReadableStream.prototype);
//Readable stream implementation
SerializerStream.prototype._read = function () {
this.serializer.serialize();
this.push(null);
};