forked from manarth/jquery_esi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.esi.js
More file actions
executable file
·98 lines (88 loc) · 2.82 KB
/
Copy pathjquery.esi.js
File metadata and controls
executable file
·98 lines (88 loc) · 2.82 KB
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
/**
* jQuery ESI Plugin v1.0
* http://github.com/manarth/jquery_esi
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Usage:
* $().esiTags().handleESI();
*/
(function ($) {
// Fetch all ESI tags.
$.fn.esiTags = function() {
// Initialise a jQuery collection to return.
esi_tags = $();
// The handler can be called as $().esiTags, or $('foo').esiTags().
// Ensure we have an iterable collection in either instance.
if (this.length == 0) {
collection = $('html');
}
else {
collection = this;
}
collection.each(function() {
// Retrieve the base DOM element, in order to access the DOM method
// getElementsByTagName.
base_element = $(this).get(0);
// Discover comment-wrapped <esi:include> tags and move them out of the comment.
jQuery("*:not(iframe)").contents()
.filter(function(){ return this.nodeType == 8 && this.nodeValue.match(/^esi/);})
.each(function(){
var dom = $.trim(this.nodeValue).replace(/^esi/,'').trim();
$(this).after(dom);
$(this).remove();
});
// Discover the <esi:include> tags.
jQuery.each(base_element.getElementsByTagName('esi:include'), function(i, val) {
// Some DOMs fail to recognise that the ESI include tag is self-
// closing, so they treat following tags as child nodes.
if ($(this).children().length > 0) {
// Move the child nodes to become siblings.
children = $(this).children().detach();
$(this).after(children);
}
esi_tags = esi_tags.add($(this));
});
// Discover the <esi:remove> tags.
jQuery.each(base_element.getElementsByTagName('esi:remove'), function(i, val) {
// ESI remove tags are not self-closing, so no special treatment for
// child nodes is needed.
esi_tags = esi_tags.add($(this));
});
});
return esi_tags;
};
// Handle ESI tags.
// Delegates to either .handleESIInclude() or .handleESIRemove() as needed.
$.fn.handleESI = function() {
$(this).each(function() {
switch (this.nodeName.toLowerCase()) {
case 'esi:include':
$(this).handleESIInclude();
break;
case 'esi:remove':
$(this).handleESIRemove();
break;
}
});
return this;
}
// Handle a single ESI Include tag.
$.fn.handleESIInclude = function() {
src = $(this).attr('src');
jQuery.ajax({
context: this,
url: src,
success: function(data, textStatus, jqXHR) {
esiElement = $(this);
esiElement.replaceWith(data);
}
});
}
// Handle a single ESI Remove tag.
$.fn.handleESIRemove = function() {
$(this).replaceWith('');
}
})(jQuery);