|
| 1 | +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
| 3 | +/* Any copyright is dedicated to the Public Domain. |
| 4 | + * http://creativecommons.org/publicdomain/zero/1.0/ */ |
| 5 | + |
| 6 | +// Dummy XML Parser |
| 7 | + |
| 8 | +function DOMNodeMock(nodeName, nodeValue) { |
| 9 | + this.nodeName = nodeName; |
| 10 | + this.nodeValue = nodeValue; |
| 11 | + Object.defineProperty(this, 'parentNode', {value: null, writable: true}); |
| 12 | +} |
| 13 | +DOMNodeMock.prototype = { |
| 14 | + get firstChild() { |
| 15 | + return this.childNodes[0]; |
| 16 | + }, |
| 17 | + get nextSibling() { |
| 18 | + var index = this.parentNode.childNodes.indexOf(this); |
| 19 | + return this.parentNode.childNodes[index + 1]; |
| 20 | + }, |
| 21 | + get textContent() { |
| 22 | + if (!this.childNodes) { |
| 23 | + return this.nodeValue || ''; |
| 24 | + } |
| 25 | + return this.childNodes.map(function (child) { |
| 26 | + return child.textContent; |
| 27 | + }).join(''); |
| 28 | + }, |
| 29 | + hasChildNodes: function () { |
| 30 | + return this.childNodes && this.childNodes.length > 0; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +function decodeXML(text) { |
| 35 | + if (text.indexOf('&') < 0) { |
| 36 | + return text; |
| 37 | + } |
| 38 | + return text.replace(/&(#(x[0-9a-f]+|\d+)|\w+);/gi, function (all, entityName, number) { |
| 39 | + if (number) { |
| 40 | + return String.fromCharCode(number[0] === 'x' ? parseInt(number.substring(1), 16) : +number); |
| 41 | + } |
| 42 | + switch (entityName) { |
| 43 | + case 'amp': |
| 44 | + return '&'; |
| 45 | + case 'lt': |
| 46 | + return '<'; |
| 47 | + case 'gt': |
| 48 | + return '>'; |
| 49 | + case 'quot': |
| 50 | + return '\"'; |
| 51 | + case 'apos': |
| 52 | + return '\''; |
| 53 | + } |
| 54 | + return '&' + entityName + ';'; |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +function DOMParserMock() {}; |
| 59 | +DOMParserMock.prototype = { |
| 60 | + parseFromString: function (content) { |
| 61 | + content = content.replace(/<\?[\s\S]*?\?>|<!--[\s\S]*?-->/g, '').trim(); |
| 62 | + var nodes = []; |
| 63 | + content = content.replace(/>([\s\S]+?)</g, function (all, text) { |
| 64 | + var i = nodes.length; |
| 65 | + var node = new DOMNodeMock('#text', decodeXML(text)); |
| 66 | + nodes.push(node); |
| 67 | + if (node.textContent.trim().length === 0) { |
| 68 | + return '><'; // ignoring whitespaces |
| 69 | + } |
| 70 | + return '>' + i + ',<'; |
| 71 | + }); |
| 72 | + content = content.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, function (all, text) { |
| 73 | + var i = nodes.length; |
| 74 | + var node = new DOMNodeMock('#text', text); |
| 75 | + nodes.push(node); |
| 76 | + return i + ','; |
| 77 | + }); |
| 78 | + var lastLength; |
| 79 | + do { |
| 80 | + lastLength = nodes.length; |
| 81 | + content = content.replace(/<([\w\:]+)((?:[\s\w:=]|'[^']*'|"[^"]*")*)(?:\/>|>([\d,]*)<\/[^>]+>)/g, |
| 82 | + function (all, name, attrs, content) { |
| 83 | + var i = nodes.length; |
| 84 | + var node = new DOMNodeMock(name); |
| 85 | + var children = []; |
| 86 | + if (content) { |
| 87 | + content = content.split(','); |
| 88 | + content.pop(); |
| 89 | + content.forEach(function (child) { |
| 90 | + var childNode = nodes[+child]; |
| 91 | + childNode.parentNode = node; |
| 92 | + children.push(childNode); |
| 93 | + }) |
| 94 | + } |
| 95 | + node.childNodes = children; |
| 96 | + nodes.push(node); |
| 97 | + return i + ','; |
| 98 | + |
| 99 | + }); |
| 100 | + } while(lastLength < nodes.length); |
| 101 | + return { |
| 102 | + documentElement: nodes.pop() |
| 103 | + }; |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +exports.DOMParserMock = DOMParserMock; |
0 commit comments