-
Notifications
You must be signed in to change notification settings - Fork 15
/
jquery.microdata.json.js
50 lines (46 loc) · 1.36 KB
/
jquery.microdata.json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* -*- mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
'use strict';
// http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata.html#json
jQuery.microdata.json = function(selector, format) {
var $ = jQuery;
function getObject(item, memory) {
var $item = $(item);
var result = {};
var types = $item.itemType();
if (types.length)
result.type = $(types).toArray();
if ($item.itemId())
result.id = $item.itemId();
result.properties = {};
$item.properties().each(function(i, elem) {
var $elem = $(elem);
var value;
if ($elem.itemScope()) {
if ($.inArray(elem, memory) != -1) {
value = 'ERROR';
} else {
memory.push(item);
value = getObject(elem, memory);
memory.pop();
}
} else {
value = $elem.itemValue();
}
$.each($elem.itemProp(), function(i, prop) {
if (!result.properties[prop])
result.properties[prop] = [];
result.properties[prop].push(value);
});
});
return result;
}
var result = {};
result.items = [];
var $items = selector ? $(selector) : $(document).items();
$items.each(function(i, item) {
var $item = $(item);
if ($item.itemScope())
result.items.push(getObject(item, []));
});
return format ? format(result) : JSON.stringify(result);
};