mirror of
https://github.com/S2-/minifyfromhtml.git
synced 2025-08-03 12:20:04 +02:00
update node modules
This commit is contained in:
296
node_modules/parse5/lib/serializer/index.js
generated
vendored
296
node_modules/parse5/lib/serializer/index.js
generated
vendored
@@ -1,162 +1,176 @@
|
||||
'use strict';
|
||||
|
||||
var defaultTreeAdapter = require('../tree_adapters/default'),
|
||||
mergeOptions = require('../utils/merge_options'),
|
||||
doctype = require('../common/doctype'),
|
||||
HTML = require('../common/html');
|
||||
const defaultTreeAdapter = require('../tree-adapters/default');
|
||||
const mergeOptions = require('../utils/merge-options');
|
||||
const doctype = require('../common/doctype');
|
||||
const HTML = require('../common/html');
|
||||
|
||||
//Aliases
|
||||
var $ = HTML.TAG_NAMES,
|
||||
NS = HTML.NAMESPACES;
|
||||
const $ = HTML.TAG_NAMES;
|
||||
const NS = HTML.NAMESPACES;
|
||||
|
||||
//Default serializer options
|
||||
var DEFAULT_OPTIONS = {
|
||||
const DEFAULT_OPTIONS = {
|
||||
treeAdapter: defaultTreeAdapter
|
||||
};
|
||||
|
||||
//Escaping regexes
|
||||
var AMP_REGEX = /&/g,
|
||||
NBSP_REGEX = /\u00a0/g,
|
||||
DOUBLE_QUOTE_REGEX = /"/g,
|
||||
LT_REGEX = /</g,
|
||||
GT_REGEX = />/g;
|
||||
const AMP_REGEX = /&/g;
|
||||
const NBSP_REGEX = /\u00a0/g;
|
||||
const DOUBLE_QUOTE_REGEX = /"/g;
|
||||
const LT_REGEX = /</g;
|
||||
const GT_REGEX = />/g;
|
||||
|
||||
//Serializer
|
||||
var Serializer = module.exports = function (node, options) {
|
||||
this.options = mergeOptions(DEFAULT_OPTIONS, options);
|
||||
this.treeAdapter = this.options.treeAdapter;
|
||||
class Serializer {
|
||||
constructor(node, options) {
|
||||
this.options = mergeOptions(DEFAULT_OPTIONS, options);
|
||||
this.treeAdapter = this.options.treeAdapter;
|
||||
|
||||
this.html = '';
|
||||
this.startNode = node;
|
||||
};
|
||||
this.html = '';
|
||||
this.startNode = node;
|
||||
}
|
||||
|
||||
// NOTE: exported as static method for the testing purposes
|
||||
Serializer.escapeString = function (str, attrMode) {
|
||||
str = str
|
||||
.replace(AMP_REGEX, '&')
|
||||
.replace(NBSP_REGEX, ' ');
|
||||
//API
|
||||
serialize() {
|
||||
this._serializeChildNodes(this.startNode);
|
||||
|
||||
if (attrMode)
|
||||
return this.html;
|
||||
}
|
||||
|
||||
//Internals
|
||||
_serializeChildNodes(parentNode) {
|
||||
const childNodes = this.treeAdapter.getChildNodes(parentNode);
|
||||
|
||||
if (childNodes) {
|
||||
for (let i = 0, cnLength = childNodes.length; i < cnLength; i++) {
|
||||
const currentNode = childNodes[i];
|
||||
|
||||
if (this.treeAdapter.isElementNode(currentNode)) {
|
||||
this._serializeElement(currentNode);
|
||||
} else if (this.treeAdapter.isTextNode(currentNode)) {
|
||||
this._serializeTextNode(currentNode);
|
||||
} else if (this.treeAdapter.isCommentNode(currentNode)) {
|
||||
this._serializeCommentNode(currentNode);
|
||||
} else if (this.treeAdapter.isDocumentTypeNode(currentNode)) {
|
||||
this._serializeDocumentTypeNode(currentNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_serializeElement(node) {
|
||||
const tn = this.treeAdapter.getTagName(node);
|
||||
const ns = this.treeAdapter.getNamespaceURI(node);
|
||||
|
||||
this.html += '<' + tn;
|
||||
this._serializeAttributes(node);
|
||||
this.html += '>';
|
||||
|
||||
if (
|
||||
tn !== $.AREA &&
|
||||
tn !== $.BASE &&
|
||||
tn !== $.BASEFONT &&
|
||||
tn !== $.BGSOUND &&
|
||||
tn !== $.BR &&
|
||||
tn !== $.COL &&
|
||||
tn !== $.EMBED &&
|
||||
tn !== $.FRAME &&
|
||||
tn !== $.HR &&
|
||||
tn !== $.IMG &&
|
||||
tn !== $.INPUT &&
|
||||
tn !== $.KEYGEN &&
|
||||
tn !== $.LINK &&
|
||||
tn !== $.META &&
|
||||
tn !== $.PARAM &&
|
||||
tn !== $.SOURCE &&
|
||||
tn !== $.TRACK &&
|
||||
tn !== $.WBR
|
||||
) {
|
||||
const childNodesHolder =
|
||||
tn === $.TEMPLATE && ns === NS.HTML ? this.treeAdapter.getTemplateContent(node) : node;
|
||||
|
||||
this._serializeChildNodes(childNodesHolder);
|
||||
this.html += '</' + tn + '>';
|
||||
}
|
||||
}
|
||||
|
||||
_serializeAttributes(node) {
|
||||
const attrs = this.treeAdapter.getAttrList(node);
|
||||
|
||||
for (let i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
|
||||
const attr = attrs[i];
|
||||
const value = Serializer.escapeString(attr.value, true);
|
||||
|
||||
this.html += ' ';
|
||||
|
||||
if (!attr.namespace) {
|
||||
this.html += attr.name;
|
||||
} else if (attr.namespace === NS.XML) {
|
||||
this.html += 'xml:' + attr.name;
|
||||
} else if (attr.namespace === NS.XMLNS) {
|
||||
if (attr.name !== 'xmlns') {
|
||||
this.html += 'xmlns:';
|
||||
}
|
||||
|
||||
this.html += attr.name;
|
||||
} else if (attr.namespace === NS.XLINK) {
|
||||
this.html += 'xlink:' + attr.name;
|
||||
} else {
|
||||
this.html += attr.namespace + ':' + attr.name;
|
||||
}
|
||||
|
||||
this.html += '="' + value + '"';
|
||||
}
|
||||
}
|
||||
|
||||
_serializeTextNode(node) {
|
||||
const content = this.treeAdapter.getTextNodeContent(node);
|
||||
const parent = this.treeAdapter.getParentNode(node);
|
||||
let parentTn = void 0;
|
||||
|
||||
if (parent && this.treeAdapter.isElementNode(parent)) {
|
||||
parentTn = this.treeAdapter.getTagName(parent);
|
||||
}
|
||||
|
||||
if (
|
||||
parentTn === $.STYLE ||
|
||||
parentTn === $.SCRIPT ||
|
||||
parentTn === $.XMP ||
|
||||
parentTn === $.IFRAME ||
|
||||
parentTn === $.NOEMBED ||
|
||||
parentTn === $.NOFRAMES ||
|
||||
parentTn === $.PLAINTEXT ||
|
||||
parentTn === $.NOSCRIPT
|
||||
) {
|
||||
this.html += content;
|
||||
} else {
|
||||
this.html += Serializer.escapeString(content, false);
|
||||
}
|
||||
}
|
||||
|
||||
_serializeCommentNode(node) {
|
||||
this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
|
||||
}
|
||||
|
||||
_serializeDocumentTypeNode(node) {
|
||||
const name = this.treeAdapter.getDocumentTypeNodeName(node);
|
||||
|
||||
this.html += '<' + doctype.serializeContent(name, null, null) + '>';
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: used in tests and by rewriting stream
|
||||
Serializer.escapeString = function(str, attrMode) {
|
||||
str = str.replace(AMP_REGEX, '&').replace(NBSP_REGEX, ' ');
|
||||
|
||||
if (attrMode) {
|
||||
str = str.replace(DOUBLE_QUOTE_REGEX, '"');
|
||||
|
||||
else {
|
||||
str = str
|
||||
.replace(LT_REGEX, '<')
|
||||
.replace(GT_REGEX, '>');
|
||||
} else {
|
||||
str = str.replace(LT_REGEX, '<').replace(GT_REGEX, '>');
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
|
||||
//API
|
||||
Serializer.prototype.serialize = function () {
|
||||
this._serializeChildNodes(this.startNode);
|
||||
|
||||
return this.html;
|
||||
};
|
||||
|
||||
|
||||
//Internals
|
||||
Serializer.prototype._serializeChildNodes = function (parentNode) {
|
||||
var childNodes = this.treeAdapter.getChildNodes(parentNode);
|
||||
|
||||
if (childNodes) {
|
||||
for (var i = 0, cnLength = childNodes.length; i < cnLength; i++) {
|
||||
var currentNode = childNodes[i];
|
||||
|
||||
if (this.treeAdapter.isElementNode(currentNode))
|
||||
this._serializeElement(currentNode);
|
||||
|
||||
else if (this.treeAdapter.isTextNode(currentNode))
|
||||
this._serializeTextNode(currentNode);
|
||||
|
||||
else if (this.treeAdapter.isCommentNode(currentNode))
|
||||
this._serializeCommentNode(currentNode);
|
||||
|
||||
else if (this.treeAdapter.isDocumentTypeNode(currentNode))
|
||||
this._serializeDocumentTypeNode(currentNode);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Serializer.prototype._serializeElement = function (node) {
|
||||
var tn = this.treeAdapter.getTagName(node),
|
||||
ns = this.treeAdapter.getNamespaceURI(node);
|
||||
|
||||
this.html += '<' + tn;
|
||||
this._serializeAttributes(node);
|
||||
this.html += '>';
|
||||
|
||||
if (tn !== $.AREA && tn !== $.BASE && tn !== $.BASEFONT && tn !== $.BGSOUND && tn !== $.BR && tn !== $.BR &&
|
||||
tn !== $.COL && tn !== $.EMBED && tn !== $.FRAME && tn !== $.HR && tn !== $.IMG && tn !== $.INPUT &&
|
||||
tn !== $.KEYGEN && tn !== $.LINK && tn !== $.MENUITEM && tn !== $.META && tn !== $.PARAM && tn !== $.SOURCE &&
|
||||
tn !== $.TRACK && tn !== $.WBR) {
|
||||
|
||||
var childNodesHolder = tn === $.TEMPLATE && ns === NS.HTML ?
|
||||
this.treeAdapter.getTemplateContent(node) :
|
||||
node;
|
||||
|
||||
this._serializeChildNodes(childNodesHolder);
|
||||
this.html += '</' + tn + '>';
|
||||
}
|
||||
};
|
||||
|
||||
Serializer.prototype._serializeAttributes = function (node) {
|
||||
var attrs = this.treeAdapter.getAttrList(node);
|
||||
|
||||
for (var i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
|
||||
var attr = attrs[i],
|
||||
value = Serializer.escapeString(attr.value, true);
|
||||
|
||||
this.html += ' ';
|
||||
|
||||
if (!attr.namespace)
|
||||
this.html += attr.name;
|
||||
|
||||
else if (attr.namespace === NS.XML)
|
||||
this.html += 'xml:' + attr.name;
|
||||
|
||||
else if (attr.namespace === NS.XMLNS) {
|
||||
if (attr.name !== 'xmlns')
|
||||
this.html += 'xmlns:';
|
||||
|
||||
this.html += attr.name;
|
||||
}
|
||||
|
||||
else if (attr.namespace === NS.XLINK)
|
||||
this.html += 'xlink:' + attr.name;
|
||||
|
||||
else
|
||||
this.html += attr.namespace + ':' + attr.name;
|
||||
|
||||
this.html += '="' + value + '"';
|
||||
}
|
||||
};
|
||||
|
||||
Serializer.prototype._serializeTextNode = function (node) {
|
||||
var content = this.treeAdapter.getTextNodeContent(node),
|
||||
parent = this.treeAdapter.getParentNode(node),
|
||||
parentTn = void 0;
|
||||
|
||||
if (parent && this.treeAdapter.isElementNode(parent))
|
||||
parentTn = this.treeAdapter.getTagName(parent);
|
||||
|
||||
if (parentTn === $.STYLE || parentTn === $.SCRIPT || parentTn === $.XMP || parentTn === $.IFRAME ||
|
||||
parentTn === $.NOEMBED || parentTn === $.NOFRAMES || parentTn === $.PLAINTEXT || parentTn === $.NOSCRIPT)
|
||||
|
||||
this.html += content;
|
||||
|
||||
else
|
||||
this.html += Serializer.escapeString(content, false);
|
||||
};
|
||||
|
||||
Serializer.prototype._serializeCommentNode = function (node) {
|
||||
this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
|
||||
};
|
||||
|
||||
Serializer.prototype._serializeDocumentTypeNode = function (node) {
|
||||
var name = this.treeAdapter.getDocumentTypeNodeName(node);
|
||||
|
||||
this.html += '<' + doctype.serializeContent(name, null, null) + '>';
|
||||
};
|
||||
module.exports = Serializer;
|
||||
|
28
node_modules/parse5/lib/serializer/serializer_stream.js
generated
vendored
28
node_modules/parse5/lib/serializer/serializer_stream.js
generated
vendored
@@ -1,28 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var ReadableStream = require('stream').Readable,
|
||||
inherits = require('util').inherits,
|
||||
Serializer = require('./index');
|
||||
|
||||
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);
|
||||
};
|
Reference in New Issue
Block a user