Moving Element dependency from Request.js to Request.HTML.js and adjusted scripts...
[mootools.git] / Source / Request / Request.HTML.js
blobdcc2de89d1b57e9604d421dfa19d8a658512419f
1 /*
2 Script: Request.HTML.js
3         Extends the basic Request Class with additional methods for interacting with HTML responses.
5 License:
6         MIT-style license.
7 */
9 Request.HTML = new Class({
11         Extends: Request,
13         options: {
14                 update: false,
15                 append: false,
16                 evalScripts: true,
17                 filter: false
18         },
20         processHTML: function(text){
21                 var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
22                 text = (match) ? match[1] : text;
24                 var container = new Element('div');
26                 return $try(function(){
27                         var root = '<root>' + text + '</root>', doc;
28                         if (Browser.Engine.trident){
29                                 doc = new ActiveXObject('Microsoft.XMLDOM');
30                                 doc.async = false;
31                                 doc.loadXML(root);
32                         } else {
33                                 doc = new DOMParser().parseFromString(root, 'text/xml');
34                         }
35                         root = doc.getElementsByTagName('root')[0];
36                         if (!root) return;
37                         for (var i = 0, k = root.childNodes.length; i < k; i++){
38                                 var child = Element.clone(root.childNodes[i], true, true);
39                                 if (child) container.grab(child);
40                         }
41                         return container;
42                 }) || container.set('html', text);
43         },
45         success: function(text){
46                 var options = this.options, response = this.response;
48                 response.html = text.stripScripts(function(script){
49                         response.javascript = script;
50                 });
52                 var temp = this.processHTML(response.html);
54                 response.tree = temp.childNodes;
55                 response.elements = temp.getElements('*');
57                 if (options.filter) response.tree = response.elements.filter(options.filter);
58                 if (options.update) $(options.update).empty().set('html', response.html);
59                 else if (options.append) $(options.append).adopt(temp.getChildren());
60                 if (options.evalScripts) $exec(response.javascript);
62                 this.onSuccess(response.tree, response.elements, response.html, response.javascript);
63         }
65 });
67 Element.Properties.send = {
69         set: function(options){
70                 var send = this.retrieve('send');
71                 if (send) send.cancel();
72                 return this.eliminate('send').store('send:options', $extend({
73                         data: this, link: 'cancel', method: this.get('method') || 'post', url: this.get('action')
74                 }, options));
75         },
77         get: function(options){
78                 if (options || !this.retrieve('send')){
79                         if (options || !this.retrieve('send:options')) this.set('send', options);
80                         this.store('send', new Request(this.retrieve('send:options')));
81                 }
82                 return this.retrieve('send');
83         }
87 Element.Properties.load = {
89         set: function(options){
90                 var load = this.retrieve('load');
91                 if (load) load.cancel();
92                 return this.eliminate('load').store('load:options', $extend({data: this, link: 'cancel', update: this, method: 'get'}, options));
93         },
95         get: function(options){
96                 if (options || ! this.retrieve('load')){
97                         if (options || !this.retrieve('load:options')) this.set('load', options);
98                         this.store('load', new Request.HTML(this.retrieve('load:options')));
99                 }
100                 return this.retrieve('load');
101         }
105 Element.implement({
107         send: function(url){
108                 var sender = this.get('send');
109                 sender.send({data: this, url: url || sender.options.url});
110                 return this;
111         }
113         load: function(){
114                 this.get('load').send(Array.link(arguments, {data: Object.type, url: String.type}));
115                 return this;
116         }