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

Commit c4a7669

Browse files
committed
Rename the .impl property and ensure all files use unwrap as needed
1 parent c4da637 commit c4a7669

23 files changed

+208
-184
lines changed

src/ShadowRenderer.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
var getTreeScope = scope.getTreeScope;
1515
var mixin = scope.mixin;
1616
var oneOf = scope.oneOf;
17+
var unsafeUnwrap = scope.unsafeUnwrap;
1718
var unwrap = scope.unwrap;
1819
var wrap = scope.wrap;
1920

@@ -484,7 +485,7 @@
484485
},
485486

486487
associateNode: function(node) {
487-
node.impl.polymerShadowRenderer_ = this;
488+
unsafeUnwrap(node).polymerShadowRenderer_ = this;
488489
}
489490
};
490491

@@ -607,7 +608,7 @@
607608
* This gets called when a node was added or removed to it.
608609
*/
609610
Node.prototype.invalidateShadowRenderer = function(force) {
610-
var renderer = this.impl.polymerShadowRenderer_;
611+
var renderer = unsafeUnwrap(this).polymerShadowRenderer_;
611612
if (renderer) {
612613
renderer.invalidate();
613614
return true;
@@ -638,7 +639,7 @@
638639
var renderer;
639640
if (shadowRoot)
640641
renderer = getRendererForShadowRoot(shadowRoot);
641-
this.impl.polymerShadowRenderer_ = renderer;
642+
unsafeUnwrap(this).polymerShadowRenderer_ = renderer;
642643
if (renderer)
643644
renderer.invalidate();
644645
};

src/querySelector.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
var HTMLCollection = scope.wrappers.HTMLCollection;
99
var NodeList = scope.wrappers.NodeList;
1010
var getTreeScope = scope.getTreeScope;
11+
var unsafeUnwrap = scope.unsafeUnwrap;
1112
var wrap = scope.wrap;
1213

1314
var originalDocumentQuerySelector = document.querySelector;
@@ -37,7 +38,7 @@
3738
}
3839
result[index++] = wrappedItem;
3940
}
40-
41+
4142
return index;
4243
}
4344

@@ -98,7 +99,7 @@
9899
// http://www.w3.org/TR/css3-selectors/#simple-selectors
99100

