From bce500121da06d0ddc445352468dbbab956992ab Mon Sep 17 00:00:00 2001 From: s7ntech Date: Wed, 17 Oct 2012 13:16:10 +0200 Subject: [PATCH 01/24] permitted run javascript code after loading hinclude content --- README.md | 13 +++++++++++++ hinclude.js | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index bf7ebd8..07a6903 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,18 @@ HInclude is declarative client-side inclusion for the Web; it allows easy composition of Web pages using the browser -- making your pages more modular, more cacheable, and easier to maintain. +You can run javascript code after loading 'hx:include' inserting the +javascript code inside the tag ... +example.. + + //...// + $('#sample').css('color': 'red'); + + +in the particular case where the content has the JavaScript code that is +executed before 'hinclude' finish on its own, for example widget + See [the demo page](http://mnot.github.com/hinclude/) for documentation and examples. + +in the particular case where the content has the JavaScript code that is executed before 'hinclude' finish on its own, for example widget diff --git a/hinclude.js b/hinclude.js index 4d467e5..f8a1c87 100644 --- a/hinclude.js +++ b/hinclude.js @@ -37,6 +37,7 @@ See http://mnot.github.com/hinclude/ for documentation. if (req.readyState == 4) { if (req.status == 200 | req.status == 304) { element.innerHTML = req.responseText; + this.runHincludeJs(element); } element.className = hinclude['classprefix'] + req.status; } @@ -58,6 +59,7 @@ See http://mnot.github.com/hinclude/ for documentation. var include = hinclude.buffer.pop(); if (include[1].status == 200 | include[1].status == 304) { include[0].innerHTML = include[1].responseText; + this.runHincludeJs(include[0]); } include[0].className = hinclude['classprefix'] + include[1].status; } @@ -82,6 +84,13 @@ See http://mnot.github.com/hinclude/ for documentation. this.include(includes[i], includes[i].getAttribute("src"), callback); } }, + + runHincludeJs: function (element) { + var codeJs = element.getElementsByTagName("hincludejs"); + for (var i=0; i < codeJs.length; i++) { + jQuery.globalEval(codeJs[i].innerHTML); + } + }, include: function (element, url, incl_cb) { var scheme = url.substring(0,url.indexOf(":")); From 82af6fbc383278f4e0d37dc4918499a46dff22a3 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Wed, 17 Oct 2012 13:30:17 +0200 Subject: [PATCH 02/24] correct syntax readme text --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 07a6903..716d731 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,18 @@ HInclude is declarative client-side inclusion for the Web; it allows easy composition of Web pages using the browser -- making your pages more modular, more cacheable, and easier to maintain. -You can run javascript code after loading 'hx:include' inserting the -javascript code inside the tag ... +You can run javascript code after loading 'hx:include' inserting the +javascript code inside the tag + + ... + example.. - - //...// - $('#sample').css('color': 'red'); - + + + //...// + $('#sample').css('color': 'red'); + + in the particular case where the content has the JavaScript code that is executed before 'hinclude' finish on its own, for example widget From 4c2082ef6a1c67e45cb2aef9a11be8bdd16b8208 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Wed, 17 Oct 2012 15:22:24 +0200 Subject: [PATCH 03/24] fix, tad removed after executing code --- hinclude.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hinclude.js b/hinclude.js index f8a1c87..582d9e6 100644 --- a/hinclude.js +++ b/hinclude.js @@ -88,7 +88,9 @@ See http://mnot.github.com/hinclude/ for documentation. runHincludeJs: function (element) { var codeJs = element.getElementsByTagName("hincludejs"); for (var i=0; i < codeJs.length; i++) { - jQuery.globalEval(codeJs[i].innerHTML); + var code = codeJs[i].innerHTML; + jQuery.globalEval(code); + codeJs[i].parentNode.removeChild(codeJs[i]); } }, From 398d013fc4aa2f0cc45826ee0c89e8a2687544cb Mon Sep 17 00:00:00 2001 From: s7ntech Date: Tue, 13 Nov 2012 10:09:45 +0100 Subject: [PATCH 04/24] removed line requires jquery library --- hinclude.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hinclude.js b/hinclude.js index 582d9e6..4dff64c 100644 --- a/hinclude.js +++ b/hinclude.js @@ -89,7 +89,7 @@ See http://mnot.github.com/hinclude/ for documentation. var codeJs = element.getElementsByTagName("hincludejs"); for (var i=0; i < codeJs.length; i++) { var code = codeJs[i].innerHTML; - jQuery.globalEval(code); + eval(code); codeJs[i].parentNode.removeChild(codeJs[i]); } }, From 6ccb9bffcaed175c6bbc9b1f80d8b92a50a85ea3 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Tue, 13 Nov 2012 12:16:26 +0100 Subject: [PATCH 05/24] renamed hincludejs with standard name tag javascript --- README.md | 2 +- hinclude.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 716d731..d8dddf4 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ example.. //...// - $('#sample').css('color': 'red'); + diff --git a/hinclude.js b/hinclude.js index 4dff64c..1416987 100644 --- a/hinclude.js +++ b/hinclude.js @@ -86,7 +86,7 @@ See http://mnot.github.com/hinclude/ for documentation. }, runHincludeJs: function (element) { - var codeJs = element.getElementsByTagName("hincludejs"); + var codeJs = element.getElementsByTagName("script"); for (var i=0; i < codeJs.length; i++) { var code = codeJs[i].innerHTML; eval(code); From de4c4a6f19d26ac48f2b249092d3d6e75f7389cd Mon Sep 17 00:00:00 2001 From: s7ntech Date: Tue, 13 Nov 2012 13:08:58 +0100 Subject: [PATCH 06/24] corrected text error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d8dddf4..0105956 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ more cacheable, and easier to maintain. You can run javascript code after loading 'hx:include' inserting the javascript code inside the tag - ... + example.. From 8a0d4ae4e08bfd3035fbbf4849a68a1ca49e351c Mon Sep 17 00:00:00 2001 From: s7ntech Date: Tue, 13 Nov 2012 19:19:04 +0100 Subject: [PATCH 07/24] correct execution and removal multiple tags script --- hinclude.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/hinclude.js b/hinclude.js index 77a604d..8a72f0a 100644 --- a/hinclude.js +++ b/hinclude.js @@ -97,7 +97,14 @@ var hinclude; for (var i=0; i < codeJs.length; i++) { var code = codeJs[i].innerHTML; eval(code); - codeJs[i].parentNode.removeChild(codeJs[i]); + } + this.removeTagScript(codeJs); + }, + + removeTagScript: function (codeJs) { + for (var i=0; i < codeJs.length; i++) { + codeJs[i].parentNode.removeChild(codeJs[i]); + i--; } }, From c72761ee803f2ed54789cb025ebbfc7f28db6375 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Thu, 3 Jan 2013 21:06:40 +0100 Subject: [PATCH 08/24] splitted check structure content, verification exist onload event into body, corrected display structure html --- hinclude.js | 155 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 141 insertions(+), 14 deletions(-) diff --git a/hinclude.js b/hinclude.js index 8a72f0a..309f0bb 100644 --- a/hinclude.js +++ b/hinclude.js @@ -37,12 +37,14 @@ var hinclude; hinclude = { classprefix: "include_", + move_head_to_document: true, // moved head script into document head + remove_js: true, // removes script by content set_content_async: function (element, req) { if (req.readyState === 4) { if (req.status === 200 || req.status === 304) { element.innerHTML = req.responseText; - this.runHincludeJs(element); + this.hinclude_check_content(element, req.responseText); } element.className = hinclude.classprefix + req.status; } @@ -58,13 +60,12 @@ var hinclude; } } }, - + show_buffered_content: function () { while (hinclude.buffer.length > 0) { var include = hinclude.buffer.pop(); if (include[1].status === 200 || include[1].status === 304) { - include[0].innerHTML = include[1].responseText; - this.runHincludeJs(include[0]); + this.hinclude_check_content(include, include[1].responseText); } include[0].className = hinclude.classprefix + include[1].status; } @@ -92,19 +93,145 @@ var hinclude; } }, - runHincludeJs: function (element) { - var codeJs = element.getElementsByTagName("script"); - for (var i=0; i < codeJs.length; i++) { - var code = codeJs[i].innerHTML; - eval(code); + // converte text into xml node + hinclude_xml_parser_content: function (content) { + var parsed_document; + if (window.ActiveXObject){// for Internet Explorer + parsed_document = new ActiveXObject('Microsoft.XMLDOM'); + parsed_document.async='false'; + parsed_document.loadXML(content); + if(parsed_document.parseError.errorCode != 0) { + parsed_document = false; + } + } else { + var parser = new DOMParser(); + parsed_document = parser.parseFromString(content,'text/xml'); + if(parsed_document.getElementsByTagName("parsererror").length > 0) { + parsed_document = false; + } + } + return parsed_document; + }, + + // verification content hinclude + hinclude_check_content: function(include, content) { + var parsed_document = this.hinclude_xml_parser_content(content); + this.hinclude_check_head_script(parsed_document); + this.move_html_to_hinclude(include, parsed_document, content); + var js_onload = this.hinclude_check_onload_body(parsed_document); + var js_code = this.hinclude_check_js_code(include); + this.run_hinclude_js(js_onload, js_code); + }, + + // verificarion exist head script + hinclude_check_head_script: function(parsed_document) { + //xml document + if(parsed_document) { + var head = parsed_document.getElementsByTagName('head'); + if(head.length > 0) { + var script = head[0].getElementsByTagName('script'); + if(script) { + this.hinclude_move_head_script_to_document(script[0]); + } + } } - this.removeTagScript(codeJs); }, - removeTagScript: function (codeJs) { - for (var i=0; i < codeJs.length; i++) { - codeJs[i].parentNode.removeChild(codeJs[i]); - i--; + // verification exist onload event + hinclude_check_onload_body: function (parsed_document) { + //xml document + if(parsed_document) { + var body = parsed_document.getElementsByTagName('body'); + var onload = false; + if(body.length > 0){ + if(body[0].getAttribute('onload')){ + onload = body[0].getAttribute('onload'); + } + } + return onload; + } + return false; + }, + + // moved head script into document head + hinclude_move_head_script_to_document: function(script) { + if(hinclude.move_head_to_document) { + var document_head= document.getElementsByTagName('head')[0]; + var document_script= document.createElement('script'); + document_script.type= 'text/javascript'; + try { + document_script.innerHTML = script.textContent; + }catch (e) { + // Internet Explorer + document_script.text = script.text; + } + document_head.appendChild(document_script); + script.parentNode.removeChild(script); + } + }, + + // inserts html content into hinclude + move_html_to_hinclude: function(include, parsed_document, content) { + var string; + if(parsed_document) { + string = this.xml_to_string(parsed_document); + }else{ + string = content; + } + include[0].innerHTML = string; + }, + + // convert xml node into string + xml_to_string: function(parsed_document) { + try { + // Gecko-based browsers, Safari, Opera. + return (new XMLSerializer()).serializeToString(parsed_document); + } + catch (e) { + try { + // Internet Explorer. + return parsed_document.xml; + } + catch (e) + {//Strange Browser ?? + alert('Xmlserializer not supported'); + } + } + return false; + }, + + // verification exists scripts into content + hinclude_check_js_code: function(include) { + var js = include[0].getElementsByTagName("script"); + var js_code = ''; + if(js.length > 0) { + var code = ''; + for (var i=0; i < js.length; i++) { + code = js[i].innerHTML; + js_code = js_code+code; + } + this.hinclude_remove_tag_script(js); + } + return js_code; + }, + + // removes script by content + hinclude_remove_tag_script: function (js) { + if(hinclude.remove_js) { + for (var i=0; i < js.length; i++) { + js[i].parentNode.removeChild(js[i]); + i--; + } + } + }, + + // execute code js + run_hinclude_js: function (js_onload, js_code) { + if(js_code) { + eval(js_code); + } + if(js_onload) { + eval(js_onload); } }, From 1924ccf8b18be44ece0fdd5bc92c0f7eb1d201c5 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Sun, 17 Mar 2013 14:07:35 +0100 Subject: [PATCH 09/24] fix error no method 'hinclude_check_content' and control type defined variable content --- hinclude.js | 111 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 47 deletions(-) diff --git a/hinclude.js b/hinclude.js index 309f0bb..e9d2edf 100644 --- a/hinclude.js +++ b/hinclude.js @@ -44,7 +44,7 @@ var hinclude; if (req.readyState === 4) { if (req.status === 200 || req.status === 304) { element.innerHTML = req.responseText; - this.hinclude_check_content(element, req.responseText); + hinclude.hinclude_check_content(element, req.responseText); } element.className = hinclude.classprefix + req.status; } @@ -62,10 +62,11 @@ var hinclude; }, show_buffered_content: function () { + if(hinclude.isEmpty(hinclude.buffer)){ return false; } while (hinclude.buffer.length > 0) { var include = hinclude.buffer.pop(); if (include[1].status === 200 || include[1].status === 304) { - this.hinclude_check_content(include, include[1].responseText); + hinclude.hinclude_check_content(include, include[1].responseText); } include[0].className = hinclude.classprefix + include[1].status; } @@ -95,21 +96,23 @@ var hinclude; // converte text into xml node hinclude_xml_parser_content: function (content) { - var parsed_document; - if (window.ActiveXObject){// for Internet Explorer - parsed_document = new ActiveXObject('Microsoft.XMLDOM'); - parsed_document.async='false'; - parsed_document.loadXML(content); - if(parsed_document.parseError.errorCode != 0) { - parsed_document = false; - } - } else { - var parser = new DOMParser(); - parsed_document = parser.parseFromString(content,'text/xml'); - if(parsed_document.getElementsByTagName("parsererror").length > 0) { - parsed_document = false; + var parsed_document = false; + if(!hinclude.isEmpty(content)) { + if (window.ActiveXObject){// for Internet Explorer + parsed_document = new ActiveXObject('Microsoft.XMLDOM'); + parsed_document.async='false'; + parsed_document.loadXML(content); + if(parsed_document.parseError.errorCode != 0) { + parsed_document = false; + } + } else { + var parser = new DOMParser(); + parsed_document = parser.parseFromString(content,'text/xml'); + if(parsed_document.getElementsByTagName("parsererror").length > 0) { + parsed_document = false; + } } - } + } return parsed_document; }, @@ -126,11 +129,11 @@ var hinclude; // verificarion exist head script hinclude_check_head_script: function(parsed_document) { //xml document - if(parsed_document) { + if(!hinclude.isEmpty(parsed_document)) { var head = parsed_document.getElementsByTagName('head'); if(head.length > 0) { var script = head[0].getElementsByTagName('script'); - if(script) { + if(!hinclude.isEmpty(script)) { this.hinclude_move_head_script_to_document(script[0]); } } @@ -140,22 +143,22 @@ var hinclude; // verification exist onload event hinclude_check_onload_body: function (parsed_document) { //xml document - if(parsed_document) { + if(!hinclude.isEmpty(parsed_document)) { var body = parsed_document.getElementsByTagName('body'); var onload = false; if(body.length > 0){ - if(body[0].getAttribute('onload')){ + if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ onload = body[0].getAttribute('onload'); } } return onload; } - return false; + return ''; }, // moved head script into document head hinclude_move_head_script_to_document: function(script) { - if(hinclude.move_head_to_document) { + if(!hinclude.isEmpty(script) && !hinclude.isEmpty(hinclude.move_head_to_document)) { var document_head= document.getElementsByTagName('head')[0]; var document_script= document.createElement('script'); document_script.type= 'text/javascript'; @@ -163,7 +166,7 @@ var hinclude; document_script.innerHTML = script.textContent; }catch (e) { // Internet Explorer - document_script.text = script.text; + document_script.text = script.text; } document_head.appendChild(document_script); script.parentNode.removeChild(script); @@ -172,12 +175,12 @@ var hinclude; // inserts html content into hinclude move_html_to_hinclude: function(include, parsed_document, content) { - var string; - if(parsed_document) { + var string = ''; + if(!hinclude.isEmpty(parsed_document)) { string = this.xml_to_string(parsed_document); - }else{ - string = content; - } + } else if(!hinclude.isEmpty(content)) { + string = content; + } include[0].innerHTML = string; }, @@ -199,25 +202,38 @@ var hinclude; } return false; }, + + isEmpty: function(value, x) { + if (value === null || value === undefined) return true; + if (value.length && value.length > 0) return false; + if (value.length === 0) return true; + + for (var key in value) { + if (hasOwnProperty.call(value, key)) return false; + } + return true; + }, // verification exists scripts into content hinclude_check_js_code: function(include) { - var js = include[0].getElementsByTagName("script"); - var js_code = ''; - if(js.length > 0) { - var code = ''; - for (var i=0; i < js.length; i++) { - code = js[i].innerHTML; - js_code = js_code+code; + var js_code = ''; + if(!hinclude.isEmpty(include)) { + var js = include[0].getElementsByTagName("script"); + if(js.length > 0) { + var code = ''; + for (var i=0; i < js.length; i++) { + code = js[i].innerHTML; + js_code = js_code+code; + } + this.hinclude_remove_tag_script(js); } - this.hinclude_remove_tag_script(js); - } + } return js_code; }, // removes script by content hinclude_remove_tag_script: function (js) { - if(hinclude.remove_js) { + if(!hinclude.isEmpty(js) && !hinclude.isEmpty(hinclude.remove_js)) { for (var i=0; i < js.length; i++) { js[i].parentNode.removeChild(js[i]); i--; @@ -227,10 +243,10 @@ var hinclude; // execute code js run_hinclude_js: function (js_onload, js_code) { - if(js_code) { + if(!hinclude.isEmpty(js_code)) { eval(js_code); } - if(js_onload) { + if(!hinclude.isEmpty(js_onload)) { eval(js_onload); } }, @@ -284,13 +300,14 @@ var hinclude; }, get_meta: function (name, value_default) { - var m = 0; var metas = document.getElementsByTagName("meta"); - for (m; m < metas.length; m += 1) { - var meta_name = metas[m].getAttribute("name"); - if (meta_name === name) { - return metas[m].getAttribute("content"); - } + if(!hinclude.isEmpty(metas)) { + for (var m = 0; m < metas.length; m += 1) { + var meta_name = metas[m].getAttribute("name"); + if (meta_name === name) { + return metas[m].getAttribute("content"); + } + } } return value_default; }, @@ -360,4 +377,4 @@ var hinclude; }; hinclude.addDOMLoadEvent(function () {hinclude.run(); }); -}()); \ No newline at end of file +}()); From cac42cf771c6110885534395913f64fbcde6427f Mon Sep 17 00:00:00 2001 From: s7ntech Date: Sun, 17 Mar 2013 15:24:24 +0100 Subject: [PATCH 10/24] correct code indentation --- hinclude.js | 645 ++++++++++++++++++++++++++-------------------------- 1 file changed, 323 insertions(+), 322 deletions(-) diff --git a/hinclude.js b/hinclude.js index e9d2edf..fc5f671 100644 --- a/hinclude.js +++ b/hinclude.js @@ -33,348 +33,349 @@ var hinclude; (function () { - "use strict"; + "use strict"; - hinclude = { - classprefix: "include_", - move_head_to_document: true, // moved head script into document head - remove_js: true, // removes script by content + hinclude = { + classprefix: "include_", + move_head_to_document: true, // moved head script into document head + remove_js: true, // removes script by content - set_content_async: function (element, req) { - if (req.readyState === 4) { - if (req.status === 200 || req.status === 304) { - element.innerHTML = req.responseText; - hinclude.hinclude_check_content(element, req.responseText); - } - element.className = hinclude.classprefix + req.status; - } - }, + set_content_async: function (element, req) { + if (req.readyState === 4) { + if (req.status === 200 || req.status === 304) { + element.innerHTML = req.responseText; + hinclude.hinclude_check_content(element, req.responseText); + } + element.className = hinclude.classprefix + req.status; + } + }, - buffer: [], - set_content_buffered: function (element, req) { - if (req.readyState === 4) { - hinclude.buffer.push([element, req]); - hinclude.outstanding -= 1; - if (hinclude.outstanding === 0) { - hinclude.show_buffered_content(); - } - } - }, + buffer: [], + set_content_buffered: function (element, req) { + if (req.readyState === 4) { + hinclude.buffer.push([element, req]); + hinclude.outstanding -= 1; + if (hinclude.outstanding === 0) { + hinclude.show_buffered_content(); + } + } + }, - show_buffered_content: function () { - if(hinclude.isEmpty(hinclude.buffer)){ return false; } - while (hinclude.buffer.length > 0) { - var include = hinclude.buffer.pop(); - if (include[1].status === 200 || include[1].status === 304) { - hinclude.hinclude_check_content(include, include[1].responseText); - } - include[0].className = hinclude.classprefix + include[1].status; - } - }, + show_buffered_content: function () { + if(hinclude.isEmpty(hinclude.buffer)){ return false; } + while (hinclude.buffer.length > 0) { + var include = hinclude.buffer.pop(); + if (include[1].status === 200 || include[1].status === 304) { + hinclude.hinclude_check_content(include, include[1].responseText); + } + include[0].className = hinclude.classprefix + include[1].status; + } + }, - outstanding: 0, - includes: [], - run: function () { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - this.includes = document.getElementsByTagName("hx:include"); - if (this.includes.length === 0) { // remove ns for IE - this.includes = document.getElementsByTagName("include"); - } - if (mode === "async") { - callback = this.set_content_async; - } else if (mode === "buffered") { - callback = this.set_content_buffered; - var timeout = this.get_meta("include_timeout", 2.5) * 1000; - setTimeout(hinclude.show_buffered_content, timeout); - } - for (i; i < this.includes.length; i += 1) { - this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); - } - }, + outstanding: 0, + includes: [], + run: function () { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + this.includes = document.getElementsByTagName("hx:include"); + if (this.includes.length === 0) { // remove ns for IE + this.includes = document.getElementsByTagName("include"); + } + if (mode === "async") { + callback = this.set_content_async; + } else if (mode === "buffered") { + callback = this.set_content_buffered; + var timeout = this.get_meta("include_timeout", 2.5) * 1000; + setTimeout(hinclude.show_buffered_content, timeout); + } + for (i; i < this.includes.length; i += 1) { + this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); + } + }, - // converte text into xml node - hinclude_xml_parser_content: function (content) { - var parsed_document = false; - if(!hinclude.isEmpty(content)) { - if (window.ActiveXObject){// for Internet Explorer - parsed_document = new ActiveXObject('Microsoft.XMLDOM'); - parsed_document.async='false'; - parsed_document.loadXML(content); - if(parsed_document.parseError.errorCode != 0) { - parsed_document = false; - } - } else { - var parser = new DOMParser(); - parsed_document = parser.parseFromString(content,'text/xml'); - if(parsed_document.getElementsByTagName("parsererror").length > 0) { - parsed_document = false; - } - } - } - return parsed_document; - }, + // convert text into xml node + hinclude_xml_parser_content: function (content) { + var parsed_document = false; + if(!hinclude.isEmpty(content)) { + if (window.ActiveXObject){// for Internet Explorer + parsed_document = new ActiveXObject('Microsoft.XMLDOM'); + parsed_document.async='false'; + parsed_document.loadXML(content); + if(parsed_document.parseError.errorCode != 0) { + parsed_document = false; + } + } else { + var parser = new DOMParser(); + parsed_document = parser.parseFromString(content,'text/xml'); + if(parsed_document.getElementsByTagName("parsererror").length > 0) { + parsed_document = false; + } + } + } + return parsed_document; + }, - // verification content hinclude - hinclude_check_content: function(include, content) { - var parsed_document = this.hinclude_xml_parser_content(content); - this.hinclude_check_head_script(parsed_document); - this.move_html_to_hinclude(include, parsed_document, content); - var js_onload = this.hinclude_check_onload_body(parsed_document); - var js_code = this.hinclude_check_js_code(include); - this.run_hinclude_js(js_onload, js_code); - }, + // verification content hinclude + hinclude_check_content: function(include, content) { + var parsed_document = this.hinclude_xml_parser_content(content); + this.hinclude_check_head_script(parsed_document); + this.move_html_to_hinclude(include, parsed_document, content); + var js_onload = this.hinclude_check_onload_body(parsed_document); + var js_code = this.hinclude_check_js_code(include); + this.run_hinclude_js(js_onload, js_code); + }, - // verificarion exist head script - hinclude_check_head_script: function(parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var head = parsed_document.getElementsByTagName('head'); - if(head.length > 0) { - var script = head[0].getElementsByTagName('script'); - if(!hinclude.isEmpty(script)) { - this.hinclude_move_head_script_to_document(script[0]); - } - } - } - }, + // verificarion exist head script + hinclude_check_head_script: function(parsed_document) { + //xml document + if(!hinclude.isEmpty(parsed_document)) { + var head = parsed_document.getElementsByTagName('head'); + if(head.length > 0) { + var script = head[0].getElementsByTagName('script'); + if(!hinclude.isEmpty(script)) { + this.hinclude_move_head_script_to_document(script[0]); + } + } + } + }, - // verification exist onload event - hinclude_check_onload_body: function (parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var body = parsed_document.getElementsByTagName('body'); - var onload = false; - if(body.length > 0){ - if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ - onload = body[0].getAttribute('onload'); - } - } - return onload; - } - return ''; - }, + // verification exist onload event + hinclude_check_onload_body: function (parsed_document) { + //xml document + if(!hinclude.isEmpty(parsed_document)) { + var body = parsed_document.getElementsByTagName('body'); + var onload = false; + if(body.length > 0){ + if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ + onload = body[0].getAttribute('onload'); + } + } + return onload; + } + return ''; + }, - // moved head script into document head - hinclude_move_head_script_to_document: function(script) { - if(!hinclude.isEmpty(script) && !hinclude.isEmpty(hinclude.move_head_to_document)) { - var document_head= document.getElementsByTagName('head')[0]; - var document_script= document.createElement('script'); - document_script.type= 'text/javascript'; - try { - document_script.innerHTML = script.textContent; - }catch (e) { - // Internet Explorer - document_script.text = script.text; - } - document_head.appendChild(document_script); - script.parentNode.removeChild(script); - } - }, + // moved head script into document head + hinclude_move_head_script_to_document: function(script) { + if(!hinclude.isEmpty(script) && !hinclude.isEmpty(hinclude.move_head_to_document)) { + var document_head= document.getElementsByTagName('head')[0]; + var document_script= document.createElement('script'); + document_script.type= 'text/javascript'; + try { + document_script.innerHTML = script.textContent; + }catch (e) { + // Internet Explorer + document_script.text = script.text; + } + document_head.appendChild(document_script); + script.parentNode.removeChild(script); + } + }, - // inserts html content into hinclude - move_html_to_hinclude: function(include, parsed_document, content) { - var string = ''; - if(!hinclude.isEmpty(parsed_document)) { - string = this.xml_to_string(parsed_document); - } else if(!hinclude.isEmpty(content)) { - string = content; - } - include[0].innerHTML = string; - }, + // inserts html content into hinclude + move_html_to_hinclude: function(include, parsed_document, content) { + var string = ''; + if(!hinclude.isEmpty(parsed_document)) { + string = this.xml_to_string(parsed_document); + } else if(!hinclude.isEmpty(content)) { + string = content; + } + include[0].innerHTML = string; + }, - // convert xml node into string - xml_to_string: function(parsed_document) { - try { - // Gecko-based browsers, Safari, Opera. - return (new XMLSerializer()).serializeToString(parsed_document); - } - catch (e) { - try { - // Internet Explorer. - return parsed_document.xml; - } - catch (e) - {//Strange Browser ?? - alert('Xmlserializer not supported'); - } - } - return false; - }, + // convert xml node into string + xml_to_string: function(parsed_document) { + try { + // Gecko-based browsers, Safari, Opera. + return (new XMLSerializer()).serializeToString(parsed_document); + } + catch (e) { + try { + // Internet Explorer. + return parsed_document.xml; + } + catch (e) + { + //Strange Browser ?? + alert('Xmlserializer not supported'); + } + } + return false; + }, - isEmpty: function(value, x) { - if (value === null || value === undefined) return true; - if (value.length && value.length > 0) return false; - if (value.length === 0) return true; + isEmpty: function(value) { + if (value === null || value === undefined) return true; + if (value.length && value.length > 0) return false; + if (value.length === 0) return true; - for (var key in value) { - if (hasOwnProperty.call(value, key)) return false; - } - return true; - }, + for (var key in value) { + if (hasOwnProperty.call(value, key)) return false; + } + return true; + }, - // verification exists scripts into content - hinclude_check_js_code: function(include) { - var js_code = ''; - if(!hinclude.isEmpty(include)) { - var js = include[0].getElementsByTagName("script"); - if(js.length > 0) { - var code = ''; - for (var i=0; i < js.length; i++) { - code = js[i].innerHTML; - js_code = js_code+code; - } - this.hinclude_remove_tag_script(js); - } - } - return js_code; - }, + // verification exists scripts into content + hinclude_check_js_code: function(include) { + var js_code = ''; + if(!hinclude.isEmpty(include)) { + var js = include[0].getElementsByTagName("script"); + if(js.length > 0) { + var code = ''; + for (var i=0; i < js.length; i++) { + code = js[i].innerHTML; + js_code = js_code+code; + } + this.hinclude_remove_tag_script(js); + } + } + return js_code; + }, - // removes script by content - hinclude_remove_tag_script: function (js) { - if(!hinclude.isEmpty(js) && !hinclude.isEmpty(hinclude.remove_js)) { - for (var i=0; i < js.length; i++) { - js[i].parentNode.removeChild(js[i]); - i--; - } - } - }, + // removes script by content + hinclude_remove_tag_script: function (js) { + if(!hinclude.isEmpty(js) && !hinclude.isEmpty(hinclude.remove_js)) { + for (var i=0; i < js.length; i++) { + js[i].parentNode.removeChild(js[i]); + i--; + } + } + }, - // execute code js - run_hinclude_js: function (js_onload, js_code) { - if(!hinclude.isEmpty(js_code)) { - eval(js_code); - } - if(!hinclude.isEmpty(js_onload)) { - eval(js_onload); - } - }, + // execute code js + run_hinclude_js: function (js_onload, js_code) { + if(!hinclude.isEmpty(js_code)) { + eval(js_code); + } + if(!hinclude.isEmpty(js_onload)) { + eval(js_onload); + } + }, - include: function (element, url, incl_cb) { - var scheme = url.substring(0, url.indexOf(":")); - if (scheme.toLowerCase() === "data") { // just text/plain for now - var data = decodeURIComponent(url.substring(url.indexOf(",") + 1, url.length)); - element.innerHTML = data; - } else { - var req = false; - if (window.XMLHttpRequest) { - try { - req = new XMLHttpRequest(); - } catch (e1) { - req = false; - } - } else if (window.ActiveXObject) { - try { - req = new ActiveXObject("Microsoft.XMLHTTP"); - } catch (e2) { - req = false; - } - } - if (req) { - this.outstanding += 1; - req.onreadystatechange = function () { - incl_cb(element, req); - }; - try { - req.open("GET", url, true); - req.send(""); - } catch (e3) { - this.outstanding -= 1; - alert("Include error: " + url + " (" + e3 + ")"); - } - } - } - }, + include: function (element, url, incl_cb) { + var scheme = url.substring(0, url.indexOf(":")); + if (scheme.toLowerCase() === "data") { // just text/plain for now + var data = decodeURIComponent(url.substring(url.indexOf(",") + 1, url.length)); + element.innerHTML = data; + } else { + var req = false; + if (window.XMLHttpRequest) { + try { + req = new XMLHttpRequest(); + } catch (e1) { + req = false; + } + } else if (window.ActiveXObject) { + try { + req = new ActiveXObject("Microsoft.XMLHTTP"); + } catch (e2) { + req = false; + } + } + if (req) { + this.outstanding += 1; + req.onreadystatechange = function () { + incl_cb(element, req); + }; + try { + req.open("GET", url, true); + req.send(""); + } catch (e3) { + this.outstanding -= 1; + alert("Include error: " + url + " (" + e3 + ")"); + } + } + } + }, - refresh: function (element_id) { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - callback = this.set_content_buffered; - for (i; i < this.includes.length; i += 1) { - if (this.includes[i].getAttribute("id") === element_id) { - this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); - } - } - }, + refresh: function (element_id) { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + callback = this.set_content_buffered; + for (i; i < this.includes.length; i += 1) { + if (this.includes[i].getAttribute("id") === element_id) { + this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); + } + } + }, - get_meta: function (name, value_default) { - var metas = document.getElementsByTagName("meta"); - if(!hinclude.isEmpty(metas)) { - for (var m = 0; m < metas.length; m += 1) { - var meta_name = metas[m].getAttribute("name"); - if (meta_name === name) { - return metas[m].getAttribute("content"); - } - } - } - return value_default; - }, + get_meta: function (name, value_default) { + var metas = document.getElementsByTagName("meta"); + if(!hinclude.isEmpty(metas)) { + for (var m = 0; m < metas.length; m += 1) { + var meta_name = metas[m].getAttribute("name"); + if (meta_name === name) { + return metas[m].getAttribute("content"); + } + } + } + return value_default; + }, - /* - * (c)2006 Dean Edwards/Matthias Miller/John Resig - * Special thanks to Dan Webb's domready.js Prototype extension - * and Simon Willison's addLoadEvent - * - * For more info, see: - * http://dean.edwards.name/weblog/2006/06/again/ - * - * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) - */ - addDOMLoadEvent: function (func) { - if (!window.__load_events) { - var init = function () { - var i = 0; - // quit if this function has already been called - if (hinclude.addDOMLoadEvent.done) {return; } - hinclude.addDOMLoadEvent.done = true; - if (window.__load_timer) { - clearInterval(window.__load_timer); - window.__load_timer = null; - } - for (i; i < window.__load_events.length; i += 1) { - window.__load_events[i](); - } - window.__load_events = null; - // clean up the __ie_onload event - /*@cc_on - document.getElementById("__ie_onload").onreadystatechange = ""; - @*/ - }; - // for Mozilla/Opera9 - if (document.addEventListener) { - document.addEventListener("DOMContentLoaded", init, false); - } - // for Internet Explorer - /*@cc_on - document.write( - "<\/scr" - + "ipt>" - ); - var script = document.getElementById("__ie_onload"); - script.onreadystatechange = function () { - if (this.readyState === "complete") { - init(); // call the onload handler - } - }; - @*/ - // for Safari - if (/WebKit/i.test(navigator.userAgent)) { // sniff - window.__load_timer = setInterval(function () { - if (/loaded|complete/.test(document.readyState)) { - init(); - } - }, 10); - } - // for other browsers - window.onload = init; - window.__load_events = []; - } - window.__load_events.push(func); - } - }; + /* + * (c)2006 Dean Edwards/Matthias Miller/John Resig + * Special thanks to Dan Webb's domready.js Prototype extension + * and Simon Willison's addLoadEvent + * + * For more info, see: + * http://dean.edwards.name/weblog/2006/06/again/ + * + * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) + */ + addDOMLoadEvent: function (func) { + if (!window.__load_events) { + var init = function () { + var i = 0; + // quit if this function has already been called + if (hinclude.addDOMLoadEvent.done) {return; } + hinclude.addDOMLoadEvent.done = true; + if (window.__load_timer) { + clearInterval(window.__load_timer); + window.__load_timer = null; + } + for (i; i < window.__load_events.length; i += 1) { + window.__load_events[i](); + } + window.__load_events = null; + // clean up the __ie_onload event + /*@cc_on + document.getElementById("__ie_onload").onreadystatechange = ""; + @*/ + }; + // for Mozilla/Opera9 + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", init, false); + } + // for Internet Explorer + /*@cc_on + document.write( + "<\/scr" + + "ipt>" + ); + var script = document.getElementById("__ie_onload"); + script.onreadystatechange = function () { + if (this.readyState === "complete") { + init(); // call the onload handler + } + }; + @*/ + // for Safari + if (/WebKit/i.test(navigator.userAgent)) { // sniff + window.__load_timer = setInterval(function () { + if (/loaded|complete/.test(document.readyState)) { + init(); + } + }, 10); + } + // for other browsers + window.onload = init; + window.__load_events = []; + } + window.__load_events.push(func); + } + }; - hinclude.addDOMLoadEvent(function () {hinclude.run(); }); + hinclude.addDOMLoadEvent(function () {hinclude.run(); }); }()); From 4eaa5c8ce0f9e8880f0fe69d3566e825b5c23364 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Tue, 19 Mar 2013 16:17:01 +0100 Subject: [PATCH 11/24] fix error move head condition --- hinclude.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hinclude.js b/hinclude.js index fc5f671..c200d14 100644 --- a/hinclude.js +++ b/hinclude.js @@ -158,7 +158,7 @@ var hinclude; // moved head script into document head hinclude_move_head_script_to_document: function(script) { - if(!hinclude.isEmpty(script) && !hinclude.isEmpty(hinclude.move_head_to_document)) { + if(!hinclude.isEmpty(script) && hinclude.move_head_to_document) { var document_head= document.getElementsByTagName('head')[0]; var document_script= document.createElement('script'); document_script.type= 'text/javascript'; From 874a7c17044238c52259d39317caab5d43553d49 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Wed, 20 Mar 2013 19:30:32 +0100 Subject: [PATCH 12/24] allowed loading external javascript --- hinclude.js | 619 +++++++++++++++++++++++++++------------------------- 1 file changed, 319 insertions(+), 300 deletions(-) diff --git a/hinclude.js b/hinclude.js index c200d14..b8b4f56 100644 --- a/hinclude.js +++ b/hinclude.js @@ -33,287 +33,302 @@ var hinclude; (function () { - "use strict"; + "use strict"; - hinclude = { - classprefix: "include_", - move_head_to_document: true, // moved head script into document head - remove_js: true, // removes script by content + hinclude = { + classprefix: "include_", + move_head_to_document: true, // moved head script into document head + remove_js: true, // removes script by content - set_content_async: function (element, req) { - if (req.readyState === 4) { - if (req.status === 200 || req.status === 304) { - element.innerHTML = req.responseText; - hinclude.hinclude_check_content(element, req.responseText); - } - element.className = hinclude.classprefix + req.status; - } - }, + set_content_async: function (element, req) { + if (req.readyState === 4) { + if (req.status === 200 || req.status === 304) { + element.innerHTML = req.responseText; + hinclude.hinclude_check_content(element, req.responseText); + } + element.className = hinclude.classprefix + req.status; + } + }, - buffer: [], - set_content_buffered: function (element, req) { - if (req.readyState === 4) { - hinclude.buffer.push([element, req]); - hinclude.outstanding -= 1; - if (hinclude.outstanding === 0) { - hinclude.show_buffered_content(); - } - } - }, + buffer: [], + set_content_buffered: function (element, req) { + if (req.readyState === 4) { + hinclude.buffer.push([element, req]); + hinclude.outstanding -= 1; + if (hinclude.outstanding === 0) { + hinclude.show_buffered_content(); + } + } + }, - show_buffered_content: function () { - if(hinclude.isEmpty(hinclude.buffer)){ return false; } - while (hinclude.buffer.length > 0) { - var include = hinclude.buffer.pop(); - if (include[1].status === 200 || include[1].status === 304) { - hinclude.hinclude_check_content(include, include[1].responseText); - } - include[0].className = hinclude.classprefix + include[1].status; - } - }, + show_buffered_content: function () { + if(hinclude.isEmpty(hinclude.buffer)){ + return false; + } + while (hinclude.buffer.length > 0) { + var include = hinclude.buffer.pop(); + if (include[1].status === 200 || include[1].status === 304) { + hinclude.hinclude_check_content(include, include[1].responseText); + } + include[0].className = hinclude.classprefix + include[1].status; + } + }, - outstanding: 0, - includes: [], - run: function () { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - this.includes = document.getElementsByTagName("hx:include"); - if (this.includes.length === 0) { // remove ns for IE - this.includes = document.getElementsByTagName("include"); - } - if (mode === "async") { - callback = this.set_content_async; - } else if (mode === "buffered") { - callback = this.set_content_buffered; - var timeout = this.get_meta("include_timeout", 2.5) * 1000; - setTimeout(hinclude.show_buffered_content, timeout); - } - for (i; i < this.includes.length; i += 1) { - this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); - } - }, + outstanding: 0, + includes: [], + run: function () { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + this.includes = document.getElementsByTagName("hx:include"); + if (this.includes.length === 0) { // remove ns for IE + this.includes = document.getElementsByTagName("include"); + } + if (mode === "async") { + callback = this.set_content_async; + } else if (mode === "buffered") { + callback = this.set_content_buffered; + var timeout = this.get_meta("include_timeout", 2.5) * 1000; + setTimeout(hinclude.show_buffered_content, timeout); + } + for (i; i < this.includes.length; i += 1) { + this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); + } + }, - // convert text into xml node - hinclude_xml_parser_content: function (content) { - var parsed_document = false; - if(!hinclude.isEmpty(content)) { - if (window.ActiveXObject){// for Internet Explorer - parsed_document = new ActiveXObject('Microsoft.XMLDOM'); - parsed_document.async='false'; - parsed_document.loadXML(content); - if(parsed_document.parseError.errorCode != 0) { - parsed_document = false; - } - } else { - var parser = new DOMParser(); - parsed_document = parser.parseFromString(content,'text/xml'); - if(parsed_document.getElementsByTagName("parsererror").length > 0) { - parsed_document = false; - } - } - } - return parsed_document; - }, + // convert text into xml node + hinclude_xml_parser_content: function (content) { + var parsed_document = false; + if(!hinclude.isEmpty(content)) { + if (window.ActiveXObject){// for Internet Explorer + parsed_document = new ActiveXObject('Microsoft.XMLDOM'); + parsed_document.async='false'; + parsed_document.loadXML(content); + if(parsed_document.parseError.errorCode != 0) { + parsed_document = false; + } + } else { + var parser = new DOMParser(); + parsed_document = parser.parseFromString(content,'text/xml'); + if(parsed_document.getElementsByTagName("parsererror").length > 0) { + parsed_document = false; + } + } + } + return parsed_document; + }, - // verification content hinclude - hinclude_check_content: function(include, content) { - var parsed_document = this.hinclude_xml_parser_content(content); - this.hinclude_check_head_script(parsed_document); - this.move_html_to_hinclude(include, parsed_document, content); - var js_onload = this.hinclude_check_onload_body(parsed_document); - var js_code = this.hinclude_check_js_code(include); - this.run_hinclude_js(js_onload, js_code); - }, + // verification content hinclude + hinclude_check_content: function(include, content) { + var parsed_document = this.hinclude_xml_parser_content(content); + this.hinclude_check_head_script(parsed_document); + this.move_html_to_hinclude(include, parsed_document, content); + var js_onload = this.hinclude_check_onload_body(parsed_document); + var js_code = this.hinclude_check_js_code(include); + this.run_hinclude_js(js_onload, js_code); + }, - // verificarion exist head script - hinclude_check_head_script: function(parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var head = parsed_document.getElementsByTagName('head'); - if(head.length > 0) { - var script = head[0].getElementsByTagName('script'); - if(!hinclude.isEmpty(script)) { - this.hinclude_move_head_script_to_document(script[0]); - } - } - } - }, + // verificarion exist head script + hinclude_check_head_script: function(parsed_document) { + //xml document + if(!hinclude.isEmpty(parsed_document)) { + var head = parsed_document.getElementsByTagName('head'); + if(head.length > 0) { + var script = head[0].getElementsByTagName('script'); + if(!hinclude.isEmpty(script)) { + this.hinclude_move_head_script_to_document(script[0]); + } + } + } + }, - // verification exist onload event - hinclude_check_onload_body: function (parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var body = parsed_document.getElementsByTagName('body'); - var onload = false; - if(body.length > 0){ - if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ - onload = body[0].getAttribute('onload'); - } - } - return onload; - } - return ''; - }, + // verification exist onload event + hinclude_check_onload_body: function (parsed_document) { + //xml document + if(!hinclude.isEmpty(parsed_document)) { + var body = parsed_document.getElementsByTagName('body'); + var onload = false; + if(body.length > 0){ + if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ + onload = body[0].getAttribute('onload'); + } + } + return onload; + } + return ''; + }, - // moved head script into document head - hinclude_move_head_script_to_document: function(script) { - if(!hinclude.isEmpty(script) && hinclude.move_head_to_document) { - var document_head= document.getElementsByTagName('head')[0]; - var document_script= document.createElement('script'); - document_script.type= 'text/javascript'; - try { - document_script.innerHTML = script.textContent; - }catch (e) { - // Internet Explorer - document_script.text = script.text; - } - document_head.appendChild(document_script); - script.parentNode.removeChild(script); - } - }, + // moved head script into document head + hinclude_move_head_script_to_document: function(script) { + if(!hinclude.isEmpty(script) && hinclude.move_head_to_document) { + var document_head= document.getElementsByTagName('head')[0]; + var document_script= document.createElement('script'); + document_script.type= 'text/javascript'; + try { + document_script.innerHTML = script.textContent; + }catch (e) { + // Internet Explorer + document_script.text = script.text; + } + document_head.appendChild(document_script); + script.parentNode.removeChild(script); + } + }, - // inserts html content into hinclude - move_html_to_hinclude: function(include, parsed_document, content) { - var string = ''; - if(!hinclude.isEmpty(parsed_document)) { - string = this.xml_to_string(parsed_document); - } else if(!hinclude.isEmpty(content)) { - string = content; - } - include[0].innerHTML = string; - }, + // inserts html content into hinclude + move_html_to_hinclude: function(include, parsed_document, content) { + var string = ''; + if(!hinclude.isEmpty(parsed_document)) { + string = this.xml_to_string(parsed_document); + } else if(!hinclude.isEmpty(content)) { + string = content; + } + include[0].innerHTML = string; + }, - // convert xml node into string - xml_to_string: function(parsed_document) { - try { - // Gecko-based browsers, Safari, Opera. - return (new XMLSerializer()).serializeToString(parsed_document); - } - catch (e) { - try { - // Internet Explorer. - return parsed_document.xml; - } - catch (e) - { - //Strange Browser ?? - alert('Xmlserializer not supported'); - } - } - return false; - }, + // convert xml node into string + xml_to_string: function(parsed_document) { + try { + // Gecko-based browsers, Safari, Opera. + return (new XMLSerializer()).serializeToString(parsed_document); + } + catch (e) { + try { + // Internet Explorer. + return parsed_document.xml; + } + catch (e) + { + //Strange Browser ?? + alert('Xmlserializer not supported'); + } + } + return false; + }, - isEmpty: function(value) { - if (value === null || value === undefined) return true; - if (value.length && value.length > 0) return false; - if (value.length === 0) return true; + isEmpty: function(value) { + if (value === null || value === undefined) return true; + if (value.length && value.length > 0) return false; + if (value.length === 0) return true; - for (var key in value) { - if (hasOwnProperty.call(value, key)) return false; - } - return true; - }, + for (var key in value) { + if (hasOwnProperty.call(value, key)) return false; + } + return true; + }, + + get_content_jsfile: function (element, req) { + if (req.readyState === 4) { + if (req.status === 200 || req.status === 304) { + hinclude.run_hinclude_js(null, req.responseText); + } + } + }, - // verification exists scripts into content - hinclude_check_js_code: function(include) { - var js_code = ''; - if(!hinclude.isEmpty(include)) { - var js = include[0].getElementsByTagName("script"); - if(js.length > 0) { - var code = ''; - for (var i=0; i < js.length; i++) { - code = js[i].innerHTML; - js_code = js_code+code; - } - this.hinclude_remove_tag_script(js); - } - } - return js_code; - }, + // verification exists scripts into content + hinclude_check_js_code: function(include) { + var js_code = ''; + if(!hinclude.isEmpty(include)) { + var js = include[0].getElementsByTagName("script"); + if(js.length > 0) { + var code = ''; + var callback = this.get_content_jsfile; + for (var i=0; i < js.length; i++) { + if(js[i].src) { + this.include(null,js[i].src,callback); + } else { + code = js[i].innerHTML; + js_code = js_code+code; + } + } + this.hinclude_remove_tag_script(js); + } + } + return js_code; + }, - // removes script by content - hinclude_remove_tag_script: function (js) { - if(!hinclude.isEmpty(js) && !hinclude.isEmpty(hinclude.remove_js)) { - for (var i=0; i < js.length; i++) { - js[i].parentNode.removeChild(js[i]); - i--; - } - } - }, + // removes script by content + hinclude_remove_tag_script: function (js) { + if(!hinclude.isEmpty(js) && !hinclude.isEmpty(hinclude.remove_js)) { + for (var i=0; i < js.length; i++) { + js[i].parentNode.removeChild(js[i]); + i--; + } + } + }, - // execute code js - run_hinclude_js: function (js_onload, js_code) { - if(!hinclude.isEmpty(js_code)) { - eval(js_code); - } - if(!hinclude.isEmpty(js_onload)) { - eval(js_onload); - } - }, + // execute code js + run_hinclude_js: function (js_onload, js_code) { + if(!hinclude.isEmpty(js_code)) { + eval(js_code); + } + if(!hinclude.isEmpty(js_onload)) { + eval(js_onload); + } + }, - include: function (element, url, incl_cb) { - var scheme = url.substring(0, url.indexOf(":")); - if (scheme.toLowerCase() === "data") { // just text/plain for now - var data = decodeURIComponent(url.substring(url.indexOf(",") + 1, url.length)); - element.innerHTML = data; - } else { - var req = false; - if (window.XMLHttpRequest) { - try { - req = new XMLHttpRequest(); - } catch (e1) { - req = false; - } - } else if (window.ActiveXObject) { - try { - req = new ActiveXObject("Microsoft.XMLHTTP"); - } catch (e2) { - req = false; - } - } - if (req) { - this.outstanding += 1; - req.onreadystatechange = function () { - incl_cb(element, req); - }; - try { - req.open("GET", url, true); - req.send(""); - } catch (e3) { - this.outstanding -= 1; - alert("Include error: " + url + " (" + e3 + ")"); - } - } - } - }, + include: function (element, url, incl_cb) { + var scheme = url.substring(0, url.indexOf(":")); + if (scheme.toLowerCase() === "data") { // just text/plain for now + var data = decodeURIComponent(url.substring(url.indexOf(",") + 1, url.length)); + element.innerHTML = data; + } else { + var req = false; + if (window.XMLHttpRequest) { + try { + req = new XMLHttpRequest(); + } catch (e1) { + req = false; + } + } else if (window.ActiveXObject) { + try { + req = new ActiveXObject("Microsoft.XMLHTTP"); + } catch (e2) { + req = false; + } + } + if (req) { + this.outstanding += 1; + req.onreadystatechange = function () { + incl_cb(element, req); + }; + try { + req.open("GET", url, true); + req.send(""); + } catch (e3) { + this.outstanding -= 1; + alert("Include error: " + url + " (" + e3 + ")"); + } + } + } + }, - refresh: function (element_id) { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - callback = this.set_content_buffered; - for (i; i < this.includes.length; i += 1) { - if (this.includes[i].getAttribute("id") === element_id) { - this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); - } - } - }, + refresh: function (element_id) { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + callback = this.set_content_buffered; + for (i; i < this.includes.length; i += 1) { + if (this.includes[i].getAttribute("id") === element_id) { + this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); + } + } + }, - get_meta: function (name, value_default) { - var metas = document.getElementsByTagName("meta"); - if(!hinclude.isEmpty(metas)) { - for (var m = 0; m < metas.length; m += 1) { - var meta_name = metas[m].getAttribute("name"); - if (meta_name === name) { - return metas[m].getAttribute("content"); - } - } - } - return value_default; - }, + get_meta: function (name, value_default) { + var metas = document.getElementsByTagName("meta"); + if(!hinclude.isEmpty(metas)) { + for (var m = 0; m < metas.length; m += 1) { + var meta_name = metas[m].getAttribute("name"); + if (meta_name === name) { + return metas[m].getAttribute("content"); + } + } + } + return value_default; + }, - /* + /* * (c)2006 Dean Edwards/Matthias Miller/John Resig * Special thanks to Dan Webb's domready.js Prototype extension * and Simon Willison's addLoadEvent @@ -323,32 +338,34 @@ var hinclude; * * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) */ - addDOMLoadEvent: function (func) { - if (!window.__load_events) { - var init = function () { - var i = 0; - // quit if this function has already been called - if (hinclude.addDOMLoadEvent.done) {return; } - hinclude.addDOMLoadEvent.done = true; - if (window.__load_timer) { - clearInterval(window.__load_timer); - window.__load_timer = null; - } - for (i; i < window.__load_events.length; i += 1) { - window.__load_events[i](); - } - window.__load_events = null; - // clean up the __ie_onload event - /*@cc_on + addDOMLoadEvent: function (func) { + if (!window.__load_events) { + var init = function () { + var i = 0; + // quit if this function has already been called + if (hinclude.addDOMLoadEvent.done) { + return; + } + hinclude.addDOMLoadEvent.done = true; + if (window.__load_timer) { + clearInterval(window.__load_timer); + window.__load_timer = null; + } + for (i; i < window.__load_events.length; i += 1) { + window.__load_events[i](); + } + window.__load_events = null; + // clean up the __ie_onload event + /*@cc_on document.getElementById("__ie_onload").onreadystatechange = ""; @*/ - }; - // for Mozilla/Opera9 - if (document.addEventListener) { - document.addEventListener("DOMContentLoaded", init, false); - } - // for Internet Explorer - /*@cc_on + }; + // for Mozilla/Opera9 + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", init, false); + } + // for Internet Explorer + /*@cc_on document.write( "<\/scr" @@ -361,21 +378,23 @@ var hinclude; } }; @*/ - // for Safari - if (/WebKit/i.test(navigator.userAgent)) { // sniff - window.__load_timer = setInterval(function () { - if (/loaded|complete/.test(document.readyState)) { - init(); - } - }, 10); - } - // for other browsers - window.onload = init; - window.__load_events = []; - } - window.__load_events.push(func); - } - }; + // for Safari + if (/WebKit/i.test(navigator.userAgent)) { // sniff + window.__load_timer = setInterval(function () { + if (/loaded|complete/.test(document.readyState)) { + init(); + } + }, 10); + } + // for other browsers + window.onload = init; + window.__load_events = []; + } + window.__load_events.push(func); + } + }; - hinclude.addDOMLoadEvent(function () {hinclude.run(); }); + hinclude.addDOMLoadEvent(function () { + hinclude.run(); + }); }()); From 6628bd10d54e5a043fce23caa2b3ce3ae666d092 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Fri, 22 Mar 2013 00:42:40 +0100 Subject: [PATCH 13/24] fix dependency js code included --- hinclude.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/hinclude.js b/hinclude.js index b8b4f56..08fd750 100644 --- a/hinclude.js +++ b/hinclude.js @@ -160,7 +160,7 @@ var hinclude; // moved head script into document head hinclude_move_head_script_to_document: function(script) { - if(!hinclude.isEmpty(script) && hinclude.move_head_to_document) { + if(script && hinclude.move_head_to_document) { var document_head= document.getElementsByTagName('head')[0]; var document_script= document.createElement('script'); document_script.type= 'text/javascript'; @@ -217,11 +217,13 @@ var hinclude; return true; }, - get_content_jsfile: function (element, req) { - if (req.readyState === 4) { - if (req.status === 200 || req.status === 304) { - hinclude.run_hinclude_js(null, req.responseText); - } + move_jsfile_to_document: function (js_src) { + if(js_src) { + var document_head= document.getElementsByTagName('head')[0]; + var document_script= document.createElement('script'); + document_script.type= 'text/javascript'; + document_script.src = js_src; + document_head.appendChild(document_script); } }, @@ -232,10 +234,9 @@ var hinclude; var js = include[0].getElementsByTagName("script"); if(js.length > 0) { var code = ''; - var callback = this.get_content_jsfile; for (var i=0; i < js.length; i++) { if(js[i].src) { - this.include(null,js[i].src,callback); + this.move_jsfile_to_document(js[i].src); } else { code = js[i].innerHTML; js_code = js_code+code; From e058b175e807b5bbd7fb15ae656507133654ef9e Mon Sep 17 00:00:00 2001 From: s7ntech Date: Fri, 22 Mar 2013 01:15:17 +0100 Subject: [PATCH 14/24] fix remove tag js --- hinclude.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hinclude.js b/hinclude.js index 08fd750..ad5a836 100644 --- a/hinclude.js +++ b/hinclude.js @@ -250,7 +250,7 @@ var hinclude; // removes script by content hinclude_remove_tag_script: function (js) { - if(!hinclude.isEmpty(js) && !hinclude.isEmpty(hinclude.remove_js)) { + if(!hinclude.isEmpty(js) && hinclude.remove_js) { for (var i=0; i < js.length; i++) { js[i].parentNode.removeChild(js[i]); i--; From ee5760b218a9fc001653e2f80212ab28ea5f1bb3 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Fri, 29 Mar 2013 10:45:16 +0100 Subject: [PATCH 15/24] restored indentation, fix error XMLSerializer, add test --- hinclude.js | 659 +++++++++++++++++++------------------- test/Makefile | 5 +- test/advanced.js | 8 + test/assets/advanced.html | 23 ++ test/assets/library.html | 11 + test/assets/onload.html | 23 ++ test/assets/sample_cod.js | 1 + test/assets/sample_lib.js | 3 + 8 files changed, 403 insertions(+), 330 deletions(-) create mode 100644 test/advanced.js create mode 100644 test/assets/advanced.html create mode 100644 test/assets/library.html create mode 100644 test/assets/onload.html create mode 100644 test/assets/sample_cod.js create mode 100644 test/assets/sample_lib.js diff --git a/hinclude.js b/hinclude.js index ad5a836..c4efc88 100644 --- a/hinclude.js +++ b/hinclude.js @@ -33,369 +33,370 @@ var hinclude; (function () { - "use strict"; + "use strict"; - hinclude = { - classprefix: "include_", - move_head_to_document: true, // moved head script into document head - remove_js: true, // removes script by content + hinclude = { + classprefix: "include_", + move_head_to_document: true, // moved head script into document head + remove_js: true, // removes script by content - set_content_async: function (element, req) { - if (req.readyState === 4) { - if (req.status === 200 || req.status === 304) { - element.innerHTML = req.responseText; - hinclude.hinclude_check_content(element, req.responseText); - } - element.className = hinclude.classprefix + req.status; - } - }, + set_content_async: function (element, req) { + if (req.readyState === 4) { + if (req.status === 200 || req.status === 304) { + element.innerHTML = req.responseText; + hinclude.hinclude_check_content(element, req.responseText); + } + element.className = hinclude.classprefix + req.status; + } + }, - buffer: [], - set_content_buffered: function (element, req) { - if (req.readyState === 4) { - hinclude.buffer.push([element, req]); - hinclude.outstanding -= 1; - if (hinclude.outstanding === 0) { - hinclude.show_buffered_content(); - } - } - }, + buffer: [], + set_content_buffered: function (element, req) { + if (req.readyState === 4) { + hinclude.buffer.push([element, req]); + hinclude.outstanding -= 1; + if (hinclude.outstanding === 0) { + hinclude.show_buffered_content(); + } + } + }, - show_buffered_content: function () { - if(hinclude.isEmpty(hinclude.buffer)){ - return false; - } - while (hinclude.buffer.length > 0) { - var include = hinclude.buffer.pop(); - if (include[1].status === 200 || include[1].status === 304) { - hinclude.hinclude_check_content(include, include[1].responseText); - } - include[0].className = hinclude.classprefix + include[1].status; - } - }, + show_buffered_content: function () { + if(hinclude.isEmpty(hinclude.buffer)){ + return false; + } + while (hinclude.buffer.length > 0) { + var include = hinclude.buffer.pop(); + if (include[1].status === 200 || include[1].status === 304) { + hinclude.hinclude_check_content(include, include[1].responseText); + } + include[0].className = hinclude.classprefix + include[1].status; + } + }, - outstanding: 0, - includes: [], - run: function () { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - this.includes = document.getElementsByTagName("hx:include"); - if (this.includes.length === 0) { // remove ns for IE - this.includes = document.getElementsByTagName("include"); - } - if (mode === "async") { - callback = this.set_content_async; - } else if (mode === "buffered") { - callback = this.set_content_buffered; - var timeout = this.get_meta("include_timeout", 2.5) * 1000; - setTimeout(hinclude.show_buffered_content, timeout); - } - for (i; i < this.includes.length; i += 1) { - this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); - } - }, + outstanding: 0, + includes: [], + run: function () { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + this.includes = document.getElementsByTagName("hx:include"); + if (this.includes.length === 0) { // remove ns for IE + this.includes = document.getElementsByTagName("include"); + } + if (mode === "async") { + callback = this.set_content_async; + } else if (mode === "buffered") { + callback = this.set_content_buffered; + var timeout = this.get_meta("include_timeout", 2.5) * 1000; + setTimeout(hinclude.show_buffered_content, timeout); + } + for (i; i < this.includes.length; i += 1) { + this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); + } + }, - // convert text into xml node - hinclude_xml_parser_content: function (content) { - var parsed_document = false; - if(!hinclude.isEmpty(content)) { - if (window.ActiveXObject){// for Internet Explorer - parsed_document = new ActiveXObject('Microsoft.XMLDOM'); - parsed_document.async='false'; - parsed_document.loadXML(content); - if(parsed_document.parseError.errorCode != 0) { - parsed_document = false; - } - } else { - var parser = new DOMParser(); - parsed_document = parser.parseFromString(content,'text/xml'); - if(parsed_document.getElementsByTagName("parsererror").length > 0) { - parsed_document = false; - } + // convert text into xml node + hinclude_xml_parser_content: function (content) { + var parsed_document = false; + if(!hinclude.isEmpty(content)) { + if (window.ActiveXObject){// for Internet Explorer + parsed_document = new ActiveXObject('Microsoft.XMLDOM'); + parsed_document.async='false'; + parsed_document.loadXML(content); + if(parsed_document.parseError.errorCode != 0) { + parsed_document = false; + } + } else { + var parser = new DOMParser(); + parsed_document = parser.parseFromString(content,'text/xml'); + if(parsed_document.getElementsByTagName("parsererror").length > 0) { + parsed_document = false; } } - return parsed_document; - }, + } + return parsed_document; + }, - // verification content hinclude - hinclude_check_content: function(include, content) { - var parsed_document = this.hinclude_xml_parser_content(content); - this.hinclude_check_head_script(parsed_document); - this.move_html_to_hinclude(include, parsed_document, content); - var js_onload = this.hinclude_check_onload_body(parsed_document); - var js_code = this.hinclude_check_js_code(include); - this.run_hinclude_js(js_onload, js_code); - }, + // verification content hinclude + hinclude_check_content: function(include, content) { + var parsed_document = this.hinclude_xml_parser_content(content); + this.hinclude_check_head_script(parsed_document); + this.move_html_to_hinclude(include, parsed_document, content); + var js_onload = this.hinclude_check_onload_body(parsed_document); + var js_code = this.hinclude_check_js_code(include); + this.run_hinclude_js(js_onload, js_code); + }, - // verificarion exist head script - hinclude_check_head_script: function(parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var head = parsed_document.getElementsByTagName('head'); - if(head.length > 0) { - var script = head[0].getElementsByTagName('script'); - if(!hinclude.isEmpty(script)) { - this.hinclude_move_head_script_to_document(script[0]); - } + // verificarion exist head script + hinclude_check_head_script: function(parsed_document) { + //xml document + if(!hinclude.isEmpty(parsed_document)) { + var head = parsed_document.getElementsByTagName('head'); + if(head.length > 0) { + var script = head[0].getElementsByTagName('script'); + if(!hinclude.isEmpty(script)) { + this.hinclude_move_head_script_to_document(script[0]); } } - }, + } + }, - // verification exist onload event - hinclude_check_onload_body: function (parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var body = parsed_document.getElementsByTagName('body'); - var onload = false; - if(body.length > 0){ - if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ - onload = body[0].getAttribute('onload'); - } + // verification exist onload event + hinclude_check_onload_body: function (parsed_document) { + //xml document + if(!hinclude.isEmpty(parsed_document)) { + var body = parsed_document.getElementsByTagName('body'); + var onload = false; + if(body.length > 0){ + if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ + onload = body[0].getAttribute('onload'); } - return onload; } - return ''; - }, + return onload; + } + return ''; + }, - // moved head script into document head - hinclude_move_head_script_to_document: function(script) { - if(script && hinclude.move_head_to_document) { - var document_head= document.getElementsByTagName('head')[0]; - var document_script= document.createElement('script'); - document_script.type= 'text/javascript'; - try { - document_script.innerHTML = script.textContent; - }catch (e) { - // Internet Explorer - document_script.text = script.text; - } - document_head.appendChild(document_script); - script.parentNode.removeChild(script); + // moved head script into document head + hinclude_move_head_script_to_document: function(script) { + if(script && hinclude.move_head_to_document) { + var document_head= document.getElementsByTagName('head')[0]; + var document_script= document.createElement('script'); + document_script.type= 'text/javascript'; + try { + document_script.innerHTML = script.textContent; + }catch (e) { + // Internet Explorer + document_script.text = script.text; } - }, + document_head.appendChild(document_script); + script.parentNode.removeChild(script); + } + }, - // inserts html content into hinclude - move_html_to_hinclude: function(include, parsed_document, content) { - var string = ''; - if(!hinclude.isEmpty(parsed_document)) { - string = this.xml_to_string(parsed_document); - } else if(!hinclude.isEmpty(content)) { - string = content; - } - include[0].innerHTML = string; - }, + // inserts html content into hinclude + move_html_to_hinclude: function(include, parsed_document, content) { + var string = ''; + if(!hinclude.isEmpty(parsed_document)) { + string = this.xml_to_string(parsed_document); + } else if(!hinclude.isEmpty(content)) { + string = content; + } + include[0].innerHTML = string; + }, - // convert xml node into string - xml_to_string: function(parsed_document) { - try { - // Gecko-based browsers, Safari, Opera. - return (new XMLSerializer()).serializeToString(parsed_document); - } - catch (e) { - try { - // Internet Explorer. - return parsed_document.xml; - } - catch (e) - { - //Strange Browser ?? - alert('Xmlserializer not supported'); - } - } - return false; - }, - - isEmpty: function(value) { - if (value === null || value === undefined) return true; - if (value.length && value.length > 0) return false; - if (value.length === 0) return true; + // convert xml node into string + xml_to_string: function(parsed_document) { + try { + // Gecko-based browsers, Safari, Opera. + var serialize = (new XMLSerializer()).serializeToString(parsed_document); + //fix strip closed tag script + serialize = serialize.replace(//g,''); + + return serialize; + } + catch (e) { + try { + // Internet Explorer. + return parsed_document.xml; + } + catch (e) + {//Strange Browser ?? + alert('Xmlserializer not supported'); + } + } + return false; + }, - for (var key in value) { - if (hasOwnProperty.call(value, key)) return false; - } - return true; - }, + isEmpty: function(value) { + if (value === null || value === undefined) return true; + if (value.length && value.length > 0) return false; + if (value.length === 0) return true; - move_jsfile_to_document: function (js_src) { - if(js_src) { - var document_head= document.getElementsByTagName('head')[0]; - var document_script= document.createElement('script'); - document_script.type= 'text/javascript'; - document_script.src = js_src; - document_head.appendChild(document_script); - } - }, + for (var key in value) { + if (hasOwnProperty.call(value, key)) return false; + } + return true; + }, - // verification exists scripts into content - hinclude_check_js_code: function(include) { - var js_code = ''; - if(!hinclude.isEmpty(include)) { - var js = include[0].getElementsByTagName("script"); - if(js.length > 0) { - var code = ''; - for (var i=0; i < js.length; i++) { - if(js[i].src) { - this.move_jsfile_to_document(js[i].src); - } else { - code = js[i].innerHTML; - js_code = js_code+code; - } - } - this.hinclude_remove_tag_script(js); - } - } - return js_code; - }, + move_jsfile_to_document: function (js_src) { + if(js_src) { + var document_head= document.getElementsByTagName('head')[0]; + var document_script= document.createElement('script'); + document_script.type= 'text/javascript'; + document_script.src = js_src; + document_head.appendChild(document_script); + } + }, - // removes script by content - hinclude_remove_tag_script: function (js) { - if(!hinclude.isEmpty(js) && hinclude.remove_js) { + // verification exists scripts into content + hinclude_check_js_code: function(include) { + var js_code = ''; + if(!hinclude.isEmpty(include)) { + var js = include[0].getElementsByTagName("script"); + if(js.length > 0) { + var code = ''; for (var i=0; i < js.length; i++) { - js[i].parentNode.removeChild(js[i]); - i--; + if(js[i].src) { + this.move_jsfile_to_document(js[i].src); + } else { + code = js[i].innerHTML; + js_code = js_code+code; + } } + this.hinclude_remove_tag_script(js); } - }, + } + return js_code; + }, - // execute code js - run_hinclude_js: function (js_onload, js_code) { - if(!hinclude.isEmpty(js_code)) { - eval(js_code); - } - if(!hinclude.isEmpty(js_onload)) { - eval(js_onload); + // removes script by content + hinclude_remove_tag_script: function (js) { + if(!hinclude.isEmpty(js) && hinclude.remove_js) { + for (var i=0; i < js.length; i++) { + js[i].parentNode.removeChild(js[i]); + i--; } - }, + } + }, + + // execute code js + run_hinclude_js: function (js_onload, js_code) { + if(!hinclude.isEmpty(js_code)) { + eval(js_code); + } + if(!hinclude.isEmpty(js_onload)) { + eval(js_onload); + } + }, - include: function (element, url, incl_cb) { - var scheme = url.substring(0, url.indexOf(":")); - if (scheme.toLowerCase() === "data") { // just text/plain for now - var data = decodeURIComponent(url.substring(url.indexOf(",") + 1, url.length)); - element.innerHTML = data; - } else { - var req = false; - if (window.XMLHttpRequest) { - try { - req = new XMLHttpRequest(); - } catch (e1) { - req = false; - } - } else if (window.ActiveXObject) { - try { - req = new ActiveXObject("Microsoft.XMLHTTP"); - } catch (e2) { - req = false; - } - } - if (req) { - this.outstanding += 1; - req.onreadystatechange = function () { - incl_cb(element, req); - }; - try { - req.open("GET", url, true); - req.send(""); - } catch (e3) { - this.outstanding -= 1; - alert("Include error: " + url + " (" + e3 + ")"); - } - } - } - }, + include: function (element, url, incl_cb) { + var scheme = url.substring(0, url.indexOf(":")); + if (scheme.toLowerCase() === "data") { // just text/plain for now + var data = decodeURIComponent(url.substring(url.indexOf(",") + 1, url.length)); + element.innerHTML = data; + } else { + var req = false; + if (window.XMLHttpRequest) { + try { + req = new XMLHttpRequest(); + } catch (e1) { + req = false; + } + } else if (window.ActiveXObject) { + try { + req = new ActiveXObject("Microsoft.XMLHTTP"); + } catch (e2) { + req = false; + } + } + if (req) { + this.outstanding += 1; + req.onreadystatechange = function () { + incl_cb(element, req); + }; + try { + req.open("GET", url, true); + req.send(""); + } catch (e3) { + this.outstanding -= 1; + alert("Include error: " + url + " (" + e3 + ")"); + } + } + } + }, - refresh: function (element_id) { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - callback = this.set_content_buffered; - for (i; i < this.includes.length; i += 1) { - if (this.includes[i].getAttribute("id") === element_id) { - this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); - } - } - }, + refresh: function (element_id) { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + callback = this.set_content_buffered; + for (i; i < this.includes.length; i += 1) { + if (this.includes[i].getAttribute("id") === element_id) { + this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); + } + } + }, - get_meta: function (name, value_default) { - var metas = document.getElementsByTagName("meta"); - if(!hinclude.isEmpty(metas)) { - for (var m = 0; m < metas.length; m += 1) { - var meta_name = metas[m].getAttribute("name"); - if (meta_name === name) { - return metas[m].getAttribute("content"); - } - } + get_meta: function (name, value_default) { + var metas = document.getElementsByTagName("meta"); + if(!hinclude.isEmpty(metas)) { + for (var m = 0; m < metas.length; m += 1) { + var meta_name = metas[m].getAttribute("name"); + if (meta_name === name) { + return metas[m].getAttribute("content"); } - return value_default; - }, + } + } + return value_default; + }, - /* - * (c)2006 Dean Edwards/Matthias Miller/John Resig - * Special thanks to Dan Webb's domready.js Prototype extension - * and Simon Willison's addLoadEvent - * - * For more info, see: - * http://dean.edwards.name/weblog/2006/06/again/ - * - * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) - */ - addDOMLoadEvent: function (func) { - if (!window.__load_events) { - var init = function () { - var i = 0; - // quit if this function has already been called - if (hinclude.addDOMLoadEvent.done) { - return; - } - hinclude.addDOMLoadEvent.done = true; - if (window.__load_timer) { - clearInterval(window.__load_timer); - window.__load_timer = null; - } - for (i; i < window.__load_events.length; i += 1) { - window.__load_events[i](); - } - window.__load_events = null; - // clean up the __ie_onload event - /*@cc_on - document.getElementById("__ie_onload").onreadystatechange = ""; - @*/ - }; - // for Mozilla/Opera9 - if (document.addEventListener) { - document.addEventListener("DOMContentLoaded", init, false); - } - // for Internet Explorer - /*@cc_on - document.write( - "<\/scr" - + "ipt>" - ); - var script = document.getElementById("__ie_onload"); - script.onreadystatechange = function () { - if (this.readyState === "complete") { - init(); // call the onload handler - } - }; - @*/ - // for Safari - if (/WebKit/i.test(navigator.userAgent)) { // sniff - window.__load_timer = setInterval(function () { - if (/loaded|complete/.test(document.readyState)) { - init(); - } - }, 10); - } - // for other browsers - window.onload = init; - window.__load_events = []; + /* + * (c)2006 Dean Edwards/Matthias Miller/John Resig + * Special thanks to Dan Webb's domready.js Prototype extension + * and Simon Willison's addLoadEvent + * + * For more info, see: + * http://dean.edwards.name/weblog/2006/06/again/ + * + * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) + */ + addDOMLoadEvent: function (func) { + if (!window.__load_events) { + var init = function () { + var i = 0; + // quit if this function has already been called + if (hinclude.addDOMLoadEvent.done) { + return; + } + hinclude.addDOMLoadEvent.done = true; + if (window.__load_timer) { + clearInterval(window.__load_timer); + window.__load_timer = null; + } + for (i; i < window.__load_events.length; i += 1) { + window.__load_events[i](); + } + window.__load_events = null; + // clean up the __ie_onload event + /*@cc_on + document.getElementById("__ie_onload").onreadystatechange = ""; + @*/ + }; + // for Mozilla/Opera9 + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", init, false); + } + // for Internet Explorer + /*@cc_on + document.write( + "<\/scr" + + "ipt>" + ); + var script = document.getElementById("__ie_onload"); + script.onreadystatechange = function () { + if (this.readyState === "complete") { + init(); // call the onload handler + } + }; + @*/ + // for Safari + if (/WebKit/i.test(navigator.userAgent)) { // sniff + window.__load_timer = setInterval(function () { + if (/loaded|complete/.test(document.readyState)) { + init(); } - window.__load_events.push(func); + }, 10); } - }; + // for other browsers + window.onload = init; + window.__load_events = []; + } + window.__load_events.push(func); + } + }; - hinclude.addDOMLoadEvent(function () { - hinclude.run(); - }); + hinclude.addDOMLoadEvent(function () {hinclude.run(); }); }()); diff --git a/test/Makefile b/test/Makefile index 07d400d..f6d19c7 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,9 @@ test_port=8081 -test: basic none +test: advanced basic none + +advanced: server + phantomjs advanced.js $(test_port) basic: server phantomjs basic.js $(test_port) diff --git a/test/advanced.js b/test/advanced.js new file mode 100644 index 0000000..ce0fbec --- /dev/null +++ b/test/advanced.js @@ -0,0 +1,8 @@ +var runTests = require('./framework.js').runTests; + +var tests = [ + ['#onload', "this onload is executed"], + ['#library', "this library is visible"] +]; + +runTests("advanced.html", tests); \ No newline at end of file diff --git a/test/assets/advanced.html b/test/assets/advanced.html new file mode 100644 index 0000000..ed11c45 --- /dev/null +++ b/test/assets/advanced.html @@ -0,0 +1,23 @@ + + + + + HInclude Tests + + + + + + +

hinclude.js test page

+ +

Advanced Include

+ +

+

+ + + \ No newline at end of file diff --git a/test/assets/library.html b/test/assets/library.html new file mode 100644 index 0000000..c3ca122 --- /dev/null +++ b/test/assets/library.html @@ -0,0 +1,11 @@ + + +Library Test + + +error + + + + + \ No newline at end of file diff --git a/test/assets/onload.html b/test/assets/onload.html new file mode 100644 index 0000000..ff4882a --- /dev/null +++ b/test/assets/onload.html @@ -0,0 +1,23 @@ + + +onLoad Test + + + + error +
+ + \ No newline at end of file diff --git a/test/assets/sample_cod.js b/test/assets/sample_cod.js new file mode 100644 index 0000000..574c05b --- /dev/null +++ b/test/assets/sample_cod.js @@ -0,0 +1 @@ +document.getElementById('library').innerHTML = hinc.library + ' visible'; \ No newline at end of file diff --git a/test/assets/sample_lib.js b/test/assets/sample_lib.js new file mode 100644 index 0000000..1cd40c9 --- /dev/null +++ b/test/assets/sample_lib.js @@ -0,0 +1,3 @@ +hinc = { + library: "this library is" +}; \ No newline at end of file From b1d0d40b196aba99e06ebd2082dce0e34100460e Mon Sep 17 00:00:00 2001 From: s7ntech Date: Fri, 5 Apr 2013 18:15:49 +0200 Subject: [PATCH 16/24] fix issue #23 Nested hincludes --- hinclude.js | 25 +++++++++++++++++++++++++ test/advanced.js | 3 ++- test/assets/advanced.html | 2 +- test/assets/child | 1 + test/assets/child_hinclude.html | 8 ++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/assets/child create mode 100644 test/assets/child_hinclude.html diff --git a/hinclude.js b/hinclude.js index c4efc88..6b50006 100644 --- a/hinclude.js +++ b/hinclude.js @@ -126,6 +126,7 @@ var hinclude; var js_onload = this.hinclude_check_onload_body(parsed_document); var js_code = this.hinclude_check_js_code(include); this.run_hinclude_js(js_onload, js_code); + this.hinclude_check_child_include(include); }, // verificarion exist head script @@ -230,6 +231,30 @@ var hinclude; } }, + // verification exists child hinclude + child_includes: [], + hinclude_check_child_include: function(include) { + if(!hinclude.isEmpty(include)) { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + this.child_includes = include[0].getElementsByTagName("hx:include"); + if (this.child_includes.length === 0) { // remove ns for IE + this.child_includes = include[0].getElementsByTagName("include"); + } + if (mode === "async") { + callback = this.set_content_async; + } else if (mode === "buffered") { + callback = this.set_content_buffered; + var timeout = this.get_meta("include_timeout", 2.5) * 1000; + setTimeout(hinclude.show_buffered_content, timeout); + } + for (i; i < this.child_includes.length; i += 1) { + this.include(this.child_includes[i], this.child_includes[i].getAttribute("src"), callback); + } + } + }, + // verification exists scripts into content hinclude_check_js_code: function(include) { var js_code = ''; diff --git a/test/advanced.js b/test/advanced.js index ce0fbec..b8af73b 100644 --- a/test/advanced.js +++ b/test/advanced.js @@ -2,7 +2,8 @@ var runTests = require('./framework.js').runTests; var tests = [ ['#onload', "this onload is executed"], - ['#library', "this library is visible"] + ['#library', "this library is visible"], + ['#child', "child included"] ]; runTests("advanced.html", tests); \ No newline at end of file diff --git a/test/assets/advanced.html b/test/assets/advanced.html index ed11c45..768220c 100644 --- a/test/assets/advanced.html +++ b/test/assets/advanced.html @@ -18,6 +18,6 @@

Advanced Include

- +

\ No newline at end of file diff --git a/test/assets/child b/test/assets/child new file mode 100644 index 0000000..2e183b9 --- /dev/null +++ b/test/assets/child @@ -0,0 +1 @@ +child included \ No newline at end of file diff --git a/test/assets/child_hinclude.html b/test/assets/child_hinclude.html new file mode 100644 index 0000000..c9f2378 --- /dev/null +++ b/test/assets/child_hinclude.html @@ -0,0 +1,8 @@ + + +Child Test + + + + + \ No newline at end of file From ee0666e81fe49949f895d95e4a442e9a2f3c9d47 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Fri, 5 Apr 2013 21:02:49 +0200 Subject: [PATCH 17/24] correction code for test jslint --- hinclude.js | 349 ++++++++++++++++++++++++++-------------------------- 1 file changed, 177 insertions(+), 172 deletions(-) diff --git a/hinclude.js b/hinclude.js index 6b50006..eac11bb 100644 --- a/hinclude.js +++ b/hinclude.js @@ -26,8 +26,8 @@ SOFTWARE. See http://mnot.github.com/hinclude/ for documentation. */ -/*jslint indent: 2, browser: true, vars: true, nomen: true */ -/*global alert, ActiveXObject */ +/*jslint indent: 2, browser: true, vars: true, nomen: true, plusplus: true, evil: true */ +/*global alert, ActiveXObject, DOMParser, XMLSerializer */ var hinclude; @@ -60,15 +60,15 @@ var hinclude; } } }, - + show_buffered_content: function () { - if(hinclude.isEmpty(hinclude.buffer)){ + if (hinclude.isEmpty(hinclude.buffer)) { return false; } while (hinclude.buffer.length > 0) { var include = hinclude.buffer.pop(); if (include[1].status === 200 || include[1].status === 304) { - hinclude.hinclude_check_content(include, include[1].responseText); + hinclude.hinclude_check_content(include, include[1].responseText); } include[0].className = hinclude.classprefix + include[1].status; } @@ -95,205 +95,209 @@ var hinclude; this.include(this.includes[i], this.includes[i].getAttribute("src"), callback); } }, - + // convert text into xml node hinclude_xml_parser_content: function (content) { - var parsed_document = false; - if(!hinclude.isEmpty(content)) { - if (window.ActiveXObject){// for Internet Explorer - parsed_document = new ActiveXObject('Microsoft.XMLDOM'); - parsed_document.async='false'; - parsed_document.loadXML(content); - if(parsed_document.parseError.errorCode != 0) { - parsed_document = false; - } - } else { - var parser = new DOMParser(); - parsed_document = parser.parseFromString(content,'text/xml'); - if(parsed_document.getElementsByTagName("parsererror").length > 0) { - parsed_document = false; - } - } - } - return parsed_document; + var parsed_document = false; + if (!hinclude.isEmpty(content)) { + if (window.ActiveXObject) {// for Internet Explorer + parsed_document = new ActiveXObject('Microsoft.XMLDOM'); + parsed_document.async = 'false'; + parsed_document.loadXML(content); + if (parsed_document.parseError.errorCode !== 0) { + parsed_document = false; + } + } else { + var parser = new DOMParser(); + parsed_document = parser.parseFromString(content, 'text/xml'); + if (parsed_document.getElementsByTagName("parsererror").length > 0) { + parsed_document = false; + } + } + } + return parsed_document; }, - + // verification content hinclude - hinclude_check_content: function(include, content) { - var parsed_document = this.hinclude_xml_parser_content(content); - this.hinclude_check_head_script(parsed_document); - this.move_html_to_hinclude(include, parsed_document, content); - var js_onload = this.hinclude_check_onload_body(parsed_document); - var js_code = this.hinclude_check_js_code(include); - this.run_hinclude_js(js_onload, js_code); - this.hinclude_check_child_include(include); + hinclude_check_content: function (include, content) { + var parsed_document = this.hinclude_xml_parser_content(content); + this.hinclude_check_head_script(parsed_document); + this.move_html_to_hinclude(include, parsed_document, content); + var js_onload = this.hinclude_check_onload_body(parsed_document); + var js_code = this.hinclude_check_js_code(include); + this.run_hinclude_js(js_onload, js_code); + this.hinclude_check_child_include(include); }, - + // verificarion exist head script - hinclude_check_head_script: function(parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var head = parsed_document.getElementsByTagName('head'); - if(head.length > 0) { - var script = head[0].getElementsByTagName('script'); - if(!hinclude.isEmpty(script)) { - this.hinclude_move_head_script_to_document(script[0]); - } - } + hinclude_check_head_script: function (parsed_document) { + //xml document + if (!hinclude.isEmpty(parsed_document)) { + var head = parsed_document.getElementsByTagName('head'); + if (head.length > 0) { + var script = head[0].getElementsByTagName('script'); + if (!hinclude.isEmpty(script)) { + this.hinclude_move_head_script_to_document(script[0]); + } } + } }, - + // verification exist onload event hinclude_check_onload_body: function (parsed_document) { - //xml document - if(!hinclude.isEmpty(parsed_document)) { - var body = parsed_document.getElementsByTagName('body'); - var onload = false; - if(body.length > 0){ - if(!hinclude.isEmpty(body[0].getAttribute('onload'))){ - onload = body[0].getAttribute('onload'); - } - } - return onload; + //xml document + if (!hinclude.isEmpty(parsed_document)) { + var body = parsed_document.getElementsByTagName('body'); + var onload = false; + if (body.length > 0) { + if (!hinclude.isEmpty(body[0].getAttribute('onload'))) { + onload = body[0].getAttribute('onload'); + } } - return ''; + return onload; + } + return ''; }, - + // moved head script into document head - hinclude_move_head_script_to_document: function(script) { - if(script && hinclude.move_head_to_document) { - var document_head= document.getElementsByTagName('head')[0]; - var document_script= document.createElement('script'); - document_script.type= 'text/javascript'; - try { - document_script.innerHTML = script.textContent; - }catch (e) { - // Internet Explorer - document_script.text = script.text; - } - document_head.appendChild(document_script); - script.parentNode.removeChild(script); + hinclude_move_head_script_to_document: function (script) { + if (script && hinclude.move_head_to_document) { + var document_head = document.getElementsByTagName('head')[0]; + var document_script = document.createElement('script'); + document_script.type = 'text/javascript'; + try { + document_script.innerHTML = script.textContent; + } catch (e) { + // Internet Explorer + document_script.text = script.text; } + document_head.appendChild(document_script); + script.parentNode.removeChild(script); + } }, - + // inserts html content into hinclude - move_html_to_hinclude: function(include, parsed_document, content) { - var string = ''; - if(!hinclude.isEmpty(parsed_document)) { - string = this.xml_to_string(parsed_document); - } else if(!hinclude.isEmpty(content)) { - string = content; - } - include[0].innerHTML = string; + move_html_to_hinclude: function (include, parsed_document, content) { + var string = ''; + if (!hinclude.isEmpty(parsed_document)) { + string = this.xml_to_string(parsed_document); + } else if (!hinclude.isEmpty(content)) { + string = content; + } + include[0].innerHTML = string; }, - + // convert xml node into string - xml_to_string: function(parsed_document) { + xml_to_string: function (parsed_document) { + try { + // Gecko-based browsers, Safari, Opera. + var serialize = (new XMLSerializer()).serializeToString(parsed_document); + //fix strip closed tag script + serialize = serialize.replace(//g, ''); + return serialize; + } catch (e1) { try { - // Gecko-based browsers, Safari, Opera. - var serialize = (new XMLSerializer()).serializeToString(parsed_document); - //fix strip closed tag script - serialize = serialize.replace(//g,''); - - return serialize; + // Internet Explorer. + return parsed_document.xml; + } catch (e2) { + //Strange Browser ?? + alert('Xmlserializer not supported'); } - catch (e) { - try { - // Internet Explorer. - return parsed_document.xml; - } - catch (e) - {//Strange Browser ?? - alert('Xmlserializer not supported'); - } - } - return false; + } + return false; }, - isEmpty: function(value) { - if (value === null || value === undefined) return true; - if (value.length && value.length > 0) return false; - if (value.length === 0) return true; - - for (var key in value) { - if (hasOwnProperty.call(value, key)) return false; + isEmpty: function (value) { + if (value === null || value === undefined) { return true; } + if (value.length && value.length > 0) { return false; } + if (value.length === 0) { return true; } + var type = typeof value; + if (type === 'object') { + var key; + for (key in value) { + if (value.hasOwnProperty(key)) { + return false; + } } - return true; + } + return true; }, - + move_jsfile_to_document: function (js_src) { - if(js_src) { - var document_head= document.getElementsByTagName('head')[0]; - var document_script= document.createElement('script'); - document_script.type= 'text/javascript'; - document_script.src = js_src; - document_head.appendChild(document_script); - } + if (js_src) { + var document_head = document.getElementsByTagName('head')[0]; + var document_script = document.createElement('script'); + document_script.type = 'text/javascript'; + document_script.src = js_src; + document_head.appendChild(document_script); + } }, - + // verification exists child hinclude child_includes: [], - hinclude_check_child_include: function(include) { - if(!hinclude.isEmpty(include)) { - var i = 0; - var mode = this.get_meta("include_mode", "buffered"); - var callback = function (element, req) {}; - this.child_includes = include[0].getElementsByTagName("hx:include"); - if (this.child_includes.length === 0) { // remove ns for IE - this.child_includes = include[0].getElementsByTagName("include"); - } - if (mode === "async") { - callback = this.set_content_async; - } else if (mode === "buffered") { - callback = this.set_content_buffered; - var timeout = this.get_meta("include_timeout", 2.5) * 1000; - setTimeout(hinclude.show_buffered_content, timeout); - } - for (i; i < this.child_includes.length; i += 1) { - this.include(this.child_includes[i], this.child_includes[i].getAttribute("src"), callback); - } + hinclude_check_child_include: function (include) { + if (!hinclude.isEmpty(include)) { + var i = 0; + var mode = this.get_meta("include_mode", "buffered"); + var callback = function (element, req) {}; + this.child_includes = include[0].getElementsByTagName("hx:include"); + if (this.child_includes.length === 0) { // remove ns for IE + this.child_includes = include[0].getElementsByTagName("include"); + } + if (mode === "async") { + callback = this.set_content_async; + } else if (mode === "buffered") { + callback = this.set_content_buffered; + var timeout = this.get_meta("include_timeout", 2.5) * 1000; + setTimeout(hinclude.show_buffered_content, timeout); + } + for (i; i < this.child_includes.length; i += 1) { + this.include(this.child_includes[i], this.child_includes[i].getAttribute("src"), callback); } + } }, - + // verification exists scripts into content - hinclude_check_js_code: function(include) { - var js_code = ''; - if(!hinclude.isEmpty(include)) { - var js = include[0].getElementsByTagName("script"); - if(js.length > 0) { - var code = ''; - for (var i=0; i < js.length; i++) { - if(js[i].src) { - this.move_jsfile_to_document(js[i].src); - } else { - code = js[i].innerHTML; - js_code = js_code+code; - } - } - this.hinclude_remove_tag_script(js); + hinclude_check_js_code: function (include) { + var js_code = ''; + if (!hinclude.isEmpty(include)) { + var js = include[0].getElementsByTagName("script"); + if (js.length > 0) { + var code = ''; + var i = 0; + for (i; i < js.length; i++) { + if (js[i].src) { + this.move_jsfile_to_document(js[i].src); + } else { + code = js[i].innerHTML; + js_code = js_code + code; } - } - return js_code; + } + this.hinclude_remove_tag_script(js); + } + } + return js_code; }, - + // removes script by content hinclude_remove_tag_script: function (js) { - if(!hinclude.isEmpty(js) && hinclude.remove_js) { - for (var i=0; i < js.length; i++) { - js[i].parentNode.removeChild(js[i]); - i--; - } + if (!hinclude.isEmpty(js) && hinclude.remove_js) { + var i = 0; + for (i; i < js.length; i++) { + js[i].parentNode.removeChild(js[i]); + i--; } + } }, - + // execute code js run_hinclude_js: function (js_onload, js_code) { - if(!hinclude.isEmpty(js_code)) { - eval(js_code); - } - if(!hinclude.isEmpty(js_onload)) { - eval(js_onload); - } + if (!hinclude.isEmpty(js_code)) { + eval(js_code); + } + if (!hinclude.isEmpty(js_onload)) { + eval(js_onload); + } }, include: function (element, url, incl_cb) { @@ -346,13 +350,14 @@ var hinclude; get_meta: function (name, value_default) { var metas = document.getElementsByTagName("meta"); - if(!hinclude.isEmpty(metas)) { - for (var m = 0; m < metas.length; m += 1) { - var meta_name = metas[m].getAttribute("name"); - if (meta_name === name) { - return metas[m].getAttribute("content"); - } + if (!hinclude.isEmpty(metas)) { + var m = 0; + for (m; m < metas.length; m += 1) { + var meta_name = metas[m].getAttribute("name"); + if (meta_name === name) { + return metas[m].getAttribute("content"); } + } } return value_default; }, @@ -373,7 +378,7 @@ var hinclude; var i = 0; // quit if this function has already been called if (hinclude.addDOMLoadEvent.done) { - return; + return; } hinclude.addDOMLoadEvent.done = true; if (window.__load_timer) { From 9e52d90f7e239062ec355e13b5185010cd064847 Mon Sep 17 00:00:00 2001 From: s7ntech Date: Sat, 1 Jun 2013 18:58:01 +0200 Subject: [PATCH 18/24] verification loading library after inclusion --- hinclude.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/hinclude.js b/hinclude.js index dc59788..a0f3ed3 100644 --- a/hinclude.js +++ b/hinclude.js @@ -224,12 +224,30 @@ var hinclude; return true; }, - move_jsfile_to_document: function (js_src) { + load_js_src_from_content: function (items, iteration) { + if (!iteration) iteration = 0; + if (items[iteration]) { + this.move_jsfile_to_document( + items[iteration], + function () { + hinclude.load_js_src_from_content(items, iteration+1); + } + ); + } + }, + + move_jsfile_to_document: function (js_src, callback) { if (js_src) { var document_head = document.getElementsByTagName('head')[0]; var document_script = document.createElement('script'); document_script.type = 'text/javascript'; document_script.src = js_src; + if (callback) { + document_script.onreadystatechange = function () { + if (this.readyState == 'loaded') callback(); + } + document_script.onload = callback; + } document_head.appendChild(document_script); } }, @@ -253,7 +271,7 @@ var hinclude; setTimeout(hinclude.show_buffered_content, timeout); } for (i; i < this.child_includes.length; i += 1) { - this.include(this.child_includes[i], this.child_includes[i].getAttribute("src"), this.includes[i].getAttribute("media"), callback); + this.include(this.child_includes[i], this.child_includes[i].getAttribute("src"), this.child_includes[i].getAttribute("media"), callback); } } }, @@ -266,14 +284,16 @@ var hinclude; if (js.length > 0) { var code = ''; var i = 0; + var js_src = []; for (i; i < js.length; i++) { if (js[i].src) { - this.move_jsfile_to_document(js[i].src); + js_src.push(js[i].src); } else { code = js[i].innerHTML; js_code = js_code + code; } } + this.load_js_src_from_content(js_src); this.hinclude_remove_tag_script(js); } } From b96306f73800acf3ce78b4ff193cc7a64781a0dd Mon Sep 17 00:00:00 2001 From: s7ntech Date: Sat, 1 Jun 2013 23:41:50 +0200 Subject: [PATCH 19/24] fix test media and sintax for jslint --- hinclude.js | 11 ++++++----- test/assets/large | 2 +- test/assets/small | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/hinclude.js b/hinclude.js index a0f3ed3..deec914 100644 --- a/hinclude.js +++ b/hinclude.js @@ -225,12 +225,12 @@ var hinclude; }, load_js_src_from_content: function (items, iteration) { - if (!iteration) iteration = 0; + if (!iteration) { iteration = 0; } if (items[iteration]) { this.move_jsfile_to_document( items[iteration], function () { - hinclude.load_js_src_from_content(items, iteration+1); + hinclude.load_js_src_from_content(items, iteration + 1); } ); } @@ -239,13 +239,14 @@ var hinclude; move_jsfile_to_document: function (js_src, callback) { if (js_src) { var document_head = document.getElementsByTagName('head')[0]; - var document_script = document.createElement('script'); + var document_script; + document_script = document.createElement('script'); document_script.type = 'text/javascript'; document_script.src = js_src; if (callback) { document_script.onreadystatechange = function () { - if (this.readyState == 'loaded') callback(); - } + if (this.readyState === 'loaded') { callback(); } + }; document_script.onload = callback; } document_head.appendChild(document_script); diff --git a/test/assets/large b/test/assets/large index e564f59..fc4159f 100644 --- a/test/assets/large +++ b/test/assets/large @@ -1 +1 @@ -Large viewport +Large viewport diff --git a/test/assets/small b/test/assets/small index 66de904..d2a3ee0 100644 --- a/test/assets/small +++ b/test/assets/small @@ -1 +1 @@ -Small viewport +Small viewport From 1502b941e97e34fb072abf59c12c76a76f7b0cef Mon Sep 17 00:00:00 2001 From: s7ntech Date: Sat, 28 Sep 2013 07:30:00 +0200 Subject: [PATCH 20/24] small fix for compatibility with ie (bug innerHTML), related to the issue #29 --- hinclude.js | 21 ++++++++++++++++++++- test/assets/onload.html | 9 +++++++-- test/assets/sample_cod.js | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/hinclude.js b/hinclude.js index deec914..2f9e61e 100644 --- a/hinclude.js +++ b/hinclude.js @@ -185,7 +185,18 @@ var hinclude; } else if (!hinclude.isEmpty(content)) { string = content; } - include[0].innerHTML = string; + if (this.detectIe7_8()) { + include[0].appendChild(this.fixInnerHtml(string)); + } else { + include[0].innerHTML = string; + } + }, + + fixInnerHtml: function (value) { + var new_element; + new_element = document.createElement('div'); + new_element.innerHTML = value; + return new_element; }, // convert xml node into string @@ -214,6 +225,7 @@ var hinclude; if (value.length === 0) { return true; } var type = typeof value; if (type === 'object') { + if (document.getElementsByTagName('html')) { return false; } var key; for (key in value) { if (value.hasOwnProperty(key)) { @@ -387,6 +399,13 @@ var hinclude; return value_default; }, + detectIe7_8: function () { + if ((document.all && !document.querySelector) || (document.all && document.querySelector && !document.addEventListener)) { + return true; + } + return false; + }, + /* * (c)2006 Dean Edwards/Matthias Miller/John Resig * Special thanks to Dan Webb's domready.js Prototype extension diff --git a/test/assets/onload.html b/test/assets/onload.html index ff4882a..d7547fd 100644 --- a/test/assets/onload.html +++ b/test/assets/onload.html @@ -3,14 +3,19 @@ onLoad Test '); + serialize = this.fix_strip_closed_tag(serialize); return serialize; } catch (e1) { try { @@ -224,6 +224,25 @@ var hinclude; return false; }, + fix_strip_closed_tag: function (content) { + var tags = content.match(/<[^>]*>/g); + if (tags.length > 0) { + var t = 0; + var tag; + var replaced; + for (t; t < tags.length; t += 1) { + tag = tags[t]; + replaced = tag.replace(/<([a-zA-Z]+)([\s\S]*?)(\/|\s\/)>/g, '<$1$2>'); + //verify correct replaced + if (tag !== replaced) { + content = content.replace(tag, replaced); + } + } + return content; + } + return content; + }, + isEmpty: function (value) { if (value === null || value === undefined) { return true; } if (value.length && value.length > 0) { return false; }