Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit e8f5ab2

Browse files
committed
Fix issue with SVGElementInstance
1 parent de3c061 commit e8f5ab2

8 files changed

Lines changed: 243 additions & 3 deletions

File tree

build.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"src/wrappers/HTMLAudioElement.js",
2222
"src/wrappers/HTMLOptionElement.js",
2323
"src/wrappers/HTMLUnknownElement.js",
24+
"src/wrappers/SVGElement.js",
25+
"src/wrappers/SVGUseElement.js",
26+
"src/wrappers/SVGElementInstance.js",
2427
"src/wrappers/CanvasRenderingContext2D.js",
2528
"src/wrappers/WebGLRenderingContext.js",
2629
"src/wrappers/Range.js",

shadowdom.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
'src/wrappers/HTMLAudioElement.js',
3838
'src/wrappers/HTMLOptionElement.js',
3939
'src/wrappers/HTMLUnknownElement.js',
40+
'src/wrappers/SVGElement.js',
41+
'src/wrappers/SVGUseElement.js',
42+
'src/wrappers/SVGElementInstance.js',
4043
'src/wrappers/CanvasRenderingContext2D.js',
4144
'src/wrappers/WebGLRenderingContext.js',
4245
'src/wrappers/Range.js',

src/wrappers.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,14 @@ window.ShadowDOMPolyfill = {};
249249
}
250250

251251
var OriginalDOMImplementation = window.DOMImplementation;
252+
var OriginalEventTarget = window.EventTarget;
252253
var OriginalEvent = window.Event;
253254
var OriginalNode = window.Node;
254255
var OriginalWindow = window.Window;
255256
var OriginalRange = window.Range;
256257
var OriginalCanvasRenderingContext2D = window.CanvasRenderingContext2D;
257258
var OriginalWebGLRenderingContext = window.WebGLRenderingContext;
259+
var OriginalSVGElementInstance = window.SVGElementInstance;
258260

259261
function isWrapper(object) {
260262
return object instanceof wrappers.EventTarget ||
@@ -267,14 +269,17 @@ window.ShadowDOMPolyfill = {};
267269
}
268270

269271
function isNative(object) {
270-
return object instanceof OriginalNode ||
272+
return OriginalEventTarget && object instanceof OriginalEventTarget ||
273+
object instanceof OriginalNode ||
271274
object instanceof OriginalEvent ||
272275
object instanceof OriginalWindow ||
273276
object instanceof OriginalRange ||
274277
object instanceof OriginalDOMImplementation ||
275278
object instanceof OriginalCanvasRenderingContext2D ||
276279
OriginalWebGLRenderingContext &&
277-
object instanceof OriginalWebGLRenderingContext;
280+
object instanceof OriginalWebGLRenderingContext ||
281+
OriginalSVGElementInstance &&
282+
object instanceof OriginalSVGElementInstance;
278283
}
279284

