Permalink
Browse files

MINOR Re-added jquery.metadata plugin, updated from 2.0 to 2.1

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@92506 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 48afb22 commit 4e91eae50a5ccc2378cbed97af8cb86d8c7a0b10 @chillu chillu committed Nov 21, 2009
View
8 thirdparty/jquery-metadata/.piston.yml
@@ -0,0 +1,8 @@
+---
+format: 1
+handler:
+ commit: 0dbb5ec89ce8f493e33b83433b03d6feaec867fc
+ branch: master
+lock: false
+repository_url: git://github.com/jquery/jquery-metadata.git
+repository_class: Piston::Git::Repository
View
33 thirdparty/jquery-metadata/META.json
@@ -0,0 +1,33 @@
+{
+ "name": "jQuery-Metadata",
+ "version": 2.0,
+ "author": [
+ "John Resig <jeresig@gmail.com>",
+ "Jörn Zaefferer <joern.zaefferer@gmail.com>",
+ "Paul McLanahan <pmclanahan@gmail.com>"
+ ],
+ "abstract": "Quietly extract JSON metadata contained within DOM elements.",
+ "license": "mit, gpl",
+ "distribution_type": "extension",
+ "requires": {
+ "jQuery": 1.2
+ },
+ "build_requires": {
+ "jQuery-testrunner": 0
+ },
+ "provides": {
+ "jQuery.Metadata": {
+ "version": 2.0,
+ "file": "jquery.metadata.js"
+ }
+ },
+ "keywords": [
+ "data",
+ "utility"
+ ],
+ "meta-spec": {
+ "version": 1.3,
+ "url": "http://module-build.sourceforge.net/META-spec-v1.3.html"
+ },
+ "generated_by": "John Resig"
+}
View
20 thirdparty/jquery-metadata/README
@@ -0,0 +1,20 @@
+jQuery Metadata Plugin
+
+By John Resig
+
+This plugin is capable of silently, and automatically, extracting metadata
+from classes, random attributes, and child elements.
+
+For example, you can do:
+<li class="someclass {some: 'data'} anotherclass">...</li>
+OR
+<li data="{some:'random', json: 'data'}">...</li>
+OR
+<li><script type="data">{some:"json",data:true}</script> ...</li>
+
+The default is the first method, but you can always change it by twiddling
+the options. This means that there is at least one option here that can
+appease you.
+
+There's also a bunch of options (like loading data into a single property
+and the ability to ignore braces {}).
View
122 thirdparty/jquery-metadata/jquery.metadata.js
@@ -0,0 +1,122 @@
+/*
+ * Metadata - jQuery plugin for parsing metadata from elements
+ *
+ * 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
+ *
+ * Revision: $Id: jquery.metadata.js 4187 2007-12-16 17:15:27Z joern.zaefferer $
+ *
+ */
+
+/**
+ * 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:
+ *
+ * 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.
+ *
+ * The metadata for an element is loaded the first time the element is accessed via jQuery.
+ *
+ * As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
+ * matched by expr, then redefine the metadata type and run another $(expr) for other elements.
+ *
+ * @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"
+ * @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"><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
+ *
+ * @param String type The encoding type
+ * @param String name The name of the attribute to be used to get metadata (optional)
+ * @cat Plugins/Metadata
+ * @descr Sets the type of encoding to be used when loading metadata for the first time
+ * @type undefined
+ * @see metadata()
+ */
+
+(function($) {
+
+$.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 = "{" + 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 );
+};
+
+})(jQuery);
View
58 thirdparty/jquery-metadata/test/index.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head>
+<link rel="stylesheet" href="testsuite.css" />
+<script type="text/javascript" src="jquery.js"></script>
+<script type="text/javascript" src="testrunner.js"></script>
+<!-- include metadata plugin two times to make sure that doesn't hurt anyone -->
+<script type="text/javascript" src="../jquery.metadata.js"></script>
+<script type="text/javascript" src="../jquery.metadata.js"></script>
+<script type="text/javascript" src="test.js"></script>
+</head>
+
+<body>
+<h1>jQuery - Metadata Test Suite</h1>
+<h2 id="banner"></h2>
+<h2 id="userAgent"></h2>
+
+<div id="main" style="display: none">
+ <ol id="one">
+ <li class="test test2" data="foo:'bar', bar:'baz', arr:[1,2]">Some stuff</li>
+ <li class="test test2" data="{test:'bar', bar:'baz'}">Some stuff</li>
+ <li class="test test2" data="{zoooo:'bar', bar:{test:'baz'}}">Some stuff</li>
+ <li class="test test2" data="{number:true, stuff:[2,8]}">Some stuff</li>
+ </ol>
+
+ <ol id="two">
+ <li class="test {foo:'bar',bar:'baz',arr:[1,2]} test2">Some stuff</li>
+ <li class="{test:'bar',bar:'baz'} test test2">Some stuff</li>
+ <li class="test test2 {zoooo:'bar',bar:{test:'baz'}}">Some stuff</li>
+ <li class="some styles {number:true,stuff:[2,8]}">Some stuff</li>
+ </ol>
+
+ <ol id="three">
+ <li class="test test2"><script type="data">foo:'bar', bar:'baz', arr:[1,2]</script>Some stuff</li>
+ <li class="test test2"><script type="data">{test:'bar', bar:'baz'}</script>Some stuff</li>
+ <li class="test test2"><script type="data">{zoooo:'bar', bar:{test:'baz'}}</script>Some stuff</li>
+ <li class="test test2"><script type="data">{number:true, stuff:[2,8]}</script>Some stuff</li>
+ </ol>
+ <script type="data">test("execute script with type='data'", function() { ok( false, "Script tag with type='data' was executed!"); });</script>
+
+ <ol id="four">
+ <li class="test test2" data="foo:'bar', bar:'baz', arr:[1,2]">Some stuff</li>
+ <li class="test test2" data="{test:'bar', bar:'baz'}">Some stuff</li>
+ <li class="test test2" data="{zoooo:'bar', bar:{test:'baz'}}">Some stuff</li>
+ <li class="test test2" data="{number:true, stuff:[2,8]}">Some stuff</li>
+ </ol>
+
+ <select class="{required:true}" name="meal" id="meal">
+ <option value="">Please select...</option>
+ <option value="1">Food</option>
+ <option value="2">Milk</option>
+ </select>
+</div>
+
+<ol id="tests"></ol>
+</body>
+
+</html>
View
3,363 thirdparty/jquery-metadata/test/jquery.js
3,363 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
79 thirdparty/jquery-metadata/test/test.js
@@ -0,0 +1,79 @@
+function testData(index) {
+ var data = jQuery.metadata.get(this);
+ switch(index) {
+ case 0:
+ ok( data.foo == "bar", "Check foo property" );
+ ok( data.bar == "baz", "Check baz property" );
+ ok( data.arr[0] == 1, "Check arr[0] property" );
+ ok( data.arr[1] == 2, "Check arr[1] property" );
+ break;
+ case 1:
+ ok( data.test == "bar", "Check test property" );
+ ok( data.bar == "baz", "Check bar property" );
+ break;
+ case 2:
+ ok( data.zoooo == "bar", "Check zoooo property" );
+ ok( data.bar.test == "baz", "Check bar.test property" );
+ break;
+ case 3:
+ ok( data.number, "Check number property" );
+ ok( data.stuff[0] == 2, "Check stuff[0] property" );
+ ok( data.stuff[1] == 8, "Check stuff[1] property" );
+ break;
+ default:
+ ok( false, ["Assertion failed on index ", index, ", with data ", data].join('') );
+ }
+}
+
+// check if set can be intercepted without breaking metadata plugin
+var oldSet = jQuery.fn.set;
+jQuery.fn.set = function() {
+ ok( true, "set was interecepted" );
+ oldSet.apply(this, arguments);
+};
+
+//jQuery.meta.single = "";
+
+test("meta: type attr - from data attribute", function() {
+ expect(11);
+ jQuery.metadata.setType("attr", "data");
+ jQuery("#one li").each(testData);
+});
+
+test("meta: type class - from className", function() {
+ expect(11);
+ jQuery.metadata.setType( "class" );
+ jQuery("#two li").each(testData);
+});
+
+test("meta: children script element - get data from child script element", function() {
+ expect(11);
+ jQuery.metadata.setType( "elem", "script" );
+ jQuery("#three li").each(testData);
+});
+
+test("check if window doesn't break anything", function() {
+ jQuery(window).get();
+});
+
+test("meta: default with single data object", function() {
+ expect(11);
+ jQuery.metadata.setType("attr","data");
+ jQuery.metadata.defaults.single = "data";
+ jQuery("#four li").each(testData);
+});
+
+test("meta with select and class", function() {
+ expect(2);
+ jQuery.metadata.setType("class");
+ jQuery.metadata.single = "stuff";
+ var e = $('#meal').metadata();
+ ok( e, "data property" );
+ ok( e.required, "property on data property" );
+});
+
+test("try to add and remove classes on metadata elements", function() {
+ $("#two li").addClass("foobar").addClass("foo bar").removeClass("foobar");
+ ok( $("#two li").is(".foo"), 'Check class foo was added.' );
+ ok( $("#two li").is(".bar"), 'Check class bar was added.' );
+});
View
299 thirdparty/jquery-metadata/test/testrunner.js
@@ -0,0 +1,299 @@
+var _config = {
+ fixture: null,
+ Test: [],
+ stats: {
+ all: 0,
+ bad: 0
+ },
+ queue: [],
+ blocking: true,
+ timeout: null,
+ expected: null,
+ currentModule: null,
+ asyncTimeout: 2 // seconds for async timeout
+};
+
+$(function() {
+ $('#userAgent').html(navigator.userAgent);
+ runTest();
+});
+
+function synchronize(callback) {
+ _config.queue[_config.queue.length] = callback;
+ if(!_config.blocking) {
+ process();
+ }
+}
+
+function process() {
+ while(_config.queue.length && !_config.blocking) {
+ var call = _config.queue[0];
+ _config.queue = _config.queue.slice(1);
+ call();
+ }
+}
+
+function stop(allowFailure) {
+ _config.blocking = true;
+ var handler = allowFailure ? start : function() {
+ ok( false, "Test timed out" );
+ start();
+ };
+ _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
+}
+function start() {
+ if(_config.timeout)
+ clearTimeout(_config.timeout);
+ _config.blocking = false;
+ process();
+}
+
+function runTest() {
+ _config.blocking = false;
+ var time = new Date();
+ _config.fixture = document.getElementById('main').innerHTML;
+ reset();
+ synchronize(function() {
+ time = new Date() - time;
+ $("<div>").html(['<p class="result">Tests completed in ',
+ time, ' milliseconds.<br/>',
+ _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
+ .join(''))
+ .appendTo("body");
+ $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
+ });
+}
+
+function test(name, callback, nowait) {
+ if(_config.currentModule)
+ name = _config.currentModule + " module: " + name;
+
+ var filter = location.search.slice(1);
+ if ( filter && encodeURIComponent(name) != filter )
+ return;
+
+ synchronize(function() {
+ _config.Test = [];
+ try {
+ callback();
+ } catch(e) {
+ if( typeof console != "undefined" && console.error && console.warn ) {
+ console.error("Test " + name + " died, exception and test follows");
+ console.error(e);
+ console.warn(callback.toString());
+ }
+ _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
+ //throw e;
+ }
+ });
+ synchronize(function() {
+ reset();
+
+ // don't output pause tests
+ if(nowait) return;
+
+ if(_config.expected && _config.expected != _config.Test.length) {
+ _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
+ }
+ _config.expected = null;
+
+ var good = 0, bad = 0;
+ var ol = document.createElement("ol");
+ ol.style.display = "none";
+ var li = "", state = "pass";
+ for ( var i = 0; i < _config.Test.length; i++ ) {
+ var li = document.createElement("li");
+ li.className = _config.Test[i][0] ? "pass" : "fail";
+ li.innerHTML = _config.Test[i][1];
+ ol.appendChild( li );
+
+ _config.stats.all++;
+ if ( !_config.Test[i][0] ) {
+ state = "fail";
+ bad++;
+ _config.stats.bad++;
+ } else good++;
+ }
+
+ var li = document.createElement("li");
+ li.className = state;
+
+ var b = document.createElement("strong");
+ b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
+ b.onclick = function(){
+ var n = this.nextSibling;
+ if ( jQuery.css( n, "display" ) == "none" )
+ n.style.display = "block";
+ else
+ n.style.display = "none";
+ };
+ $(b).dblclick(function(event) {
+ var target = jQuery(event.target).filter("strong").clone();
+ if ( target.length ) {
+ target.children().remove();
+ location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text()));
+ }
+ });
+ li.appendChild( b );
+ li.appendChild( ol );
+
+ document.getElementById("tests").appendChild( li );
+ });
+}
+
+// call on start of module test to prepend name to all tests
+function module(moduleName) {
+ _config.currentModule = moduleName;
+}
+
+/**
+ * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
+ */
+function expect(asserts) {
+ _config.expected = asserts;
+}
+
+/**
+ * Resets the test setup. Useful for tests that modify the DOM.
+ */
+function reset() {
+ document.getElementById('main').innerHTML = _config.fixture;
+}
+
+/**
+ * Asserts true.
+ * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
+ */
+function ok(a, msg) {
+ _config.Test.push( [ !!a, msg ] );
+}
+
+/**
+ * Asserts that two arrays are the same
+ */
+function isSet(a, b, msg) {
+ var ret = true;
+ if ( a && b && a.length != undefined && a.length == b.length ) {
+ for ( var i = 0; i < a.length; i++ )
+ if ( a[i] != b[i] )
+ ret = false;
+ } else
+ ret = false;
+ if ( !ret )
+ _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
+ else
+ _config.Test.push( [ ret, msg ] );
+}
+
+/**
+ * Asserts that two objects are equivalent
+ */
+function isObj(a, b, msg) {
+ var ret = true;
+
+ if ( a && b ) {
+ for ( var i in a )
+ if ( a[i] != b[i] )
+ ret = false;
+
+ for ( i in b )
+ if ( a[i] != b[i] )
+ ret = false;
+ } else
+ ret = false;
+
+ _config.Test.push( [ ret, msg ] );
+}
+
+function serialArray( a ) {
+ var r = [];
+
+ if ( a && a.length )
+ for ( var i = 0; i < a.length; i++ ) {
+ var str = a[i].nodeName;
+ if ( str ) {
+ str = str.toLowerCase();
+ if ( a[i].id )
+ str += "#" + a[i].id;
+ } else
+ str = a[i];
+ r.push( str );
+ }
+
+ return "[ " + r.join(", ") + " ]"
+}
+
+/**
+ * Returns an array of elements with the given IDs, eg.
+ * @example q("main", "foo", "bar")
+ * @result [<div id="main">, <span id="foo">, <input id="bar">]
+ */
+function q() {
+ var r = [];
+ for ( var i = 0; i < arguments.length; i++ )
+ r.push( document.getElementById( arguments[i] ) );
+ return r;
+}
+
+/**
+ * Asserts that a select matches the given IDs
+ * @example t("Check for something", "//[a]", ["foo", "baar"]);
+ * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
+ */
+function t(a,b,c) {
+ var f = jQuery(b);
+ var s = "";
+ for ( var i = 0; i < f.length; i++ )
+ s += (s && ",") + '"' + f[i].id + '"';
+ isSet(f, q.apply(q,c), a + " (" + b + ")");
+}
+
+/**
+ * Add random number to url to stop IE from caching
+ *
+ * @example url("data/test.html")
+ * @result "data/test.html?10538358428943"
+ *
+ * @example url("data/test.php?foo=bar")
+ * @result "data/test.php?foo=bar&10538358345554"
+ */
+function url(value) {
+ return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
+}
+
+/**
+ * Checks that the first two arguments are equal, with an optional message.
+ * Prints out both expected and actual values on failure.
+ *
+ * Prefered to ok( expected == actual, message )
+ *
+ * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
+ *
+ * @param Object expected
+ * @param Object actual
+ * @param String message (optional)
+ */
+function equals(expected, actual, message) {
+ var result = expected == actual;
+ message = message || (result ? "okay" : "failed");
+ _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
+}
+
+/**
+ * Trigger an event on an element.
+ *
+ * @example triggerEvent( document.body, "click" );
+ *
+ * @param DOMElement elem
+ * @param String type
+ */
+function triggerEvent( elem, type, event ) {
+ if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
+ event = document.createEvent("MouseEvents");
+ event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
+ 0, 0, 0, 0, 0, false, false, false, false, 0, null);
+ elem.dispatchEvent( event );
+ } else if ( jQuery.browser.msie ) {
+ elem.fireEvent("on"+type);
+ }
+}
View
12 thirdparty/jquery-metadata/test/testsuite.css
@@ -0,0 +1,12 @@
+body, div, h1 { font-family: 'trebuchet ms', verdana, arial; margin: 0; padding: 0 }
+body {font-size: 10pt; }
+h1 { padding: 15px; font-size: large; background-color: #06b; color: white; }
+h2 { padding: 10px; background-color: #eee; color: black; margin: 0; font-size: small; font-weight: normal }
+
+.pass { color: green; }
+.fail { color: red; }
+p.result { margin-left: 1em; }
+
+#banner { height: 2em; border-bottom: 1px solid white; }
+h2.pass { background-color: green; }
+h2.fail { background-color: red; }

0 comments on commit 4e91eae

Please sign in to comment.