|
| 1 | +/* |
| 2 | + * Copyright 2014 The Polymer Authors. All rights reserved. |
| 3 | + * Use of this source code is goverened by a BSD-style |
| 4 | + * license that can be found in the LICENSE file. |
| 5 | + */ |
| 6 | + |
| 7 | +suite('SVGElementInstance', function() { |
| 8 | + |
| 9 | + var div; |
| 10 | + |
| 11 | + teardown(function() { |
| 12 | + if (div) { |
| 13 | + if (div.parentNode) |
| 14 | + div.parentNode.removeChild(div); |
| 15 | + div = undefined; |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + var svgCode = '\ |
| 20 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">\ |
| 21 | + <defs>\ |
| 22 | + <g id="g">\ |
| 23 | + <line/>\ |
| 24 | + </g>\ |
| 25 | + </defs>\ |
| 26 | + <use xlink:href="#g" />\ |
| 27 | + </svg>'; |
| 28 | + |
| 29 | + function getInstanceRoot() { |
| 30 | + div = document.body.appendChild(document.createElement('div')); |
| 31 | + div.innerHTML = svgCode; |
| 32 | + var svg = div.firstElementChild; |
| 33 | + var useElement = svg.firstElementChild.nextElementSibling; |
| 34 | + return useElement.instanceRoot; |
| 35 | + } |
| 36 | + |
| 37 | + test('instanceof SVGUseElement', function() { |
| 38 | + div = document.body.appendChild(document.createElement('div')); |
| 39 | + div.innerHTML = svgCode; |
| 40 | + var svg = div.firstElementChild; |
| 41 | + var useElement = svg.firstElementChild.nextElementSibling; |
| 42 | + assert.instanceOf(useElement, SVGUseElement); |
| 43 | + assert.instanceOf(useElement, SVGElement); |
| 44 | + assert.instanceOf(useElement, Element); |
| 45 | + assert.instanceOf(useElement, EventTarget); |
| 46 | + }); |
| 47 | + |
| 48 | + test('instanceof SVGElementInstance', function() { |
| 49 | + // Firefox does not implement SVGElementInstance. |
| 50 | + if (/Firefox/.test(navigator.userAgent)) |
| 51 | + return; |
| 52 | + |
| 53 | + var instanceRoot = getInstanceRoot(); |
| 54 | + assert.instanceOf(instanceRoot, SVGElementInstance); |
| 55 | + assert.instanceOf(instanceRoot, EventTarget); |
| 56 | + }); |
| 57 | + |
| 58 | + test('correspondingUseElement', function() { |
| 59 | + // Firefox does not implement SVGElementInstance. |
| 60 | + if (/Firefox/.test(navigator.userAgent)) |
| 61 | + return; |
| 62 | + |
| 63 | + div = document.body.appendChild(document.createElement('div')); |
| 64 | + div.innerHTML = svgCode; |
| 65 | + var svg = div.firstElementChild; |
| 66 | + var useElement = svg.firstElementChild.nextElementSibling; |
| 67 | + var instanceRoot = useElement.instanceRoot; |
| 68 | + assert.equal(useElement, instanceRoot.correspondingUseElement); |
| 69 | + }); |
| 70 | + |
| 71 | + test('correspondingElement', function() { |
| 72 | + // Firefox does not implement SVGElementInstance. |
| 73 | + if (/Firefox/.test(navigator.userAgent)) |
| 74 | + return; |
| 75 | + |
| 76 | + var instanceRoot = getInstanceRoot(); |
| 77 | + assert.equal('g', instanceRoot.correspondingElement.localName); |
| 78 | + }); |
| 79 | + |
| 80 | + test('tree', function() { |
| 81 | + // Firefox does not implement SVGElementInstance. |
| 82 | + if (/Firefox/.test(navigator.userAgent)) |
| 83 | + return; |
| 84 | + |
| 85 | + var instanceRoot = getInstanceRoot(); |
| 86 | + |
| 87 | + assert.equal('line', instanceRoot.firstChild.correspondingElement.localName); |
| 88 | + assert.equal('line', instanceRoot.lastChild.correspondingElement.localName); |
| 89 | + |
| 90 | + // IE always returns new wrappers for all the accessors. |
| 91 | + if (/Trident/.test(navigator.userAgent)) |
| 92 | + return; |
| 93 | + |
| 94 | + assert.equal(instanceRoot.firstChild, instanceRoot.lastChild); |
| 95 | + assert.equal(instanceRoot, instanceRoot.firstChild.parentNode); |
| 96 | + assert.isNull(instanceRoot.parentNode); |
| 97 | + }); |
| 98 | + |
| 99 | +}); |
0 commit comments