Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Unified Diff: node_modules/vulcanize/node_modules/whacko/node_modules/parse5/lib/serialization/serializer.js

Issue 800513006: Added vulcanize under third_party/npm_modules (Closed) Base URL: https://chromium.googlesource.com/infra/third_party/npm_modules.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: node_modules/vulcanize/node_modules/whacko/node_modules/parse5/lib/serialization/serializer.js
diff --git a/node_modules/vulcanize/node_modules/whacko/node_modules/parse5/lib/serialization/serializer.js b/node_modules/vulcanize/node_modules/whacko/node_modules/parse5/lib/serialization/serializer.js
new file mode 100644
index 0000000000000000000000000000000000000000..7e60e5c7c04baebb96196477a604d7bedb8756b0
--- /dev/null
+++ b/node_modules/vulcanize/node_modules/whacko/node_modules/parse5/lib/serialization/serializer.js
@@ -0,0 +1,184 @@
+'use strict';
+
+var DefaultTreeAdapter = require('../tree_adapters/default'),
+ HTML = require('../common/html');
+
+//Aliases
+var $ = HTML.TAG_NAMES,
+ NS = HTML.NAMESPACES;
+
+
+//Escaping regexes
+var AMP_REGEX = /&/g,
+ NBSP_REGEX = /\u00a0/g,
+ DOUBLE_QUOTE_REGEX = /"/g,
+ LT_REGEX = /</g,
+ GT_REGEX = />/g;
+
+
+//Escape string
+function escapeString(str, attrMode) {
+ str = str
+ .replace(AMP_REGEX, '&amp;')
+ .replace(NBSP_REGEX, '&nbsp;');
+
+ if (attrMode)
+ str = str.replace(DOUBLE_QUOTE_REGEX, '&quot;');
+
+ else {
+ str = str
+ .replace(LT_REGEX, '&lt;')
+ .replace(GT_REGEX, '&gt;');
+ }
+
+ return str;
+}
+
+//Enquote doctype ID
+function enquoteDoctypeId(id) {
+ var quote = id.indexOf('"') !== -1 ? '\'' : '"';
+
+ return quote + id + quote;
+}
+
+
+//Serializer
+var Serializer = module.exports = function (treeAdapter) {
+ this.treeAdapter = treeAdapter || DefaultTreeAdapter;
+};
+
+
+//API
+Serializer.prototype.serialize = function (node) {
+ this.html = '';
+ this._serializeChildNodes(node);
+
+ 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),
+ qualifiedTn = (ns === NS.HTML || ns === NS.SVG || ns === NS.MATHML) ? tn : (ns + ':' + tn);
+
+ this.html += '<' + qualifiedTn;
+ 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) {
+
+ if (tn === $.PRE || tn === $.TEXTAREA || tn === $.LISTING) {
+ var firstChild = this.treeAdapter.getFirstChild(node);
+
+ if (firstChild && this.treeAdapter.isTextNode(firstChild)) {
+ var content = this.treeAdapter.getTextNodeContent(firstChild);
+
+ if (content[0] === '\n')
+ this.html += '\n';
+ }
+ }
+
+ var childNodesHolder = tn === $.TEMPLATE && ns === NS.HTML ?
+ this.treeAdapter.getChildNodes(node)[0] :
+ node;
+
+ this._serializeChildNodes(childNodesHolder);
+ this.html += '</' + qualifiedTn + '>';
+ }
+};
+
+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];
+
+ 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 += '="' + escapeString(attr.value, true) + '"';
+ }
+};
+
+Serializer.prototype._serializeTextNode = function (node) {
+ var parent = this.treeAdapter.getParentNode(node),
+ parentTn = parent && this.treeAdapter.getTagName(parent),
+ content = this.treeAdapter.getTextNodeContent(node);
+
+ if (parentTn === $.STYLE || parentTn === $.SCRIPT || parentTn === $.XMP || parentTn === $.IFRAME ||
+ parentTn === $.NOEMBED || parentTn === $.NOFRAMES || parentTn === $.PLAINTEXT || parentTn === $.NOSCRIPT) {
+ this.html += content;
+ }
+
+ else
+ this.html += 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),
+ publicId = this.treeAdapter.getDocumentTypeNodePublicId(node),
+ systemId = this.treeAdapter.getDocumentTypeNodeSystemId(node);
+
+ this.html += '<!DOCTYPE ' + name;
+
+ if (publicId !== null)
+ this.html += ' PUBLIC ' + enquoteDoctypeId(publicId);
+
+ else if (systemId !== null)
+ this.html += ' SYSTEM';
+
+ if (systemId !== null)
+ this.html += ' ' + enquoteDoctypeId(systemId);
+
+
+ this.html += '>';
+};

Powered by Google App Engine
This is Rietveld 408576698