Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad console assignment #241

Merged
merged 1 commit into from
Apr 21, 2021
Merged

Fix bad console assignment #241

merged 1 commit into from
Apr 21, 2021

Conversation

Krinkle
Copy link
Contributor

@Krinkle Krinkle commented Apr 11, 2021

Follows-up 826d2c7.

Before that commit, the reason global "console" was globbered is that the
var console part of the inner var console = {} assignment was hosted by
the JavaScript engine. Thus the variable always existed in the local scope as
type "undefined", and so the conditional check never saw the real console, and
always created a custom one. Thus it always deleted the original console reference,
as well as any other it contained.

In commit 826d2c7, this was incorrectly fixed by assigning the unchecked
expression referring to console, thus no longer having a fallback to {} in older
browsers, because an unchecked reference like that throws an Uncaught ReferenceError.

As a result, browserstack-runner was unable to run in IE 9 or older.

Fixes #161.
Fixes #164.
Fixes #212.

Follows-up 826d2c7.

Before that commit, the reason global "console" was globbered is that the
`var console` part of the inner `var console = {}` assignment was hosted by
the JavaScript engine. Thus the variable always existed in the local scope as
type "undefined", and so the conditional check never saw the real console, and
always created a custom one. Thus it always deleted the original console reference,
as well as any other it contained.

In commit 826d2c7, this was incorrectly fixed by assigning the unchecked
expression referring to `console`, thus no longer having a fallback to `{}` in older
browsers, because an unchecked reference like that throws an Uncaught ReferenceError.

As a result, browserstack-runner was unable to run in IE 9 or older.

Fixes browserstack#161.
Fixes browserstack#164.
Fixes browserstack#212.
Krinkle added a commit to qunitjs/browserstack-runner that referenced this pull request Apr 11, 2021
Follows-up 826d2c7.

Before that commit, the reason global "console" was globbered is that the
`var console` part of the inner `var console = {}` assignment was hosted by
the JavaScript engine. Thus the variable always existed in the local scope as
type "undefined", and so the conditional check never saw the real console, and
always created a custom one. Thus it always deleted the original console reference,
as well as any other it contained.

In commit 826d2c7, this was incorrectly fixed by assigning the unchecked
expression referring to `console`, thus no longer having a fallback to `{}` in older
browsers, because an unchecked reference like that throws an Uncaught ReferenceError.

As a result, browserstack-runner was unable to run in IE 9 or older.

Fixes browserstack#161.
Fixes browserstack#164.
Fixes browserstack#212.
Ref browserstack#241.
// This must not replace the console object itself so that other console methods
// from the browser or added by the tested application remain unaffected.
// https://github.com/browserstack/browserstack-runner/pull/199
var browserstack_console = window.console || {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we copy the prototype and update the concerned method only so that we do not lose over other methods as well as retain the current logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't console and window.console in browser are same.
image

Copy link
Contributor Author

@Krinkle Krinkle Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in JavaScript the global scope of variables is reflected into an object called window where each global variable is a property. There are some exceptions, but in general, yes, when a variable actually exists and is defined as global, then foo === window.foo.

The problem, and the reason this code exists, is with when the variable does not exist, such as in old Internet Explorer.

Try this one:

window.thisdontexist
//> undefined
thisdontexist
//> Uncaught ReferenceError: thisdontexist is not defined

Previously this code used console. Which means that since the release of 826d2c7, browserstack-runner was completely broken in IE6-8, because after this ReferenceError: console is not defined, the program is terminated, the callback never installed, the worker unreachable, and the test results never arrive / timeout.

var x = window.console || {};
// Modern browser > Console
// IE 6-8         > {}
var x = console || {};
// Modern browser > Console
// IE 6-8         > Uncaught ReferenceError: console is not defined / Program terminated.

@francisf francisf merged commit d76b3df into browserstack:master Apr 21, 2021
@Krinkle Krinkle deleted the patch-1 branch April 21, 2021 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants