Skip to content

Commit

Permalink
fix `The toString function should return native string for overridden…
Browse files Browse the repository at this point in the history
… descriptor accessors` (close DevExpress#1713)
  • Loading branch information
LavrovArtem committed Aug 8, 2018
1 parent 55c3d75 commit 1dc43df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/client/sandbox/native-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class NativeMethods {
const documentCookieDescriptor = win.Object.getOwnPropertyDescriptor(win[this.documentCookiePropOwnerName].prototype, 'cookie');

// TODO: remove this condition after the GH-1649 fix
if (documentCookieDescriptor.get.toString().indexOf('native code') === -1) {
if (documentCookieDescriptor.get.toString().indexOf('native code') === -1 ||
documentCookieDescriptor.get.toString.toString().indexOf('native code') === -1) {
try {
const parentNativeMethods = win.parent['%hammerhead%'].nativeMethods;

Expand Down
14 changes: 12 additions & 2 deletions src/client/utils/property-overriding.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import nativeMethods from '../sandbox/native-methods';

function replaceNativeAccessor (descriptor, accessorName, newAccessor) {
if (newAccessor && descriptor[accessorName]) {
const stringifiedNativeAccessor = descriptor[accessorName].toString();

newAccessor.toString = () => stringifiedNativeAccessor;
}

descriptor[accessorName] = newAccessor;
}

export function createOverriddenDescriptor (obj, prop, { getter, setter }) {
const descriptor = nativeMethods.objectGetOwnPropertyDescriptor.call(window.Object, obj, prop);

if (getter !== null)
descriptor.get = getter;
replaceNativeAccessor(descriptor, 'get', getter);

if (setter !== null)
descriptor.set = setter;
replaceNativeAccessor(descriptor, 'set', setter);

return descriptor;
}
Expand Down
9 changes: 9 additions & 0 deletions test/client/fixtures/sandbox/node/attributes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,3 +1088,12 @@ test('Url resolving in an instance of document.implementation (GH-1673)', functi

strictEqual(anchorEl.href, 'http://localhost:1993/some/page.html');
});

test('The toString function should return native string for overridden descriptor accessors (GH-1713)', function () {
ok(HTMLElement.prototype.__lookupGetter__('firstChild').toString().indexOf('[native code]') !== -1);
ok(HTMLElement.prototype.__lookupGetter__('lastChild').toString().indexOf('[native code]') !== -1);
ok(HTMLAnchorElement.prototype.__lookupGetter__('port').toString().indexOf('[native code]') !== -1);
ok(HTMLAnchorElement.prototype.__lookupSetter__('port').toString().indexOf('[native code]') !== -1);
ok(HTMLAnchorElement.prototype.__lookupGetter__('pathname').toString().indexOf('[native code]') !== -1);
ok(HTMLAnchorElement.prototype.__lookupSetter__('pathname').toString().indexOf('[native code]') !== -1);
});

0 comments on commit 1dc43df

Please sign in to comment.