OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 "use strict"; |
| 5 |
| 6 (function(exports) { |
| 7 |
| 8 class FakeNode { |
| 9 constructor() { |
| 10 this.parentNode = null; |
| 11 this.nextSibling = null; |
| 12 this.previousSibling = null; |
| 13 } |
| 14 contains(other) { |
| 15 while (other && other != this) |
| 16 other = other.parentNode; |
| 17 return other === this; |
| 18 } |
| 19 remove() { |
| 20 if (!this.parentNode) |
| 21 return; |
| 22 if (this.nextSibling) |
| 23 this.nextSibling._previousSibling = this._previousSibling; |
| 24 if (this.previousSibling) |
| 25 this.previousSibling._nextSibling = this._nextSibling; |
| 26 this._parentNode = null; |
| 27 this._nextSibling = null; |
| 28 this._previousSibling = null; |
| 29 } |
| 30 } |
| 31 |
| 32 class FakeParentNode extends FakeNode { |
| 33 constructor() { |
| 34 super(); |
| 35 this.firstChild = null; |
| 36 this.lastChild = null; |
| 37 } |
| 38 appendChild(node) { |
| 39 if (node.contains(this)) |
| 40 throw new Error("HierarchyRequestError"); |
| 41 node.remove(); |
| 42 node.parentNode = this; |
| 43 node.previousSibling = this.lastChild; |
| 44 if (this.lastChild) |
| 45 this.lastChild.nextSibling = node; |
| 46 this.lastChild = node; |
| 47 if (!this.firstChild) |
| 48 this.firstChild = node; |
| 49 return node; |
| 50 } |
| 51 } |
| 52 |
| 53 class FakeDocumentFragment extends FakeParentNode { |
| 54 } |
| 55 |
| 56 class FakeElement extends FakeParentNode { |
| 57 constructor(tagName) { |
| 58 super(); |
| 59 this.tagName = tagName; |
| 60 this.attributes = new Map(); |
| 61 this.children = []; |
| 62 } |
| 63 setAttribute(name, value) { |
| 64 this.attributes.set(name, value); |
| 65 } |
| 66 } |
| 67 |
| 68 class FakeText extends FakeNode { |
| 69 constructor(value) { |
| 70 super(); |
| 71 this.value = value; |
| 72 } |
| 73 } |
| 74 |
| 75 class FakeDomParserBindings { |
| 76 static setAttribute(element, name, value) { |
| 77 element.setAttribute(name, value); |
| 78 } |
| 79 |
| 80 static appendChild(parent, child) { |
| 81 parent.appendChild(child); |
| 82 } |
| 83 |
| 84 static createText(value) { |
| 85 return new FakeText(value); |
| 86 } |
| 87 |
| 88 static createElement(tagName) { |
| 89 return new FakeElement(tagName); |
| 90 } |
| 91 |
| 92 static createDocumentFragment() { |
| 93 return new FakeDocumentFragment(); |
| 94 } |
| 95 } |
| 96 |
| 97 exports.FakeDomParserBindings = FakeDomParserBindings; |
| 98 |
| 99 })(this); |
OLD | NEW |