280285
/**

src/wrappers/SVGElement.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Polymer Authors. All rights reserved.
2+
// Use of this source code is goverened by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
(function(scope) {
6+
'use strict';
7+
8+
var registerObject = scope.registerObject;
9+
10+
var SVG_NS = 'http://www.w3.org/2000/svg';
11+
var svgTitleElement = document.createElementNS(SVG_NS, 'title');
12+
var SVGTitleElement = registerObject(svgTitleElement);
13+
var SVGElement = Object.getPrototypeOf(SVGTitleElement.prototype).constructor;
14+
15+
scope.wrappers.SVGElement = SVGElement;
16+
})(window.ShadowDOMPolyfill);

src/wrappers/SVGElementInstance.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2014 The Polymer Authors. All rights reserved.
2+
// Use of this source code is goverened by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
(function(scope) {
6+
'use strict';
7+
8+
var EventTarget = scope.wrappers.EventTarget;
9+
var mixin = scope.mixin;
10+
var registerWrapper = scope.registerWrapper;
11+
var wrap = scope.wrap;
12+
13+
var OriginalSVGElementInstance = window.SVGElementInstance;
14+
if (!OriginalSVGElementInstance)
15+
return;
16+
17+
function SVGElementInstance(impl) {
18+
EventTarget.call(this, impl);
19+
}
20+
21+
SVGElementInstance.prototype = Object.create(EventTarget.prototype);
22+
mixin(SVGElementInstance.prototype, {
23+
/** @type {SVGElement} */
24+
get correspondingElement() {
25+
return wrap(this.impl.correspondingElement);
26+
},
27+
28+
/** @type {SVGUseElement} */
29+
get correspondingUseElement() {
30+
return wrap(this.impl.correspondingUseElement);
31+
},
32+
33+
/** @type {SVGElementInstance} */
34+
get parentNode() {
35+
return wrap(this.impl.parentNode);
36+
},
37+
38+
/** @type {SVGElementInstanceList} */
39+
get childNodes() {
40+
throw new Error('Not implemented');
41+
},
42+
43+
/** @type {SVGElementInstance} */
44+
get firstChild() {
45+
return wrap(this.impl.firstChild);
46+
},
47+
48+
/** @type {SVGElementInstance} */
49+
get lastChild() {
50+
return wrap(this.impl.lastChild);
51+
},
52+
53+
/** @type {SVGElementInstance} */
54+
get previousSibling() {
55+
return wrap(this.impl.previousSibling);
56+
},
57+
58+
/** @type {SVGElementInstance} */
59+
get nextSibling() {
60+
return wrap(this.impl.nextSibling);
61+
}
62+
});
63+
64+
registerWrapper(OriginalSVGElementInstance, SVGElementInstance);
65+
66+
scope.wrappers.SVGElementInstance = SVGElementInstance;
67+
})(window.ShadowDOMPolyfill);

src/wrappers/SVGUseElement.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Polymer Authors. All rights reserved.
2+
// Use of this source code is goverened by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
(function(scope) {
6+
'use strict';
7+
8+
var mixin = scope.mixin;
9+
var registerWrapper = scope.registerWrapper;
10+
var unwrap = scope.unwrap;
11+
var wrap = scope.wrap;
12+
13+
var OriginalSVGUseElement = window.SVGUseElement;
14+
15+
// IE uses SVGElement as parent interface, SVG2 (Blink & Gecko) uses
16+
// SVGGraphicsElement. Use the <g> element to get the right prototype.
17+
18+
var SVG_NS = 'http://www.w3.org/2000/svg';
19+
var gWrapper = wrap(document.createElementNS(SVG_NS, 'g'));
20+
var useElement = document.createElementNS(SVG_NS, 'use');
21+
var SVGGElement = gWrapper.constructor;
22+
var parentInterfacePrototype = Object.getPrototypeOf(SVGGElement.prototype);
23+
var parentInterface = parentInterfacePrototype.constructor;
24+
25+
function SVGUseElement(impl) {
26+
parentInterface.call(this, impl);
27+
}
28+
29+
SVGUseElement.prototype = Object.create(parentInterfacePrototype);
30+
31+
// Firefox does not expose instanceRoot.
32+
if ('instanceRoot' in useElement) {
33+
mixin(SVGUseElement.prototype, {
34+
get instanceRoot() {
35+
return wrap(unwrap(this).instanceRoot);
36+
},
37+
get animatedInstanceRoot() {
38+
return wrap(unwrap(this).animatedInstanceRoot);
39+
},
40+
});
41+
}
42+
43+
registerWrapper(OriginalSVGUseElement, SVGUseElement, useElement);
44+
45+
scope.wrappers.SVGUseElement = SVGUseElement;
46+
})(window.ShadowDOMPolyfill);

test/js/SVGElementInstance.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
});

test/test.main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ var modules = [
109109
'MutationObserver/transient.js',
110110
'Node.js',
111111
'ParentNodeInterface.js',
112+
'Range.js',
113+
'SVGElementInstance.js',
112114
'ShadowRoot.js',
113115
'Text.js',
114-
'Range.js',
115116
'Window.js',
116117
'custom-element.js',
117118
'events.js',

0 commit comments

Comments
 (0)