Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parse5

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse5 - npm Package Compare versions

Comparing version 2.2.3 to 3.0.0

lib/index.d.ts

49

lib/common/doctype.js
'use strict';
var DOCUMENT_MODE = require('./html').DOCUMENT_MODE;
//Const

@@ -74,6 +76,6 @@ var VALID_DOCTYPE_NAME = 'html',

],
QUIRKS_MODE_NO_SYSTEM_ID_PUBLIC_ID_PREFIXES = [
QUIRKS_MODE_NO_SYSTEM_ID_PUBLIC_ID_PREFIXES = QUIRKS_MODE_PUBLIC_ID_PREFIXES.concat([
'-//w3c//dtd html 4.01 frameset//',
'-//w3c//dtd html 4.01 transitional//'
],
]),
QUIRKS_MODE_PUBLIC_IDS = [

@@ -83,3 +85,11 @@ '-//w3o//dtd w3 html strict 3.0//en//',

'html'
];
],
LIMITED_QUIRKS_PUBLIC_ID_PREFIXES = [
'-//W3C//DTD XHTML 1.0 Frameset//',
'-//W3C//DTD XHTML 1.0 Transitional//'
],
LIMITED_QUIRKS_WITH_SYSTEM_ID_PUBLIC_ID_PREFIXES = LIMITED_QUIRKS_PUBLIC_ID_PREFIXES.concat([
'-//W3C//DTD HTML 4.01 Frameset//',
'-//W3C//DTD HTML 4.01 Transitional//'
]);

@@ -94,10 +104,19 @@

function hasPrefix(publicId, prefixes) {
for (var i = 0; i < prefixes.length; i++) {
if (publicId.indexOf(prefixes[i]) === 0)
return true;
}
return false;
}
//API
exports.isQuirks = function (name, publicId, systemId) {
exports.getDocumentMode = function (name, publicId, systemId) {
if (name !== VALID_DOCTYPE_NAME)
return true;
return DOCUMENT_MODE.QUIRKS;
if (systemId && systemId.toLowerCase() === QUIRKS_MODE_SYSTEM_ID)
return true;
return DOCUMENT_MODE.QUIRKS;

@@ -108,16 +127,16 @@ if (publicId !== null) {

if (QUIRKS_MODE_PUBLIC_IDS.indexOf(publicId) > -1)
return true;
return DOCUMENT_MODE.QUIRKS;
var prefixes = QUIRKS_MODE_PUBLIC_ID_PREFIXES;
var prefixes = systemId === null ? QUIRKS_MODE_NO_SYSTEM_ID_PUBLIC_ID_PREFIXES : QUIRKS_MODE_PUBLIC_ID_PREFIXES;
if (systemId === null)
prefixes = prefixes.concat(QUIRKS_MODE_NO_SYSTEM_ID_PUBLIC_ID_PREFIXES);
if (hasPrefix(publicId, prefixes))
return DOCUMENT_MODE.QUIRKS;
for (var i = 0; i < prefixes.length; i++) {
if (publicId.indexOf(prefixes[i]) === 0)
return true;
}
prefixes = systemId === null ? LIMITED_QUIRKS_PUBLIC_ID_PREFIXES : LIMITED_QUIRKS_WITH_SYSTEM_ID_PUBLIC_ID_PREFIXES;
if (hasPrefix(publicId, prefixes))
return DOCUMENT_MODE.LIMITED_QUIRKS;
}
return false;
return DOCUMENT_MODE.NO_QUIRKS;
};

@@ -124,0 +143,0 @@

@@ -138,3 +138,3 @@ 'use strict';

//Tags that causes exit from foreign content
var EXITS_FOREIGN_CONTENT = {};
var EXITS_FOREIGN_CONTENT = Object.create(null);

@@ -141,0 +141,0 @@ EXITS_FOREIGN_CONTENT[$.B] = true;

@@ -23,2 +23,8 @@ 'use strict';

exports.DOCUMENT_MODE = {
NO_QUIRKS: 'no-quirks',
QUIRKS: 'quirks',
LIMITED_QUIRKS: 'limited-quirks'
};
var $ = exports.TAG_NAMES = {

@@ -171,5 +177,5 @@ A: 'a',

var SPECIAL_ELEMENTS = exports.SPECIAL_ELEMENTS = {};
var SPECIAL_ELEMENTS = exports.SPECIAL_ELEMENTS = Object.create(null);
SPECIAL_ELEMENTS[NS.HTML] = {};
SPECIAL_ELEMENTS[NS.HTML] = Object.create(null);
SPECIAL_ELEMENTS[NS.HTML][$.ADDRESS] = true;

@@ -257,3 +263,3 @@ SPECIAL_ELEMENTS[NS.HTML][$.APPLET] = true;

SPECIAL_ELEMENTS[NS.MATHML] = {};
SPECIAL_ELEMENTS[NS.MATHML] = Object.create(null);
SPECIAL_ELEMENTS[NS.MATHML][$.MI] = true;

@@ -266,5 +272,5 @@ SPECIAL_ELEMENTS[NS.MATHML][$.MO] = true;

SPECIAL_ELEMENTS[NS.SVG] = {};
SPECIAL_ELEMENTS[NS.SVG] = Object.create(null);
SPECIAL_ELEMENTS[NS.SVG][$.TITLE] = true;
SPECIAL_ELEMENTS[NS.SVG][$.FOREIGN_OBJECT] = true;
SPECIAL_ELEMENTS[NS.SVG][$.DESC] = true;
'use strict';
module.exports = function mergeOptions(defaults, options) {
options = options || {};
options = options || Object.create(null);

@@ -12,3 +12,3 @@ return [defaults, options].reduce(function (merged, optObj) {

return merged;
}, {});
}, Object.create(null));
};

