50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var ReadableStream = require('stream').Readable,
|
||
|
inherits = require('util').inherits,
|
||
|
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)
|
||
|
});
|
||
|
};
|
||
|
|
||
|
inherits(SerializerStream, ReadableStream);
|
||
|
|
||
|
//Readable stream implementation
|
||
|
SerializerStream.prototype._read = function () {
|
||
|
this.serializer.serialize();
|
||
|
this.push(null);
|
||
|
};
|