Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
Tidy up option sanitization code
Browse files Browse the repository at this point in the history
  • Loading branch information
Craga89 committed Aug 11, 2012
1 parent f77eb90 commit f4c69d3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 103 deletions.
2 changes: 1 addition & 1 deletion dist/basic/jquery.qtip.css
@@ -1,4 +1,4 @@
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-06
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-11
* http://craigsworks.com/projects/qtip2/
* Copyright (c) 2012 Craig Michael Thompson; Licensed MIT, GPL */

Expand Down
68 changes: 19 additions & 49 deletions dist/basic/jquery.qtip.js
@@ -1,4 +1,4 @@
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-06
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-11
* http://craigsworks.com/projects/qtip2/
* Copyright (c) 2012 Craig Michael Thompson; Licensed MIT, GPL */

Expand Down Expand Up @@ -77,79 +77,49 @@
// Option object sanitizer
function sanitizeOptions(opts)
{
var content;
var invalid = function(a) { return a === NULL || 'object' !== typeof a; },
invalidContent = function(c) { return $.isFunction(c) && ((!c && !c.attr) || c.length < 1 || ('object' === typeof c && !c.jquery)); };

if(!opts || 'object' !== typeof opts) { return FALSE; }

if(opts.metadata === NULL || 'object' !== typeof opts.metadata) {
opts.metadata = {
type: opts.metadata
};
if(invalid(opts.metadata)) {
opts.metadata = { type: opts.metadata };
}

if('content' in opts) {
if(opts.content === NULL || 'object' !== typeof opts.content || opts.content.jquery) {
opts.content = {
text: opts.content
};
if(invalid(opts.content) || opts.content.jquery) {
opts.content = { text: opts.content };
}

content = opts.content.text || FALSE;
if(!$.isFunction(content) && ((!content && !content.attr) || content.length < 1 || ('object' === typeof content && !content.jquery))) {
if(!invalidContent(opts.content.text || FALSE)) {
opts.content.text = FALSE;
}

if('title' in opts.content) {
if(opts.content.title === NULL || 'object' !== typeof opts.content.title) {
opts.content.title = {
text: opts.content.title
};
if(invalid(opts.content.title)) {
opts.content.title = { text: opts.content.title };
}

content = opts.content.title.text || FALSE;
if(!$.isFunction(content) && ((!content && !content.attr) || content.length < 1 || ('object' === typeof content && !content.jquery))) {
if(!invalidContent(opts.content.title.text || FALSE)) {
opts.content.title.text = FALSE;
}
}
}

if('position' in opts) {
if(opts.position === NULL || 'object' !== typeof opts.position) {
opts.position = {
my: opts.position,
at: opts.position
};
}
if('position' in opts && invalid(opts.position)) {
opts.position = { my: opts.position, at: opts.position };
}

if('show' in opts) {
if(opts.show === NULL || 'object' !== typeof opts.show) {
if(opts.show.jquery) {
opts.show = { target: opts.show };
}
else {
opts.show = { event: opts.show };
}
}
if('show' in opts && invalid(opts.show)) {
opts.show = opts.show.jquery ? { target: opts.show } : { event: opts.show };
}

if('hide' in opts) {
if(opts.hide === NULL || 'object' !== typeof opts.hide) {
if(opts.hide.jquery) {
opts.hide = { target: opts.hide };
}
else {
opts.hide = { event: opts.hide };
}
}
if('hide' in opts && invalid(opts.hide)) {
opts.hide = opts.hide.jquery ? { target: opts.hide } : { event: opts.hide };
}

if('style' in opts) {
if(opts.style === NULL || 'object' !== typeof opts.style) {
opts.style = {
classes: opts.style
};
}
if('style' in opts && invalid(opts.style)) {
opts.style = { classes: opts.style };
}

// Sanitize plugin options
Expand Down
2 changes: 1 addition & 1 deletion dist/basic/jquery.qtip.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/jquery.qtip.css
@@ -1,4 +1,4 @@
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-06
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-11
* http://craigsworks.com/projects/qtip2/
* Copyright (c) 2012 Craig Michael Thompson; Licensed MIT, GPL */

Expand Down
68 changes: 19 additions & 49 deletions dist/jquery.qtip.js
@@ -1,4 +1,4 @@
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-06
/*! qTip2 - Pretty powerful tooltips - v2.0.0 - 2012-08-11
* http://craigsworks.com/projects/qtip2/
* Copyright (c) 2012 Craig Michael Thompson; Licensed MIT, GPL */

Expand Down Expand Up @@ -77,79 +77,49 @@
// Option object sanitizer
function sanitizeOptions(opts)
{
var content;
var invalid = function(a) { return a === NULL || 'object' !== typeof a; },
invalidContent = function(c) { return $.isFunction(c) && ((!c && !c.attr) || c.length < 1 || ('object' === typeof c && !c.jquery)); };

if(!opts || 'object' !== typeof opts) { return FALSE; }

if(opts.metadata === NULL || 'object' !== typeof opts.metadata) {
opts.metadata = {
type: opts.metadata
};
if(invalid(opts.metadata)) {
opts.metadata = { type: opts.metadata };
}

if('content' in opts) {
if(opts.content === NULL || 'object' !== typeof opts.content || opts.content.jquery) {
opts.content = {
text: opts.content
};
if(invalid(opts.content) || opts.content.jquery) {
opts.content = { text: opts.content };
}

content = opts.content.text || FALSE;
if(!$.isFunction(content) && ((!content && !content.attr) || content.length < 1 || ('object' === typeof content && !content.jquery))) {
if(!invalidContent(opts.content.text || FALSE)) {
opts.content.text = FALSE;
}

if('title' in opts.content) {
if(opts.content.title === NULL || 'object' !== typeof opts.content.title) {
opts.content.title = {
text: opts.content.title
};
if(invalid(opts.content.title)) {
opts.content.title = { text: opts.content.title };
}

content = opts.content.title.text || FALSE;
if(!$.isFunction(content) && ((!content && !content.attr) || content.length < 1 || ('object' === typeof content && !content.jquery))) {
if(!invalidContent(opts.content.title.text || FALSE)) {
opts.content.title.text = FALSE;
}
}
}

if('position' in opts) {
if(opts.position === NULL || 'object' !== typeof opts.position) {
opts.position = {
my: opts.position,
at: opts.position
};
}
if('position' in opts && invalid(opts.position)) {
opts.position = { my: opts.position, at: opts.position };
}

if('show' in opts) {
if(opts.show === NULL || 'object' !== typeof opts.show) {
if(opts.show.jquery) {
opts.show = { target: opts.show };
}
else {
opts.show = { event: opts.show };
}
}
if('show' in opts && invalid(opts.show)) {
opts.show = opts.show.jquery ? { target: opts.show } : { event: opts.show };
}

if('hide' in opts) {
if(opts.hide === NULL || 'object' !== typeof opts.hide) {
if(opts.hide.jquery) {
opts.hide = { target: opts.hide };
}
else {
opts.hide = { event: opts.hide };
}
}
if('hide' in opts && invalid(opts.hide)) {
opts.hide = opts.hide.jquery ? { target: opts.hide } : { event: opts.hide };
}

if('style' in opts) {
if(opts.style === NULL || 'object' !== typeof opts.style) {
opts.style = {
classes: opts.style
};
}
if('style' in opts && invalid(opts.style)) {
opts.style = { classes: opts.style };
}

// Sanitize plugin options
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.qtip.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/core.js
Expand Up @@ -2,7 +2,7 @@
function sanitizeOptions(opts)
{
var invalid = function(a) { return a === NULL || 'object' !== typeof a; },
invalidContent = function(c) { $.isFunction(c) && ((!c && !c.attr) || c.length < 1 || ('object' === typeof c && !c.jquery)) };
invalidContent = function(c) { return $.isFunction(c) && ((!c && !c.attr) || c.length < 1 || ('object' === typeof c && !c.jquery)); };

if(!opts || 'object' !== typeof opts) { return FALSE; }

Expand Down

0 comments on commit f4c69d3

Please sign in to comment.