2 * Metadata - jQuery plugin for parsing metadata from elements
4 * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
10 * Revision: $Id: jquery.metadata.js 3640 2007-10-11 18:34:38Z pmclanahan $
15 * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
16 * in the JSON will become a property of the element itself.
18 * There are four supported types of metadata storage:
20 * attr: Inside an attribute. The name parameter indicates *which* attribute.
22 * class: Inside the class attribute, wrapped in curly braces: { }
24 * elem: Inside a child element (e.g. a script tag). The
25 * name parameter indicates *which* element.
26 * html5: Values are stored in data-* attributes.
28 * The metadata for an element is loaded the first time the element is accessed via jQuery.
30 * As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
31 * matched by expr, then redefine the metadata type and run another $(expr) for other elements.
33 * @name $.metadata.setType
35 * @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
36 * @before $.metadata.setType("class")
37 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
38 * @desc Reads metadata from the class attribute
40 * @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
41 * @before $.metadata.setType("attr", "data")
42 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
43 * @desc Reads metadata from a "data" attribute
45 * @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
46 * @before $.metadata.setType("elem", "script")
47 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
48 * @desc Reads metadata from a nested script element
50 * @example <p id="one" class="some_class" data-item_id="1" data-item_label="Label">This is a p</p>
51 * @before $.metadata.setType("html5")
52 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
53 * @desc Reads metadata from a series of data-* attributes
55 * @param String type The encoding type
56 * @param String name The name of the attribute to be used to get metadata (optional)
57 * @cat Plugins/Metadata
58 * @descr Sets the type of encoding to be used when loading metadata for the first time
73 setType: function( type, name ){
74 this.defaults.type = type;
75 this.defaults.name = name;
77 get: function( elem, opts ){
78 var settings = $.extend({},this.defaults,opts);
79 // check for empty string in single property
80 if ( !settings.single.length ) settings.single = 'metadata';
82 var data = $.data(elem, settings.single);
83 // returned cached data if it already exists
84 if ( data ) return data;
88 var getData = function(data) {
89 if(typeof data != "string") return data;
91 if( data.indexOf('{') < 0 ) {
92 data = eval("(" + data + ")");
96 var getObject = function(data) {
97 if(typeof data != "string") return data;
99 data = eval("(" + data + ")");
103 if ( settings.type == "html5" ) {
105 $( elem.attributes ).each(function() {
106 var name = this.nodeName;
107 if(name.match(/^data-/)) name = name.replace(/^data-/, '');
109 object[name] = getObject(this.nodeValue);
112 if ( settings.type == "class" ) {
113 var m = settings.cre.exec( elem.className );
116 } else if ( settings.type == "elem" ) {
117 if( !elem.getElementsByTagName ) return;
118 var e = elem.getElementsByTagName(settings.name);
120 data = $.trim(e[0].innerHTML);
121 } else if ( elem.getAttribute != undefined ) {
122 var attr = elem.getAttribute( settings.name );
126 object = getObject(data.indexOf("{") < 0 ? "{" + data + "}" : data);
129 $.data( elem, settings.single, object );
136 * Returns the metadata object for the first member of the jQuery object.
139 * @descr Returns element's metadata object
140 * @param Object opts An object contianing settings to override the defaults
142 * @cat Plugins/Metadata
144 $.fn.metadata = function( opts ){
145 return $.metadata.get( this[0], opts );