From 7c76d920aa6dfca9a62509d490be27fcea2a0a46 Mon Sep 17 00:00:00 2001 From: inikulin Date: Sat, 18 Oct 2014 15:30:21 +0400 Subject: [PATCH] Add element tagName property for htmlparser2 adapter. Bump version --- lib/tree_adapters/htmlparser2.js | 539 ++++++++++++++++--------------- package.json | 4 +- 2 files changed, 272 insertions(+), 271 deletions(-) diff --git a/lib/tree_adapters/htmlparser2.js b/lib/tree_adapters/htmlparser2.js index bcf4947c..c559797c 100644 --- a/lib/tree_adapters/htmlparser2.js +++ b/lib/tree_adapters/htmlparser2.js @@ -1,269 +1,270 @@ -'use strict'; - -//Node construction -exports.createDocument = -exports.createDocumentFragment = function () { - return { - type: 'root', - name: 'root', - parent: null, - prev: null, - next: null, - children: [] - }; -}; - -exports.createElement = function (tagName, namespaceURI, attrs) { - var attribs = {}, - attribsNamespace = {}, - attribsPrefix = {}; - - for (var i = 0; i < attrs.length; i++) { - var attrName = attrs[i].name; - - attribs[attrName] = attrs[i].value; - attribsNamespace[attrName] = attrs[i].namespace; - attribsPrefix[attrName] = attrs[i].prefix; - } - - return { - type: tagName === 'script' || tagName === 'style' ? tagName : 'tag', - name: tagName, - namespace: namespaceURI, - attribs: attribs, - 'x-attribsNamespace': attribsNamespace, - 'x-attribsPrefix': attribsPrefix, - children: [], - parent: null, - prev: null, - next: null - }; -}; - -exports.createCommentNode = function (data) { - return { - type: 'comment', - data: data, - parent: null, - prev: null, - next: null - }; -}; - -var createTextNode = function (value) { - return { - type: 'text', - data: value, - parent: null, - prev: null, - next: null - } -}; - - -//Tree mutation -exports.setDocumentType = function (document, name, publicId, systemId) { - var data = '!DOCTYPE'; - - if (name) - data += ' ' + name; - - if (publicId) - data += ' PUBLIC "' + publicId + '"'; - - if (systemId) - data += ' "' + systemId + '"'; - - var doctypeNode = null; - - for (var i = 0; i < document.children.length; i++) { - if (document.children[i].type === 'directive' && document.children[i].name === '!doctype') { - doctypeNode = document.children[i]; - break; - } - } - - if (doctypeNode) { - doctypeNode.data = data; - doctypeNode['x-name'] = name; - doctypeNode['x-publicId'] = publicId; - doctypeNode['x-systemId'] = systemId; - } - - else { - appendChild(document, { - type: 'directive', - name: '!doctype', - data: data, - 'x-name': name, - 'x-publicId': publicId, - 'x-systemId': systemId - }); - } - -}; - -exports.setQuirksMode = function (document) { - document.quirksMode = true; -}; - -exports.isQuirksMode = function (document) { - return document.quirksMode; -}; - -var appendChild = exports.appendChild = function (parentNode, newNode) { - var prev = parentNode.children[parentNode.children.length - 1]; - - if (prev) { - prev.next = newNode; - newNode.prev = prev; - } - - parentNode.children.push(newNode); - newNode.parent = parentNode; -}; - -var insertBefore = exports.insertBefore = function (parentNode, newNode, referenceNode) { - var insertionIdx = parentNode.children.indexOf(referenceNode), - prev = referenceNode.prev; - - if (prev) { - prev.next = newNode; - newNode.prev = prev; - } - - referenceNode.prev = newNode; - newNode.next = referenceNode; - - parentNode.children.splice(insertionIdx, 0, newNode); - newNode.parent = parentNode; -}; - -exports.detachNode = function (node) { - if (node.parent) { - var idx = node.parent.children.indexOf(node), - prev = node.prev, - next = node.next; - - node.prev = null; - node.next = null; - - if (prev) - prev.next = next; - - if (next) - next.prev = prev; - - node.parent.children.splice(idx, 1); - node.parent = null; - } -}; - -exports.insertText = function (parentNode, text) { - var lastChild = parentNode.children[parentNode.children.length - 1]; - - if (lastChild && lastChild.type === 'text') - lastChild.data += text; - else - appendChild(parentNode, createTextNode(text)); -}; - -exports.insertTextBefore = function (parentNode, text, referenceNode) { - var prevNode = parentNode.children[parentNode.children.indexOf(referenceNode) - 1]; - - if (prevNode && prevNode.type === 'text') - prevNode.data += text; - else - insertBefore(parentNode, createTextNode(text), referenceNode); -}; - -exports.adoptAttributes = function (recipientNode, 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; - } - } -}; - - -//Tree traversing -exports.getFirstChild = function (node) { - return node.children[0]; -}; - -exports.getChildNodes = function (node) { - return node.children; -}; - -exports.getParentNode = function (node) { - return node.parent; -}; - -exports.getAttrList = function (node) { - 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] - }); - } - } - - return attrList; -}; - - -//Node data -exports.getTagName = function (element) { - return element.name; -}; - -exports.getNamespaceURI = function (element) { - return element.namespace; -}; - -exports.getTextNodeContent = function (textNode) { - return textNode.data; -}; - -exports.getCommentNodeContent = function (commentNode) { - return commentNode.data; -}; - -exports.getDocumentTypeNodeName = function (doctypeNode) { - return doctypeNode['x-name']; -}; - -exports.getDocumentTypeNodePublicId = function (doctypeNode) { - return doctypeNode['x-publicId']; -}; - -exports.getDocumentTypeNodeSystemId = function (doctypeNode) { - return doctypeNode['x-systemId']; -}; - - -//Node types -exports.isTextNode = function (node) { - return node.type === 'text'; -}; - -exports.isCommentNode = function (node) { - return node.type === 'comment'; -}; - -exports.isDocumentTypeNode = function (node) { - return node.type === 'directive' && node.name === '!doctype'; -}; - -exports.isElementNode = function (node) { - return !!node.attribs; -}; +'use strict'; + +//Node construction +exports.createDocument = +exports.createDocumentFragment = function () { + return { + type: 'root', + name: 'root', + parent: null, + prev: null, + next: null, + children: [] + }; +}; + +exports.createElement = function (tagName, namespaceURI, attrs) { + var attribs = {}, + attribsNamespace = {}, + attribsPrefix = {}; + + for (var i = 0; i < attrs.length; i++) { + var attrName = attrs[i].name; + + attribs[attrName] = attrs[i].value; + attribsNamespace[attrName] = attrs[i].namespace; + attribsPrefix[attrName] = attrs[i].prefix; + } + + return { + type: tagName === 'script' || tagName === 'style' ? tagName : 'tag', + name: tagName, + tagName: tagName, + namespace: namespaceURI, + attribs: attribs, + 'x-attribsNamespace': attribsNamespace, + 'x-attribsPrefix': attribsPrefix, + children: [], + parent: null, + prev: null, + next: null + }; +}; + +exports.createCommentNode = function (data) { + return { + type: 'comment', + data: data, + parent: null, + prev: null, + next: null + }; +}; + +var createTextNode = function (value) { + return { + type: 'text', + data: value, + parent: null, + prev: null, + next: null + } +}; + + +//Tree mutation +exports.setDocumentType = function (document, name, publicId, systemId) { + var data = '!DOCTYPE'; + + if (name) + data += ' ' + name; + + if (publicId) + data += ' PUBLIC "' + publicId + '"'; + + if (systemId) + data += ' "' + systemId + '"'; + + var doctypeNode = null; + + for (var i = 0; i < document.children.length; i++) { + if (document.children[i].type === 'directive' && document.children[i].name === '!doctype') { + doctypeNode = document.children[i]; + break; + } + } + + if (doctypeNode) { + doctypeNode.data = data; + doctypeNode['x-name'] = name; + doctypeNode['x-publicId'] = publicId; + doctypeNode['x-systemId'] = systemId; + } + + else { + appendChild(document, { + type: 'directive', + name: '!doctype', + data: data, + 'x-name': name, + 'x-publicId': publicId, + 'x-systemId': systemId + }); + } + +}; + +exports.setQuirksMode = function (document) { + document.quirksMode = true; +}; + +exports.isQuirksMode = function (document) { + return document.quirksMode; +}; + +var appendChild = exports.appendChild = function (parentNode, newNode) { + var prev = parentNode.children[parentNode.children.length - 1]; + + if (prev) { + prev.next = newNode; + newNode.prev = prev; + } + + parentNode.children.push(newNode); + newNode.parent = parentNode; +}; + +var insertBefore = exports.insertBefore = function (parentNode, newNode, referenceNode) { + var insertionIdx = parentNode.children.indexOf(referenceNode), + prev = referenceNode.prev; + + if (prev) { + prev.next = newNode; + newNode.prev = prev; + } + + referenceNode.prev = newNode; + newNode.next = referenceNode; + + parentNode.children.splice(insertionIdx, 0, newNode); + newNode.parent = parentNode; +}; + +exports.detachNode = function (node) { + if (node.parent) { + var idx = node.parent.children.indexOf(node), + prev = node.prev, + next = node.next; + + node.prev = null; + node.next = null; + + if (prev) + prev.next = next; + + if (next) + next.prev = prev; + + node.parent.children.splice(idx, 1); + node.parent = null; + } +}; + +exports.insertText = function (parentNode, text) { + var lastChild = parentNode.children[parentNode.children.length - 1]; + + if (lastChild && lastChild.type === 'text') + lastChild.data += text; + else + appendChild(parentNode, createTextNode(text)); +}; + +exports.insertTextBefore = function (parentNode, text, referenceNode) { + var prevNode = parentNode.children[parentNode.children.indexOf(referenceNode) - 1]; + + if (prevNode && prevNode.type === 'text') + prevNode.data += text; + else + insertBefore(parentNode, createTextNode(text), referenceNode); +}; + +exports.adoptAttributes = function (recipientNode, 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; + } + } +}; + + +//Tree traversing +exports.getFirstChild = function (node) { + return node.children[0]; +}; + +exports.getChildNodes = function (node) { + return node.children; +}; + +exports.getParentNode = function (node) { + return node.parent; +}; + +exports.getAttrList = function (node) { + 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] + }); + } + } + + return attrList; +}; + + +//Node data +exports.getTagName = function (element) { + return element.name; +}; + +exports.getNamespaceURI = function (element) { + return element.namespace; +}; + +exports.getTextNodeContent = function (textNode) { + return textNode.data; +}; + +exports.getCommentNodeContent = function (commentNode) { + return commentNode.data; +}; + +exports.getDocumentTypeNodeName = function (doctypeNode) { + return doctypeNode['x-name']; +}; + +exports.getDocumentTypeNodePublicId = function (doctypeNode) { + return doctypeNode['x-publicId']; +}; + +exports.getDocumentTypeNodeSystemId = function (doctypeNode) { + return doctypeNode['x-systemId']; +}; + + +//Node types +exports.isTextNode = function (node) { + return node.type === 'text'; +}; + +exports.isCommentNode = function (node) { + return node.type === 'comment'; +}; + +exports.isDocumentTypeNode = function (node) { + return node.type === 'directive' && node.name === '!doctype'; +}; + +exports.isElementNode = function (node) { + return !!node.attribs; +}; diff --git a/package.json b/package.json index 98a50a39..82a2ae21 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "parse5", "description": "WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.", - "version": "1.1.4", + "version": "1.1.5", "author": "Ivan Nikulin (https://github.com/inikulin)", "contributors": [ "Sebastian Mayr (http://blog.smayr.name)", "Sean Lang (http://slang.cx)" ], - "homepage": "http://inikulin.github.io/parse5/", + "homepage": "http://inikulin.github.io/parse5/", "devDependencies": { "mocha": "1.21.4" },