2 Script: Request.HTML.js
3 Extends the basic Request Class with additional methods for interacting with HTML responses.
9 Request.HTML = new Class({
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');
33 doc = new DOMParser().parseFromString(root, 'text/xml');
35 root = doc.getElementsByTagName('root')[0];
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);
42 }) || container.set('html', text);
45 success: function(text){
46 var options = this.options, response = this.response;
48 response.html = text.stripScripts(function(script){
49 response.javascript = script;
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);
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')
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')));
82 return this.retrieve('send');
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));
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')));
100 return this.retrieve('load');
108 var sender = this.get('send');
109 sender.send({data: this, url: url || sender.options.url});
114 this.get('load').send(Array.link(arguments, {data: Object.type, url: String.type}));