-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.microdata.json.js
109 lines (88 loc) · 2.95 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*- 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;
var memo_scope = [];
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);
memo_scope.push(elem);
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);
});
});
var add_items = $item.sub_items().not($item.properties()).not($(memo_scope));
add_items.each(function(i, elem) {
var $elem = $(elem);
var value;
//only unlinked itemscope checked
if (!$elem.itemScope())
return;
if ($.inArray(elem, memo_scope) != -1)
return;
if ($.inArray(elem, memory) != -1) {
value = 'ERROR';
} else {
memory.push(item);
memo_scope.push(elem);
value = getObject(elem, memory);
memory.pop();
}
//try fix unnamed items
if ($elem.itemProp().length==0) {
var id = ":unnamed";
if (!result.properties[id])
result.properties[id] = [];
result.properties[id].push(value);
} else {
$.each($elem.itemProp(), function(i, prop) {
if (!result.properties[prop])
result.properties[prop] = [];
result.properties[prop].push(value);
});
}
});
return result;
}
function processItemScope($items, result) {
$items.each(function(i, item) {
var $item = $(item);
if ($item.itemScope()) {
result.items.push(getObject(item, []));
// JCD: Process any top-level descendant itemscopes, but where the node doesn't directly contain an itemprop attribute.
// var descendants = $item.find('[itemscope]').not($item.find('[itemscope] [itemscope]')).not($item.find('[itemprop]'));
var descendants = $item.find('[itemscope]').not($item.find('[itemscope] [itemscope]')).not($item.find('[itemprop]')).not($(memo_scope));
processItemScope(descendants, result);
}
});
return result;
}
var result = {};
result.items = [];
var $items = selector ? $(selector) : $(document).items();
result = processItemScope($items, result);
return format ? format(result) : JSON.stringify(result);
};