simple-dom
Advanced tools
Comparing version 0.2.7 to 0.3.0
@@ -1,548 +0,529 @@ | ||
(function() { | ||
"use strict"; | ||
function simple$dom$document$node$$Node(nodeType, nodeName, nodeValue) { | ||
this.nodeType = nodeType; | ||
this.nodeName = nodeName; | ||
this.nodeValue = nodeValue; | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
factory((global.SimpleDOMTests = {})); | ||
}(this, function (exports) { 'use strict'; | ||
this.childNodes = new simple$dom$document$node$$ChildNodes(this); | ||
function Node(nodeType, nodeName, nodeValue) { | ||
this.nodeType = nodeType; | ||
this.nodeName = nodeName; | ||
this.nodeValue = nodeValue; | ||
this.parentNode = null; | ||
this.previousSibling = null; | ||
this.nextSibling = null; | ||
this.firstChild = null; | ||
this.lastChild = null; | ||
} | ||
this.childNodes = new ChildNodes(this); | ||
simple$dom$document$node$$Node.prototype._cloneNode = function() { | ||
return new simple$dom$document$node$$Node(this.nodeType, this.nodeName, this.nodeValue); | ||
}; | ||
this.parentNode = null; | ||
this.previousSibling = null; | ||
this.nextSibling = null; | ||
this.firstChild = null; | ||
this.lastChild = null; | ||
} | ||
simple$dom$document$node$$Node.prototype.cloneNode = function(deep) { | ||
var node = this._cloneNode(); | ||
Node.prototype._cloneNode = function() { | ||
return new Node(this.nodeType, this.nodeName, this.nodeValue); | ||
}; | ||
if (deep) { | ||
var child = this.firstChild, nextChild = child; | ||
Node.prototype.cloneNode = function(deep) { | ||
var node = this._cloneNode(); | ||
while (nextChild) { | ||
nextChild = child.nextSibling; | ||
node.appendChild(child.cloneNode(true)); | ||
child = nextChild; | ||
} | ||
} | ||
if (deep) { | ||
var child = this.firstChild, nextChild = child; | ||
return node; | ||
}; | ||
simple$dom$document$node$$Node.prototype.appendChild = function(node) { | ||
if (node.nodeType === simple$dom$document$node$$Node.DOCUMENT_FRAGMENT_NODE) { | ||
simple$dom$document$node$$insertFragment(node, this, this.lastChild, null); | ||
return; | ||
while (nextChild) { | ||
nextChild = child.nextSibling; | ||
node.appendChild(child.cloneNode(true)); | ||
child = nextChild; | ||
} | ||
} | ||
if (node.parentNode) { node.parentNode.removeChild(node); } | ||
return node; | ||
}; | ||
node.parentNode = this; | ||
var refNode = this.lastChild; | ||
if (refNode === null) { | ||
this.firstChild = node; | ||
this.lastChild = node; | ||
} else { | ||
node.previousSibling = refNode; | ||
refNode.nextSibling = node; | ||
this.lastChild = node; | ||
} | ||
}; | ||
Node.prototype.appendChild = function(node) { | ||
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { | ||
insertFragment(node, this, this.lastChild, null); | ||
return; | ||
} | ||
function simple$dom$document$node$$insertFragment(fragment, newParent, before, after) { | ||
if (!fragment.firstChild) { return; } | ||
if (node.parentNode) { node.parentNode.removeChild(node); } | ||
var firstChild = fragment.firstChild; | ||
var lastChild = firstChild; | ||
var node = firstChild; | ||
node.parentNode = this; | ||
var refNode = this.lastChild; | ||
if (refNode === null) { | ||
this.firstChild = node; | ||
this.lastChild = node; | ||
} else { | ||
node.previousSibling = refNode; | ||
refNode.nextSibling = node; | ||
this.lastChild = node; | ||
} | ||
}; | ||
firstChild.previousSibling = before; | ||
if (before) { | ||
before.nextSibling = firstChild; | ||
} else { | ||
newParent.firstChild = firstChild; | ||
} | ||
function insertFragment(fragment, newParent, before, after) { | ||
if (!fragment.firstChild) { return; } | ||
while (node) { | ||
node.parentNode = newParent; | ||
lastChild = node; | ||
node = node.nextSibling; | ||
} | ||
var firstChild = fragment.firstChild; | ||
var lastChild = firstChild; | ||
var node = firstChild; | ||
lastChild.nextSibling = after; | ||
if (after) { | ||
after.previousSibling = lastChild; | ||
} else { | ||
newParent.lastChild = lastChild; | ||
} | ||
firstChild.previousSibling = before; | ||
if (before) { | ||
before.nextSibling = firstChild; | ||
} else { | ||
newParent.firstChild = firstChild; | ||
} | ||
simple$dom$document$node$$Node.prototype.insertBefore = function(node, refNode) { | ||
if (refNode == null) { | ||
return this.appendChild(node); | ||
} | ||
while (node) { | ||
node.parentNode = newParent; | ||
lastChild = node; | ||
node = node.nextSibling; | ||
} | ||
if (node.nodeType === simple$dom$document$node$$Node.DOCUMENT_FRAGMENT_NODE) { | ||
simple$dom$document$node$$insertFragment(node, this, refNode ? refNode.previousSibling : null, refNode); | ||
return; | ||
} | ||
lastChild.nextSibling = after; | ||
if (after) { | ||
after.previousSibling = lastChild; | ||
} else { | ||
newParent.lastChild = lastChild; | ||
} | ||
} | ||
node.parentNode = this; | ||
Node.prototype.insertBefore = function(node, refNode) { | ||
if (refNode == null) { | ||
return this.appendChild(node); | ||
} | ||
var previousSibling = refNode.previousSibling; | ||
if (previousSibling) { | ||
previousSibling.nextSibling = node; | ||
node.previousSibling = previousSibling; | ||
}else{ | ||
node.previousSibling = null | ||
} | ||
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { | ||
insertFragment(node, this, refNode ? refNode.previousSibling : null, refNode); | ||
return; | ||
} | ||
refNode.previousSibling = node; | ||
node.nextSibling = refNode; | ||
node.parentNode = this; | ||
if (this.firstChild === refNode) { | ||
this.firstChild = node; | ||
} | ||
}; | ||
var previousSibling = refNode.previousSibling; | ||
if (previousSibling) { | ||
previousSibling.nextSibling = node; | ||
node.previousSibling = previousSibling; | ||
}else{ | ||
node.previousSibling = null | ||
} | ||
simple$dom$document$node$$Node.prototype.removeChild = function(refNode) { | ||
if (this.firstChild === refNode) { | ||
this.firstChild = refNode.nextSibling; | ||
} | ||
if (this.lastChild === refNode) { | ||
this.lastChild = refNode.previousSibling; | ||
} | ||
if (refNode.previousSibling) { | ||
refNode.previousSibling.nextSibling = refNode.nextSibling; | ||
} | ||
if (refNode.nextSibling) { | ||
refNode.nextSibling.previousSibling = refNode.previousSibling; | ||
} | ||
refNode.parentNode = null; | ||
refNode.nextSibling = null; | ||
refNode.previousSibling = null; | ||
}; | ||
refNode.previousSibling = node; | ||
node.nextSibling = refNode; | ||
simple$dom$document$node$$Node.ELEMENT_NODE = 1; | ||
simple$dom$document$node$$Node.ATTRIBUTE_NODE = 2; | ||
simple$dom$document$node$$Node.TEXT_NODE = 3; | ||
simple$dom$document$node$$Node.CDATA_SECTION_NODE = 4; | ||
simple$dom$document$node$$Node.ENTITY_REFERENCE_NODE = 5; | ||
simple$dom$document$node$$Node.ENTITY_NODE = 6; | ||
simple$dom$document$node$$Node.PROCESSING_INSTRUCTION_NODE = 7; | ||
simple$dom$document$node$$Node.COMMENT_NODE = 8; | ||
simple$dom$document$node$$Node.DOCUMENT_NODE = 9; | ||
simple$dom$document$node$$Node.DOCUMENT_TYPE_NODE = 10; | ||
simple$dom$document$node$$Node.DOCUMENT_FRAGMENT_NODE = 11; | ||
simple$dom$document$node$$Node.NOTATION_NODE = 12; | ||
if (this.firstChild === refNode) { | ||
this.firstChild = node; | ||
} | ||
}; | ||
function simple$dom$document$node$$ChildNodes(node) { | ||
this.node = node; | ||
Node.prototype.removeChild = function(refNode) { | ||
if (this.firstChild === refNode) { | ||
this.firstChild = refNode.nextSibling; | ||
} | ||
if (this.lastChild === refNode) { | ||
this.lastChild = refNode.previousSibling; | ||
} | ||
if (refNode.previousSibling) { | ||
refNode.previousSibling.nextSibling = refNode.nextSibling; | ||
} | ||
if (refNode.nextSibling) { | ||
refNode.nextSibling.previousSibling = refNode.previousSibling; | ||
} | ||
refNode.parentNode = null; | ||
refNode.nextSibling = null; | ||
refNode.previousSibling = null; | ||
}; | ||
simple$dom$document$node$$ChildNodes.prototype.item = function(index) { | ||
var child = this.node.firstChild; | ||
Node.ELEMENT_NODE = 1; | ||
Node.ATTRIBUTE_NODE = 2; | ||
Node.TEXT_NODE = 3; | ||
Node.CDATA_SECTION_NODE = 4; | ||
Node.ENTITY_REFERENCE_NODE = 5; | ||
Node.ENTITY_NODE = 6; | ||
Node.PROCESSING_INSTRUCTION_NODE = 7; | ||
Node.COMMENT_NODE = 8; | ||
Node.DOCUMENT_NODE = 9; | ||
Node.DOCUMENT_TYPE_NODE = 10; | ||
Node.DOCUMENT_FRAGMENT_NODE = 11; | ||
Node.NOTATION_NODE = 12; | ||
for (var i = 0; child && index !== i; i++) { | ||
child = child.nextSibling; | ||
} | ||
function ChildNodes(node) { | ||
this.node = node; | ||
} | ||
return child; | ||
}; | ||
ChildNodes.prototype.item = function(index) { | ||
var child = this.node.firstChild; | ||
var simple$dom$document$node$$default = simple$dom$document$node$$Node; | ||
for (var i = 0; child && index !== i; i++) { | ||
child = child.nextSibling; | ||
} | ||
function simple$dom$document$element$$Element(tagName) { | ||
tagName = tagName.toUpperCase(); | ||
return child; | ||
}; | ||
this.nodeConstructor(1, tagName, null); | ||
this.attributes = []; | ||
this.tagName = tagName; | ||
} | ||
function Element(tagName) { | ||
tagName = tagName.toUpperCase(); | ||
simple$dom$document$element$$Element.prototype = Object.create(simple$dom$document$node$$default.prototype); | ||
simple$dom$document$element$$Element.prototype.constructor = simple$dom$document$element$$Element; | ||
simple$dom$document$element$$Element.prototype.nodeConstructor = simple$dom$document$node$$default; | ||
this.nodeConstructor(1, tagName, null); | ||
this.attributes = []; | ||
this.tagName = tagName; | ||
} | ||
simple$dom$document$element$$Element.prototype._cloneNode = function() { | ||
var node = new simple$dom$document$element$$Element(this.tagName); | ||
Element.prototype = Object.create(Node.prototype); | ||
Element.prototype.constructor = Element; | ||
Element.prototype.nodeConstructor = Node; | ||
node.attributes = this.attributes.map(function(attr) { | ||
return { name: attr.name, value: attr.value, specified: attr.specified }; | ||
}); | ||
Element.prototype._cloneNode = function() { | ||
var node = new Element(this.tagName); | ||
return node; | ||
}; | ||
node.attributes = this.attributes.map(function(attr) { | ||
return { name: attr.name, value: attr.value, specified: attr.specified }; | ||
}); | ||
simple$dom$document$element$$Element.prototype.getAttribute = function(_name) { | ||
var attributes = this.attributes; | ||
var name = _name.toLowerCase(); | ||
var attr; | ||
for (var i=0, l=attributes.length; i<l; i++) { | ||
attr = attributes[i]; | ||
if (attr.name === name) { | ||
return attr.value; | ||
} | ||
return node; | ||
}; | ||
Element.prototype.getAttribute = function(_name) { | ||
var attributes = this.attributes; | ||
var name = _name.toLowerCase(); | ||
var attr; | ||
for (var i=0, l=attributes.length; i<l; i++) { | ||
attr = attributes[i]; | ||
if (attr.name === name) { | ||
return attr.value; | ||
} | ||
return ''; | ||
}; | ||
} | ||
return ''; | ||
}; | ||
simple$dom$document$element$$Element.prototype.setAttribute = function(_name, _value) { | ||
var attributes = this.attributes; | ||
var name = _name.toLowerCase(); | ||
var value; | ||
if (typeof _value === 'string') { | ||
value = _value; | ||
} else { | ||
value = '' + _value; | ||
Element.prototype.setAttribute = function(_name, _value) { | ||
var attributes = this.attributes; | ||
var name = _name.toLowerCase(); | ||
var value; | ||
if (typeof _value === 'string') { | ||
value = _value; | ||
} else { | ||
value = '' + _value; | ||
} | ||
var attr; | ||
for (var i=0, l=attributes.length; i<l; i++) { | ||
attr = attributes[i]; | ||
if (attr.name === name) { | ||
attr.value = value; | ||
return; | ||
} | ||
var attr; | ||
for (var i=0, l=attributes.length; i<l; i++) { | ||
attr = attributes[i]; | ||
if (attr.name === name) { | ||
attr.value = value; | ||
return; | ||
} | ||
} | ||
attributes.push({ | ||
name: name, | ||
value: value, | ||
specified: true // serializer compat with old IE | ||
}); | ||
}; | ||
} | ||
attributes.push({ | ||
name: name, | ||
value: value, | ||
specified: true // serializer compat with old IE | ||
}); | ||
}; | ||
simple$dom$document$element$$Element.prototype.removeAttribute = function(name) { | ||
var attributes = this.attributes; | ||
for (var i=0, l=attributes.length; i<l; i++) { | ||
var attr = attributes[i]; | ||
if (attr.name === name) { | ||
attributes.splice(i, 1); | ||
return; | ||
} | ||
Element.prototype.removeAttribute = function(name) { | ||
var attributes = this.attributes; | ||
for (var i=0, l=attributes.length; i<l; i++) { | ||
var attr = attributes[i]; | ||
if (attr.name === name) { | ||
attributes.splice(i, 1); | ||
return; | ||
} | ||
}; | ||
var simple$dom$document$element$$default = simple$dom$document$element$$Element; | ||
function simple$dom$document$document$fragment$$DocumentFragment() { | ||
this.nodeConstructor(11, '#document-fragment', null); | ||
} | ||
}; | ||
simple$dom$document$document$fragment$$DocumentFragment.prototype._cloneNode = function() { | ||
return new simple$dom$document$document$fragment$$DocumentFragment(); | ||
}; | ||
function DocumentFragment() { | ||
this.nodeConstructor(11, '#document-fragment', null); | ||
} | ||
simple$dom$document$document$fragment$$DocumentFragment.prototype = Object.create(simple$dom$document$node$$default.prototype); | ||
simple$dom$document$document$fragment$$DocumentFragment.prototype.constructor = simple$dom$document$document$fragment$$DocumentFragment; | ||
simple$dom$document$document$fragment$$DocumentFragment.prototype.nodeConstructor = simple$dom$document$node$$default; | ||
DocumentFragment.prototype._cloneNode = function() { | ||
return new DocumentFragment(); | ||
}; | ||
var simple$dom$document$document$fragment$$default = simple$dom$document$document$fragment$$DocumentFragment; | ||
DocumentFragment.prototype = Object.create(Node.prototype); | ||
DocumentFragment.prototype.constructor = DocumentFragment; | ||
DocumentFragment.prototype.nodeConstructor = Node; | ||
function $$document$text$$Text(text) { | ||
this.nodeConstructor(3, '#text', text); | ||
} | ||
function Text(text) { | ||
this.nodeConstructor(3, '#text', text); | ||
} | ||
$$document$text$$Text.prototype._cloneNode = function() { | ||
return new $$document$text$$Text(this.nodeValue); | ||
}; | ||
Text.prototype._cloneNode = function() { | ||
return new Text(this.nodeValue); | ||
}; | ||
$$document$text$$Text.prototype = Object.create(simple$dom$document$node$$default.prototype); | ||
$$document$text$$Text.prototype.constructor = $$document$text$$Text; | ||
$$document$text$$Text.prototype.nodeConstructor = simple$dom$document$node$$default; | ||
Text.prototype = Object.create(Node.prototype); | ||
Text.prototype.constructor = Text; | ||
Text.prototype.nodeConstructor = Node; | ||
var $$document$text$$default = $$document$text$$Text; | ||
function Comment(text) { | ||
this.nodeConstructor(8, '#comment', text); | ||
} | ||
function $$document$comment$$Comment(text) { | ||
this.nodeConstructor(8, '#comment', text); | ||
} | ||
Comment.prototype._cloneNode = function() { | ||
return new Comment(this.nodeValue); | ||
}; | ||
$$document$comment$$Comment.prototype._cloneNode = function() { | ||
return new $$document$comment$$Comment(this.nodeValue); | ||
}; | ||
Comment.prototype = Object.create(Node.prototype); | ||
Comment.prototype.constructor = Comment; | ||
Comment.prototype.nodeConstructor = Node; | ||
$$document$comment$$Comment.prototype = Object.create(simple$dom$document$node$$default.prototype); | ||
$$document$comment$$Comment.prototype.constructor = $$document$comment$$Comment; | ||
$$document$comment$$Comment.prototype.nodeConstructor = simple$dom$document$node$$default; | ||
function RawHTMLSection(text) { | ||
this.nodeConstructor(-1, "#raw-html-section", text); | ||
} | ||
var $$document$comment$$default = $$document$comment$$Comment; | ||
RawHTMLSection.prototype = Object.create(Node.prototype); | ||
RawHTMLSection.prototype.constructor = RawHTMLSection; | ||
RawHTMLSection.prototype.nodeConstructor = Node; | ||
function $$document$raw$html$section$$RawHTMLSection(text) { | ||
this.nodeConstructor(-1, "#raw-html-section", text); | ||
} | ||
function Document() { | ||
this.nodeConstructor(9, '#document', null); | ||
this.documentElement = new Element('html'); | ||
this.head = new Element('head'); | ||
this.body = new Element('body'); | ||
this.documentElement.appendChild(this.head); | ||
this.documentElement.appendChild(this.body); | ||
this.appendChild(this.documentElement); | ||
} | ||
$$document$raw$html$section$$RawHTMLSection.prototype = Object.create(simple$dom$document$node$$default.prototype); | ||
$$document$raw$html$section$$RawHTMLSection.prototype.constructor = $$document$raw$html$section$$RawHTMLSection; | ||
$$document$raw$html$section$$RawHTMLSection.prototype.nodeConstructor = simple$dom$document$node$$default; | ||
Document.prototype = Object.create(Node.prototype); | ||
Document.prototype.constructor = Document; | ||
Document.prototype.nodeConstructor = Node; | ||
var $$document$raw$html$section$$default = $$document$raw$html$section$$RawHTMLSection; | ||
Document.prototype.createElement = function(tagName) { | ||
return new Element(tagName); | ||
}; | ||
function simple$dom$document$$Document() { | ||
this.nodeConstructor(9, '#document', null); | ||
this.documentElement = new simple$dom$document$element$$default('html'); | ||
this.head = new simple$dom$document$element$$default('head'); | ||
this.body = new simple$dom$document$element$$default('body'); | ||
this.documentElement.appendChild(this.head); | ||
this.documentElement.appendChild(this.body); | ||
this.appendChild(this.documentElement); | ||
} | ||
Document.prototype.createTextNode = function(text) { | ||
return new Text(text); | ||
}; | ||
simple$dom$document$$Document.prototype = Object.create(simple$dom$document$node$$default.prototype); | ||
simple$dom$document$$Document.prototype.constructor = simple$dom$document$$Document; | ||
simple$dom$document$$Document.prototype.nodeConstructor = simple$dom$document$node$$default; | ||
Document.prototype.createComment = function(text) { | ||
return new Comment(text); | ||
}; | ||
simple$dom$document$$Document.prototype.createElement = function(tagName) { | ||
return new simple$dom$document$element$$default(tagName); | ||
}; | ||
Document.prototype.createRawHTMLSection = function(text) { | ||
return new RawHTMLSection(text); | ||
}; | ||
simple$dom$document$$Document.prototype.createTextNode = function(text) { | ||
return new $$document$text$$default(text); | ||
}; | ||
Document.prototype.createDocumentFragment = function() { | ||
return new DocumentFragment(); | ||
}; | ||
simple$dom$document$$Document.prototype.createComment = function(text) { | ||
return new $$document$comment$$default(text); | ||
}; | ||
function HTMLParser(tokenize, document, voidMap) { | ||
this.tokenize = tokenize; | ||
this.document = document; | ||
this.voidMap = voidMap; | ||
this.parentStack = []; | ||
} | ||
simple$dom$document$$Document.prototype.createRawHTMLSection = function(text) { | ||
return new $$document$raw$html$section$$default(text); | ||
}; | ||
HTMLParser.prototype.isVoid = function(element) { | ||
return this.voidMap[element.nodeName] === true; | ||
}; | ||
simple$dom$document$$Document.prototype.createDocumentFragment = function() { | ||
return new simple$dom$document$document$fragment$$default(); | ||
}; | ||
HTMLParser.prototype.pushElement = function(token) { | ||
var el = this.document.createElement(token.tagName); | ||
var simple$dom$document$$default = simple$dom$document$$Document; | ||
function simple$dom$html$parser$$HTMLParser(tokenize, document, voidMap) { | ||
this.tokenize = tokenize; | ||
this.document = document; | ||
this.voidMap = voidMap; | ||
this.parentStack = []; | ||
for (var i=0;i<token.attributes.length;i++) { | ||
var attr = token.attributes[i]; | ||
el.setAttribute(attr[0], attr[1]); | ||
} | ||
simple$dom$html$parser$$HTMLParser.prototype.isVoid = function(element) { | ||
return this.voidMap[element.nodeName] === true; | ||
}; | ||
if (this.isVoid(el)) { | ||
return this.appendChild(el); | ||
} | ||
simple$dom$html$parser$$HTMLParser.prototype.pushElement = function(token) { | ||
var el = this.document.createElement(token.tagName); | ||
this.parentStack.push(el); | ||
}; | ||
for (var i=0;i<token.attributes.length;i++) { | ||
var attr = token.attributes[i]; | ||
el.setAttribute(attr[0], attr[1]); | ||
} | ||
HTMLParser.prototype.popElement = function(token) { | ||
var el = this.parentStack.pop(); | ||
if (this.isVoid(el)) { | ||
return this.appendChild(el); | ||
} | ||
if (el.nodeName !== token.tagName.toUpperCase()) { | ||
throw new Error('unbalanced tag'); | ||
} | ||
this.parentStack.push(el); | ||
}; | ||
this.appendChild(el); | ||
}; | ||
simple$dom$html$parser$$HTMLParser.prototype.popElement = function(token) { | ||
var el = this.parentStack.pop(); | ||
HTMLParser.prototype.appendText = function(token) { | ||
var text = this.document.createTextNode(token.chars); | ||
this.appendChild(text); | ||
}; | ||
if (el.nodeName !== token.tagName.toUpperCase()) { | ||
throw new Error('unbalanced tag'); | ||
} | ||
HTMLParser.prototype.appendComment = function(token) { | ||
var comment = this.document.createComment(token.chars); | ||
this.appendChild(comment); | ||
}; | ||
this.appendChild(el); | ||
}; | ||
HTMLParser.prototype.appendChild = function(node) { | ||
var parentNode = this.parentStack[this.parentStack.length-1]; | ||
parentNode.appendChild(node); | ||
}; | ||
simple$dom$html$parser$$HTMLParser.prototype.appendText = function(token) { | ||
var text = this.document.createTextNode(token.chars); | ||
this.appendChild(text); | ||
}; | ||
HTMLParser.prototype.parse = function(html/*, context*/) { | ||
// TODO use context for namespaceURI issues | ||
var fragment = this.document.createDocumentFragment(); | ||
this.parentStack.push(fragment); | ||
simple$dom$html$parser$$HTMLParser.prototype.appendComment = function(token) { | ||
var comment = this.document.createComment(token.chars); | ||
this.appendChild(comment); | ||
}; | ||
var tokens = this.tokenize(html); | ||
for (var i=0, l=tokens.length; i<l; i++) { | ||
var token = tokens[i]; | ||
switch (token.type) { | ||
case 'StartTag': | ||
this.pushElement(token); | ||
break; | ||
case 'EndTag': | ||
this.popElement(token); | ||
break; | ||
case 'Chars': | ||
this.appendText(token); | ||
break; | ||
case 'Comment': | ||
this.appendComment(token); | ||
break; | ||
} | ||
} | ||
simple$dom$html$parser$$HTMLParser.prototype.appendChild = function(node) { | ||
var parentNode = this.parentStack[this.parentStack.length-1]; | ||
parentNode.appendChild(node); | ||
}; | ||
return this.parentStack.pop(); | ||
}; | ||
simple$dom$html$parser$$HTMLParser.prototype.parse = function(html/*, context*/) { | ||
// TODO use context for namespaceURI issues | ||
var fragment = this.document.createDocumentFragment(); | ||
this.parentStack.push(fragment); | ||
function HTMLSerializer(voidMap) { | ||
this.voidMap = voidMap; | ||
} | ||
var tokens = this.tokenize(html); | ||
for (var i=0, l=tokens.length; i<l; i++) { | ||
var token = tokens[i]; | ||
switch (token.type) { | ||
case 'StartTag': | ||
this.pushElement(token); | ||
break; | ||
case 'EndTag': | ||
this.popElement(token); | ||
break; | ||
case 'Chars': | ||
this.appendText(token); | ||
break; | ||
case 'Comment': | ||
this.appendComment(token); | ||
break; | ||
} | ||
} | ||
HTMLSerializer.prototype.openTag = function(element) { | ||
return '<' + element.nodeName.toLowerCase() + this.attributes(element.attributes) + '>'; | ||
}; | ||
return this.parentStack.pop(); | ||
}; | ||
HTMLSerializer.prototype.closeTag = function(element) { | ||
return '</' + element.nodeName.toLowerCase() + '>'; | ||
}; | ||
var simple$dom$html$parser$$default = simple$dom$html$parser$$HTMLParser; | ||
function simple$dom$html$serializer$$HTMLSerializer(voidMap) { | ||
this.voidMap = voidMap; | ||
HTMLSerializer.prototype.isVoid = function(element) { | ||
return this.voidMap[element.nodeName] === true; | ||
}; | ||
HTMLSerializer.prototype.attributes = function(namedNodeMap) { | ||
var buffer = ''; | ||
for (var i=0, l=namedNodeMap.length; i<l; i++) { | ||
buffer += this.attr(namedNodeMap[i]); | ||
} | ||
return buffer; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.openTag = function(element) { | ||
return '<' + element.nodeName.toLowerCase() + this.attributes(element.attributes) + '>'; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.closeTag = function(element) { | ||
return '</' + element.nodeName.toLowerCase() + '>'; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.isVoid = function(element) { | ||
return this.voidMap[element.nodeName] === true; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.attributes = function(namedNodeMap) { | ||
var buffer = ''; | ||
for (var i=0, l=namedNodeMap.length; i<l; i++) { | ||
buffer += this.attr(namedNodeMap[i]); | ||
HTMLSerializer.prototype.escapeAttrValue = function(attrValue) { | ||
return attrValue.replace(/[&"]/g, function(match) { | ||
switch(match) { | ||
case '&': | ||
return '&'; | ||
case '\"': | ||
return '"'; | ||
} | ||
return buffer; | ||
}; | ||
}); | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.escapeAttrValue = function(attrValue) { | ||
return attrValue.replace(/[&"]/g, function(match) { | ||
switch(match) { | ||
case '&': | ||
return '&'; | ||
case '\"': | ||
return '"'; | ||
} | ||
}); | ||
}; | ||
HTMLSerializer.prototype.attr = function(attr) { | ||
if (!attr.specified) { | ||
return ''; | ||
} | ||
if (attr.value) { | ||
return ' ' + attr.name + '="' + this.escapeAttrValue(attr.value) + '"'; | ||
} | ||
return ' ' + attr.name; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.attr = function(attr) { | ||
if (!attr.specified) { | ||
return ''; | ||
HTMLSerializer.prototype.escapeText = function(textNodeValue) { | ||
return textNodeValue.replace(/[&<>]/g, function(match) { | ||
switch(match) { | ||
case '&': | ||
return '&'; | ||
case '<': | ||
return '<'; | ||
case '>': | ||
return '>'; | ||
} | ||
if (attr.value) { | ||
return ' ' + attr.name + '="' + this.escapeAttrValue(attr.value) + '"'; | ||
} | ||
return ' ' + attr.name; | ||
}; | ||
}); | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.escapeText = function(textNodeValue) { | ||
return textNodeValue.replace(/[&<>]/g, function(match) { | ||
switch(match) { | ||
case '&': | ||
return '&'; | ||
case '<': | ||
return '<'; | ||
case '>': | ||
return '>'; | ||
} | ||
}); | ||
}; | ||
HTMLSerializer.prototype.text = function(text) { | ||
return this.escapeText(text.nodeValue); | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.text = function(text) { | ||
return this.escapeText(text.nodeValue); | ||
}; | ||
HTMLSerializer.prototype.rawHTMLSection = function(text) { | ||
return text.nodeValue; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.rawHTMLSection = function(text) { | ||
return text.nodeValue; | ||
}; | ||
HTMLSerializer.prototype.comment = function(comment) { | ||
return '<!--'+comment.nodeValue+'-->'; | ||
}; | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.comment = function(comment) { | ||
return '<!--'+comment.nodeValue+'-->'; | ||
}; | ||
HTMLSerializer.prototype.serializeChildren = function(node) { | ||
var buffer = ''; | ||
var next = node.firstChild; | ||
if (next) { | ||
buffer += this.serialize(next); | ||
simple$dom$html$serializer$$HTMLSerializer.prototype.serialize = function(node) { | ||
var buffer = ''; | ||
var next; | ||
// open | ||
switch (node.nodeType) { | ||
case 1: | ||
buffer += this.openTag(node); | ||
break; | ||
case 3: | ||
buffer += this.text(node); | ||
break; | ||
case -1: | ||
buffer += this.rawHTMLSection(node); | ||
break; | ||
case 8: | ||
buffer += this.comment(node); | ||
break; | ||
default: | ||
break; | ||
while(next = next.nextSibling) { | ||
buffer += this.serialize(next); | ||
} | ||
} | ||
return buffer; | ||
}; | ||
next = node.firstChild; | ||
if (next) { | ||
buffer += this.serialize(next); | ||
HTMLSerializer.prototype.serialize = function(node) { | ||
var buffer = ''; | ||
while(next = next.nextSibling) { | ||
buffer += this.serialize(next); | ||
} | ||
} | ||
// open | ||
switch (node.nodeType) { | ||
case 1: | ||
buffer += this.openTag(node); | ||
break; | ||
case 3: | ||
buffer += this.text(node); | ||
break; | ||
case -1: | ||
buffer += this.rawHTMLSection(node); | ||
break; | ||
case 8: | ||
buffer += this.comment(node); | ||
break; | ||
default: | ||
break; | ||
} | ||
if (node.nodeType === 1 && !this.isVoid(node)) { | ||
buffer += this.closeTag(node); | ||
} | ||
buffer += this.serializeChildren(node); | ||
return buffer; | ||
}; | ||
if (node.nodeType === 1 && !this.isVoid(node)) { | ||
buffer += this.closeTag(node); | ||
} | ||
var simple$dom$html$serializer$$default = simple$dom$html$serializer$$HTMLSerializer; | ||
return buffer; | ||
}; | ||
var simple$dom$void$map$$default = { | ||
AREA: true, | ||
BASE: true, | ||
BR: true, | ||
COL: true, | ||
COMMAND: true, | ||
EMBED: true, | ||
HR: true, | ||
IMG: true, | ||
INPUT: true, | ||
KEYGEN: true, | ||
LINK: true, | ||
META: true, | ||
PARAM: true, | ||
SOURCE: true, | ||
TRACK: true, | ||
WBR: true | ||
}; | ||
var _voidMap = { | ||
AREA: true, | ||
BASE: true, | ||
BR: true, | ||
COL: true, | ||
COMMAND: true, | ||
EMBED: true, | ||
HR: true, | ||
IMG: true, | ||
INPUT: true, | ||
KEYGEN: true, | ||
LINK: true, | ||
META: true, | ||
PARAM: true, | ||
SOURCE: true, | ||
TRACK: true, | ||
WBR: true | ||
}; | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define([], factory); | ||
} else if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
} else { | ||
root.SimpleDOM = factory(); | ||
} | ||
}(this, function () { | ||
return { | ||
Node: simple$dom$document$node$$default, | ||
Element: simple$dom$document$element$$default, | ||
DocumentFragment: simple$dom$document$document$fragment$$default, | ||
Document: simple$dom$document$$default, | ||
voidMap: simple$dom$void$map$$default, | ||
HTMLSerializer: simple$dom$html$serializer$$default, | ||
HTMLParser: simple$dom$html$parser$$default | ||
}; | ||
})); | ||
}).call(this); | ||
exports.Node = Node; | ||
exports.Element = Element; | ||
exports.DocumentFragment = DocumentFragment; | ||
exports.Document = Document; | ||
exports.HTMLParser = HTMLParser; | ||
exports.HTMLSerializer = HTMLSerializer; | ||
exports.voidMap = _voidMap; | ||
})); | ||
//# sourceMappingURL=simple-dom.js.map |
@@ -1,17 +0,7 @@ | ||
import Node from 'simple-dom/document/node'; | ||
import Element from 'simple-dom/document/element'; | ||
import DocumentFragment from 'simple-dom/document/document-fragment'; | ||
import Document from 'simple-dom/document'; | ||
import HTMLParser from 'simple-dom/html-parser'; | ||
import HTMLSerializer from 'simple-dom/html-serializer'; | ||
import voidMap from 'simple-dom/void-map'; | ||
export { | ||
Node, | ||
Element, | ||
DocumentFragment, | ||
Document, | ||
HTMLParser, | ||
HTMLSerializer, | ||
voidMap | ||
}; | ||
export { default as Node } from './simple-dom/document/node'; | ||
export { default as Element } from './simple-dom/document/element'; | ||
export { default as DocumentFragment } from './simple-dom/document/document-fragment'; | ||
export { default as Document } from './simple-dom/document'; | ||
export { default as HTMLParser } from './simple-dom/html-parser'; | ||
export { default as HTMLSerializer } from './simple-dom/html-serializer'; | ||
export { default as voidMap } from './simple-dom/void-map'; |
@@ -71,5 +71,17 @@ function HTMLSerializer(voidMap) { | ||
HTMLSerializer.prototype.serializeChildren = function(node) { | ||
var buffer = ''; | ||
var next = node.firstChild; | ||
if (next) { | ||
buffer += this.serialize(next); | ||
while(next = next.nextSibling) { | ||
buffer += this.serialize(next); | ||
} | ||
} | ||
return buffer; | ||
}; | ||
HTMLSerializer.prototype.serialize = function(node) { | ||
var buffer = ''; | ||
var next; | ||
@@ -94,11 +106,4 @@ // open | ||
next = node.firstChild; | ||
if (next) { | ||
buffer += this.serialize(next); | ||
buffer += this.serializeChildren(node); | ||
while(next = next.nextSibling) { | ||
buffer += this.serialize(next); | ||
} | ||
} | ||
if (node.nodeType === 1 && !this.isVoid(node)) { | ||
@@ -105,0 +110,0 @@ buffer += this.closeTag(node); |
{ | ||
"name": "simple-dom", | ||
"version": "0.2.7", | ||
"version": "0.3.0", | ||
"description": "A simple JS DOM.", | ||
"main": "dist/simple-dom.js", | ||
"jsnext:main": "lib/simple-dom.js", | ||
"scripts": { | ||
"prepublish": "bin/build.sh", | ||
"prepublish": "npm run build", | ||
"build": "node bin/build.js", | ||
"test": "testem ci" | ||
@@ -23,8 +25,8 @@ }, | ||
"devDependencies": { | ||
"es6-module-transpiler": "^0.9.6", | ||
"qunit-extras": "^1.4.1", | ||
"qunitjs": "^1.16.0", | ||
"simple-html-tokenizer": "^0.1.1", | ||
"testem": "^0.6.24" | ||
"rollup": "^0.15.0", | ||
"simple-html-tokenizer": "^0.2.0", | ||
"testem": "^1.4.0" | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2289
285923
18