[FIX] qweb: properly handle xml comments

This commit is contained in:
Géry Debongnie
2019-10-15 13:41:18 +02:00
committed by aab-odoo
parent d40ff53f5a
commit 2825a5a55d
4 changed files with 17 additions and 9 deletions
+1 -1
View File
@@ -101,7 +101,7 @@ export class Component<T extends Env, Props extends {}> {
readonly __owl__: Internal<Env, Props>; readonly __owl__: Internal<Env, Props>;
static template?: string | null = null; static template?: string | null = null;
static _template?: string | null = null; static _template?: string | null = null;
static current: Component<any,any> | null = null; static current: Component<any, any> | null = null;
static components = {}; static components = {};
static props?: any; static props?: any;
static defaultProps?: any; static defaultProps?: any;
+9 -3
View File
@@ -104,8 +104,6 @@ const UTILS: Utils = {
function parseXML(xml: string): Document { function parseXML(xml: string): Document {
const parser = new DOMParser(); const parser = new DOMParser();
// we remove comments from the xml string
xml = xml.replace(/<!--[\s\S]*?-->/g, "");
const doc = parser.parseFromString(xml, "text/xml"); const doc = parser.parseFromString(xml, "text/xml");
if (doc.getElementsByTagName("parsererror").length) { if (doc.getElementsByTagName("parsererror").length) {
let msg = "Invalid XML in template."; let msg = "Invalid XML in template.";
@@ -421,7 +419,11 @@ export class QWeb extends EventBus {
text = text.replace(whitespaceRE, " "); text = text.replace(whitespaceRE, " ");
} }
if (ctx.parentNode) { if (ctx.parentNode) {
if (node.nodeType === 3) {
ctx.addLine(`c${ctx.parentNode}.push({text: \`${text}\`});`); ctx.addLine(`c${ctx.parentNode}.push({text: \`${text}\`});`);
} else if (node.nodeType === 8) {
ctx.addLine(`c${ctx.parentNode}.push(h('!', \`${text}\`));`);
}
} else if (ctx.parentTextNode) { } else if (ctx.parentTextNode) {
ctx.addLine(`vn${ctx.parentTextNode}.text += \`${text}\`;`); ctx.addLine(`vn${ctx.parentTextNode}.text += \`${text}\`;`);
} else { } else {
@@ -569,7 +571,11 @@ export class QWeb extends EventBus {
} }
} }
_compileGenericNode(node: ChildNode, ctx: CompilationContext, withHandlers: boolean = true): number { _compileGenericNode(
node: ChildNode,
ctx: CompilationContext,
withHandlers: boolean = true
): number {
// nodeType 1 is generic tag // nodeType 1 is generic tag
if (node.nodeType !== 1) { if (node.nodeType !== 1) {
throw new Error("unsupported node type"); throw new Error("unsupported node type");
+4 -2
View File
@@ -771,13 +771,15 @@ exports[`static templates empty div 1`] = `
}" }"
`; `;
exports[`static templates ignore comments 1`] = ` exports[`static templates properly handle comments 1`] = `
"function anonymous(context,extra "function anonymous(context,extra
) { ) {
var h = this.h; var h = this.h;
let c1 = [], p1 = {key:1}; let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1); var vn1 = h('div', p1, c1);
c1.push({text: \`hello owl\`}); c1.push({text: \`hello \`});
c1.push(h('!', \` comment\`));
c1.push({text: \`owl\`});
return vn1; return vn1;
}" }"
`; `;
+2 -2
View File
@@ -50,9 +50,9 @@ describe("static templates", () => {
expect(renderToString(qweb, "test")).toBe("<div><span>word</span></div>"); expect(renderToString(qweb, "test")).toBe("<div><span>word</span></div>");
}); });
test("ignore comments", () => { test("properly handle comments", () => {
qweb.addTemplate("test", "<div>hello <!-- comment-->owl</div>"); qweb.addTemplate("test", "<div>hello <!-- comment-->owl</div>");
expect(renderToString(qweb, "test")).toBe("<div>hello owl</div>"); expect(renderToString(qweb, "test")).toBe("<div>hello <!-- comment-->owl</div>");
}); });
}); });