This repository has been archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Updated to v2.1 from the plugin site and fixed a potential bug. #3
Open
Craga89
wants to merge
5
commits into
jquery-archive:master
Choose a base branch
from
Craga89:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
193036b
Updated to version 2.1 released on jQuery plugins page
Craga89 e3ece8b
Removed redundant whitespace
Craga89 34c877f
If empty object is the result of the metadata detection, don't apply …
Craga89 5ab6498
Updated version number and revision number
Craga89 b8d97fc
Fixed some more whitespace consistency issues
Craga89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,26 +1,30 @@ | ||
/* | ||
* Metadata - jQuery plugin for parsing metadata from elements | ||
* | ||
* Copyright (c) 2006 John Resig, Yehuda Katz, Jörn Zaefferer, Paul McLanahan | ||
* Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan | ||
* | ||
* Dual licensed under the MIT and GPL licenses: | ||
* http://www.opensource.org/licenses/mit-license.php | ||
* http://www.gnu.org/licenses/gpl.html | ||
* | ||
* Version: 2.1 | ||
* | ||
*/ | ||
|
||
/** | ||
* Sets the type of metadata to use. Metadata is encoded in JSON, and each property | ||
* in the JSON will become a property of the element itself. | ||
* | ||
* There are three supported types of metadata storage: | ||
* There are four supported types of metadata storage: | ||
* | ||
* attr: Inside an attribute. The name parameter indicates *which* attribute. | ||
* | ||
* class: Inside the class attribute, wrapped in curly braces: { } | ||
* | ||
* elem: Inside a child element (e.g. a script tag). The | ||
* name parameter indicates *which* element. | ||
* | ||
* html5: Values are stored in data-* attributes. | ||
* | ||
* The metadata for an element is loaded the first time the element is accessed via jQuery. | ||
* | ||
|
@@ -29,21 +33,26 @@ | |
* | ||
* @name $.metadata.setType | ||
* | ||
* @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p> | ||
* @before $.metadata.setType("class") | ||
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label" | ||
* @example <p id='one' class='some_class {item_id: 1, item_label: 'Label'}'>This is a p</p> | ||
* @before $.metadata.setType('class') | ||
* @after $('#one').metadata().item_id == 1; $('#one').metadata().item_label == 'Label' | ||
* @desc Reads metadata from the class attribute | ||
* | ||
* @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p> | ||
* @before $.metadata.setType("attr", "data") | ||
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label" | ||
* @desc Reads metadata from a "data" attribute | ||
* @example <p id='one' class='some_class' data='{item_id: 1, item_label: 'Label'}'>This is a p</p> | ||
* @before $.metadata.setType('attr', 'data') | ||
* @after $('#one').metadata().item_id == 1; $('#one').metadata().item_label == 'Label' | ||
* @desc Reads metadata from a 'data' attribute | ||
* | ||
* @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p> | ||
* @before $.metadata.setType("elem", "script") | ||
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label" | ||
* @example <p id='one' class='some_class'><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p> | ||
* @before $.metadata.setType('elem', 'script') | ||
* @after $('#one').metadata().item_id == 1; $('#one').metadata().item_label == 'Label' | ||
* @desc Reads metadata from a nested script element | ||
* | ||
* @example <p id='one' class='some_class' data-item_id='1' data-item_label='Label'>This is a p</p> | ||
* @before $.metadata.setType('html5') | ||
* @after $('#one').metadata().item_id == 1; $('#one').metadata().item_label == 'Label' | ||
* @desc Reads metadata from a series of data-* attributes | ||
* | ||
* @param String type The encoding type | ||
* @param String name The name of the attribute to be used to get metadata (optional) | ||
* @cat Plugins/Metadata | ||
|
@@ -52,69 +61,108 @@ | |
* @see metadata() | ||
*/ | ||
|
||
(function($) { | ||
(function($, window, undefined) { | ||
|
||
$.extend({ | ||
metadata : { | ||
defaults : { | ||
type: 'class', | ||
name: 'metadata', | ||
cre: /({.*})/, | ||
single: 'metadata' | ||
}, | ||
setType: function( type, name ){ | ||
this.defaults.type = type; | ||
this.defaults.name = name; | ||
}, | ||
get: function( elem, opts ){ | ||
var settings = $.extend({}, this.defaults,opts), | ||
object = {}, data, match, attr; | ||
|
||
// check for empty string in single property | ||
if ( !settings.single.length ) { | ||
settings.single = 'metadata'; | ||
} | ||
|
||
// returned cached data if it already exists | ||
data = $.data(elem, settings.single); | ||
if(data) { return data; } | ||
|
||
data = '{}'; | ||
|
||
function getData(data) { | ||
if('string' !== typeof data) { | ||
return data; | ||
} | ||
|
||
$.extend({ | ||
metadata : { | ||
defaults : { | ||
type: 'class', | ||
name: 'metadata', | ||
cre: /({.*})/, | ||
single: 'metadata' | ||
}, | ||
setType: function( type, name ){ | ||
this.defaults.type = type; | ||
this.defaults.name = name; | ||
}, | ||
get: function( elem, opts ){ | ||
var settings = $.extend({},this.defaults,opts); | ||
// check for empty string in single property | ||
if ( !settings.single.length ) settings.single = 'metadata'; | ||
|
||
var data = $.data(elem, settings.single); | ||
// returned cached data if it already exists | ||
if ( data ) return data; | ||
|
||
data = "{}"; | ||
|
||
if ( settings.type == "class" ) { | ||
var m = settings.cre.exec( elem.className ); | ||
if ( m ) | ||
data = m[1]; | ||
} else if ( settings.type == "elem" ) { | ||
if( !elem.getElementsByTagName ) | ||
return undefined; | ||
var e = elem.getElementsByTagName(settings.name); | ||
if ( e.length ) | ||
data = $.trim(e[0].innerHTML); | ||
} else if ( elem.getAttribute != undefined ) { | ||
var attr = elem.getAttribute( settings.name ); | ||
if ( attr ) | ||
data = attr; | ||
if(data.indexOf('{') < 0) { | ||
data = eval('(' + data + ')'); | ||
} | ||
} | ||
|
||
function getObject(data) { | ||
if('string' === typeof data) { | ||
data = eval('(' + data + ')'); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
if(settings.type == 'html5') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using "===" as JSLint may ask? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, faster to use === |
||
$( elem.attributes ).each(function() { | ||
var name = this.nodeName; | ||
|
||
if(name.match(/^data-/)) { | ||
name = name.replace(/^data-/, ''); | ||
object[name] = getObject( this.nodeValue ); | ||
} | ||
}); | ||
} | ||
else { | ||
if(settings.type == 'class') { | ||
match = settings.cre.exec( elem.className ); | ||
if(match) { | ||
data = match[1]; | ||
} | ||
} | ||
else if(settings.type == 'elem') { | ||
if(!elem.getElementsByTagName) { return; } | ||
|
||
match = elem.getElementsByTagName( settings.name ); | ||
if(match.length) { | ||
data = $.trim(match[0].innerHTML); | ||
} | ||
} | ||
else if(undefined !== elem.getAttribute) { | ||
attr = elem.getAttribute( settings.name ); | ||
if(attr) { | ||
data = attr; | ||
} | ||
} | ||
|
||
object = getObject(data.indexOf('{') < 0 ? '{' + data + '}' : data); | ||
} | ||
|
||
if(data !== '{}') { | ||
$.data( elem, settings.single, object ); | ||
} | ||
|
||
return object; | ||
} | ||
|
||
if ( data.indexOf( '{' ) <0 ) | ||
data = "{" + data + "}"; | ||
|
||
data = eval("(" + data + ")"); | ||
|
||
$.data( elem, settings.single, data ); | ||
return data; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* Returns the metadata object for the first member of the jQuery object. | ||
* | ||
* @name metadata | ||
* @descr Returns element's metadata object | ||
* @param Object opts An object contianing settings to override the defaults | ||
* @type jQuery | ||
* @cat Plugins/Metadata | ||
*/ | ||
$.fn.metadata = function( opts ){ | ||
return $.metadata.get( this[0], opts ); | ||
}; | ||
/** | ||
* Returns the metadata object for the first member of the jQuery object. | ||
* | ||
* @name metadata | ||
* @descr Returns element's metadata object | ||
* @param Object opts An object contianing settings to override the defaults | ||
* @type jQuery | ||
* @cat Plugins/Metadata | ||
*/ | ||
$.fn.metadata = function( opts ){ | ||
return $.metadata.get( this[0], opts ); | ||
}; | ||
|
||
})(jQuery); | ||
})(jQuery, this); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a broken char here.