[FIX] qweb: remove comments in xml strings before parsing

It was done in loadTemplates, but this does not work if we want to add
templates from another source.
This commit is contained in:
Géry Debongnie
2019-09-19 15:18:59 +02:00
parent c9910077a4
commit 8edc637033
4 changed files with 22 additions and 3 deletions
+3
View File
@@ -98,6 +98,9 @@ const UTILS: Utils = {
function parseXML(xml: string): Document {
const parser = new DOMParser();
// we remove comments from the xml string
xml = xml.replace(/<!--[\s\S]*?-->/g, "");
const doc = parser.parseFromString(xml, "text/xml");
if (doc.getElementsByTagName("parsererror").length) {
let msg = "Invalid XML in template.";
+1 -3
View File
@@ -48,9 +48,7 @@ export async function loadTemplates(url: string): Promise<string> {
if (!result.ok) {
throw new Error("Error while fetching xml templates");
}
let templates = await result.text();
templates = templates.replace(/<!--[\s\S]*?-->/g, "");
return templates;
return await result.text();
}
export function escape(str: string | number | undefined): string {
@@ -768,6 +768,18 @@ exports[`static templates empty div 1`] = `
}"
`;
exports[`static templates ignore comments 1`] = `
"function anonymous(context,extra
) {
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
result = vn1;
c1.push({text: \`hello owl\`});
return vn1;
}"
`;
exports[`static templates simple dynamic value 1`] = `
"function anonymous(context,extra
) {
+6
View File
@@ -49,6 +49,12 @@ describe("static templates", () => {
qweb.addTemplate("test", "<div><span>word</span></div>");
expect(renderToString(qweb, "test")).toBe("<div><span>word</span></div>");
});
test("ignore comments", () => {
qweb.addTemplate("test", "<div>hello <!-- comment-->owl</div>");
expect(renderToString(qweb, "test")).toBe("<div>hello owl</div>");
});
});
describe("error handling", () => {