mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
+10
-3
@@ -1,7 +1,7 @@
|
|||||||
import { EventBus } from "../core/event_bus";
|
import { EventBus } from "../core/event_bus";
|
||||||
import { h, patch, VNode } from "../vdom/index";
|
import { h, patch, VNode } from "../vdom/index";
|
||||||
import { CompilationContext } from "./compilation_context";
|
import { CompilationContext } from "./compilation_context";
|
||||||
import { shallowEqual } from "../utils";
|
import { shallowEqual, escape } from "../utils";
|
||||||
import { addNS } from "../vdom/vdom";
|
import { addNS } from "../vdom/vdom";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -348,8 +348,15 @@ export class QWeb extends EventBus {
|
|||||||
return vnode.text!;
|
return vnode.text!;
|
||||||
}
|
}
|
||||||
const node = document.createElement(vnode.sel);
|
const node = document.createElement(vnode.sel);
|
||||||
const result = patch(node, vnode);
|
const elem = patch(node, vnode).elm as HTMLElement;
|
||||||
return (<HTMLElement>result.elm).outerHTML;
|
function escapeTextNodes(node) {
|
||||||
|
if (node.nodeType === 3) {
|
||||||
|
node.textContent = escape(node.textContent);
|
||||||
|
}
|
||||||
|
for (let n of node.childNodes) { escapeTextNodes(n) }
|
||||||
|
}
|
||||||
|
escapeTextNodes(elem);
|
||||||
|
return elem.outerHTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+3
-6
@@ -58,12 +58,9 @@ export function escape(str: string | number | undefined): string {
|
|||||||
if (typeof str === "number") {
|
if (typeof str === "number") {
|
||||||
return String(str);
|
return String(str);
|
||||||
}
|
}
|
||||||
return str
|
const p = document.createElement('p');
|
||||||
.replace(/&/g, "&")
|
p.textContent = str;
|
||||||
.replace(/</g, "<")
|
return p.innerHTML;
|
||||||
.replace(/>/g, ">")
|
|
||||||
.replace(/"/g, "'")
|
|
||||||
.replace(/`/g, "`");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+2
-5
@@ -123,11 +123,8 @@ export function renderToString(
|
|||||||
context: EvalContext = {},
|
context: EvalContext = {},
|
||||||
extra?: any
|
extra?: any
|
||||||
): string {
|
): string {
|
||||||
const node = renderToDOM(qweb, t, context, extra);
|
const result = qweb.renderToString(t, context, extra);
|
||||||
const result = node instanceof Text ? node.textContent! : node.outerHTML;
|
expect(qweb.templates[t].fn.toString()).toMatchSnapshot();
|
||||||
if (result !== qweb.renderToString(t, context, extra)) {
|
|
||||||
throw new Error("HTML string returned by renderToString helper does not match QWeb render");
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -347,6 +347,29 @@ exports[`attributes tuple variable 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`attributes various escapes 1`] = `
|
||||||
|
"function anonymous(context, extra
|
||||||
|
) {
|
||||||
|
// Template name: \\"test\\"
|
||||||
|
let scope = Object.create(context);
|
||||||
|
var h = this.h;
|
||||||
|
var _1 = '<foo';
|
||||||
|
var _2 = scope['bar'];
|
||||||
|
var _3 = \`<\${scope['baz']}>\`;
|
||||||
|
var _4 = scope['qux'];
|
||||||
|
let c5 = [], p5 = {key:5,attrs:{foo: _1,bar: _2,baz: _3}};
|
||||||
|
if (_4 instanceof Array) {
|
||||||
|
p5.attrs[_4[0]] = _4[1];
|
||||||
|
} else {
|
||||||
|
for (let key in _4) {
|
||||||
|
p5.attrs[key] = _4[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var vn5 = h('div', p5, c5);
|
||||||
|
return vn5;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`debugging t-debug 1`] = `
|
exports[`debugging t-debug 1`] = `
|
||||||
"function anonymous(context, extra
|
"function anonymous(context, extra
|
||||||
) {
|
) {
|
||||||
@@ -1553,6 +1576,22 @@ exports[`t-call (template calling with used set body 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`t-esc escaping 1`] = `
|
||||||
|
"function anonymous(context, extra
|
||||||
|
) {
|
||||||
|
// Template name: \\"test\\"
|
||||||
|
let scope = Object.create(context);
|
||||||
|
var h = this.h;
|
||||||
|
let c1 = [], p1 = {key:1};
|
||||||
|
var vn1 = h('span', p1, c1);
|
||||||
|
var _2 = scope['var'];
|
||||||
|
if (_2 || _2 === 0) {
|
||||||
|
c1.push({text: _2});
|
||||||
|
}
|
||||||
|
return vn1;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`t-esc escaping on a node 1`] = `
|
exports[`t-esc escaping on a node 1`] = `
|
||||||
"function anonymous(context, extra
|
"function anonymous(context, extra
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -120,9 +120,9 @@ describe("t-esc", () => {
|
|||||||
expect(renderToString(qweb, "test", { var: "ok" })).toBe("<span>ok</span>");
|
expect(renderToString(qweb, "test", { var: "ok" })).toBe("<span>ok</span>");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip("escaping", () => {
|
test("escaping", () => {
|
||||||
qweb.addTemplate("test", `<span><t t-esc="var"/></span>`);
|
qweb.addTemplate("test", `<span><t t-esc="var"/></span>`);
|
||||||
expect(renderToString(qweb, "test", { var: "<ok>" })).toBe("<span><ok></span>");
|
expect(renderToString(qweb, "test", { var: "<ok>abc</ok>" })).toBe("<span>&lt;ok&gt;abc&lt;/ok&gt;</span>");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("escaping on a node", () => {
|
test("escaping on a node", () => {
|
||||||
@@ -620,7 +620,7 @@ describe("attributes", () => {
|
|||||||
expect(result).toBe(`<div foo="a 0 is 1 of 2 ]"></div>`);
|
expect(result).toBe(`<div foo="a 0 is 1 of 2 ]"></div>`);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip("various escapes", () => {
|
test("various escapes", () => {
|
||||||
// not needed??
|
// not needed??
|
||||||
qweb.addTemplate(
|
qweb.addTemplate(
|
||||||
"test",
|
"test",
|
||||||
@@ -636,7 +636,7 @@ describe("attributes", () => {
|
|||||||
baz: 1,
|
baz: 1,
|
||||||
qux: { qux: "<>" }
|
qux: { qux: "<>" }
|
||||||
});
|
});
|
||||||
const expected = `<div foo="<foo" bar="<bar>" baz="<"<baz>">" qux="<>"></div>`;
|
const expected = "<div foo=\"<foo\" bar=\"0\" baz=\"<1>\" qux=\"<>\"></div>";
|
||||||
expect(result).toBe(expected);
|
expect(result).toBe(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user