├── .gitignore ├── package.json ├── index.js ├── README.md ├── LICENSE └── lib ├── nodes.js └── domexception.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dom4" 3 | , "version": "0.0.1" 4 | , "private": true 5 | , "license": "MIT" 6 | , "dependencies": { 7 | "parse5": "0.8.3" 8 | } 9 | , "main": "index.js" 10 | } 11 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | exports.DOMException = require("./lib/domexception"); 3 | exports.Document = require("./lib/nodes").Document; 4 | exports.DocumentFragment = require("./lib/nodes").DocumentFragment; 5 | exports.Element = require("./lib/nodes").Element; 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | dom4 2 | ==== 3 | 4 | An implementation of DOM4 for Node. 5 | 6 | At this point the goal is relatively limited: 7 | 8 | * Provide enough DOM to be constructible with Parse5 9 | * Provide enough DOM to be usable with jQuery 10 | 11 | But it can possibly grow over time. 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Robin Berjon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /lib/nodes.js: -------------------------------------------------------------------------------- 1 | 2 | var DOMException = require("./domexception") 3 | , util = require("util") 4 | ; 5 | 6 | function NonElementParentNode () { 7 | this._idMap = {}; 8 | } 9 | NonElementParentNode.prototype = { 10 | getElementById: function (id) { 11 | if (!this._idMap || !this._idMap[id]) return null; 12 | return this._idMap[id][0]; 13 | } 14 | // These two are called whenever an element is added or removed, or its .id changes 15 | , _addToIDMap: function (id, el) { 16 | if (!this._idMap[id]) this._idMap[id] = [el]; 17 | else { 18 | // XXX we don't just append, we have to compare positions 19 | this._idMap[id].push(el); 20 | } 21 | } 22 | , _removeFromIDMap: function (id, el) { 23 | if (!this._idMap[id]) return; 24 | var idx = this._idMap[id].indexOf(el); 25 | if (idx < 0) return; 26 | this._idMap[id].splice(idx, 1); 27 | } 28 | }; 29 | 30 | 31 | function ParentNode () {} 32 | ParentNode.prototype = { 33 | 34 | }; 35 | 36 | 37 | 38 | function Document () { 39 | NonElementParentNode.call(this); 40 | ParentNode.call(this); 41 | } 42 | Document.prototype = { 43 | }; 44 | util.inherits(Document, NonElementParentNode); 45 | util.inherits(Document, ParentNode); 46 | 47 | 48 | 49 | 50 | function DocumentFragment () { 51 | NonElementParentNode.call(this); 52 | ParentNode.call(this); 53 | } 54 | DocumentFragment.prototype = { 55 | }; 56 | util.inherits(DocumentFragment, NonElementParentNode); 57 | util.inherits(DocumentFragment, ParentNode); 58 | 59 | 60 | 61 | 62 | function Element () { 63 | ParentNode.call(this); 64 | } 65 | Element.prototype = { 66 | }; 67 | util.inherits(Element, ParentNode); 68 | 69 | 70 | 71 | // expose them all 72 | exports.Document = Document; 73 | exports.DocumentFragment = DocumentFragment; 74 | exports.Element = Element; 75 | -------------------------------------------------------------------------------- /lib/domexception.js: -------------------------------------------------------------------------------- 1 | /*jshint es5:true */ 2 | 3 | var errorTable = { 4 | IndexSizeError: { msg: "The index is not in the allowed range", code: 1 } 5 | , HierarchyRequestError: { msg: "The operation would yield an incorrect node tree", code: 3 } 6 | , WrongDocumentError: { msg: "The object is in the wrong document", code: 4 } 7 | , InvalidCharacterError: { msg: "The string contains invalid characters", code: 5 } 8 | , NoModificationAllowedError: { msg: "The object can not be modified", code: 7 } 9 | , NotFoundError: { msg: "The object can not be found here", code: 8 } 10 | , NotSupportedError: { msg: "The operation is not supported", code: 9 } 11 | , InvalidStateError: { msg: "The object is in an invalid state", code: 11 } 12 | , SyntaxError: { msg: "The string did not match the expected pattern", code: 12 } 13 | , InvalidModificationError: { msg: "The object can not be modified in this way", code: 13 } 14 | , NamespaceError: { msg: "The operation is not allowed by Namespaces in XML.", code:14 } 15 | , InvalidAccessError: { msg: "The object does not support the operation or argument", code: 15 } 16 | , SecurityError: { msg: "The operation is insecure", code: 18 } 17 | , NetworkError: { msg: "A network error occurred", code: 19 } 18 | , AbortError: { msg: "The operation was aborted", code: 20 } 19 | , URLMismatchError: { msg: "The given URL does not match another URL", code: 21 } 20 | , QuotaExceededError: { msg: "The quota has been exceeded", code: 22 } 21 | , TimeoutError: { msg: "The operation timed out", code: 23 } 22 | , InvalidNodeTypeError: { msg: "The supplied node is incorrect or has an incorrect ancestor for this operation", code: 24 } 23 | , DataCloneError: { msg: "The object can not be cloned", code: 25 } 24 | , EncodingError: { msg: "The encoding operation (either encoded or decoding } failed.", code: 0 } 25 | }; 26 | 27 | 28 | function DOMException (code, name, message) { 29 | this._code = code; 30 | this._name = name; 31 | this._message = message; 32 | } 33 | DOMException.prototype = { 34 | INDEX_SIZE_ERR: 1 35 | , DOMSTRING_SIZE_ERR: 2 36 | , HIERARCHY_REQUEST_ERR: 3 37 | , WRONG_DOCUMENT_ERR: 4 38 | , INVALID_CHARACTER_ERR: 5 39 | , NO_DATA_ALLOWED_ERR: 6 40 | , NO_MODIFICATION_ALLOWED_ERR: 7 41 | , NOT_FOUND_ERR: 8 42 | , NOT_SUPPORTED_ERR: 9 43 | , INUSE_ATTRIBUTE_ERR: 10 44 | , INVALID_STATE_ERR: 11 45 | , SYNTAX_ERR: 12 46 | , INVALID_MODIFICATION_ERR: 13 47 | , NAMESPACE_ERR: 14 48 | , INVALID_ACCESS_ERR: 15 49 | , VALIDATION_ERR: 16 50 | , TYPE_MISMATCH_ERR: 17 51 | , SECURITY_ERR: 18 52 | , NETWORK_ERR: 19 53 | , ABORT_ERR: 20 54 | , URL_MISMATCH_ERR: 21 55 | , QUOTA_EXCEEDED_ERR: 22 56 | , TIMEOUT_ERR: 23 57 | , INVALID_NODE_TYPE_ERR: 24 58 | , DATA_CLONE_ERR: 25 59 | , get code () { return this._code; } 60 | , get name () { return this._name; } 61 | , get message () { return this._message; } 62 | }; 63 | DOMException.throw = function (name) { 64 | if (errorTable[name]) { 65 | throw new DOMException(errorTable[name].code || 0, name, errorTable[name].message); 66 | } 67 | else { 68 | throw new DOMException(); 69 | } 70 | }; 71 | 72 | exports = DOMException; 73 | --------------------------------------------------------------------------------