100101
function querySelectorAllFiltered (p, index, result, selector) {
101-
var target = this.impl;
102+
var target = unsafeUnwrap(this);
102103
var list;
103104
var root = getTreeScope(this).root;
104105
if (root instanceof scope.wrappers.ShadowRoot) {
@@ -120,7 +121,7 @@
120121

121122
var SelectorsInterface = {
122123
querySelector: function(selector) {
123-
var target = this.impl;
124+
var target = unsafeUnwrap(this);
124125
var wrappedItem;
125126
var root = getTreeScope(this).root;
126127
if (root instanceof scope.wrappers.ShadowRoot) {
@@ -165,7 +166,7 @@
165166
};
166167

167168
function getElementsByTagNameFiltered (p, index, result, localName, lowercase) {
168-
var target = this.impl;
169+
var target = unsafeUnwrap(this);
169170
var list;
170171
var root = getTreeScope(this).root;
171172
if (root instanceof scope.wrappers.ShadowRoot) {
@@ -186,7 +187,7 @@
186187
}
187188

188189
function getElementsByTagNameNSFiltered (p, index, result, ns, localName) {
189-
var target = this.impl;
190+
var target = unsafeUnwrap(this);
190191
var list;
191192
var root = getTreeScope(this).root;
192193
if (root instanceof scope.wrappers.ShadowRoot) {

src/wrappers.js

+33-43
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,31 @@ window.ShadowDOMPolyfill = {};
136136
return /^\w[a-zA-Z_0-9]*$/.test(name);
137137
}
138138

139+
// The name of the implementation property is intentionally hard to
140+
// remember. Unfortunately, browsers are slower doing obj[expr] than
141+
// obj.foo so we resort to repeat this ugly name. This ugly name is never
142+
// used outside of this file though.
143+
139144
function getGetter(name) {
140145
return hasEval && isIdentifierName(name) ?
141-
new Function('return this.impl.' + name) :
142-
function() { return this.impl[name]; };
146+
new Function('return this.__impl4cf1e782hg__.' + name) :
147+
function() { return this.__impl4cf1e782hg__[name]; };
143148
}
144149

145150
function getSetter(name) {
146151
return hasEval && isIdentifierName(name) ?
147-
new Function('v', 'this.impl.' + name + ' = v') :
148-
function(v) { this.impl[name] = v; };
152+
new Function('v', 'this.__impl4cf1e782hg__.' + name + ' = v') :
153+
function(v) { this.__impl4cf1e782hg__[name] = v; };
149154
}
150155

151156
function getMethod(name) {
152157
return hasEval && isIdentifierName(name) ?
153-
new Function('return this.impl.' + name +
154-
'.apply(this.impl, arguments)') :
155-
function() { return this.impl[name].apply(this.impl, arguments); };
158+
new Function('return this.__impl4cf1e782hg__.' + name +
159+
'.apply(this.__impl4cf1e782hg__, arguments)') :
160+
function() {
161+
return this.__impl4cf1e782hg__[name].apply(
162+
this.__impl4cf1e782hg__, arguments);
163+
};
156164
}
157165

158166
function getDescriptor(source, name) {
@@ -273,41 +281,12 @@ window.ShadowDOMPolyfill = {};
273281
return GeneratedWrapper;
274282
}
275283

276-
var OriginalDOMImplementation = window.DOMImplementation;
277-
var OriginalEventTarget = window.EventTarget;
278-
var OriginalEvent = window.Event;
279-
var OriginalNode = window.Node;
280-
var OriginalWindow = window.Window;
281-
var OriginalRange = window.Range;
282-
var OriginalCanvasRenderingContext2D = window.CanvasRenderingContext2D;
283-
var OriginalWebGLRenderingContext = window.WebGLRenderingContext;
284-
var OriginalSVGElementInstance = window.SVGElementInstance;
285-
var OriginalFormData = window.FormData;
286-
287284
function isWrapper(object) {
288-
return object instanceof wrappers.EventTarget ||
289-
object instanceof wrappers.Event ||
290-
object instanceof wrappers.Range ||
291-
object instanceof wrappers.DOMImplementation ||
292-
object instanceof wrappers.CanvasRenderingContext2D ||
293-
object instanceof wrappers.FormData ||
294-
wrappers.WebGLRenderingContext &&
295-
object instanceof wrappers.WebGLRenderingContext;
285+
return object && object.__impl4cf1e782hg__;
296286
}
297287

298288
function isNative(object) {
299-
return OriginalEventTarget && object instanceof OriginalEventTarget ||
300-
object instanceof OriginalNode ||
301-
object instanceof OriginalEvent ||
302-
object instanceof OriginalWindow ||
303-
object instanceof OriginalRange ||
304-
object instanceof OriginalDOMImplementation ||
305-
object instanceof OriginalCanvasRenderingContext2D ||
306-
object instanceof OriginalFormData ||
307-
OriginalWebGLRenderingContext &&
308-
object instanceof OriginalWebGLRenderingContext ||
309-
OriginalSVGElementInstance &&
310-
object instanceof OriginalSVGElementInstance;
289+
return !isWrapper(object);
311290
}
312291

313292
/**
@@ -321,8 +300,8 @@ window.ShadowDOMPolyfill = {};
321300
return null;
322301

323302
assert(isNative(impl));
324-
return impl.polymerWrapper_ ||
325-
(impl.polymerWrapper_ = new (getWrapperConstructor(impl))(impl));
303+
return impl.__wrapper8e3dd93a60__ ||
304+
(impl.__wrapper8e3dd93a60__ = new (getWrapperConstructor(impl))(impl));
326305
}
327306

328307
/**
@@ -334,7 +313,16 @@ window.ShadowDOMPolyfill = {};
334313
if (wrapper === null)
335314
return null;
336315
assert(isWrapper(wrapper));
337-
return wrapper.impl;
316+
return wrapper.__impl4cf1e782hg__;
317+
}
318+
319+
function unsafeUnwrap(wrapper) {
320+
return wrapper.__impl4cf1e782hg__;
321+
}
322+
323+
function setWrapper(impl, wrapper) {
324+
wrapper.__impl4cf1e782hg__ = impl;
325+
impl.__wrapper8e3dd93a60__ = wrapper;
338326
}
339327

340328
/**
@@ -366,7 +354,7 @@ window.ShadowDOMPolyfill = {};
366354
return;
367355
assert(isNative(node));
368356
assert(wrapper === undefined || isWrapper(wrapper));
369-
node.polymerWrapper_ = wrapper;
357+
node.__wrapper8e3dd93a60__ = wrapper;
370358
}
371359

372360
var getterDescriptor = {
@@ -382,7 +370,7 @@ window.ShadowDOMPolyfill = {};
382370

383371
function defineWrapGetter(constructor, name) {
384372
defineGetter(constructor, name, function() {
385-
return wrap(this.impl[name]);
373+
return wrap(this.__impl4cf1e782hg__[name]);
386374
});
387375
}
388376

@@ -417,6 +405,8 @@ window.ShadowDOMPolyfill = {};
417405
scope.registerObject = registerObject;
418406
scope.registerWrapper = register;
419407
scope.rewrap = rewrap;
408+
scope.setWrapper = setWrapper;
409+
scope.unsafeUnwrap = unsafeUnwrap;
420410
scope.unwrap = unwrap;
421411
scope.unwrapIfNeeded = unwrapIfNeeded;
422412
scope.wrap = wrap;

src/wrappers/CanvasRenderingContext2D.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@
77

88
var mixin = scope.mixin;
99
var registerWrapper = scope.registerWrapper;
10+
var setWrapper = scope.setWrapper;
11+
var unsafeUnwrap = scope.unsafeUnwrap;
1012
var unwrap = scope.unwrap;
1113
var unwrapIfNeeded = scope.unwrapIfNeeded;
1214
var wrap = scope.wrap;
1315

1416
var OriginalCanvasRenderingContext2D = window.CanvasRenderingContext2D;
1517

1618
function CanvasRenderingContext2D(impl) {
17-
this.impl = impl;
19+
setWrapper(impl, this);
1820
}
1921

2022
mixin(CanvasRenderingContext2D.prototype, {
2123
get canvas() {
22-
return wrap(this.impl.canvas);
24+
return wrap(unsafeUnwrap(this).canvas);
2325
},
2426

2527
drawImage: function() {
2628
arguments[0] = unwrapIfNeeded(arguments[0]);
27-
this.impl.drawImage.apply(this.impl, arguments);
29+
unsafeUnwrap(this).drawImage.apply(unsafeUnwrap(this), arguments);
2830
},
2931

3032
createPattern: function() {
3133
arguments[0] = unwrap(arguments[0]);
32-
return this.impl.createPattern.apply(this.impl, arguments);
34+
return unsafeUnwrap(this).createPattern.apply(unsafeUnwrap(this), arguments);
3335
}
3436
});
3537

src/wrappers/CharacterData.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
var enqueueMutation = scope.enqueueMutation;
1111
var mixin = scope.mixin;
1212
var registerWrapper = scope.registerWrapper;
13+
var unsafeUnwrap = scope.unsafeUnwrap;
1314

1415
var OriginalCharacterData = window.CharacterData;
1516

@@ -25,14 +26,14 @@
2526
this.data = value;
2627
},
2728
get data() {
28-
return this.impl.data;
29+
return unsafeUnwrap(this).data;
2930
},
3031
set data(value) {
31-
var oldValue = this.impl.data;
32+
var oldValue = unsafeUnwrap(this).data;
3233
enqueueMutation(this, 'characterData', {
3334
oldValue: oldValue
3435
});
35-
this.impl.data = value;
36+
unsafeUnwrap(this).data = value;
3637
}
3738
});
3839

src/wrappers/DOMTokenList.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,43 @@
55
(function(scope) {
66
'use strict';
77

8+
var setWrapper = scope.setWrapper;
9+
var unsafeUnwrap = scope.unsafeUnwrap;
10+
811
function invalidateClass(el) {
912
scope.invalidateRendererBasedOnAttribute(el, 'class');
1013
}
1114

1215
function DOMTokenList(impl, ownerElement) {
13-
this.impl = impl;
16+
setWrapper(impl, this);
1417
this.ownerElement_ = ownerElement;
1518
}
1619

1720
DOMTokenList.prototype = {
1821
get length() {
19-
return this.impl.length;
22+
return unsafeUnwrap(this).length;
2023
},
2124
item: function(index) {
22-
return this.impl.item(index);
25+
return unsafeUnwrap(this).item(index);
2326
},
2427
contains: function(token) {
25-
return this.impl.contains(token);
28+
return unsafeUnwrap(this).contains(token);
2629
},
2730
add: function() {
28-
this.impl.add.apply(this.impl, arguments);
31+
unsafeUnwrap(this).add.apply(unsafeUnwrap(this), arguments);
2932
invalidateClass(this.ownerElement_);
3033
},
3134
remove: function() {
32-
this.impl.remove.apply(this.impl, arguments);
35+
unsafeUnwrap(this).remove.apply(unsafeUnwrap(this), arguments);
3336
invalidateClass(this.ownerElement_);
3437
},
3538
toggle: function(token) {
36-
var rv = this.impl.toggle.apply(this.impl, arguments);
39+
var rv = unsafeUnwrap(this).toggle.apply(unsafeUnwrap(this), arguments);
3740
invalidateClass(this.ownerElement_);
3841
return rv;
3942
},
4043
toString: function() {
41-
return this.impl.toString();
44+
return unsafeUnwrap(this).toString();
4245
}
4346
};
4447

0 commit comments

Comments
 (0)