Commit 0f76b594 authored by Thibaut Frain's avatar Thibaut Frain

Relative to absolute urls translation

parent 18d4f072
...@@ -82,6 +82,7 @@ module.exports = function (grunt) { ...@@ -82,6 +82,7 @@ module.exports = function (grunt) {
'sinon', 'sinon',
'RSVP', 'RSVP',
'DOMParser', 'DOMParser',
'URI',
'__RenderJSIframeGadget', '__RenderJSIframeGadget',
'__RenderJSEmbeddedGadget' '__RenderJSEmbeddedGadget'
] ]
......
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
"sinon": "~1.7.3", "sinon": "~1.7.3",
"connect-livereload": "~0.3.0", "connect-livereload": "~0.3.0",
"grunt-open": "~0.2.2", "grunt-open": "~0.2.2",
"grunt-contrib-concat": "~0.3.0" "grunt-contrib-concat": "~0.3.0",
"URIjs": "~1.12.x"
}, },
"scripts": { "scripts": {
"test": "./node_modules/.bin/grunt test", "test": "./node_modules/.bin/grunt test",
......
...@@ -318,6 +318,9 @@ ...@@ -318,6 +318,9 @@
options.sandbox = "public"; options.sandbox = "public";
} }
// transform url to absolute url if it is relative
url = renderJS.getAbsoluteURL(url, this.__path);
// Change the global variable to update the loading queue // Change the global variable to update the loading queue
queue = new RSVP.Queue() queue = new RSVP.Queue()
// Wait for previous gadget loading to finish first // Wait for previous gadget loading to finish first
...@@ -420,6 +423,27 @@ ...@@ -420,6 +423,27 @@
renderJS.AcquisitionError.prototype.constructor = renderJS.AcquisitionError.prototype.constructor =
renderJS.AcquisitionError; renderJS.AcquisitionError;
/////////////////////////////////////////////////////////////////
// renderJS.getAbsoluteURL
/////////////////////////////////////////////////////////////////
renderJS.getAbsoluteURL = function (url, base_url) {
var doc, base, link,
html = "<!doctype><html><head></head></html>",
isAbsoluteOrDataURL = new RegExp('^(?:[a-z]+:)?//|data:', 'i');
if (url && base_url && !isAbsoluteOrDataURL.test(url)) {
doc = (new DOMParser()).parseFromString(html, 'text/html');
base = doc.createElement('base');
link = doc.createElement('link');
doc.head.appendChild(base);
doc.head.appendChild(link);
base.href = base_url;
link.href = url;
return link.href;
}
return url;
};
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// renderJS.declareJS // renderJS.declareJS
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -511,7 +535,8 @@ ...@@ -511,7 +535,8 @@
tmp_constructor.__template_element = tmp_constructor.__template_element =
(new DOMParser()).parseFromString(xhr.responseText, "text/html"); (new DOMParser()).parseFromString(xhr.responseText, "text/html");
parsed_html = renderJS.parseGadgetHTMLDocument( parsed_html = renderJS.parseGadgetHTMLDocument(
tmp_constructor.__template_element tmp_constructor.__template_element,
url
); );
for (key in parsed_html) { for (key in parsed_html) {
if (parsed_html.hasOwnProperty(key)) { if (parsed_html.hasOwnProperty(key)) {
...@@ -586,7 +611,7 @@ ...@@ -586,7 +611,7 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// renderJS.parseGadgetHTMLDocument // renderJS.parseGadgetHTMLDocument
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
renderJS.parseGadgetHTMLDocument = function (document_element) { renderJS.parseGadgetHTMLDocument = function (document_element, url) {
var settings = { var settings = {
title: "", title: "",
interface_list: [], interface_list: [],
...@@ -594,7 +619,13 @@ ...@@ -594,7 +619,13 @@
required_js_list: [] required_js_list: []
}, },
i, i,
element; element,
isAbsoluteURL = new RegExp('^(?:[a-z]+:)?//', 'i');
if (!url || !isAbsoluteURL.test(url)) {
throw new Error("The second parameter should be an absolute url");
}
if (document_element.nodeType === 9) { if (document_element.nodeType === 9) {
settings.title = document_element.title; settings.title = document_element.title;
...@@ -604,13 +635,19 @@ ...@@ -604,13 +635,19 @@
// XXX Manage relative URL during extraction of URLs // XXX Manage relative URL during extraction of URLs
// element.href returns absolute URL in firefox but "" in chrome; // element.href returns absolute URL in firefox but "" in chrome;
if (element.rel === "stylesheet") { if (element.rel === "stylesheet") {
settings.required_css_list.push(element.getAttribute("href")); settings.required_css_list.push(
renderJS.getAbsoluteURL(element.getAttribute("href"), url)
);
} else if (element.nodeName === "SCRIPT" && } else if (element.nodeName === "SCRIPT" &&
(element.type === "text/javascript" || (element.type === "text/javascript" ||
!element.type)) { !element.type)) {
settings.required_js_list.push(element.getAttribute("src")); settings.required_js_list.push(
renderJS.getAbsoluteURL(element.getAttribute("src"), url)
);
} else if (element.rel === "http://www.renderjs.org/rel/interface") { } else if (element.rel === "http://www.renderjs.org/rel/interface") {
settings.interface_list.push(element.getAttribute("href")); settings.interface_list.push(
renderJS.getAbsoluteURL(element.getAttribute("href"), url)
);
} }
} }
} }
...@@ -762,7 +799,7 @@ ...@@ -762,7 +799,7 @@
function init() { function init() {
// XXX HTML properties can only be set when the DOM is fully loaded // XXX HTML properties can only be set when the DOM is fully loaded
var settings = renderJS.parseGadgetHTMLDocument(document), var settings = renderJS.parseGadgetHTMLDocument(document, url),
j, j,
key; key;
for (key in settings) { for (key in settings) {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<script src="../node_modules/rsvp/dist/rsvp-2.0.4.js" type="text/javascript"></script> <script src="../node_modules/rsvp/dist/rsvp-2.0.4.js" type="text/javascript"></script>
<script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script> <script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script>
<script src="../node_modules/sinon/pkg/sinon.js" type="text/javascript"></script> <script src="../node_modules/sinon/pkg/sinon.js" type="text/javascript"></script>
<script src="../node_modules/URIjs/src/URI.js"></script>
<script src="../dist/renderjs-latest.js" type="text/javascript"></script> <script src="../dist/renderjs-latest.js" type="text/javascript"></script>
<script src="renderjs_test.js" type="text/javascript"></script> <script src="renderjs_test.js" type="text/javascript"></script>
</head> </head>
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment