This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from gemini-testing/feature/sizzle
Allow to use Sizzle for browsers without CSS3
- Loading branch information
Showing
14 changed files
with
406 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
var util = require('util'), | ||
q = require('q'), | ||
|
||
StateError = require('../errors/state-error'), | ||
|
||
NO_CLIENT_FUNC = 'ERRNOFUNC'; | ||
|
||
/** | ||
* @param {Browser} browser | ||
* @param {String} script | ||
* @constructor | ||
*/ | ||
function ClientBridge(browser, script) { | ||
this._browser = browser; | ||
this._script = script; | ||
} | ||
|
||
/** | ||
* @param {String} name | ||
* @param {Array} [args] | ||
*/ | ||
ClientBridge.prototype.call = function(name, args) { | ||
args = args || []; | ||
return this._callCommand(this._clientMethodCommand(name, args), true); | ||
}; | ||
|
||
/** | ||
* @param {String} command | ||
* @param {Boolean} shouldInject | ||
*/ | ||
ClientBridge.prototype._callCommand = function(command, injectAllowed) { | ||
var _this = this; | ||
return this._browser.evalScript(command) | ||
.then(function(result) { | ||
if (!result || !result.error) { | ||
return q.resolve(result); | ||
} | ||
|
||
if (result.error !== NO_CLIENT_FUNC) { | ||
return q.reject(new StateError(result.message)); | ||
} | ||
|
||
if (injectAllowed) { | ||
return _this._inject() | ||
.then(function() { | ||
return _this._callCommand(command, false); | ||
}); | ||
} | ||
return q.reject(new StateError('Unable to inject gemini client script')); | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {String} name | ||
* @param {Array} args | ||
*/ | ||
ClientBridge.prototype._clientMethodCommand = function(name, args) { | ||
var call = util.format('__gemini.%s(%s)', | ||
name, | ||
args.map(JSON.stringify).join(', ') | ||
); | ||
return this._guardClientCall(call); | ||
}; | ||
|
||
/** | ||
* @param {String} call | ||
*/ | ||
ClientBridge.prototype._guardClientCall = function(call) { | ||
return util.format( | ||
'typeof __gemini !== "undefined"? %s : {error: "%s"})', | ||
call, | ||
NO_CLIENT_FUNC | ||
); | ||
}; | ||
|
||
ClientBridge.prototype._inject = function() { | ||
return this._browser.evalScript(this._script); | ||
}; | ||
|
||
module.exports = ClientBridge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
exports.first = function(selector) { | ||
return document.querySelector(selector); | ||
}; | ||
|
||
exports.all = function(selector) { | ||
return document.querySelectorAll(selector); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
/*jshint newcap:false*/ | ||
var Sizzle = require('sizzle'); | ||
|
||
exports.first = function(selector) { | ||
var elems = Sizzle(selector + ':first'); | ||
return elems.length > 0? elems[0] : null; | ||
}; | ||
|
||
exports.all = function(selector) { | ||
return Sizzle(selector); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.