From 5fb83c8b08d8bd827984fdb766fe941bfd38e3a0 Mon Sep 17 00:00:00 2001 From: inikulin Date: Sat, 18 Oct 2014 17:16:57 +0400 Subject: [PATCH] Support latest htmlparser2 tree format, bump version --- lib/tree_adapters/htmlparser2.js | 77 +++++++++++++++++++++++++++----- package.json | 2 +- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/lib/tree_adapters/htmlparser2.js b/lib/tree_adapters/htmlparser2.js index c559797c..ff7c2de0 100644 --- a/lib/tree_adapters/htmlparser2.js +++ b/lib/tree_adapters/htmlparser2.js @@ -1,16 +1,72 @@ 'use strict'; +//Conversion tables for DOM Level1 structure emulation +var nodeTypes = { + element: 1, + text: 3, + cdata: 4, + comment: 8 +}; + +var nodePropertyShorthands = { + tagName: 'name', + childNodes: 'children', + parentNode: 'parent', + previousSibling: 'prev', + nextSibling: 'next', + nodeValue: 'data' +}; + +//Node +var Node = function (props) { + for (var key in props) { + if (props.hasOwnProperty(key)) + this[key] = props[key]; + } +}; + +Node.prototype = { + get firstChild() { + var children = this.children; + return children && children[0] || null; + }, + + get lastChild() { + var children = this.children; + return children && children[children.length - 1] || null; + }, + + get nodeType() { + return nodeTypes[this.type] || nodeTypes.element; + } +}; + +Object.keys(nodePropertyShorthands).forEach(function (key) { + var shorthand = nodePropertyShorthands[key]; + + Object.defineProperty(Node.prototype, key, { + get: function () { + return this[shorthand] || null; + }, + set: function (val) { + this[shorthand] = val; + return val; + } + }); +}); + + //Node construction exports.createDocument = exports.createDocumentFragment = function () { - return { + return new Node({ type: 'root', name: 'root', parent: null, prev: null, next: null, children: [] - }; + }); }; exports.createElement = function (tagName, namespaceURI, attrs) { @@ -26,10 +82,9 @@ exports.createElement = function (tagName, namespaceURI, attrs) { attribsPrefix[attrName] = attrs[i].prefix; } - return { + return new Node({ type: tagName === 'script' || tagName === 'style' ? tagName : 'tag', name: tagName, - tagName: tagName, namespace: namespaceURI, attribs: attribs, 'x-attribsNamespace': attribsNamespace, @@ -38,27 +93,27 @@ exports.createElement = function (tagName, namespaceURI, attrs) { parent: null, prev: null, next: null - }; + }); }; exports.createCommentNode = function (data) { - return { + return new Node({ type: 'comment', data: data, parent: null, prev: null, next: null - }; + }); }; var createTextNode = function (value) { - return { + return new Node({ type: 'text', data: value, parent: null, prev: null, next: null - } + }); }; @@ -92,14 +147,14 @@ exports.setDocumentType = function (document, name, publicId, systemId) { } else { - appendChild(document, { + appendChild(document, new Node({ type: 'directive', name: '!doctype', data: data, 'x-name': name, 'x-publicId': publicId, 'x-systemId': systemId - }); + })); } }; diff --git a/package.json b/package.json index 82a2ae21..2a76da4b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "parse5", "description": "WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.", - "version": "1.1.5", + "version": "1.1.6", "author": "Ivan Nikulin (https://github.com/inikulin)", "contributors": [ "Sebastian Mayr (http://blog.smayr.name)",