@@ -6,17 +6,4 @@ 'use strict';

/** @namespace parse5 */
/**
* Parses an HTML string.
* @function parse
* @memberof parse5
* @instance
* @param {string} html - Input HTML string.
* @param {ParserOptions} [options] - Parsing options.
* @returns {ASTNode<Document>} document
* @example
* var parse5 = require('parse5');
*
* var document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
*/
// Shorthands
exports.parse = function parse(html, options) {

@@ -28,20 +15,2 @@ var parser = new Parser(options);

/**
* Parses an HTML fragment.
* @function parseFragment
* @memberof parse5
* @instance
* @param {ASTNode} [fragmentContext] - Parsing context element. If specified, given fragment
* will be parsed as if it was set to the context element's `innerHTML` property.
* @param {string} html - Input HTML fragment string.
* @param {ParserOptions} [options] - Parsing options.
* @returns {ASTNode<DocumentFragment>} documentFragment
* @example
* var parse5 = require('parse5');
*
* var documentFragment = parse5.parseFragment('<table></table>');
*
* // Parses the html fragment in the context of the parsed <table> element.
* var trFragment = parser.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
*/
exports.parseFragment = function parseFragment(fragmentContext, html, options) {

@@ -59,21 +28,2 @@ if (typeof fragmentContext === 'string') {

/**
* Serializes an AST node to an HTML string.
* @function serialize
* @memberof parse5
* @instance
* @param {ASTNode} node - Node to serialize.
* @param {SerializerOptions} [options] - Serialization options.
* @returns {String} html
* @example
* var parse5 = require('parse5');
*
* var document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
*
* // Serializes a document.
* var html = parse5.serialize(document);
*
* // Serializes the <body> element content.
* var bodyInnerHtml = parse5.serialize(document.childNodes[0].childNodes[1]);
*/
exports.serialize = function (node, options) {

@@ -85,19 +35,4 @@ var serializer = new Serializer(node, options);

/**
* Provides built-in tree adapters that can be used for parsing and serialization.
* @var treeAdapters
* @memberof parse5
* @instance
* @property {TreeAdapter} default - Default tree format for parse5.
* @property {TreeAdapter} htmlparser2 - Quite popular [htmlparser2](https://github.com/fb55/htmlparser2) tree format
* (e.g. used by [cheerio](https://github.com/MatthewMueller/cheerio) and [jsdom](https://github.com/tmpvar/jsdom)).
* @example
* var parse5 = require('parse5');
*
* // Uses the default tree adapter for parsing.
* var document = parse5.parse('<div></div>', { treeAdapter: parse5.treeAdapters.default });
*
* // Uses the htmlparser2 tree adapter with the SerializerStream.
* var serializer = new parse5.SerializerStream(node, { treeAdapter: parse5.treeAdapters.htmlparser2 });
*/
// Tree adapters
exports.treeAdapters = {

@@ -110,4 +45,5 @@ default: require('./tree_adapters/default'),

// Streaming
exports.ParserStream = require('./parser/stream');
exports.SerializerStream = require('./serializer/stream');
exports.ParserStream = require('./parser/parser_stream');
exports.PlainTextConversionStream = require('./parser/plain_text_conversion_stream');
exports.SerializerStream = require('./serializer/serializer_stream');
exports.SAXParser = require('./sax');

@@ -18,9 +18,2 @@ 'use strict';

/**
* @typedef {Object} ElementLocationInfo
* @extends StartTagLocationInfo
*
* @property {StartTagLocationInfo} startTag - Element's start tag location info.
* @property {LocationInfo} endTag - Element's end tag location info.
*/
if (!loc.startTag) {

@@ -27,0 +20,0 @@ loc.startTag = {

@@ -16,3 +16,2 @@ 'use strict';

isEol = false,
lineStartPosStack = [0],
lineStartPos = 0,

@@ -23,10 +22,2 @@ col = -1,

function attachLocationInfo(token) {
/**
* @typedef {Object} LocationInfo
*
* @property {Number} line - One-based line index
* @property {Number} col - One-based column index
* @property {Number} startOffset - Zero-based first character index
* @property {Number} endOffset - Zero-based last character index
*/
token.location = {

@@ -48,3 +39,2 @@ line: tokenLine,

line++;
lineStartPosStack.push(this.preprocessor.sourcePos);
lineStartPos = this.preprocessor.sourcePos;

@@ -65,7 +55,2 @@ }

while (lineStartPos > this.preprocessor.sourcePos && lineStartPosStack.length > 1) {
lineStartPos = lineStartPosStack.pop();
line--;
}
col = this.preprocessor.sourcePos - lineStartPos + 1;

@@ -124,10 +109,4 @@ };

if (!this.currentToken.location.attrs)
this.currentToken.location.attrs = {};
this.currentToken.location.attrs = Object.create(null);
/**
* @typedef {Object} StartTagLocationInfo
* @extends LocationInfo
*
* @property {Dictionary<String, LocationInfo>} attrs - Start tag attributes' location info.
*/
this.currentToken.location.attrs[this.currentAttr.name] = this.currentAttrLocation;

@@ -134,0 +113,0 @@ };

@@ -56,3 +56,3 @@ 'use strict';

neAttrsLength = neAttrs.length,
neAttrsMap = {};
neAttrsMap = Object.create(null);

@@ -59,0 +59,0 @@ //NOTE: build attrs map for the new element so we can perform fast lookups

@@ -0,0 +0,0 @@ 'use strict';

@@ -10,9 +10,2 @@ 'use strict';

/**
* @typedef {Object} SAXParserOptions
*
* @property {Boolean} [locationInfo=false] - Enables source code location information for the tokens.
* When enabled, each token event handler will receive {@link LocationInfo} (or {@link StartTagLocationInfo})
* object as its last argument.
*/
var DEFAULT_OPTIONS = {

@@ -22,31 +15,2 @@ locationInfo: false

/**
* Streaming [SAX]{@link https://en.wikipedia.org/wiki/Simple_API_for_XML}-style HTML parser.
* A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform)
* (which means you can pipe *through* it, see example).
* @class SAXParser
* @memberof parse5
* @instance
* @extends stream.Transform
* @param {SAXParserOptions} options - Parsing options.
* @example
* var parse5 = require('parse5');
* var http = require('http');
* var fs = require('fs');
*
* var file = fs.createWriteStream('/home/google.com.html');
* var parser = new parse5.SAXParser();
*
* parser.on('text', function(text) {
* // Handle page text content
* ...
* });
*
* http.get('http://google.com', function(res) {
* // SAXParser is the Transform stream, which means you can pipe
* // through it. So, you can analyze page content and, e.g., save it
* // to the file at the same time:
* res.pipe(parser).pipe(file);
* });
*/
var SAXParser = module.exports = function (options) {

@@ -95,29 +59,2 @@ TransformStream.call(this);

/**
* Stops parsing. Useful if you want the parser to stop consuming CPU time once you've obtained the desired info
* from the input stream. Doesn't prevent piping, so that data will flow through the parser as usual.
*
* @function stop
* @memberof parse5#SAXParser
* @instance
* @example
* var parse5 = require('parse5');
* var http = require('http');
* var fs = require('fs');
*
* var file = fs.createWriteStream('/home/google.com.html');
* var parser = new parse5.SAXParser();
*
* parser.on('doctype', function(name, publicId, systemId) {
* // Process doctype info ans stop parsing
* ...
* parser.stop();
* });
*
* http.get('http://google.com', function(res) {
* // Despite the fact that parser.stop() was called whole
* // content of the page will be written to the file
* res.pipe(parser).pipe(file);
* });
*/
SAXParser.prototype.stop = function () {

@@ -162,55 +99,11 @@ this.stopped = true;

if (token.type === Tokenizer.START_TAG_TOKEN)
/**
* Raised when the parser encounters a start tag.
* @event startTag
* @memberof parse5#SAXParser
* @instance
* @type {Function}
* @param {String} name - Tag name.
* @param {Array} attrs - List of attributes in the `{ name: String, value: String, prefix?: String }` form.
* @param {Boolean} selfClosing - Indicates if the tag is self-closing.
* @param {StartTagLocationInfo} [location] - Start tag source code location info.
* Available if location info is enabled in {@link SAXParserOptions}.
*/
this.emit('startTag', token.tagName, token.attrs, token.selfClosing, this.currentTokenLocation);
else if (token.type === Tokenizer.END_TAG_TOKEN)
/**
* Raised then parser encounters an end tag.
* @event endTag
* @memberof parse5#SAXParser
* @instance
* @type {Function}
* @param {String} name - Tag name.
* @param {LocationInfo} [location] - End tag source code location info.
* Available if location info is enabled in {@link SAXParserOptions}.
*/
this.emit('endTag', token.tagName, this.currentTokenLocation);
else if (token.type === Tokenizer.COMMENT_TOKEN)
/**
* Raised then parser encounters a comment.
* @event comment
* @memberof parse5#SAXParser
* @instance
* @type {Function}
* @param {String} text - Comment text.
* @param {LocationInfo} [location] - Comment source code location info.
* Available if location info is enabled in {@link SAXParserOptions}.
*/
this.emit('comment', token.data, this.currentTokenLocation);
else if (token.type === Tokenizer.DOCTYPE_TOKEN)
/**
* Raised then parser encounters a [document type declaration]{@link https://en.wikipedia.org/wiki/Document_type_declaration}.
* @event doctype
* @memberof parse5#SAXParser
* @instance
* @type {Function}
* @param {String} name - Document type name.
* @param {String} publicId - Document type public identifier.
* @param {String} systemId - Document type system identifier.
* @param {LocationInfo} [location] - Document type declaration source code location info.
* Available if location info is enabled in {@link SAXParserOptions}.
*/
this.emit('doctype', token.name, token.publicId, token.systemId, this.currentTokenLocation);

@@ -221,12 +114,2 @@ };

if (this.pendingText !== null) {
/**
* Raised then parser encounters text content.
* @event text
* @memberof parse5#SAXParser
* @instance
* @type {Function}
* @param {String} text - Text content.
* @param {LocationInfo} [location] - Text content code location info.
* Available if location info is enabled in {@link SAXParserOptions}.
*/
this.emit('text', this.pendingText, this.currentTokenLocation);

@@ -233,0 +116,0 @@ this.pendingText = null;

@@ -13,7 +13,2 @@ 'use strict';

//Default serializer options
/**
* @typedef {Object} SerializerOptions
*
* @property {TreeAdapter} [treeAdapter=parse5.treeAdapters.default] - Specifies input tree format.
*/
var DEFAULT_OPTIONS = {

@@ -102,13 +97,2 @@ treeAdapter: defaultTreeAdapter

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 ?

@@ -177,7 +161,5 @@ this.treeAdapter.getTemplateContent(node) :

Serializer.prototype._serializeDocumentTypeNode = function (node) {
var name = this.treeAdapter.getDocumentTypeNodeName(node),
publicId = this.treeAdapter.getDocumentTypeNodePublicId(node),
systemId = this.treeAdapter.getDocumentTypeNodeSystemId(node);
var name = this.treeAdapter.getDocumentTypeNodeName(node);
this.html += '<' + doctype.serializeContent(name, publicId, systemId) + '>';
this.html += '<' + doctype.serializeContent(name, null, null) + '>';
};
'use strict';
/**
* @typedef {Object} TreeAdapter
*/
var DOCUMENT_MODE = require('../common/html').DOCUMENT_MODE;
//Node construction
/**
* Creates a document node.
*
* @function createDocument
* @memberof TreeAdapter
*
* @returns {ASTNode<Document>} document
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L19|default implementation.}
*/
exports.createDocument = function () {
return {
nodeName: '#document',
quirksMode: false,
mode: DOCUMENT_MODE.NO_QUIRKS,
childNodes: []

@@ -27,16 +14,5 @@ };

/**
* Creates a document fragment node.
*
* @function createDocumentFragment
* @memberof TreeAdapter
*
* @returns {ASTNode<DocumentFragment>} fragment
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L37|default implementation.}
*/
exports.createDocumentFragment = function () {
return {
nodeName: '#document-fragment',
quirksMode: false,
childNodes: []

@@ -46,18 +22,2 @@ };

/**
* Creates an element node.
*
* @function createElement
* @memberof TreeAdapter
*
* @param {String} tagName - Tag name of the element.
* @param {String} namespaceURI - Namespace of the element.
* @param {Array} attrs - Attribute name-value pair array.
* Foreign attributes may contain `namespace` and `prefix` fields as well.
*
* @returns {ASTNode<Element>} element
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L61|default implementation.}
*/
exports.createElement = function (tagName, namespaceURI, attrs) {

@@ -74,15 +34,2 @@ return {

/**
* Creates a comment node.
*
* @function createCommentNode
* @memberof TreeAdapter
*
* @param {String} data - Comment text.
*
* @returns {ASTNode<CommentNode>} comment
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L85|default implementation.}
*/
exports.createCommentNode = function (data) {

@@ -106,13 +53,2 @@ return {

//Tree mutation
/**
* Appends a child node to the given parent node.
*
* @function appendChild
* @memberof TreeAdapter
*
* @param {ASTNode} parentNode - Parent node.
* @param {ASTNode} newNode - Child node.
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L114|default implementation.}
*/
var appendChild = exports.appendChild = function (parentNode, newNode) {

@@ -123,14 +59,2 @@ parentNode.childNodes.push(newNode);

/**
* Inserts a child node to the given parent node before the given reference node.
*
* @function insertBefore
* @memberof TreeAdapter
*
* @param {ASTNode} parentNode - Parent node.
* @param {ASTNode} newNode - Child node.
* @param {ASTNode} referenceNode - Reference node.
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L131|default implementation.}
*/
var insertBefore = exports.insertBefore = function (parentNode, newNode, referenceNode) {

@@ -143,13 +67,2 @@ var insertionIdx = parentNode.childNodes.indexOf(referenceNode);

/**
* Sets the `<template>` element content element.
*
* @function setTemplateContent
* @memberof TreeAdapter
*
* @param {ASTNode<TemplateElement>} templateElement - `<template>` element.
* @param {ASTNode<DocumentFragment>} contentTemplate - Content element.
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L149|default implementation.}
*/
exports.setTemplateContent = function (templateElement, contentElement) {

@@ -159,15 +72,2 @@ templateElement.content = contentElement;

/**
* Returns the `<template>` element content element.
*
* @function getTemplateContent
* @memberof TreeAdapter
*
* @param {ASTNode<TemplateElement>} templateElement - `<template>` element.
* @returns {ASTNode<DocumentFragment>}
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L166|default implementation.}
*/
exports.getTemplateContent = function (templateElement) {

@@ -177,17 +77,2 @@ return templateElement.content;

/**
* Sets the document type. If the `document` already contains a document type node, the `name`, `publicId` and `systemId`
* properties of this node will be updated with the provided values. Otherwise, creates a new document type node
* with the given properties and inserts it into the `document`.
*
* @function setDocumentType
* @memberof TreeAdapter
*
* @param {ASTNode<Document>} document - Document node.
* @param {String} name - Document type name.
* @param {String} publicId - Document type public identifier.
* @param {String} systemId - Document type system identifier.
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L185|default implementation.}
*/
exports.setDocumentType = function (document, name, publicId, systemId) {

@@ -219,42 +104,10 @@ var doctypeNode = null;

/**
* Sets the document's quirks mode flag.
*
* @function setQuirksMode
* @memberof TreeAdapter
*
* @param {ASTNode<Document>} document - Document node.
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L221|default implementation.}
*/
exports.setQuirksMode = function (document) {
document.quirksMode = true;
exports.setDocumentMode = function (document, mode) {
document.mode = mode;
};
/**
* Determines if the document's quirks mode flag is set.
*
* @function isQuirksMode
* @memberof TreeAdapter
*
* @param {ASTNode<Document>} document - Document node.
* @returns {Boolean}
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L237|default implementation.}
*/
exports.isQuirksMode = function (document) {
return document.quirksMode;
exports.getDocumentMode = function (document) {
return document.mode;
};
/**
* Removes a node from its parent.
*
* @function detachNode
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L251|default implementation.}
*/
exports.detachNode = function (node) {

@@ -269,15 +122,2 @@ if (node.parentNode) {

/**
* Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the
* text node content. Otherwise, inserts a new text node with the given text.
*
*
* @function insertText
* @memberof TreeAdapter
*
* @param {ASTNode} parentNode - Node to insert text into.
* @param {String} text - Text to insert.
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L273|default implementation.}
*/
exports.insertText = function (parentNode, text) {

@@ -296,17 +136,2 @@ if (parentNode.childNodes.length) {

/**
* Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node,
* the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with
* the given text before the reference node.
*
*
* @function insertTextBefore
* @memberof TreeAdapter
*
* @param {ASTNode} parentNode - Node to insert text into.
* @param {String} text - Text to insert.
* @param {ASTNode} referenceNode - Node to insert text before.
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L301|default implementation.}
*/
exports.insertTextBefore = function (parentNode, text, referenceNode) {

@@ -321,22 +146,11 @@ var prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];

/**
* Copies attributes to the given node. Only attributes that are not yet present in the node are copied.
*
* @function adoptAttributes
* @memberof TreeAdapter
*
* @param {ASTNode} recipientNode - Node to copy attributes into.
* @param {Array} attrs - Attributes to copy.
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L321|default implementation.}
*/
exports.adoptAttributes = function (recipientNode, attrs) {
exports.adoptAttributes = function (recipient, attrs) {
var recipientAttrsMap = [];
for (var i = 0; i < recipientNode.attrs.length; i++)
recipientAttrsMap.push(recipientNode.attrs[i].name);
for (var i = 0; i < recipient.attrs.length; i++)
recipientAttrsMap.push(recipient.attrs[i].name);
for (var j = 0; j < attrs.length; j++) {
if (recipientAttrsMap.indexOf(attrs[j].name) === -1)
recipientNode.attrs.push(attrs[j]);
recipient.attrs.push(attrs[j]);
}

@@ -347,15 +161,2 @@ };

//Tree traversing
/**
* Returns the first child of the given node.
*
* @function getFirstChild
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {ASTNode} firstChild
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L348|default implementation.}
*/
exports.getFirstChild = function (node) {

@@ -365,14 +166,2 @@ return node.childNodes[0];

/**
* Returns the given node's children in an array.
*
* @function getChildNodes
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {Array} children
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L364|default implementation.}
*/
exports.getChildNodes = function (node) {

@@ -382,14 +171,2 @@ return node.childNodes;

/**
* Returns the given node's parent.
*
* @function getParentNode
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {ASTNode} parent
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L380|default implementation.}
*/
exports.getParentNode = function (node) {

@@ -399,33 +176,7 @@ return node.parentNode;

/**
* Returns the given node's attributes in an array, in the form of name-value pairs.
* Foreign attributes may contain `namespace` and `prefix` fields as well.
*
* @function getAttrList
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {Array} attributes
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L397|default implementation.}
*/
exports.getAttrList = function (node) {
return node.attrs;
exports.getAttrList = function (element) {
return element.attrs;
};
//Node data
/**
* Returns the given element's tag name.
*
* @function getTagName
* @memberof TreeAdapter
*
* @param {ASTNode<Element>} element - Element.
*
* @returns {String} tagName
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L415|default implementation.}
*/
exports.getTagName = function (element) {

@@ -435,14 +186,2 @@ return element.tagName;

/**
* Returns the given element's namespace.
*
* @function getNamespaceURI
* @memberof TreeAdapter
*
* @param {ASTNode<Element>} element - Element.
*
* @returns {String} namespaceURI
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L431|default implementation.}
*/
exports.getNamespaceURI = function (element) {

@@ -452,14 +191,2 @@ return element.namespaceURI;

/**
* Returns the given text node's content.
*
* @function getTextNodeContent
* @memberof TreeAdapter
*
* @param {ASTNode<Text>} textNode - Text node.
*
* @returns {String} text
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L447|default implementation.}
*/
exports.getTextNodeContent = function (textNode) {

@@ -469,14 +196,2 @@ return textNode.value;

/**
* Returns the given comment node's content.
*
* @function getCommentNodeContent
* @memberof TreeAdapter
*
* @param {ASTNode<Comment>} commentNode - Comment node.
*
* @returns {String} commentText
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L463|default implementation.}
*/
exports.getCommentNodeContent = function (commentNode) {

@@ -486,14 +201,2 @@ return commentNode.data;

/**
* Returns the given document type node's name.
*
* @function getDocumentTypeNodeName
* @memberof TreeAdapter
*
* @param {ASTNode<DocumentType>} doctypeNode - Document type node.
*
* @returns {String} name
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L479|default implementation.}
*/
exports.getDocumentTypeNodeName = function (doctypeNode) {

@@ -503,14 +206,2 @@ return doctypeNode.name;

/**
* Returns the given document type node's public identifier.
*
* @function getDocumentTypeNodePublicId
* @memberof TreeAdapter
*
* @param {ASTNode<DocumentType>} doctypeNode - Document type node.
*
* @returns {String} publicId
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L495|default implementation.}
*/
exports.getDocumentTypeNodePublicId = function (doctypeNode) {

@@ -520,14 +211,2 @@ return doctypeNode.publicId;

/**
* Returns the given document type node's system identifier.
*
* @function getDocumentTypeNodeSystemId
* @memberof TreeAdapter
*
* @param {ASTNode<DocumentType>} doctypeNode - Document type node.
*
* @returns {String} systemId
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L511|default implementation.}
*/
exports.getDocumentTypeNodeSystemId = function (doctypeNode) {

@@ -538,14 +217,2 @@ return doctypeNode.systemId;

//Node types
/**
* Determines if the given node is a text node.
*
* @function isTextNode
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {Boolean}
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L526|default implementation.}
*/
exports.isTextNode = function (node) {

@@ -555,14 +222,2 @@ return node.nodeName === '#text';

/**
* Determines if the given node is a comment node.
*
* @function isCommentNode
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {Boolean}
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L544|default implementation.}
*/
exports.isCommentNode = function (node) {

@@ -572,14 +227,2 @@ return node.nodeName === '#comment';

/**
* Determines if the given node is a document type node.
*
* @function isDocumentTypeNode
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {Boolean}
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L560|default implementation.}
*/
exports.isDocumentTypeNode = function (node) {

@@ -589,16 +232,4 @@ return node.nodeName === '#documentType';

/**
* Determines if the given node is an element.
*
* @function isElementNode
* @memberof TreeAdapter
*
* @param {ASTNode} node - Node.
*
* @returns {Boolean}
*
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L576|default implementation.}
*/
exports.isElementNode = function (node) {
return !!node.tagName;
};
'use strict';
var doctype = require('../common/doctype');
var doctype = require('../common/doctype'),
DOCUMENT_MODE = require('../common/html').DOCUMENT_MODE;
//Conversion tables for DOM Level1 structure emulation

@@ -64,18 +66,29 @@ var nodeTypes = {

//Node construction
exports.createDocument =
exports.createDocumentFragment = function () {
return new Node({
type: 'root',
name: 'root',
parent: null,
prev: null,
next: null,
children: []
});
};
exports.createDocument = function () {
return new Node({
type: 'root',
name: 'root',
parent: null,
prev: null,
next: null,
children: [],
'x-mode': DOCUMENT_MODE.NO_QUIRKS
});
};
exports.createDocumentFragment = function () {
return new Node({
type: 'root',
name: 'root',
parent: null,
prev: null,
next: null,
children: []
});
};
exports.createElement = function (tagName, namespaceURI, attrs) {
var attribs = {},
attribsNamespace = {},
attribsPrefix = {};
var attribs = Object.create(null),
attribsNamespace = Object.create(null),
attribsPrefix = Object.create(null);

@@ -193,8 +206,8 @@ for (var i = 0; i < attrs.length; i++) {

exports.setQuirksMode = function (document) {
document.quirksMode = true;
exports.setDocumentMode = function (document, mode) {
document['x-mode'] = mode;
};
exports.isQuirksMode = function (document) {
return document.quirksMode;
exports.getDocumentMode = function (document) {
return document['x-mode'];
};

@@ -240,10 +253,10 @@

exports.adoptAttributes = function (recipientNode, attrs) {
exports.adoptAttributes = function (recipient, attrs) {
for (var i = 0; i < attrs.length; i++) {
var attrName = attrs[i].name;
if (typeof recipientNode.attribs[attrName] === 'undefined') {
recipientNode.attribs[attrName] = attrs[i].value;
recipientNode['x-attribsNamespace'][attrName] = attrs[i].namespace;
recipientNode['x-attribsPrefix'][attrName] = attrs[i].prefix;
if (typeof recipient.attribs[attrName] === 'undefined') {
recipient.attribs[attrName] = attrs[i].value;
recipient['x-attribsNamespace'][attrName] = attrs[i].namespace;
recipient['x-attribsPrefix'][attrName] = attrs[i].prefix;
}

@@ -267,14 +280,12 @@ }

exports.getAttrList = function (node) {
exports.getAttrList = function (element) {
var attrList = [];
for (var name in node.attribs) {
if (node.attribs.hasOwnProperty(name)) {
attrList.push({
name: name,
value: node.attribs[name],
namespace: node['x-attribsNamespace'][name],
prefix: node['x-attribsPrefix'][name]
});
}
for (var name in element.attribs) {
attrList.push({
name: name,
value: element.attribs[name],
namespace: element['x-attribsNamespace'][name],
prefix: element['x-attribsPrefix'][name]
});
}

@@ -281,0 +292,0 @@

{
"name": "parse5",
"description": "WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.js",
"version": "2.2.3",
"description": "HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.",
"version": "3.0.0",
"author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
"contributors": [
"Alan Clarke (https://github.com/alanclarke)",
"Evan You (http://evanyou.me)",
"Saksham Aggarwal <s.agg2021@gmail.com>",
"Sebastian Mayr <sebmaster16@gmail.com> (http://blog.smayr.name)",
"Sean Lang <slang800@gmail.com> (http://slang.cx)"
],
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",

@@ -18,12 +12,13 @@ "devDependencies": {

"gulp-benchmark": "^1.1.1",
"gulp-concat": "^2.6.0",
"gulp-download": "0.0.1",
"gulp-eslint": "^3.0.1",
"gulp-insert": "^0.5.0",
"gulp-install": "^0.6.0",
"gulp-jsdoc-to-markdown": "^1.1.1",
"gulp-mocha": "^2.1.3",
"gulp-rename": "^1.2.2",
"gulp-typedoc": "^2.0.0",
"gulp-typescript": "^3.1.2",
"publish-please": "^2.2.0",
"through2": "^2.0.0"
"through2": "^2.0.0",
"typedoc": "^0.5.1",
"typescript": "^2.0.6"
},

@@ -53,2 +48,3 @@ "keywords": [

"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"repository": {

@@ -65,3 +61,6 @@ "type": "git",

"lib"
]
],
"dependencies": {
"@types/node": "^6.0.46"
}
}
<p align="center">
<a href="https://github.com/inikulin/parse5">
<img src="https://raw.github.com/inikulin/parse5/master/docs/logo.png" alt="parse5" />
<img src="https://raw.github.com/inikulin/parse5/master/media/logo.png" alt="parse5" />
</a>

@@ -8,3 +8,3 @@ </p>

<p align="center">
<i>WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.js</i>
<i>HTML parsing/serialization toolset for Node.js. <a href="https://html.spec.whatwg.org/multipage/">WHATWG HTML Living Standard (aka HTML5)</a>-compliant.</i>
</p>

@@ -28,7 +28,7 @@

<p align="center">
<a href="https://github.com/inikulin/parse5/wiki/Documentation">Documentation</a>
<a href="http://inikulin.github.io/parse5">Documentation</a>
</p>
<p align="center">
<a href="https://github.com/inikulin/parse5/wiki/Documentation#version-history">Version history</a>
<a href="http://inikulin.github.io/parse5#version-history">Version history</a>
</p>

@@ -35,0 +35,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc