Commit 3a87d366 authored by Romain Courteaud's avatar Romain Courteaud

Use the base tag to build the dependency url

parent c04e9c65
...@@ -1495,10 +1495,12 @@ ...@@ -1495,10 +1495,12 @@
title: "", title: "",
interface_list: [], interface_list: [],
required_css_list: [], required_css_list: [],
required_js_list: [] required_js_list: [],
path: url
}, },
i, i,
element; element,
base_found = false;
if (!url || !isAbsoluteOrDataURL.test(url)) { if (!url || !isAbsoluteOrDataURL.test(url)) {
throw new Error("The url should be absolute: " + url); throw new Error("The url should be absolute: " + url);
...@@ -1515,19 +1517,31 @@ ...@@ -1515,19 +1517,31 @@
// 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( settings.required_css_list.push(
renderJS.getAbsoluteURL(element.getAttribute("href"), url) renderJS.getAbsoluteURL(element.getAttribute("href"),
settings.path)
); );
} 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( settings.required_js_list.push(
renderJS.getAbsoluteURL(element.getAttribute("src"), url) renderJS.getAbsoluteURL(element.getAttribute("src"),
settings.path)
); );
} else if (element.rel === } else if (element.rel ===
"http://www.renderjs.org/rel/interface") { "http://www.renderjs.org/rel/interface") {
settings.interface_list.push( settings.interface_list.push(
renderJS.getAbsoluteURL(element.getAttribute("href"), url) renderJS.getAbsoluteURL(element.getAttribute("href"),
settings.path)
); );
} else if ((element.nodeName === "BASE") && !base_found &&
element.getAttribute("href")) {
settings.path = renderJS.getAbsoluteURL(
element.getAttribute("href"),
settings.path
);
// Only use the first base element found
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base#Usage_notes
base_found = true;
} }
} }
} }
......
...@@ -86,6 +86,7 @@ ...@@ -86,6 +86,7 @@
// Check that parseGadgetHTMLDocument returns the default value // Check that parseGadgetHTMLDocument returns the default value
// if the string is not a valid xml // if the string is not a valid xml
deepEqual(parseGadgetHTML("", "http://example.org"), { deepEqual(parseGadgetHTML("", "http://example.org"), {
path: "http://example.org",
title: "", title: "",
interface_list: [], interface_list: [],
required_css_list: [], required_css_list: [],
...@@ -118,6 +119,7 @@ ...@@ -118,6 +119,7 @@
document.implementation.createHTMLDocument(""), document.implementation.createHTMLDocument(""),
"http://example.org" "http://example.org"
), { ), {
path: "http://example.org",
title: "", title: "",
interface_list: [], interface_list: [],
required_css_list: [], required_css_list: [],
...@@ -162,6 +164,31 @@ ...@@ -162,6 +164,31 @@
// equal(settings.title, '', 'Title not found'); // equal(settings.title, '', 'Title not found');
// }); // });
test('Extract base url', function () {
// Check that parseGadgetHTMLDocument correctly extract the title
var settings,
html = "<html>" +
"<head>" +
"<base href='./bar/bar2/'></base>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org");
equal(settings.path, 'http://example.org/bar/bar2/', 'Base extracted');
});
test('Extract only one base url', function () {
// Check that parseGadgetHTMLDocument correctly extract the first title
var settings,
html = "<html>" +
"<head>" +
"<base href='./bar/bar2/'></base>" +
"<base href='./bar3/bar4/'></base>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org");
equal(settings.path, 'http://example.org/bar/bar2/', 'Base extracted');
});
// XXX innerHTML is not extracted anymore // XXX innerHTML is not extracted anymore
// test('Extract body', function () { // test('Extract body', function () {
// // Check that parseGadgetHTML correctly extract the body // // Check that parseGadgetHTML correctly extract the body
...@@ -215,6 +242,38 @@ ...@@ -215,6 +242,38 @@
"CSS extracted"); "CSS extracted");
}); });
test('Extract CSS after base tag', function () {
// Check that parseGadgetHTMLDocument correctly extract the CSS
var settings,
html = "<html>" +
"<head>" +
"<base href='./bar/bar2/'></base>" +
"<link rel='stylesheet' href='../lib/qunit/qunit.css' " +
"type='text/css'/>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org/foo/");
deepEqual(settings.required_css_list,
['http://example.org/foo/bar/lib/qunit/qunit.css'],
"CSS extracted");
});
test('Extract CSS before base tag', function () {
// Check that parseGadgetHTMLDocument correctly extract the CSS
var settings,
html = "<html>" +
"<head>" +
"<link rel='stylesheet' href='../lib/qunit/qunit.css' " +
"<base href='./bar/bar2/'></base>" +
"type='text/css'/>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org/foo/");
deepEqual(settings.required_css_list,
['http://example.org/lib/qunit/qunit.css'],
"CSS extracted");
});
test('Extract CSS order', function () { test('Extract CSS order', function () {
// Check that parseGadgetHTMLDocument correctly keep CSS order // Check that parseGadgetHTMLDocument correctly keep CSS order
var settings, var settings,
...@@ -261,6 +320,38 @@ ...@@ -261,6 +320,38 @@
"interface extracted"); "interface extracted");
}); });
test('Extract interface after base tag', function () {
// Check that parseGadgetHTMLDocument correctly extract the interface
var settings,
html = "<html>" +
"<head>" +
"<base href='./bar/bar2/'></base>" +
"<link rel='http://www.renderjs.org/rel/interface'" +
" href='./interface/renderable'/>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org/foo/");
deepEqual(settings.interface_list,
['http://example.org/foo/bar/bar2/interface/renderable'],
"interface extracted");
});
test('Extract interface before base tag', function () {
// Check that parseGadgetHTMLDocument correctly extract the interface
var settings,
html = "<html>" +
"<head>" +
"<link rel='http://www.renderjs.org/rel/interface'" +
" href='./interface/renderable'/>" +
"<base href='./bar/bar2/'></base>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org/foo/");
deepEqual(settings.interface_list,
['http://example.org/foo/interface/renderable'],
"interface extracted");
});
test('Extract interface order', function () { test('Extract interface order', function () {
// Check that parseGadgetHTMLDocument correctly keep interface order // Check that parseGadgetHTMLDocument correctly keep interface order
var settings, var settings,
...@@ -307,6 +398,38 @@ ...@@ -307,6 +398,38 @@
"JS extracted"); "JS extracted");
}); });
test('Extract JS after base tag', function () {
// Check that parseGadgetHTMLDocument correctly extract the JS
var settings,
html = "<html>" +
"<head>" +
"<base href='./bar/bar2/'></base>" +
"<script src='../lib/qunit/qunit.js' " +
"type='text/javascript'></script>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org/foo/");
deepEqual(settings.required_js_list,
['http://example.org/foo/bar/lib/qunit/qunit.js'],
"JS extracted");
});
test('Extract JS before base tag', function () {
// Check that parseGadgetHTMLDocument correctly extract the JS
var settings,
html = "<html>" +
"<head>" +
"<script src='../lib/qunit/qunit.js' " +
"type='text/javascript'></script>" +
"<base href='./bar/bar2/'></base>" +
"</head></html>";
settings = parseGadgetHTML(html, "http://example.org/foo/");
deepEqual(settings.required_js_list,
['http://example.org/lib/qunit/qunit.js'],
"JS extracted");
});
test('Extract JS order', function () { test('Extract JS order', function () {
// Check that parseGadgetHTMLDocument correctly keep JS order // Check that parseGadgetHTMLDocument correctly keep JS order
var settings, var settings,
...@@ -345,6 +468,7 @@ ...@@ -345,6 +468,7 @@
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' + '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' +
'</head><body><p>Non valid XML</p></body></html>', '</head><body><p>Non valid XML</p></body></html>',
'http://example.org/foo/'), { 'http://example.org/foo/'), {
path: "http://example.org/foo/",
title: "Test non valid XML", title: "Test non valid XML",
interface_list: [], interface_list: [],
required_css_list: [], required_css_list: [],
...@@ -4846,6 +4970,96 @@ ...@@ -4846,6 +4970,96 @@
}); });
}); });
test('can declareGadget relativeurl in HTML', function () {
var gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test12345.html',
html_relative_url2 = 'test12346.html',
html_url2 = 'https://example.org/files/qunittest/test12346.html',
spy;
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body><div data-foo='bar' " +
"data-gadget-scope='bar' data-gadget-url='" + html_relative_url2 +
"'></div></body></html>"]);
this.server.respondWith("GET", html_url2, [200, {
"Content-Type": "text/html"
}, "raw html"]);
spy = sinon.spy(renderJS, "parseGadgetHTMLDocument");
stop();
expect(6);
gadget.declareGadget(html_url)
.then(function (g) {
equal(spy.callCount, 2);
equal(spy.firstCall.args[1], html_url);
equal(spy.secondCall.args[1], html_url2);
// Second gadget is a child
return g.getDeclaredGadget("bar");
})
.then(function (g2) {
equal(g2.__path, html_url2);
// The gadget element is the one defined in HTML
equal(g2.element.getAttribute("data-foo"), "bar");
// The gadget is public by default
equal(g2.element.innerHTML, "raw html");
})
.fail(function (e) {
ok(false, e);
})
.always(function () {
start();
spy.restore();
});
});
test('can declareGadget relativeurl in HTML with base tag', function () {
var gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test12345.html',
html_relative_url2 = 'test12346.html',
html_url2 = 'https://example.org/files/qunittest/foo/test12346.html',
spy;
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><head><base href='./foo/'></head><body><div data-foo='bar' " +
"data-gadget-scope='bar' data-gadget-url='" + html_relative_url2 +
"'></div></body></html>"]);
this.server.respondWith("GET", html_url2, [200, {
"Content-Type": "text/html"
}, "raw html"]);
spy = sinon.spy(renderJS, "parseGadgetHTMLDocument");
stop();
expect(6);
gadget.declareGadget(html_url)
.then(function (g) {
equal(spy.callCount, 2);
equal(spy.firstCall.args[1], html_url);
equal(spy.secondCall.args[1], html_url2);
// Second gadget is a child
return g.getDeclaredGadget("bar");
})
.then(function (g2) {
equal(g2.__path, html_url2);
// The gadget element is the one defined in HTML
equal(g2.element.getAttribute("data-foo"), "bar");
// The gadget is public by default
equal(g2.element.innerHTML, "raw html");
})
.fail(function (e) {
ok(false, e);
})
.always(function () {
start();
spy.restore();
});
});
test('can declareGadget a sandboxed gadget in HTML directly', function () { test('can declareGadget a sandboxed gadget in HTML directly', function () {
var gadget = new RenderJSGadget(), var gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test123456.html', html_url = 'https://example.org/files/qunittest/test123456.html',
......
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