Skip to content

Commit

Permalink
Replace util.getGlobal() with global
Browse files Browse the repository at this point in the history
Let browserify do the work of giving us the window object and avoid
using anonymous code evaluation.
  • Loading branch information
tilgovi committed Jul 1, 2015
1 parent f57907d commit 0016813
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 53 deletions.
8 changes: 3 additions & 5 deletions browser.js
Expand Up @@ -22,23 +22,21 @@ exports.util = util;
// Ext namespace (for core-provided extension modules)
exports.ext = {};

var g = util.getGlobal();

// If wicked-good-xpath is available, install it. This will not overwrite any
// native XPath functionality.
var wgxpath = g.wgxpath;
var wgxpath = global.wgxpath;
if (typeof wgxpath !== "undefined" &&
wgxpath !== null &&
typeof wgxpath.install === "function") {
wgxpath.install();
}

// Store a reference to the current annotator object, if one exists.
var _annotator = g.annotator;
var _annotator = global.annotator;

// Restores the Annotator property on the global object to it's
// previous value and returns the Annotator.
exports.noConflict = function noConflict() {
g.annotator = _annotator;
global.annotator = _annotator;
return this;
};
6 changes: 3 additions & 3 deletions src/ext/document.js
Expand Up @@ -24,7 +24,7 @@ function isEmpty(obj) {

// absoluteUrl turns a possibly relative URL into an absolute one
function absoluteUrl(url) {
var d = util.getGlobal().document.createElement('a');
var d = global.document.createElement('a');
d.href = url;
return d.href;
}
Expand Down Expand Up @@ -112,13 +112,13 @@ function getTitle(d, keys) {
}

// Fall back to document title
return util.getGlobal().document.title;
return global.document.title;
}


function getLinks() {
// we know our current location is a link for the document
var results = [{href: util.getGlobal().document.location.href}];
var results = [{href: global.document.location.href}];

// look for some relevant link relations
$("link").each(function (_, link) {
Expand Down
2 changes: 1 addition & 1 deletion src/ext/unsupported.js
Expand Up @@ -15,7 +15,7 @@ var _t = annotator.util.gettext;
*/
function checkSupport(scope) {
if (typeof scope === 'undefined' || scope === null) {
scope = annotator.util.getGlobal();
scope = annotator.global;
}

var errors = [];
Expand Down
2 changes: 1 addition & 1 deletion src/notification.js
Expand Up @@ -61,7 +61,7 @@ function banner(message, severity) {
.addClass(bannerClasses.show)
.addClass(bannerClasses[severity])
.html(util.escapeHtml(message || ""))
.appendTo(util.getGlobal().document.body);
.appendTo(global.document.body);

$(element).on('click', close);

Expand Down
2 changes: 1 addition & 1 deletion src/ui/highlighter.js
Expand Up @@ -31,7 +31,7 @@ function highlightRange(normedRange, cssClass) {
for (var i = 0, len = nodes.length; i < len; i++) {
var node = nodes[i];
if (!white.test(node.nodeValue)) {
var hl = util.getGlobal().document.createElement('span');
var hl = global.document.createElement('span');
hl.className = cssClass;
node.parentNode.replaceChild(hl, node);
hl.appendChild(node);
Expand Down
7 changes: 3 additions & 4 deletions src/ui/main.js
Expand Up @@ -9,8 +9,7 @@ var highlighter = require('./highlighter');
var textselector = require('./textselector');
var viewer = require('./viewer');

var g = util.getGlobal(),
_t = util.gettext;
var _t = util.gettext;


// trim strips whitespace from either end of a string.
Expand Down Expand Up @@ -76,7 +75,7 @@ function injectDynamicStyle() {
':not(annotator-filter)';

// use the maximum z-index in the page
var max = maxZIndex(util.$(g.document.body).find(sel).get());
var max = maxZIndex(util.$(global.document.body).find(sel).get());

// but don't go smaller than 1010, because this isn't bulletproof --
// dynamic elements in the page (notifications, dialogs, etc.) may well
Expand Down Expand Up @@ -211,7 +210,7 @@ function main(options) {
options = {};
}

options.element = options.element || util.getGlobal().document.body;
options.element = options.element || global.document.body;
options.editorExtensions = options.editorExtensions || [];
options.viewerExtensions = options.viewerExtensions || [];

Expand Down
7 changes: 3 additions & 4 deletions src/ui/markdown.js
Expand Up @@ -3,7 +3,6 @@

var util = require('../util');

var g = util.getGlobal();
var _t = util.gettext;


Expand All @@ -19,8 +18,8 @@ var _t = util.gettext;
var render = exports.render = function render(annotation) {
var convert = util.escapeHtml;

if (g.Showdown && typeof g.Showdown.converter === 'function') {
convert = new g.Showdown.converter().makeHtml;
if (global.Showdown && typeof global.Showdown.converter === 'function') {
convert = new global.Showdown.converter().makeHtml;
}

if (annotation.text) {
Expand Down Expand Up @@ -48,7 +47,7 @@ var render = exports.render = function render(annotation) {
* });
*/
exports.viewerExtension = function viewerExtension(viewer) {
if (!g.Showdown || typeof g.Showdown.converter !== 'function') {
if (!global.Showdown || typeof global.Showdown.converter !== 'function') {
console.warn(_t("To use the Markdown plugin, you must " +
"include Showdown into the page first."));
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/textselector.js
Expand Up @@ -59,7 +59,7 @@ TextSelector.prototype.captureDocumentSelection = function () {
len,
ranges = [],
rangesToIgnore = [],
selection = util.getGlobal().getSelection();
selection = global.getSelection();

if (selection.isCollapsed) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion src/ui/widget.js
Expand Up @@ -86,7 +86,7 @@ Widget.prototype.isShown = function () {
Widget.prototype.checkOrientation = function () {
this.resetOrientation();

var $win = $(util.getGlobal()),
var $win = $(global),
$widget = this.element.children(":first"),
offset = $widget.offset(),
viewport = {
Expand Down
18 changes: 3 additions & 15 deletions src/util.js
Expand Up @@ -22,21 +22,10 @@ function escapeHtml(string) {
}


// getGlobal returns the global object (window in a browser, the global
// namespace object in Node, etc.)
function getGlobal() {
// jshint -W054
return new Function('return this')();
// jshint +W054
}


// I18N
var gettext = (function () {
var g = getGlobal();

if (typeof g.Gettext === 'function') {
var _gettext = new g.Gettext({domain: "annotator"});
if (typeof global.Gettext === 'function') {
var _gettext = new global.Gettext({domain: "annotator"});
return function (msgid) { return _gettext.gettext(msgid); };
}

Expand All @@ -48,7 +37,7 @@ var gettext = (function () {
// corner of the page (taking into account padding/margin/border on the body
// element as necessary).
function mousePosition(event) {
var body = getGlobal().document.body;
var body = global.document.body;
var offset = {top: 0, left: 0};

if ($(body).css('position') !== "static") {
Expand All @@ -66,5 +55,4 @@ exports.$ = $;
exports.Promise = Promise;
exports.gettext = gettext;
exports.escapeHtml = escapeHtml;
exports.getGlobal = getGlobal;
exports.mousePosition = mousePosition;
2 changes: 1 addition & 1 deletion test/spec/ui/adder_spec.js
Expand Up @@ -132,7 +132,7 @@ describe('ui.adder.Adder', function () {
});

it("hides the adder when the button is left-clicked", function () {
$(util.getGlobal().document.body).trigger('mouseup');
$(global.document.body).trigger('mouseup');
a.element.find('button').trigger({
type: 'click',
which: 1
Expand Down
10 changes: 4 additions & 6 deletions test/spec/ui/markdown_spec.js
Expand Up @@ -3,8 +3,6 @@ var assert = require('assertive-chai').assert;
var markdown = require('../../../src/ui/markdown'),
util = require('../../../src/util');

var g = util.getGlobal();

describe('ui.markdown.render', function () {
var sandbox;
var makeHtml;
Expand All @@ -14,7 +12,7 @@ describe('ui.markdown.render', function () {
sandbox.stub(util, 'escapeHtml').returns('escaped');
makeHtml = sandbox.stub().returns('converted');

g.Showdown = {
global.Showdown = {
converter: function () {
return {makeHtml: makeHtml};
}
Expand All @@ -36,7 +34,7 @@ describe('ui.markdown.render', function () {
});

it("should HTML escape text if Showdown is not available", function () {
g.Showdown = null;
global.Showdown = null;

assert.equal('escaped', markdown.render({text: 'foo'}));
sinon.assert.calledWith(util.escapeHtml, 'foo');
Expand All @@ -53,7 +51,7 @@ describe('ui.markdown.viewerExtension', function () {
mockViewer = {
setRenderer: sandbox.stub()
};
g.Showdown = {
global.Showdown = {
converter: function () {
return {makeHtml: sandbox.stub()};
}
Expand All @@ -66,7 +64,7 @@ describe('ui.markdown.viewerExtension', function () {

it("should log a warning if Showdown is not present in the page", function () {
sandbox.stub(console, 'warn');
g.Showdown = null;
global.Showdown = null;

markdown.viewerExtension(mockViewer);

Expand Down
20 changes: 10 additions & 10 deletions test/spec/ui/textselector_spec.js
Expand Up @@ -38,11 +38,11 @@ describe('ui.textselector.TextSelector', function () {
beforeEach(function () {
var mockSelection;
mockSelection = new h.MockSelection(h.fix(), ['/div/p', 0, '/div/p', 1, 'Hello world!', '--']);
sinon.stub(util.getGlobal(), 'getSelection').returns(mockSelection);
sinon.stub(global, 'getSelection').returns(mockSelection);
});

afterEach(function () {
util.getGlobal().getSelection.restore();
global.getSelection.restore();
});

it("should capture and normalise the current document selections", function () {
Expand All @@ -64,41 +64,41 @@ describe('ui.textselector.TextSelector', function () {
};
mockSelection = new h.MockSelection(h.fix(), ['/div/p', 0, '/div/p', 1, 'Hello world!', '--']);
sinon.stub(util, 'mousePosition').returns(mockOffset);
sinon.stub(util.getGlobal(), 'getSelection').returns(mockSelection);
sinon.stub(global, 'getSelection').returns(mockSelection);
});

afterEach(function () {
util.mousePosition.restore();
util.getGlobal().getSelection.restore();
global.getSelection.restore();
});

it("should receive the selected ranges when a selection is made", function () {
$(util.getGlobal().document.body).trigger('mouseup');
$(global.document.body).trigger('mouseup');
assert.equal(selections.length, 1);
var s = selections[0];
assert.equal(s.ranges[0].text(), 'Hello world!');
});

it("should receive the triggering event object when a selection is made", function () {
$(util.getGlobal().document.body).trigger('mouseup');
$(global.document.body).trigger('mouseup');
assert.equal(selections.length, 1);
var s = selections[0];
assert.equal(s.event.type, 'mouseup');
});

it("should be called with empty ranges if an empty selection is made", function () {
mockSelection.removeAllRanges();
$(util.getGlobal().document.body).trigger('mouseup');
$(global.document.body).trigger('mouseup');
assert.equal(selections.length, 1);
assert.deepEqual(selections[0].ranges, []);
});

it("should be called with empty ranges if the selection is of an Annotator element", function () {
// Set the selection to a div which has the 'annotator-adder' class set
util.getGlobal().getSelection.restore();
global.getSelection.restore();
mockSelection = new h.MockSelection(h.fix(), ['/div/div/p', 0, '/div/div/p', 1, 'Part of the Annotator UI.', '--']);
sinon.stub(util.getGlobal(), 'getSelection').returns(mockSelection);
$(util.getGlobal().document.body).trigger('mouseup');
sinon.stub(global, 'getSelection').returns(mockSelection);
$(global.document.body).trigger('mouseup');
assert.equal(selections.length, 1);
assert.deepEqual(selections[0].ranges, []);
});
Expand Down

0 comments on commit 0016813

Please sign in to comment.