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

Commit

Permalink
Rename the .impl property and ensure all files use unwrap as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Aug 8, 2014
1 parent c4da637 commit c4a7669
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 184 deletions.
7 changes: 4 additions & 3 deletions src/ShadowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
var getTreeScope = scope.getTreeScope;
var mixin = scope.mixin;
var oneOf = scope.oneOf;
var unsafeUnwrap = scope.unsafeUnwrap;
var unwrap = scope.unwrap;
var wrap = scope.wrap;

Expand Down Expand Up @@ -484,7 +485,7 @@
},

associateNode: function(node) {
node.impl.polymerShadowRenderer_ = this;
unsafeUnwrap(node).polymerShadowRenderer_ = this;
}
};

Expand Down Expand Up @@ -607,7 +608,7 @@
* This gets called when a node was added or removed to it.
*/
Node.prototype.invalidateShadowRenderer = function(force) {
var renderer = this.impl.polymerShadowRenderer_;
var renderer = unsafeUnwrap(this).polymerShadowRenderer_;
if (renderer) {
renderer.invalidate();
return true;
Expand Down Expand Up @@ -638,7 +639,7 @@
var renderer;
if (shadowRoot)
renderer = getRendererForShadowRoot(shadowRoot);
this.impl.polymerShadowRenderer_ = renderer;
unsafeUnwrap(this).polymerShadowRenderer_ = renderer;
if (renderer)
renderer.invalidate();
};
Expand Down
11 changes: 6 additions & 5 deletions src/querySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var HTMLCollection = scope.wrappers.HTMLCollection;
var NodeList = scope.wrappers.NodeList;
var getTreeScope = scope.getTreeScope;
var unsafeUnwrap = scope.unsafeUnwrap;
var wrap = scope.wrap;

var originalDocumentQuerySelector = document.querySelector;
Expand Down Expand Up @@ -37,7 +38,7 @@
}
result[index++] = wrappedItem;
}

return index;
}

Expand Down Expand Up @@ -98,7 +99,7 @@
// http://www.w3.org/TR/css3-selectors/#simple-selectors

function querySelectorAllFiltered (p, index, result, selector) {
var target = this.impl;
var target = unsafeUnwrap(this);
var list;
var root = getTreeScope(this).root;
if (root instanceof scope.wrappers.ShadowRoot) {
Expand All @@ -120,7 +121,7 @@

var SelectorsInterface = {
querySelector: function(selector) {
var target = this.impl;
var target = unsafeUnwrap(this);
var wrappedItem;
var root = getTreeScope(this).root;
if (root instanceof scope.wrappers.ShadowRoot) {
Expand Down Expand Up @@ -165,7 +166,7 @@
};

function getElementsByTagNameFiltered (p, index, result, localName, lowercase) {
var target = this.impl;
var target = unsafeUnwrap(this);
var list;
var root = getTreeScope(this).root;
if (root instanceof scope.wrappers.ShadowRoot) {
Expand All @@ -186,7 +187,7 @@
}

function getElementsByTagNameNSFiltered (p, index, result, ns, localName) {
var target = this.impl;
var target = unsafeUnwrap(this);
var list;
var root = getTreeScope(this).root;
if (root instanceof scope.wrappers.ShadowRoot) {
Expand Down
76 changes: 33 additions & 43 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,31 @@ window.ShadowDOMPolyfill = {};
return /^\w[a-zA-Z_0-9]*$/.test(name);
}

// The name of the implementation property is intentionally hard to
// remember. Unfortunately, browsers are slower doing obj[expr] than
// obj.foo so we resort to repeat this ugly name. This ugly name is never
// used outside of this file though.

function getGetter(name) {
return hasEval && isIdentifierName(name) ?
new Function('return this.impl.' + name) :
function() { return this.impl[name]; };
new Function('return this.__impl4cf1e782hg__.' + name) :
function() { return this.__impl4cf1e782hg__[name]; };
}

function getSetter(name) {
return hasEval && isIdentifierName(name) ?
new Function('v', 'this.impl.' + name + ' = v') :
function(v) { this.impl[name] = v; };
new Function('v', 'this.__impl4cf1e782hg__.' + name + ' = v') :
function(v) { this.__impl4cf1e782hg__[name] = v; };
}

function getMethod(name) {
return hasEval && isIdentifierName(name) ?
new Function('return this.impl.' + name +
'.apply(this.impl, arguments)') :
function() { return this.impl[name].apply(this.impl, arguments); };
new Function('return this.__impl4cf1e782hg__.' + name +
'.apply(this.__impl4cf1e782hg__, arguments)') :
function() {
return this.__impl4cf1e782hg__[name].apply(
this.__impl4cf1e782hg__, arguments);
};
}

function getDescriptor(source, name) {
Expand Down Expand Up @@ -273,41 +281,12 @@ window.ShadowDOMPolyfill = {};
return GeneratedWrapper;
}

var OriginalDOMImplementation = window.DOMImplementation;
var OriginalEventTarget = window.EventTarget;
var OriginalEvent = window.Event;
var OriginalNode = window.Node;
var OriginalWindow = window.Window;
var OriginalRange = window.Range;
var OriginalCanvasRenderingContext2D = window.CanvasRenderingContext2D;
var OriginalWebGLRenderingContext = window.WebGLRenderingContext;
var OriginalSVGElementInstance = window.SVGElementInstance;
var OriginalFormData = window.FormData;

function isWrapper(object) {
return object instanceof wrappers.EventTarget ||
object instanceof wrappers.Event ||
object instanceof wrappers.Range ||
object instanceof wrappers.DOMImplementation ||
object instanceof wrappers.CanvasRenderingContext2D ||
object instanceof wrappers.FormData ||
wrappers.WebGLRenderingContext &&
object instanceof wrappers.WebGLRenderingContext;
return object && object.__impl4cf1e782hg__;
}

function isNative(object) {
return OriginalEventTarget && object instanceof OriginalEventTarget ||
object instanceof OriginalNode ||
object instanceof OriginalEvent ||
object instanceof OriginalWindow ||
object instanceof OriginalRange ||
object instanceof OriginalDOMImplementation ||
object instanceof OriginalCanvasRenderingContext2D ||
object instanceof OriginalFormData ||
OriginalWebGLRenderingContext &&
object instanceof OriginalWebGLRenderingContext ||
OriginalSVGElementInstance &&
object instanceof OriginalSVGElementInstance;
return !isWrapper(object);
}

/**
Expand All @@ -321,8 +300,8 @@ window.ShadowDOMPolyfill = {};
return null;

assert(isNative(impl));
return impl.polymerWrapper_ ||
(impl.polymerWrapper_ = new (getWrapperConstructor(impl))(impl));
return impl.__wrapper8e3dd93a60__ ||
(impl.__wrapper8e3dd93a60__ = new (getWrapperConstructor(impl))(impl));
}

/**
Expand All @@ -334,7 +313,16 @@ window.ShadowDOMPolyfill = {};
if (wrapper === null)
return null;
assert(isWrapper(wrapper));
return wrapper.impl;
return wrapper.__impl4cf1e782hg__;
}

function unsafeUnwrap(wrapper) {
return wrapper.__impl4cf1e782hg__;
}

function setWrapper(impl, wrapper) {
wrapper.__impl4cf1e782hg__ = impl;
impl.__wrapper8e3dd93a60__ = wrapper;
}

/**
Expand Down Expand Up @@ -366,7 +354,7 @@ window.ShadowDOMPolyfill = {};
return;
assert(isNative(node));
assert(wrapper === undefined || isWrapper(wrapper));
node.polymerWrapper_ = wrapper;
node.__wrapper8e3dd93a60__ = wrapper;
}

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

function defineWrapGetter(constructor, name) {
defineGetter(constructor, name, function() {
return wrap(this.impl[name]);
return wrap(this.__impl4cf1e782hg__[name]);
});
}

Expand Down Expand Up @@ -417,6 +405,8 @@ window.ShadowDOMPolyfill = {};
scope.registerObject = registerObject;
scope.registerWrapper = register;
scope.rewrap = rewrap;
scope.setWrapper = setWrapper;
scope.unsafeUnwrap = unsafeUnwrap;
scope.unwrap = unwrap;
scope.unwrapIfNeeded = unwrapIfNeeded;
scope.wrap = wrap;
Expand Down
10 changes: 6 additions & 4 deletions src/wrappers/CanvasRenderingContext2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@

var mixin = scope.mixin;
var registerWrapper = scope.registerWrapper;
var setWrapper = scope.setWrapper;
var unsafeUnwrap = scope.unsafeUnwrap;
var unwrap = scope.unwrap;
var unwrapIfNeeded = scope.unwrapIfNeeded;
var wrap = scope.wrap;

var OriginalCanvasRenderingContext2D = window.CanvasRenderingContext2D;

function CanvasRenderingContext2D(impl) {
this.impl = impl;
setWrapper(impl, this);
}

mixin(CanvasRenderingContext2D.prototype, {
get canvas() {
return wrap(this.impl.canvas);
return wrap(unsafeUnwrap(this).canvas);
},

drawImage: function() {
arguments[0] = unwrapIfNeeded(arguments[0]);
this.impl.drawImage.apply(this.impl, arguments);
unsafeUnwrap(this).drawImage.apply(unsafeUnwrap(this), arguments);
},

createPattern: function() {
arguments[0] = unwrap(arguments[0]);
return this.impl.createPattern.apply(this.impl, arguments);
return unsafeUnwrap(this).createPattern.apply(unsafeUnwrap(this), arguments);
}
});

Expand Down
7 changes: 4 additions & 3 deletions src/wrappers/CharacterData.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var enqueueMutation = scope.enqueueMutation;
var mixin = scope.mixin;
var registerWrapper = scope.registerWrapper;
var unsafeUnwrap = scope.unsafeUnwrap;

var OriginalCharacterData = window.CharacterData;

Expand All @@ -25,14 +26,14 @@
this.data = value;
},
get data() {
return this.impl.data;
return unsafeUnwrap(this).data;
},
set data(value) {
var oldValue = this.impl.data;
var oldValue = unsafeUnwrap(this).data;
enqueueMutation(this, 'characterData', {
oldValue: oldValue
});
this.impl.data = value;
unsafeUnwrap(this).data = value;
}
});

Expand Down
19 changes: 11 additions & 8 deletions src/wrappers/DOMTokenList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@
(function(scope) {
'use strict';

var setWrapper = scope.setWrapper;
var unsafeUnwrap = scope.unsafeUnwrap;

function invalidateClass(el) {
scope.invalidateRendererBasedOnAttribute(el, 'class');
}

function DOMTokenList(impl, ownerElement) {
this.impl = impl;
setWrapper(impl, this);
this.ownerElement_ = ownerElement;
}

DOMTokenList.prototype = {
get length() {
return this.impl.length;
return unsafeUnwrap(this).length;
},
item: function(index) {
return this.impl.item(index);
return unsafeUnwrap(this).item(index);
},
contains: function(token) {
return this.impl.contains(token);
return unsafeUnwrap(this).contains(token);
},
add: function() {
this.impl.add.apply(this.impl, arguments);
unsafeUnwrap(this).add.apply(unsafeUnwrap(this), arguments);
invalidateClass(this.ownerElement_);
},
remove: function() {
this.impl.remove.apply(this.impl, arguments);
unsafeUnwrap(this).remove.apply(unsafeUnwrap(this), arguments);
invalidateClass(this.ownerElement_);
},
toggle: function(token) {
var rv = this.impl.toggle.apply(this.impl, arguments);
var rv = unsafeUnwrap(this).toggle.apply(unsafeUnwrap(this), arguments);
invalidateClass(this.ownerElement_);
return rv;
},
toString: function() {
return this.impl.toString();
return unsafeUnwrap(this).toString();
}
};

Expand Down
Loading

0 comments on commit c4a7669

Please sign in to comment.