Skip to content

Commit

Permalink
Support latest htmlparser2 tree format, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed Oct 18, 2014
1 parent 7c76d92 commit 5fb83c8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
77 changes: 66 additions & 11 deletions lib/tree_adapters/htmlparser2.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand All @@ -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
}
});
};


Expand Down Expand Up @@ -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
});
}));
}

};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (https://github.com/inikulin)",
"contributors": [
"Sebastian Mayr <[email protected]> (http://blog.smayr.name)",
Expand Down

0 comments on commit 5fb83c8

Please sign in to comment.