mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d0d7482b0f | |||
| 8fe4c0c76e | |||
| c1afaeb92a | |||
| 3883cec079 | |||
| 9cb74d619b | |||
| a93f015795 | |||
| 02a187d80b | |||
| d3b0d1971e | |||
| b90aa0e23a | |||
| 163366997c | |||
| 588b655c11 | |||
| f5d5273c25 | |||
| 9fd662fdce | |||
| 7786077921 | |||
| 30bc605c84 | |||
| d1118455aa | |||
| f8073cb153 |
+1
-2
@@ -1,9 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@odoo/owl",
|
"name": "@odoo/owl",
|
||||||
"version": "2.0.0-beta-12",
|
"version": "2.0.0-beta-19",
|
||||||
"description": "Odoo Web Library (OWL)",
|
"description": "Odoo Web Library (OWL)",
|
||||||
"main": "dist/owl.cjs.js",
|
"main": "dist/owl.cjs.js",
|
||||||
"browser": "dist/owl.iife.js",
|
|
||||||
"module": "dist/owl.es.js",
|
"module": "dist/owl.es.js",
|
||||||
"types": "dist/types/owl.d.ts",
|
"types": "dist/types/owl.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
+16
-8
@@ -6,6 +6,14 @@ import dts from "rollup-plugin-dts";
|
|||||||
|
|
||||||
let input, output;
|
let input, output;
|
||||||
|
|
||||||
|
const IIFE_FILENAME = "dist/owl.iife.js";
|
||||||
|
const CJS_FILENAME = "dist/owl.cjs.js";
|
||||||
|
const ES_FILENAME = "dist/owl.es.js";
|
||||||
|
|
||||||
|
if (pkg.module !== ES_FILENAME || pkg.main !== CJS_FILENAME) {
|
||||||
|
throw new Error("package.json has been modified. Build script should be updated accordingly");
|
||||||
|
}
|
||||||
|
|
||||||
const outro = `
|
const outro = `
|
||||||
__info__.version = '${pkg.version}';
|
__info__.version = '${pkg.version}';
|
||||||
__info__.date = '${new Date().toISOString()}';
|
__info__.date = '${new Date().toISOString()}';
|
||||||
@@ -23,19 +31,19 @@ switch (process.argv[4]) {
|
|||||||
case "runtime":
|
case "runtime":
|
||||||
input = "src/runtime/index.ts";
|
input = "src/runtime/index.ts";
|
||||||
output = [
|
output = [
|
||||||
getConfigForFormat('esm', addSuffix(pkg.module, 'runtime'), outro),
|
getConfigForFormat('esm', addSuffix(ES_FILENAME, 'runtime'), outro),
|
||||||
getConfigForFormat('cjs', addSuffix(pkg.main, 'runtime'), outro),
|
getConfigForFormat('cjs', addSuffix(CJS_FILENAME, 'runtime'), outro),
|
||||||
getConfigForFormat('iife', addSuffix(pkg.browser, 'runtime'), outro),
|
getConfigForFormat('iife', addSuffix(IIFE_FILENAME, 'runtime'), outro),
|
||||||
getConfigForFormat('iife', addSuffix(pkg.browser, 'runtime'), outro, true),
|
getConfigForFormat('iife', addSuffix(IIFE_FILENAME, 'runtime'), outro, true),
|
||||||
]
|
]
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
input = "src/index.ts",
|
input = "src/index.ts",
|
||||||
output = [
|
output = [
|
||||||
getConfigForFormat('esm', pkg.module, outro),
|
getConfigForFormat('esm', ES_FILENAME, outro),
|
||||||
getConfigForFormat('cjs', pkg.main, outro),
|
getConfigForFormat('cjs', CJS_FILENAME, outro),
|
||||||
getConfigForFormat('iife', pkg.browser, outro),
|
getConfigForFormat('iife', IIFE_FILENAME, outro),
|
||||||
getConfigForFormat('iife', pkg.browser, outro, true),
|
getConfigForFormat('iife', IIFE_FILENAME, outro, true),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { isProp } from "../runtime/blockdom/attributes";
|
||||||
import {
|
import {
|
||||||
compileExpr,
|
compileExpr,
|
||||||
compileExprToArray,
|
compileExprToArray,
|
||||||
@@ -22,13 +23,14 @@ import {
|
|||||||
ASTTif,
|
ASTTif,
|
||||||
ASTTKey,
|
ASTTKey,
|
||||||
ASTTOut,
|
ASTTOut,
|
||||||
ASTTSet,
|
|
||||||
ASTTranslation,
|
|
||||||
ASTType,
|
|
||||||
ASTTPortal,
|
ASTTPortal,
|
||||||
EventHandlers,
|
ASTTranslation,
|
||||||
|
ASTTSet,
|
||||||
|
ASTType,
|
||||||
Attrs,
|
Attrs,
|
||||||
|
EventHandlers,
|
||||||
} from "./parser";
|
} from "./parser";
|
||||||
|
import { OwlError } from "../runtime/error_handling";
|
||||||
|
|
||||||
type BlockType = "block" | "text" | "multi" | "list" | "html" | "comment";
|
type BlockType = "block" | "text" | "multi" | "list" | "html" | "comment";
|
||||||
|
|
||||||
@@ -230,6 +232,7 @@ export class CodeGenerator {
|
|||||||
translatableAttributes: string[] = TRANSLATABLE_ATTRS;
|
translatableAttributes: string[] = TRANSLATABLE_ATTRS;
|
||||||
ast: AST;
|
ast: AST;
|
||||||
staticDefs: { id: string; expr: string }[] = [];
|
staticDefs: { id: string; expr: string }[] = [];
|
||||||
|
slotNames: Set<String> = new Set();
|
||||||
helpers: Set<string> = new Set();
|
helpers: Set<string> = new Set();
|
||||||
|
|
||||||
constructor(ast: AST, options: CodeGenOptions) {
|
constructor(ast: AST, options: CodeGenOptions) {
|
||||||
@@ -534,7 +537,7 @@ export class CodeGenerator {
|
|||||||
.slice(1)
|
.slice(1)
|
||||||
.map((m) => {
|
.map((m) => {
|
||||||
if (!MODS.has(m)) {
|
if (!MODS.has(m)) {
|
||||||
throw new Error(`Unknown event modifier: '${m}'`);
|
throw new OwlError(`Unknown event modifier: '${m}'`);
|
||||||
}
|
}
|
||||||
return `"${m}"`;
|
return `"${m}"`;
|
||||||
});
|
});
|
||||||
@@ -578,13 +581,22 @@ export class CodeGenerator {
|
|||||||
attrName = key.slice(7);
|
attrName = key.slice(7);
|
||||||
attrs["block-attribute-" + idx] = attrName;
|
attrs["block-attribute-" + idx] = attrName;
|
||||||
} else if (key.startsWith("t-att")) {
|
} else if (key.startsWith("t-att")) {
|
||||||
|
attrName = key === "t-att" ? null : key.slice(6);
|
||||||
expr = compileExpr(ast.attrs[key]);
|
expr = compileExpr(ast.attrs[key]);
|
||||||
|
if (attrName && isProp(ast.tag, attrName)) {
|
||||||
|
// we force a new string or new boolean to bypass the equality check in blockdom when patching same value
|
||||||
|
if (attrName === "value") {
|
||||||
|
// When the expression is falsy, fall back to an empty string
|
||||||
|
expr = `new String((${expr}) || "")`;
|
||||||
|
} else {
|
||||||
|
expr = `new Boolean(${expr})`;
|
||||||
|
}
|
||||||
|
}
|
||||||
const idx = block!.insertData(expr, "attr");
|
const idx = block!.insertData(expr, "attr");
|
||||||
if (key === "t-att") {
|
if (key === "t-att") {
|
||||||
attrs[`block-attributes`] = String(idx);
|
attrs[`block-attributes`] = String(idx);
|
||||||
} else {
|
} else {
|
||||||
attrName = key.slice(6);
|
attrs[`block-attribute-${idx}`] = attrName!;
|
||||||
attrs[`block-attribute-${idx}`] = attrName;
|
|
||||||
}
|
}
|
||||||
} else if (this.translatableAttributes.includes(key)) {
|
} else if (this.translatableAttributes.includes(key)) {
|
||||||
attrs[key] = this.translateFn(ast.attrs[key]);
|
attrs[key] = this.translateFn(ast.attrs[key]);
|
||||||
@@ -865,8 +877,9 @@ export class CodeGenerator {
|
|||||||
this.define(`key${this.target.loopLevel}`, ast.key ? compileExpr(ast.key) : loopVar);
|
this.define(`key${this.target.loopLevel}`, ast.key ? compileExpr(ast.key) : loopVar);
|
||||||
if (this.dev) {
|
if (this.dev) {
|
||||||
// Throw error on duplicate keys in dev mode
|
// Throw error on duplicate keys in dev mode
|
||||||
|
this.helpers.add("OwlError");
|
||||||
this.addLine(
|
this.addLine(
|
||||||
`if (keys${block.id}.has(key${this.target.loopLevel})) { throw new Error(\`Got duplicate key in t-foreach: \${key${this.target.loopLevel}}\`)}`
|
`if (keys${block.id}.has(key${this.target.loopLevel})) { throw new OwlError(\`Got duplicate key in t-foreach: \${key${this.target.loopLevel}}\`)}`
|
||||||
);
|
);
|
||||||
this.addLine(`keys${block.id}.add(key${this.target.loopLevel});`);
|
this.addLine(`keys${block.id}.add(key${this.target.loopLevel});`);
|
||||||
}
|
}
|
||||||
@@ -1088,7 +1101,7 @@ export class CodeGenerator {
|
|||||||
name = _name;
|
name = _name;
|
||||||
value = `bind(ctx, ${value || undefined})`;
|
value = `bind(ctx, ${value || undefined})`;
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Invalid prop suffix");
|
throw new OwlError("Invalid prop suffix");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
name = /^[a-z_]+$/i.test(name) ? name : `'${name}'`;
|
name = /^[a-z_]+$/i.test(name) ? name : `'${name}'`;
|
||||||
@@ -1233,28 +1246,37 @@ export class CodeGenerator {
|
|||||||
let blockString: string;
|
let blockString: string;
|
||||||
let slotName;
|
let slotName;
|
||||||
let dynamic = false;
|
let dynamic = false;
|
||||||
|
let isMultiple = false;
|
||||||
if (ast.name.match(INTERP_REGEXP)) {
|
if (ast.name.match(INTERP_REGEXP)) {
|
||||||
dynamic = true;
|
dynamic = true;
|
||||||
|
isMultiple = true;
|
||||||
slotName = interpolate(ast.name);
|
slotName = interpolate(ast.name);
|
||||||
} else {
|
} else {
|
||||||
slotName = "'" + ast.name + "'";
|
slotName = "'" + ast.name + "'";
|
||||||
|
isMultiple = isMultiple || this.slotNames.has(ast.name);
|
||||||
|
this.slotNames.add(ast.name);
|
||||||
}
|
}
|
||||||
const dynProps = ast.attrs ? ast.attrs["t-props"] : null;
|
const dynProps = ast.attrs ? ast.attrs["t-props"] : null;
|
||||||
if (ast.attrs) {
|
if (ast.attrs) {
|
||||||
delete ast.attrs["t-props"];
|
delete ast.attrs["t-props"];
|
||||||
}
|
}
|
||||||
|
let key = this.target.loopLevel ? `key${this.target.loopLevel}` : "key";
|
||||||
|
if (isMultiple) {
|
||||||
|
key = `${key} + \`${this.generateComponentKey()}\``;
|
||||||
|
}
|
||||||
|
|
||||||
const props = ast.attrs ? this.formatPropObject(ast.attrs) : [];
|
const props = ast.attrs ? this.formatPropObject(ast.attrs) : [];
|
||||||
const scope = this.getPropString(props, dynProps);
|
const scope = this.getPropString(props, dynProps);
|
||||||
if (ast.defaultContent) {
|
if (ast.defaultContent) {
|
||||||
const name = this.compileInNewTarget("defaultContent", ast.defaultContent, ctx);
|
const name = this.compileInNewTarget("defaultContent", ast.defaultContent, ctx);
|
||||||
blockString = `callSlot(ctx, node, key, ${slotName}, ${dynamic}, ${scope}, ${name})`;
|
blockString = `callSlot(ctx, node, ${key}, ${slotName}, ${dynamic}, ${scope}, ${name})`;
|
||||||
} else {
|
} else {
|
||||||
if (dynamic) {
|
if (dynamic) {
|
||||||
let name = generateId("slot");
|
let name = generateId("slot");
|
||||||
this.define(name, slotName);
|
this.define(name, slotName);
|
||||||
blockString = `toggler(${name}, callSlot(ctx, node, key, ${name}, ${dynamic}, ${scope}))`;
|
blockString = `toggler(${name}, callSlot(ctx, node, ${key}, ${name}, ${dynamic}, ${scope}))`;
|
||||||
} else {
|
} else {
|
||||||
blockString = `callSlot(ctx, node, key, ${slotName}, ${dynamic}, ${scope})`;
|
blockString = `callSlot(ctx, node, ${key}, ${slotName}, ${dynamic}, ${scope})`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// event handling
|
// event handling
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { OwlError } from "../runtime/error_handling";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Owl QWeb Expression Parser
|
* Owl QWeb Expression Parser
|
||||||
*
|
*
|
||||||
@@ -106,14 +108,14 @@ let tokenizeString: Tokenizer = function (expr) {
|
|||||||
i++;
|
i++;
|
||||||
cur = expr[i];
|
cur = expr[i];
|
||||||
if (!cur) {
|
if (!cur) {
|
||||||
throw new Error("Invalid expression");
|
throw new OwlError("Invalid expression");
|
||||||
}
|
}
|
||||||
s += cur;
|
s += cur;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (expr[i] !== start) {
|
if (expr[i] !== start) {
|
||||||
throw new Error("Invalid expression");
|
throw new OwlError("Invalid expression");
|
||||||
}
|
}
|
||||||
s += start;
|
s += start;
|
||||||
if (start === "`") {
|
if (start === "`") {
|
||||||
@@ -223,7 +225,7 @@ export function tokenize(expr: string): Token[] {
|
|||||||
error = e; // Silence all errors and throw a generic error below
|
error = e; // Silence all errors and throw a generic error below
|
||||||
}
|
}
|
||||||
if (current.length || error) {
|
if (current.length || error) {
|
||||||
throw new Error(`Tokenizer error: could not tokenize \`${expr}\``);
|
throw new OwlError(`Tokenizer error: could not tokenize \`${expr}\``);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-17
@@ -1,3 +1,5 @@
|
|||||||
|
import { OwlError } from "../runtime/error_handling";
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// AST Type definition
|
// AST Type definition
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
@@ -319,7 +321,7 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (tagName.startsWith("block-")) {
|
if (tagName.startsWith("block-")) {
|
||||||
throw new Error(`Invalid tag name: '${tagName}'`);
|
throw new OwlError(`Invalid tag name: '${tagName}'`);
|
||||||
}
|
}
|
||||||
ctx = Object.assign({}, ctx);
|
ctx = Object.assign({}, ctx);
|
||||||
if (tagName === "pre") {
|
if (tagName === "pre") {
|
||||||
@@ -340,13 +342,15 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
const value = node.getAttribute(attr)!;
|
const value = node.getAttribute(attr)!;
|
||||||
if (attr.startsWith("t-on")) {
|
if (attr.startsWith("t-on")) {
|
||||||
if (attr === "t-on") {
|
if (attr === "t-on") {
|
||||||
throw new Error("Missing event name with t-on directive");
|
throw new OwlError("Missing event name with t-on directive");
|
||||||
}
|
}
|
||||||
on = on || {};
|
on = on || {};
|
||||||
on[attr.slice(5)] = value;
|
on[attr.slice(5)] = value;
|
||||||
} else if (attr.startsWith("t-model")) {
|
} else if (attr.startsWith("t-model")) {
|
||||||
if (!["input", "select", "textarea"].includes(tagName)) {
|
if (!["input", "select", "textarea"].includes(tagName)) {
|
||||||
throw new Error("The t-model directive only works with <input>, <textarea> and <select>");
|
throw new OwlError(
|
||||||
|
"The t-model directive only works with <input>, <textarea> and <select>"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let baseExpr, expr;
|
let baseExpr, expr;
|
||||||
@@ -359,7 +363,7 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
baseExpr = value.slice(0, index);
|
baseExpr = value.slice(0, index);
|
||||||
expr = value.slice(index + 1, -1);
|
expr = value.slice(index + 1, -1);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Invalid t-model expression: "${value}" (it should be assignable)`);
|
throw new OwlError(`Invalid t-model expression: "${value}" (it should be assignable)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const typeAttr = node.getAttribute("type");
|
const typeAttr = node.getAttribute("type");
|
||||||
@@ -390,10 +394,10 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
ctx.tModelInfo = model;
|
ctx.tModelInfo = model;
|
||||||
}
|
}
|
||||||
} else if (attr.startsWith("block-")) {
|
} else if (attr.startsWith("block-")) {
|
||||||
throw new Error(`Invalid attribute: '${attr}'`);
|
throw new OwlError(`Invalid attribute: '${attr}'`);
|
||||||
} else if (attr !== "t-name") {
|
} else if (attr !== "t-name") {
|
||||||
if (attr.startsWith("t-") && !attr.startsWith("t-att")) {
|
if (attr.startsWith("t-") && !attr.startsWith("t-att")) {
|
||||||
throw new Error(`Unknown QWeb directive: '${attr}'`);
|
throw new OwlError(`Unknown QWeb directive: '${attr}'`);
|
||||||
}
|
}
|
||||||
const tModel = ctx.tModelInfo;
|
const tModel = ctx.tModelInfo;
|
||||||
if (tModel && ["t-att-value", "t-attf-value"].includes(attr)) {
|
if (tModel && ["t-att-value", "t-attf-value"].includes(attr)) {
|
||||||
@@ -447,7 +451,7 @@ function parseTEscNode(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (ast.type === ASTType.TComponent) {
|
if (ast.type === ASTType.TComponent) {
|
||||||
throw new Error("t-esc is not supported on Component nodes");
|
throw new OwlError("t-esc is not supported on Component nodes");
|
||||||
}
|
}
|
||||||
return tesc;
|
return tesc;
|
||||||
}
|
}
|
||||||
@@ -503,7 +507,7 @@ function parseTForEach(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
node.removeAttribute("t-as");
|
node.removeAttribute("t-as");
|
||||||
const key = node.getAttribute("t-key");
|
const key = node.getAttribute("t-key");
|
||||||
if (!key) {
|
if (!key) {
|
||||||
throw new Error(
|
throw new OwlError(
|
||||||
`"Directive t-foreach should always be used with a t-key!" (expression: t-foreach="${collection}" t-as="${elem}")`
|
`"Directive t-foreach should always be used with a t-key!" (expression: t-foreach="${collection}" t-as="${elem}")`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -686,7 +690,9 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
let isDynamic = node.hasAttribute("t-component");
|
let isDynamic = node.hasAttribute("t-component");
|
||||||
|
|
||||||
if (isDynamic && name !== "t") {
|
if (isDynamic && name !== "t") {
|
||||||
throw new Error(`Directive 't-component' can only be used on <t> nodes (used on a <${name}>)`);
|
throw new OwlError(
|
||||||
|
`Directive 't-component' can only be used on <t> nodes (used on a <${name}>)`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(firstLetter === firstLetter.toUpperCase() || isDynamic)) {
|
if (!(firstLetter === firstLetter.toUpperCase() || isDynamic)) {
|
||||||
@@ -713,7 +719,7 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
on[name.slice(5)] = value;
|
on[name.slice(5)] = value;
|
||||||
} else {
|
} else {
|
||||||
const message = directiveErrorMap.get(name.split("-").slice(0, 2).join("-"));
|
const message = directiveErrorMap.get(name.split("-").slice(0, 2).join("-"));
|
||||||
throw new Error(message || `unsupported directive on Component: ${name}`);
|
throw new OwlError(message || `unsupported directive on Component: ${name}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
props = props || {};
|
props = props || {};
|
||||||
@@ -729,7 +735,7 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
const slotNodes = Array.from(clone.querySelectorAll("[t-set-slot]"));
|
const slotNodes = Array.from(clone.querySelectorAll("[t-set-slot]"));
|
||||||
for (let slotNode of slotNodes) {
|
for (let slotNode of slotNodes) {
|
||||||
if (slotNode.tagName !== "t") {
|
if (slotNode.tagName !== "t") {
|
||||||
throw new Error(
|
throw new OwlError(
|
||||||
`Directive 't-set-slot' can only be used on <t> nodes (used on a <${slotNode.tagName}>)`
|
`Directive 't-set-slot' can only be used on <t> nodes (used on a <${slotNode.tagName}>)`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -904,7 +910,7 @@ function normalizeTIf(el: Element) {
|
|||||||
let nattr = (name: string) => +!!node.getAttribute(name);
|
let nattr = (name: string) => +!!node.getAttribute(name);
|
||||||
if (prevElem && (pattr("t-if") || pattr("t-elif"))) {
|
if (prevElem && (pattr("t-if") || pattr("t-elif"))) {
|
||||||
if (pattr("t-foreach")) {
|
if (pattr("t-foreach")) {
|
||||||
throw new Error(
|
throw new OwlError(
|
||||||
"t-if cannot stay at the same level as t-foreach when using t-elif or t-else"
|
"t-if cannot stay at the same level as t-foreach when using t-elif or t-else"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -913,19 +919,19 @@ function normalizeTIf(el: Element) {
|
|||||||
return a + b;
|
return a + b;
|
||||||
}) > 1
|
}) > 1
|
||||||
) {
|
) {
|
||||||
throw new Error("Only one conditional branching directive is allowed per node");
|
throw new OwlError("Only one conditional branching directive is allowed per node");
|
||||||
}
|
}
|
||||||
// All text (with only spaces) and comment nodes (nodeType 8) between
|
// All text (with only spaces) and comment nodes (nodeType 8) between
|
||||||
// branch nodes are removed
|
// branch nodes are removed
|
||||||
let textNode;
|
let textNode;
|
||||||
while ((textNode = node.previousSibling) !== prevElem) {
|
while ((textNode = node.previousSibling) !== prevElem) {
|
||||||
if (textNode!.nodeValue!.trim().length && textNode!.nodeType !== 8) {
|
if (textNode!.nodeValue!.trim().length && textNode!.nodeType !== 8) {
|
||||||
throw new Error("text is not allowed between branching directives");
|
throw new OwlError("text is not allowed between branching directives");
|
||||||
}
|
}
|
||||||
textNode!.remove();
|
textNode!.remove();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new OwlError(
|
||||||
"t-elif and t-else directives must be preceded by a t-if or t-elif directive"
|
"t-elif and t-else directives must be preceded by a t-if or t-elif directive"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -946,7 +952,7 @@ function normalizeTEsc(el: Element) {
|
|||||||
);
|
);
|
||||||
for (const el of elements) {
|
for (const el of elements) {
|
||||||
if (el.childNodes.length) {
|
if (el.childNodes.length) {
|
||||||
throw new Error("Cannot have t-esc on a component that already has content");
|
throw new OwlError("Cannot have t-esc on a component that already has content");
|
||||||
}
|
}
|
||||||
const value = el.getAttribute("t-esc");
|
const value = el.getAttribute("t-esc");
|
||||||
el.removeAttribute("t-esc");
|
el.removeAttribute("t-esc");
|
||||||
@@ -1000,7 +1006,7 @@ function parseXML(xml: string): XMLDocument {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Error(msg);
|
throw new OwlError(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
import { Component, ComponentConstructor, Props } from "./component";
|
import { Component, ComponentConstructor, Props } from "./component";
|
||||||
import { ComponentNode } from "./component_node";
|
import { ComponentNode } from "./component_node";
|
||||||
import { nodeErrorHandlers } from "./error_handling";
|
import { nodeErrorHandlers, OwlError } from "./error_handling";
|
||||||
import { Fiber, MountOptions } from "./fibers";
|
import { Fiber, MountOptions } from "./fibers";
|
||||||
import { Scheduler } from "./scheduler";
|
import { Scheduler } from "./scheduler";
|
||||||
import { validateProps } from "./template_helpers";
|
import { validateProps } from "./template_helpers";
|
||||||
@@ -154,9 +154,9 @@ export class App<
|
|||||||
if (isStatic) {
|
if (isStatic) {
|
||||||
C = parent.constructor.components[name as any];
|
C = parent.constructor.components[name as any];
|
||||||
if (!C) {
|
if (!C) {
|
||||||
throw new Error(`Cannot find the definition of component "${name}"`);
|
throw new OwlError(`Cannot find the definition of component "${name}"`);
|
||||||
} else if (!(C.prototype instanceof Component)) {
|
} else if (!(C.prototype instanceof Component)) {
|
||||||
throw new Error(
|
throw new OwlError(
|
||||||
`"${name}" is not a Component. It must inherit from the Component class`
|
`"${name}" is not a Component. It must inherit from the Component class`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ export function updateClass(this: HTMLElement, val: any, oldVal: any) {
|
|||||||
export function makePropSetter(name: string): Setter<HTMLElement> {
|
export function makePropSetter(name: string): Setter<HTMLElement> {
|
||||||
return function setProp(this: HTMLElement, value: any) {
|
return function setProp(this: HTMLElement, value: any) {
|
||||||
// support 0, fallback to empty string for other falsy values
|
// support 0, fallback to empty string for other falsy values
|
||||||
(this as any)[name] = value === 0 ? 0 : value || "";
|
(this as any)[name] = value === 0 ? 0 : value ? value.valueOf() : "";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { OwlError } from "../error_handling";
|
||||||
import {
|
import {
|
||||||
attrsSetter,
|
attrsSetter,
|
||||||
attrsUpdater,
|
attrsUpdater,
|
||||||
@@ -245,7 +246,7 @@ function buildTree(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Error("boom");
|
throw new OwlError("boom");
|
||||||
}
|
}
|
||||||
|
|
||||||
function addRef(tree: IntermediateTree) {
|
function addRef(tree: IntermediateTree) {
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ function createElementHandler(evName: string, capture: boolean = false): EventHa
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listener(ev: Event) {
|
function listener(ev: Event) {
|
||||||
const currentTarget = ev.currentTarget;
|
const currentTarget = ev.currentTarget as HTMLElement;
|
||||||
if (!currentTarget || !document.contains(currentTarget as HTMLElement)) return;
|
if (!currentTarget || !currentTarget.ownerDocument.contains(currentTarget)) return;
|
||||||
const data = (currentTarget as any)[eventKey];
|
const data = (currentTarget as any)[eventKey];
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
config.mainEventHandler(data, ev, currentTarget);
|
config.mainEventHandler(data, ev, currentTarget);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { App, Env } from "./app";
|
import type { App, Env } from "./app";
|
||||||
import { BDom, VNode } from "./blockdom";
|
import { BDom, VNode } from "./blockdom";
|
||||||
import { Component, ComponentConstructor, Props } from "./component";
|
import { Component, ComponentConstructor, Props } from "./component";
|
||||||
import { fibersInError, handleError } from "./error_handling";
|
import { fibersInError, handleError, OwlError } from "./error_handling";
|
||||||
import { Fiber, makeChildFiber, makeRootFiber, MountFiber, MountOptions } from "./fibers";
|
import { Fiber, makeChildFiber, makeRootFiber, MountFiber, MountOptions } from "./fibers";
|
||||||
import {
|
import {
|
||||||
clearReactivesForCallback,
|
clearReactivesForCallback,
|
||||||
@@ -18,7 +18,7 @@ let currentNode: ComponentNode | null = null;
|
|||||||
|
|
||||||
export function getCurrent(): ComponentNode {
|
export function getCurrent(): ComponentNode {
|
||||||
if (!currentNode) {
|
if (!currentNode) {
|
||||||
throw new Error("No active component (a hook function should only be called in 'setup')");
|
throw new OwlError("No active component (a hook function should only be called in 'setup')");
|
||||||
}
|
}
|
||||||
return currentNode;
|
return currentNode;
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,6 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
|
|||||||
|
|
||||||
renderFn: Function;
|
renderFn: Function;
|
||||||
parent: ComponentNode | null;
|
parent: ComponentNode | null;
|
||||||
level: number;
|
|
||||||
childEnv: Env;
|
childEnv: Env;
|
||||||
children: { [key: string]: ComponentNode } = Object.create(null);
|
children: { [key: string]: ComponentNode } = Object.create(null);
|
||||||
refs: any = {};
|
refs: any = {};
|
||||||
@@ -108,7 +107,6 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
|
|||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.props = props;
|
this.props = props;
|
||||||
this.parentKey = parentKey;
|
this.parentKey = parentKey;
|
||||||
this.level = parent ? parent.level + 1 : 0;
|
|
||||||
const defaultProps = C.defaultProps;
|
const defaultProps = C.defaultProps;
|
||||||
props = Object.assign({}, props);
|
props = Object.assign({}, props);
|
||||||
if (defaultProps) {
|
if (defaultProps) {
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
import type { ComponentNode } from "./component_node";
|
import type { ComponentNode } from "./component_node";
|
||||||
import type { Fiber } from "./fibers";
|
import type { Fiber } from "./fibers";
|
||||||
|
|
||||||
|
// Custom error class that wraps error that happen in the owl lifecycle
|
||||||
|
export class OwlError extends Error {
|
||||||
|
cause?: any;
|
||||||
|
}
|
||||||
|
|
||||||
// Maps fibers to thrown errors
|
// Maps fibers to thrown errors
|
||||||
export const fibersInError: WeakMap<Fiber, any> = new WeakMap();
|
export const fibersInError: WeakMap<Fiber, any> = new WeakMap();
|
||||||
export const nodeErrorHandlers: WeakMap<ComponentNode, ((error: any) => void)[]> = new WeakMap();
|
export const nodeErrorHandlers: WeakMap<ComponentNode, ((error: any) => void)[]> = new WeakMap();
|
||||||
@@ -37,7 +42,14 @@ function _handleError(node: ComponentNode | null, error: any): boolean {
|
|||||||
|
|
||||||
type ErrorParams = { error: any } & ({ node: ComponentNode } | { fiber: Fiber });
|
type ErrorParams = { error: any } & ({ node: ComponentNode } | { fiber: Fiber });
|
||||||
export function handleError(params: ErrorParams) {
|
export function handleError(params: ErrorParams) {
|
||||||
const error = params.error;
|
let { error } = params;
|
||||||
|
// Wrap error if it wasn't wrapped by wrapError (ie when not in dev mode)
|
||||||
|
if (!(error instanceof OwlError)) {
|
||||||
|
error = Object.assign(
|
||||||
|
new OwlError(`An error occured in the owl lifecycle (see this Error's "cause" property)`),
|
||||||
|
{ cause: error }
|
||||||
|
);
|
||||||
|
}
|
||||||
const node = "node" in params ? params.node : params.fiber.node;
|
const node = "node" in params ? params.node : params.fiber.node;
|
||||||
const fiber = "fiber" in params ? params.fiber : node.fiber!;
|
const fiber = "fiber" in params ? params.fiber : node.fiber!;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { filterOutModifiersFromData } from "./blockdom/config";
|
import { filterOutModifiersFromData } from "./blockdom/config";
|
||||||
import { STATUS } from "./status";
|
import { STATUS } from "./status";
|
||||||
|
import { OwlError } from "./error_handling";
|
||||||
|
|
||||||
export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarget | null) => {
|
export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarget | null) => {
|
||||||
const { data: _data, modifiers } = filterOutModifiersFromData(data);
|
const { data: _data, modifiers } = filterOutModifiersFromData(data);
|
||||||
@@ -33,7 +34,7 @@ export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarg
|
|||||||
if (Object.hasOwnProperty.call(data, 0)) {
|
if (Object.hasOwnProperty.call(data, 0)) {
|
||||||
const handler = data[0];
|
const handler = data[0];
|
||||||
if (typeof handler !== "function") {
|
if (typeof handler !== "function") {
|
||||||
throw new Error(`Invalid handler (expected a function, received: '${handler}')`);
|
throw new OwlError(`Invalid handler (expected a function, received: '${handler}')`);
|
||||||
}
|
}
|
||||||
let node = data[1] ? data[1].__owl__ : null;
|
let node = data[1] ? data[1].__owl__ : null;
|
||||||
if (node ? node.status === STATUS.MOUNTED : true) {
|
if (node ? node.status === STATUS.MOUNTED : true) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BDom, mount } from "./blockdom";
|
import { BDom, mount } from "./blockdom";
|
||||||
import type { ComponentNode } from "./component_node";
|
import type { ComponentNode } from "./component_node";
|
||||||
import { fibersInError, handleError } from "./error_handling";
|
import { fibersInError, handleError, OwlError } from "./error_handling";
|
||||||
import { STATUS } from "./status";
|
import { STATUS } from "./status";
|
||||||
|
|
||||||
export function makeChildFiber(node: ComponentNode, parent: Fiber): Fiber {
|
export function makeChildFiber(node: ComponentNode, parent: Fiber): Fiber {
|
||||||
@@ -43,7 +43,7 @@ export function makeRootFiber(node: ComponentNode): Fiber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function throwOnRender() {
|
function throwOnRender() {
|
||||||
throw new Error("Attempted to render cancelled fiber");
|
throw new OwlError("Attempted to render cancelled fiber");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ export const blockDom = {
|
|||||||
export { App, mount } from "./app";
|
export { App, mount } from "./app";
|
||||||
export { xml } from "./template_set";
|
export { xml } from "./template_set";
|
||||||
export { Component } from "./component";
|
export { Component } from "./component";
|
||||||
|
export type { ComponentConstructor } from "./component";
|
||||||
export { useComponent, useState } from "./component_node";
|
export { useComponent, useState } from "./component_node";
|
||||||
export { status } from "./status";
|
export { status } from "./status";
|
||||||
export { reactive, markRaw, toRaw } from "./reactivity";
|
export { reactive, markRaw, toRaw } from "./reactivity";
|
||||||
@@ -54,5 +55,6 @@ export {
|
|||||||
onError,
|
onError,
|
||||||
} from "./lifecycle_hooks";
|
} from "./lifecycle_hooks";
|
||||||
export { validate } from "./validation";
|
export { validate } from "./validation";
|
||||||
|
export { OwlError } from "./error_handling";
|
||||||
|
|
||||||
export const __info__ = {};
|
export const __info__ = {};
|
||||||
|
|||||||
@@ -1,21 +1,30 @@
|
|||||||
import { getCurrent } from "./component_node";
|
import { getCurrent } from "./component_node";
|
||||||
import { nodeErrorHandlers } from "./error_handling";
|
import { nodeErrorHandlers, OwlError } from "./error_handling";
|
||||||
|
|
||||||
const TIMEOUT = Symbol("timeout");
|
const TIMEOUT = Symbol("timeout");
|
||||||
function wrapError(fn: (...args: any[]) => any, hookName: string) {
|
function wrapError(fn: (...args: any[]) => any, hookName: string) {
|
||||||
const error = new Error(`The following error occurred in ${hookName}: `) as Error & {
|
const error = new OwlError(`The following error occurred in ${hookName}: `) as Error & {
|
||||||
cause: any;
|
cause: any;
|
||||||
};
|
};
|
||||||
const timeoutError = new Error(`${hookName}'s promise hasn't resolved after 3 seconds`);
|
const timeoutError = new OwlError(`${hookName}'s promise hasn't resolved after 3 seconds`);
|
||||||
const node = getCurrent();
|
const node = getCurrent();
|
||||||
return (...args: any[]) => {
|
return (...args: any[]) => {
|
||||||
|
const onError = (cause: any) => {
|
||||||
|
error.cause = cause;
|
||||||
|
if (cause instanceof Error) {
|
||||||
|
error.message += `"${cause.message}"`;
|
||||||
|
} else {
|
||||||
|
error.message = `Something that is not an Error was thrown in ${hookName} (see this Error's "cause" property)`;
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
};
|
||||||
try {
|
try {
|
||||||
const result = fn(...args);
|
const result = fn(...args);
|
||||||
if (result instanceof Promise) {
|
if (result instanceof Promise) {
|
||||||
if (hookName === "onWillStart" || hookName === "onWillUpdateProps") {
|
if (hookName === "onWillStart" || hookName === "onWillUpdateProps") {
|
||||||
const fiber = node.fiber;
|
const fiber = node.fiber;
|
||||||
Promise.race([
|
Promise.race([
|
||||||
result,
|
result.catch(() => {}),
|
||||||
new Promise((resolve) => setTimeout(() => resolve(TIMEOUT), 3000)),
|
new Promise((resolve) => setTimeout(() => resolve(TIMEOUT), 3000)),
|
||||||
]).then((res) => {
|
]).then((res) => {
|
||||||
if (res === TIMEOUT && node.fiber === fiber) {
|
if (res === TIMEOUT && node.fiber === fiber) {
|
||||||
@@ -23,20 +32,11 @@ function wrapError(fn: (...args: any[]) => any, hookName: string) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return result.catch((cause) => {
|
return result.catch(onError);
|
||||||
error.cause = cause;
|
|
||||||
if (cause instanceof Error) {
|
|
||||||
error.message += `"${cause.message}"`;
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} catch (cause) {
|
} catch (cause) {
|
||||||
if (cause instanceof Error) {
|
onError(cause);
|
||||||
error.message += `"${cause.message}"`;
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { onWillUnmount } from "./lifecycle_hooks";
|
import { onWillUnmount } from "./lifecycle_hooks";
|
||||||
import { BDom, text, VNode } from "./blockdom";
|
import { BDom, text, VNode } from "./blockdom";
|
||||||
import { Component } from "./component";
|
import { Component } from "./component";
|
||||||
|
import { OwlError } from "./error_handling";
|
||||||
|
|
||||||
const VText: any = text("").constructor;
|
const VText: any = text("").constructor;
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ class VPortal extends VText implements Partial<VNode<VPortal>> {
|
|||||||
}
|
}
|
||||||
this.target = el && el.querySelector(this.selector);
|
this.target = el && el.querySelector(this.selector);
|
||||||
if (!this.target) {
|
if (!this.target) {
|
||||||
throw new Error("invalid portal target");
|
throw new OwlError("invalid portal target");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.realBDom!.mount(this.target!, null);
|
this.realBDom!.mount(this.target!, null);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Callback } from "./utils";
|
import { Callback } from "./utils";
|
||||||
|
import { OwlError } from "./error_handling";
|
||||||
|
|
||||||
// Allows to get the target of a Reactive (used for making a new Reactive from the underlying object)
|
// Allows to get the target of a Reactive (used for making a new Reactive from the underlying object)
|
||||||
export const TARGET = Symbol("Target");
|
export const TARGET = Symbol("Target");
|
||||||
@@ -197,7 +198,7 @@ export function reactive<T extends Target>(
|
|||||||
callback: Callback = () => {}
|
callback: Callback = () => {}
|
||||||
): Reactive<T> | NonReactive<T> {
|
): Reactive<T> | NonReactive<T> {
|
||||||
if (!canBeMadeReactive(target)) {
|
if (!canBeMadeReactive(target)) {
|
||||||
throw new Error(`Cannot make the given value reactive`);
|
throw new OwlError(`Cannot make the given value reactive`);
|
||||||
}
|
}
|
||||||
if (SKIP in target) {
|
if (SKIP in target) {
|
||||||
return target as NonReactive<T>;
|
return target as NonReactive<T>;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { html } from "./blockdom/index";
|
|||||||
import { isOptional, validateSchema } from "./validation";
|
import { isOptional, validateSchema } from "./validation";
|
||||||
import type { ComponentConstructor } from "./component";
|
import type { ComponentConstructor } from "./component";
|
||||||
import { markRaw } from "./reactivity";
|
import { markRaw } from "./reactivity";
|
||||||
|
import { OwlError } from "./error_handling";
|
||||||
|
|
||||||
const ObjectCreate = Object.create;
|
const ObjectCreate = Object.create;
|
||||||
/**
|
/**
|
||||||
@@ -70,7 +71,7 @@ function prepareList(collection: any): [any[], any[], number, any[]] {
|
|||||||
values = Object.keys(collection);
|
values = Object.keys(collection);
|
||||||
keys = Object.values(collection);
|
keys = Object.values(collection);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Invalid loop expression");
|
throw new OwlError("Invalid loop expression");
|
||||||
}
|
}
|
||||||
const n = values.length;
|
const n = values.length;
|
||||||
return [keys, values, n, new Array(n)];
|
return [keys, values, n, new Array(n)];
|
||||||
@@ -191,7 +192,7 @@ function multiRefSetter(refs: RefMap, name: string): RefSetter {
|
|||||||
if (el) {
|
if (el) {
|
||||||
count++;
|
count++;
|
||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
throw new Error("Cannot have 2 elements with same ref name at the same time");
|
throw new OwlError("Cannot have 2 elements with same ref name at the same time");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count === 0 || el) {
|
if (count === 0 || el) {
|
||||||
@@ -233,7 +234,7 @@ export function validateProps<P>(name: string | ComponentConstructor<P>, props:
|
|||||||
: name in schema && !("*" in schema) && !isOptional(schema[name]);
|
: name in schema && !("*" in schema) && !isOptional(schema[name]);
|
||||||
for (let p in defaultProps) {
|
for (let p in defaultProps) {
|
||||||
if (isMandatory(p)) {
|
if (isMandatory(p)) {
|
||||||
throw new Error(
|
throw new OwlError(
|
||||||
`A default value cannot be defined for a mandatory prop (name: '${p}', component: ${ComponentClass.name})`
|
`A default value cannot be defined for a mandatory prop (name: '${p}', component: ${ComponentClass.name})`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -242,7 +243,9 @@ export function validateProps<P>(name: string | ComponentConstructor<P>, props:
|
|||||||
|
|
||||||
const errors = validateSchema(props, schema);
|
const errors = validateSchema(props, schema);
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
throw new Error(`Invalid props for component '${ComponentClass.name}': ` + errors.join(", "));
|
throw new OwlError(
|
||||||
|
`Invalid props for component '${ComponentClass.name}': ` + errors.join(", ")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,4 +267,5 @@ export const helpers = {
|
|||||||
bind,
|
bind,
|
||||||
createCatcher,
|
createCatcher,
|
||||||
markRaw,
|
markRaw,
|
||||||
|
OwlError,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { comment, createBlock, html, list, multi, text, toggler } from "./blockd
|
|||||||
import { getCurrent } from "./component_node";
|
import { getCurrent } from "./component_node";
|
||||||
import { Portal, portalTemplate } from "./portal";
|
import { Portal, portalTemplate } from "./portal";
|
||||||
import { helpers } from "./template_helpers";
|
import { helpers } from "./template_helpers";
|
||||||
|
import { OwlError } from "./error_handling";
|
||||||
|
|
||||||
const bdom = { text, createBlock, list, multi, html, toggler, comment };
|
const bdom = { text, createBlock, list, multi, html, toggler, comment };
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ function parseXML(xml: string): Document {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Error(msg);
|
throw new OwlError(msg);
|
||||||
}
|
}
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
@@ -76,7 +77,7 @@ export class TemplateSet {
|
|||||||
if (currentAsString === newAsString) {
|
if (currentAsString === newAsString) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new Error(`Template ${name} already defined with different content`);
|
throw new OwlError(`Template ${name} already defined with different content`);
|
||||||
}
|
}
|
||||||
this.rawTemplates[name] = template;
|
this.rawTemplates[name] = template;
|
||||||
}
|
}
|
||||||
@@ -102,7 +103,7 @@ export class TemplateSet {
|
|||||||
const componentName = getCurrent().component.constructor.name;
|
const componentName = getCurrent().component.constructor.name;
|
||||||
extraInfo = ` (for component "${componentName}")`;
|
extraInfo = ` (for component "${componentName}")`;
|
||||||
} catch {}
|
} catch {}
|
||||||
throw new Error(`Missing template: "${name}"${extraInfo}`);
|
throw new OwlError(`Missing template: "${name}"${extraInfo}`);
|
||||||
}
|
}
|
||||||
const isFn = typeof rawTemplate === "function" && !(rawTemplate instanceof Element);
|
const isFn = typeof rawTemplate === "function" && !(rawTemplate instanceof Element);
|
||||||
const templateFn = isFn ? rawTemplate : this._compileTemplate(name, rawTemplate);
|
const templateFn = isFn ? rawTemplate : this._compileTemplate(name, rawTemplate);
|
||||||
@@ -119,7 +120,7 @@ export class TemplateSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_compileTemplate(name: string, template: string | Element): ReturnType<typeof compile> {
|
_compileTemplate(name: string, template: string | Element): ReturnType<typeof compile> {
|
||||||
throw new Error(`Unable to compile a template. Please use owl full build instead`);
|
throw new OwlError(`Unable to compile a template. Please use owl full build instead`);
|
||||||
}
|
}
|
||||||
|
|
||||||
callTemplate(owner: any, subTemplate: string, ctx: any, parent: any, key: any): any {
|
callTemplate(owner: any, subTemplate: string, ctx: any, parent: any, key: any): any {
|
||||||
|
|||||||
+12
-5
@@ -1,3 +1,4 @@
|
|||||||
|
import { OwlError } from "./error_handling";
|
||||||
export type Callback = () => void;
|
export type Callback = () => void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,12 +28,18 @@ export function batched(callback: Callback): Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function validateTarget(target: HTMLElement) {
|
export function validateTarget(target: HTMLElement) {
|
||||||
if (!(target instanceof HTMLElement)) {
|
// Get the document and HTMLElement corresponding to the target to allow mounting in iframes
|
||||||
throw new Error("Cannot mount component: the target is not a valid DOM element");
|
const document = target && target.ownerDocument;
|
||||||
}
|
if (document) {
|
||||||
|
const HTMLElement = document.defaultView!.HTMLElement;
|
||||||
|
if (target instanceof HTMLElement) {
|
||||||
if (!document.body.contains(target)) {
|
if (!document.body.contains(target)) {
|
||||||
throw new Error("Cannot mount a component on a detached dom node");
|
throw new OwlError("Cannot mount a component on a detached dom node");
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new OwlError("Cannot mount component: the target is not a valid DOM element");
|
||||||
}
|
}
|
||||||
|
|
||||||
export class EventBus extends EventTarget {
|
export class EventBus extends EventTarget {
|
||||||
@@ -54,7 +61,7 @@ export function whenReady(fn?: any): Promise<void> {
|
|||||||
export async function loadFile(url: string): Promise<string> {
|
export async function loadFile(url: string): Promise<string> {
|
||||||
const result = await fetch(url);
|
const result = await fetch(url);
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
throw new Error("Error while fetching xml templates");
|
throw new OwlError("Error while fetching xml templates");
|
||||||
}
|
}
|
||||||
return await result.text();
|
return await result.text();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { OwlError } from "./error_handling";
|
||||||
|
|
||||||
type BaseType =
|
type BaseType =
|
||||||
| typeof String
|
| typeof String
|
||||||
| typeof Boolean
|
| typeof Boolean
|
||||||
@@ -70,7 +72,7 @@ function toSchema(spec: SimplifiedSchema): NormalizedSchema {
|
|||||||
export function validate(obj: { [key: string]: any }, spec: Schema) {
|
export function validate(obj: { [key: string]: any }, spec: Schema) {
|
||||||
let errors = validateSchema(obj, spec);
|
let errors = validateSchema(obj, spec);
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
throw new Error("Invalid object: " + errors.join(", "));
|
throw new OwlError("Invalid object: " + errors.join(", "));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,19 @@ exports[`app can configure an app with props 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`app can mount app in an iframe 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<div class=\\"my-div\\"/>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return block1();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`app destroy remove the widget from the DOM 1`] = `
|
exports[`app destroy remove the widget from the DOM 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -76,4 +76,22 @@ describe("app", () => {
|
|||||||
"Component 'Root' does not have a static props description"
|
"Component 'Root' does not have a static props description"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("can mount app in an iframe", async () => {
|
||||||
|
class SomeComponent extends Component {
|
||||||
|
static template = xml`<div class="my-div"/>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const iframe = document.createElement("iframe");
|
||||||
|
fixture.appendChild(iframe);
|
||||||
|
const app = new App(SomeComponent);
|
||||||
|
const iframeDoc = iframe.contentDocument!;
|
||||||
|
const comp = await app.mount(iframeDoc.body);
|
||||||
|
const div = iframeDoc.querySelector(".my-div");
|
||||||
|
expect(div).not.toBe(null);
|
||||||
|
expect(iframeDoc.contains(div)).toBe(true);
|
||||||
|
app.destroy();
|
||||||
|
expect(iframeDoc.contains(div)).toBe(false);
|
||||||
|
expect(status(comp)).toBe("destroyed");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -708,6 +708,20 @@ exports[`attributes updating classes (with obj notation) 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`attributes updating property with falsy value 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<input block-attribute-0=\\"value\\"/>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let attr1 = new String((ctx['v']) || \\"\\");
|
||||||
|
return block1([attr1]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`attributes various escapes 1`] = `
|
exports[`attributes various escapes 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
@@ -745,7 +759,7 @@ exports[`special cases for some specific html attributes/properties input of typ
|
|||||||
let block1 = createBlock(\`<input type=\\"checkbox\\" block-attribute-0=\\"indeterminate\\"/>\`);
|
let block1 = createBlock(\`<input type=\\"checkbox\\" block-attribute-0=\\"indeterminate\\"/>\`);
|
||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let attr1 = ctx['v'];
|
let attr1 = new Boolean(ctx['v']);
|
||||||
return block1([attr1]);
|
return block1([attr1]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -759,7 +773,21 @@ exports[`special cases for some specific html attributes/properties input type=
|
|||||||
let block1 = createBlock(\`<input type=\\"checkbox\\" block-attribute-0=\\"checked\\"/>\`);
|
let block1 = createBlock(\`<input type=\\"checkbox\\" block-attribute-0=\\"checked\\"/>\`);
|
||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let attr1 = ctx['flag'];
|
let attr1 = new Boolean(ctx['flag']);
|
||||||
|
return block1([attr1]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`special cases for some specific html attributes/properties input with t-att-value (patching with same value 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<input block-attribute-0=\\"value\\"/>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let attr1 = new String((ctx['v']) || \\"\\");
|
||||||
return block1([attr1]);
|
return block1([attr1]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -773,7 +801,21 @@ exports[`special cases for some specific html attributes/properties input with t
|
|||||||
let block1 = createBlock(\`<input block-attribute-0=\\"value\\"/>\`);
|
let block1 = createBlock(\`<input block-attribute-0=\\"value\\"/>\`);
|
||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let attr1 = ctx['v'];
|
let attr1 = new String((ctx['v']) || \\"\\");
|
||||||
|
return block1([attr1]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`special cases for some specific html attributes/properties input, type checkbox, with t-att-checked (patching with same value 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<input type=\\"checkbox\\" block-attribute-0=\\"checked\\"/>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let attr1 = new Boolean(ctx['v']);
|
||||||
return block1([attr1]);
|
return block1([attr1]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -787,7 +829,7 @@ exports[`special cases for some specific html attributes/properties select with
|
|||||||
let block1 = createBlock(\`<select block-attribute-0=\\"value\\"><option value=\\"potato\\">Potato</option><option value=\\"tomato\\">Tomato</option><option value=\\"onion\\">Onion</option></select>\`);
|
let block1 = createBlock(\`<select block-attribute-0=\\"value\\"><option value=\\"potato\\">Potato</option><option value=\\"tomato\\">Tomato</option><option value=\\"onion\\">Onion</option></select>\`);
|
||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let attr1 = ctx['value'];
|
let attr1 = new String((ctx['value']) || \\"\\");
|
||||||
return block1([attr1]);
|
return block1([attr1]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -801,7 +843,7 @@ exports[`special cases for some specific html attributes/properties textarea wit
|
|||||||
let block1 = createBlock(\`<textarea block-attribute-0=\\"value\\"/>\`);
|
let block1 = createBlock(\`<textarea block-attribute-0=\\"value\\"/>\`);
|
||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let attr1 = ctx['v'];
|
let attr1 = new String((ctx['v']) || \\"\\");
|
||||||
return block1([attr1]);
|
return block1([attr1]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ exports[`misc other complex template 1`] = `
|
|||||||
ctx[\`category\`] = v_block15[i1];
|
ctx[\`category\`] = v_block15[i1];
|
||||||
const key1 = ctx['category'].id;
|
const key1 = ctx['category'].id;
|
||||||
let attr6 = ctx['category'].id;
|
let attr6 = ctx['category'].id;
|
||||||
let attr7 = ctx['category'].id==ctx['options'].active_category_id;
|
let attr7 = new Boolean(ctx['category'].id==ctx['options'].active_category_id);
|
||||||
let txt5 = ctx['category'].name;
|
let txt5 = ctx['category'].name;
|
||||||
c_block15[i1] = withKey(block16([attr6, attr7, txt5]), key1);
|
c_block15[i1] = withKey(block16([attr6, attr7, txt5]), key1);
|
||||||
}
|
}
|
||||||
@@ -277,7 +277,7 @@ exports[`misc other complex template 1`] = `
|
|||||||
const b15 = list(c_block15);
|
const b15 = list(c_block15);
|
||||||
b14 = block14([], [b15]);
|
b14 = block14([], [b15]);
|
||||||
}
|
}
|
||||||
let attr8 = ctx['search'].value;
|
let attr8 = new String((ctx['search'].value) || \\"\\");
|
||||||
let hdlr4 = [ctx['updateFilter'], ctx];
|
let hdlr4 = [ctx['updateFilter'], ctx];
|
||||||
let hdlr5 = [ctx['updateFilter'], ctx];
|
let hdlr5 = [ctx['updateFilter'], ctx];
|
||||||
let hdlr6 = [ctx['clearSearch'], ctx];
|
let hdlr6 = [ctx['clearSearch'], ctx];
|
||||||
@@ -291,7 +291,7 @@ exports[`misc other complex template 1`] = `
|
|||||||
if (!ctx['trigger'].manual&&ctx['trigger'].project_id===ctx['project'].id&&ctx['trigger'].category_id===ctx['options'].active_category_id) {
|
if (!ctx['trigger'].manual&&ctx['trigger'].project_id===ctx['project'].id&&ctx['trigger'].category_id===ctx['options'].active_category_id) {
|
||||||
let attr9 = \`trigger_\${ctx['trigger'].id}\`;
|
let attr9 = \`trigger_\${ctx['trigger'].id}\`;
|
||||||
let attr10 = \`trigger_\${ctx['trigger'].id}\`;
|
let attr10 = \`trigger_\${ctx['trigger'].id}\`;
|
||||||
let attr11 = ctx['options'].trigger_display[ctx['trigger'].id];
|
let attr11 = new Boolean(ctx['options'].trigger_display[ctx['trigger'].id]);
|
||||||
let attr12 = ctx['trigger'].id;
|
let attr12 = ctx['trigger'].id;
|
||||||
let hdlr7 = [ctx['updateTriggerDisplay'], ctx];
|
let hdlr7 = [ctx['updateTriggerDisplay'], ctx];
|
||||||
let attr13 = \`trigger_\${ctx['trigger'].id}\`;
|
let attr13 = \`trigger_\${ctx['trigger'].id}\`;
|
||||||
|
|||||||
@@ -329,6 +329,35 @@ describe("attributes", () => {
|
|||||||
expect(fixture.innerHTML).toBe('<div value=""></div>');
|
expect(fixture.innerHTML).toBe('<div value=""></div>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("updating property with falsy value", async () => {
|
||||||
|
// render input with initial value
|
||||||
|
const template = `<input t-att-value="v"></input>`;
|
||||||
|
const bnode1 = renderToBdom(template, { v: false });
|
||||||
|
const fixture = makeTestFixture();
|
||||||
|
mount(bnode1, fixture);
|
||||||
|
|
||||||
|
const input = fixture.querySelector("input")!;
|
||||||
|
expect(input.value).toBe("");
|
||||||
|
|
||||||
|
patch(bnode1, renderToBdom(template, { v: "owl" }));
|
||||||
|
expect(input.value).toBe("owl");
|
||||||
|
|
||||||
|
patch(bnode1, renderToBdom(template, { v: false }));
|
||||||
|
expect(input.value).toBe("");
|
||||||
|
|
||||||
|
patch(bnode1, renderToBdom(template, { v: "owl" }));
|
||||||
|
expect(input.value).toBe("owl");
|
||||||
|
|
||||||
|
patch(bnode1, renderToBdom(template, { v: undefined }));
|
||||||
|
expect(input.value).toBe("");
|
||||||
|
|
||||||
|
patch(bnode1, renderToBdom(template, { v: "owl" }));
|
||||||
|
expect(input.value).toBe("owl");
|
||||||
|
|
||||||
|
patch(bnode1, renderToBdom(template, { v: null }));
|
||||||
|
expect(input.value).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
test("changing a class with t-att-class", () => {
|
test("changing a class with t-att-class", () => {
|
||||||
// render input with initial value
|
// render input with initial value
|
||||||
const template = `<div t-att-class="v"/>`;
|
const template = `<div t-att-class="v"/>`;
|
||||||
@@ -429,6 +458,42 @@ describe("special cases for some specific html attributes/properties", () => {
|
|||||||
expect(input.value).toBe("potato");
|
expect(input.value).toBe("potato");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("input with t-att-value (patching with same value", () => {
|
||||||
|
// render input with initial value
|
||||||
|
const template = `<input t-att-value="v"/>`;
|
||||||
|
const bnode1 = renderToBdom(template, { v: "zucchini" });
|
||||||
|
const fixture = makeTestFixture();
|
||||||
|
mount(bnode1, fixture);
|
||||||
|
const input = fixture.querySelector("input")!;
|
||||||
|
expect(input.value).toBe("zucchini");
|
||||||
|
|
||||||
|
// change value manually in input, to simulate user input
|
||||||
|
input.value = "tomato";
|
||||||
|
expect(input.value).toBe("tomato");
|
||||||
|
|
||||||
|
const bnode2 = renderToBdom(template, { v: "zucchini" });
|
||||||
|
patch(bnode1, bnode2);
|
||||||
|
expect(input.value).toBe("zucchini");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("input, type checkbox, with t-att-checked (patching with same value", () => {
|
||||||
|
// render input with initial value
|
||||||
|
const template = `<input type="checkbox" t-att-checked="v"/>`;
|
||||||
|
const bnode1 = renderToBdom(template, { v: true });
|
||||||
|
const fixture = makeTestFixture();
|
||||||
|
mount(bnode1, fixture);
|
||||||
|
const input = fixture.querySelector("input")!;
|
||||||
|
expect(input.checked).toBe(true);
|
||||||
|
|
||||||
|
// change checked manually in input, to simulate user input
|
||||||
|
input.checked = false;
|
||||||
|
expect(input.checked).toBe(false);
|
||||||
|
|
||||||
|
const bnode2 = renderToBdom(template, { v: true });
|
||||||
|
patch(bnode1, bnode2);
|
||||||
|
expect(input.checked).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
test("input of type checkbox with t-att-indeterminate", () => {
|
test("input of type checkbox with t-att-indeterminate", () => {
|
||||||
const template = `<input type="checkbox" t-att-indeterminate="v"/>`;
|
const template = `<input type="checkbox" t-att-indeterminate="v"/>`;
|
||||||
const bnode1 = renderToBdom(template, { v: true });
|
const bnode1 = renderToBdom(template, { v: true });
|
||||||
|
|||||||
@@ -101,6 +101,72 @@ exports[`basics simple catchError 2`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`can catch errors Errors have the right cause 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return text(ctx['state'].value);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`can catch errors Errors in owl lifecycle are wrapped in dev mode: async hook 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return text(ctx['state'].value);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`can catch errors Errors in owl lifecycle are wrapped out of dev mode: async hook 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return text(ctx['state'].value);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`can catch errors Errors in owl lifecycle are wrapped outside dev mode: sync hook 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return text(ctx['state'].value);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`can catch errors Thrown values that are not errors are wrapped in dev mode 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return text(ctx['state'].value);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`can catch errors Thrown values that are not errors are wrapped outside dev mode 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return text(ctx['state'].value);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`can catch errors an error in onWillDestroy 1`] = `
|
exports[`can catch errors an error in onWillDestroy 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
@@ -1296,3 +1362,16 @@ exports[`errors and promises errors in rerender 1`] = `
|
|||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`errors and promises wrapped errors in async code are correctly caught 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<div>abc</div>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return block1();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|||||||
@@ -58,6 +58,20 @@ exports[`event handling handler receive the event as argument 2`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`event handling handler works when app is mounted in an iframe 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<span block-handler-0=\\"click\\">click me</span>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let hdlr1 = [ctx['inc'], ctx];
|
||||||
|
return block1([hdlr1]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`event handling input blur event is not called if component is destroyed 1`] = `
|
exports[`event handling input blur event is not called if component is destroyed 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -604,6 +604,64 @@ exports[`slots default slot work with text nodes 2`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`slots dynamic slot in multiple locations 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
let { capture, markRaw } = helpers;
|
||||||
|
const comp1 = app.createComponent(\`Child\`, true, false, false, true);
|
||||||
|
const comp2 = app.createComponent(\`Slotter\`, true, true, false, false);
|
||||||
|
|
||||||
|
function slot1(ctx, node, key = \\"\\") {
|
||||||
|
const b2 = text(\`hello \`);
|
||||||
|
const b3 = comp1({}, key + \`__1\`, node, this, null);
|
||||||
|
return multi([b2, b3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
const ctx1 = capture(ctx);
|
||||||
|
return comp2({location: ctx['state'].location,slots: markRaw({'coffee': {__render: slot1, __ctx: ctx1}})}, key + \`__2\`, node, this, null);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots dynamic slot in multiple locations 2`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
let { callSlot } = helpers;
|
||||||
|
|
||||||
|
let block2 = createBlock(\`<p><block-child-0/></p>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let b2,b4;
|
||||||
|
if (ctx['props'].location===1) {
|
||||||
|
const slot1 = ('coffee');
|
||||||
|
const b3 = toggler(slot1, callSlot(ctx, node, key + \`__1\`, slot1, true, {}));
|
||||||
|
b2 = block2([], [b3]);
|
||||||
|
}
|
||||||
|
if (ctx['props'].location===2) {
|
||||||
|
const slot2 = ('coffee');
|
||||||
|
b4 = toggler(slot2, callSlot(ctx, node, key + \`__2\`, slot2, true, {}));
|
||||||
|
}
|
||||||
|
return multi([b2, b4]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots dynamic slot in multiple locations 3`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<div>child</div>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return block1();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`slots dynamic t-slot call 1`] = `
|
exports[`slots dynamic t-slot call 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
@@ -645,7 +703,7 @@ exports[`slots dynamic t-slot call 2`] = `
|
|||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let hdlr1 = [ctx['toggle'], ctx];
|
let hdlr1 = [ctx['toggle'], ctx];
|
||||||
const slot1 = (ctx['current'].slot);
|
const slot1 = (ctx['current'].slot);
|
||||||
const b2 = toggler(slot1, callSlot(ctx, node, key, slot1, true, {}));
|
const b2 = toggler(slot1, callSlot(ctx, node, key + \`__1\`, slot1, true, {}));
|
||||||
return block1([hdlr1], [b2]);
|
return block1([hdlr1], [b2]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -695,7 +753,7 @@ exports[`slots dynamic t-slot call with default 2`] = `
|
|||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
let hdlr1 = [ctx['toggle'], ctx];
|
let hdlr1 = [ctx['toggle'], ctx];
|
||||||
const b3 = callSlot(ctx, node, key, (ctx['current'].slot), true, {}, defaultContent1);
|
const b3 = callSlot(ctx, node, key + \`__1\`, (ctx['current'].slot), true, {}, defaultContent1);
|
||||||
return block1([hdlr1], [b3]);
|
return block1([hdlr1], [b3]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -726,7 +784,7 @@ exports[`slots fun: two calls to the same slot 2`] = `
|
|||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
const b2 = callSlot(ctx, node, key, 'default', false, {});
|
const b2 = callSlot(ctx, node, key, 'default', false, {});
|
||||||
const b3 = callSlot(ctx, node, key, 'default', false, {});
|
const b3 = callSlot(ctx, node, key + \`__1\`, 'default', false, {});
|
||||||
return multi([b2, b3]);
|
return multi([b2, b3]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -1527,7 +1585,7 @@ exports[`slots simple dynamic slot with slot scope 2`] = `
|
|||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
const slot1 = ('slotName');
|
const slot1 = ('slotName');
|
||||||
const b2 = toggler(slot1, callSlot(ctx, node, key, slot1, true, {bool: ctx['state'].bool}));
|
const b2 = toggler(slot1, callSlot(ctx, node, key + \`__1\`, slot1, true, {bool: ctx['state'].bool}));
|
||||||
return block1([], [b2]);
|
return block1([], [b2]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -1854,7 +1912,7 @@ exports[`slots slot content has different key from other content -- dynamic slot
|
|||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
const b2 = comp1({parent: 'SlotDisplay'}, key + \`__1\`, node, this, null);
|
const b2 = comp1({parent: 'SlotDisplay'}, key + \`__1\`, node, this, null);
|
||||||
const slot1 = (ctx['slotName']);
|
const slot1 = (ctx['slotName']);
|
||||||
const b3 = toggler(slot1, callSlot(ctx, node, key, slot1, true, {}));
|
const b3 = toggler(slot1, callSlot(ctx, node, key + \`__2\`, slot1, true, {}));
|
||||||
return multi([b2, b3]);
|
return multi([b2, b3]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@@ -1995,6 +2053,118 @@ exports[`slots slot content is bound to caller 2`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`slots slot in multiple locations 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
let { markRaw } = helpers;
|
||||||
|
const comp1 = app.createComponent(\`Child\`, true, false, false, true);
|
||||||
|
const comp2 = app.createComponent(\`Slotter\`, true, true, false, false);
|
||||||
|
|
||||||
|
function slot1(ctx, node, key = \\"\\") {
|
||||||
|
const b2 = text(\` hello \`);
|
||||||
|
const b3 = comp1({}, key + \`__1\`, node, this, null);
|
||||||
|
return multi([b2, b3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return comp2({location: ctx['state'].location,slots: markRaw({'default': {__render: slot1, __ctx: ctx}})}, key + \`__2\`, node, this, null);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots slot in multiple locations 2`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
let { callSlot } = helpers;
|
||||||
|
|
||||||
|
let block2 = createBlock(\`<p><block-child-0/></p>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let b2,b4;
|
||||||
|
if (ctx['props'].location===1) {
|
||||||
|
const b3 = callSlot(ctx, node, key, 'default', false, {});
|
||||||
|
b2 = block2([], [b3]);
|
||||||
|
}
|
||||||
|
if (ctx['props'].location===2) {
|
||||||
|
b4 = callSlot(ctx, node, key + \`__1\`, 'default', false, {});
|
||||||
|
}
|
||||||
|
return multi([b2, b4]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots slot in multiple locations 3`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<div>child</div>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return block1();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots slot in t-foreach locations 1`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
let { markRaw } = helpers;
|
||||||
|
const comp1 = app.createComponent(\`Child\`, true, false, false, true);
|
||||||
|
const comp2 = app.createComponent(\`Slotter\`, true, true, false, false);
|
||||||
|
|
||||||
|
function slot1(ctx, node, key = \\"\\") {
|
||||||
|
const b2 = text(\` hello \`);
|
||||||
|
const b3 = comp1({}, key + \`__1\`, node, this, null);
|
||||||
|
return multi([b2, b3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return comp2({list: ctx['state'].list,slots: markRaw({'default': {__render: slot1, __ctx: ctx}})}, key + \`__2\`, node, this, null);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots slot in t-foreach locations 2`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
let { prepareList, callSlot, withKey } = helpers;
|
||||||
|
|
||||||
|
let block2 = createBlock(\`<p><block-text-0/><block-child-0/></p>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
ctx = Object.create(ctx);
|
||||||
|
const [k_block1, v_block1, l_block1, c_block1] = prepareList(ctx['props'].list);;
|
||||||
|
for (let i1 = 0; i1 < l_block1; i1++) {
|
||||||
|
ctx[\`elem\`] = v_block1[i1];
|
||||||
|
ctx[\`elem_index\`] = i1;
|
||||||
|
const key1 = ctx['elem_index'];
|
||||||
|
let txt1 = ctx['elem'];
|
||||||
|
const b3 = callSlot(ctx, node, key1, 'default', false, {});
|
||||||
|
c_block1[i1] = withKey(block2([txt1], [b3]), key1);
|
||||||
|
}
|
||||||
|
return list(c_block1);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`slots slot in t-foreach locations 3`] = `
|
||||||
|
"function anonymous(app, bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<div>child</div>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return block1();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`slots slot preserves properly parented relationship 1`] = `
|
exports[`slots slot preserves properly parented relationship 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ exports[`list of components crash on duplicate key in dev mode 1`] = `
|
|||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
let { prepareList, withKey } = helpers;
|
let { prepareList, OwlError, withKey } = helpers;
|
||||||
const comp1 = app.createComponent(\`Child\`, true, false, false, true);
|
const comp1 = app.createComponent(\`Child\`, true, false, false, true);
|
||||||
|
|
||||||
return function template(ctx, node, key = \\"\\") {
|
return function template(ctx, node, key = \\"\\") {
|
||||||
@@ -53,7 +53,7 @@ exports[`list of components crash on duplicate key in dev mode 1`] = `
|
|||||||
for (let i1 = 0; i1 < l_block1; i1++) {
|
for (let i1 = 0; i1 < l_block1; i1++) {
|
||||||
ctx[\`item\`] = v_block1[i1];
|
ctx[\`item\`] = v_block1[i1];
|
||||||
const key1 = 'child';
|
const key1 = 'child';
|
||||||
if (keys1.has(key1)) { throw new Error(\`Got duplicate key in t-foreach: \${key1}\`)}
|
if (keys1.has(key1)) { throw new OwlError(\`Got duplicate key in t-foreach: \${key1}\`)}
|
||||||
keys1.add(key1);
|
keys1.add(key1);
|
||||||
const props1 = {};
|
const props1 = {};
|
||||||
helpers.validateProps(\`Child\`, props1, ctx);
|
helpers.validateProps(\`Child\`, props1, ctx);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
snapshotEverything,
|
snapshotEverything,
|
||||||
useLogLifecycle,
|
useLogLifecycle,
|
||||||
} from "../helpers";
|
} from "../helpers";
|
||||||
|
import { OwlError } from "../../src/runtime/error_handling";
|
||||||
|
|
||||||
let fixture: HTMLElement;
|
let fixture: HTMLElement;
|
||||||
|
|
||||||
@@ -159,16 +160,17 @@ describe("errors and promises", () => {
|
|||||||
static template = xml`<div><t t-esc="this.will.crash"/></div>`;
|
static template = xml`<div><t t-esc="this.will.crash"/></div>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let error: Error;
|
let error: OwlError;
|
||||||
try {
|
try {
|
||||||
await mount(App, fixture);
|
await mount(App, fixture);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e as Error;
|
error = e as OwlError;
|
||||||
}
|
}
|
||||||
expect(error!).toBeDefined();
|
expect(error!).toBeDefined();
|
||||||
|
expect(error!.cause).toBeDefined();
|
||||||
const regexp =
|
const regexp =
|
||||||
/Cannot read properties of undefined \(reading 'crash'\)|Cannot read property 'crash' of undefined/g;
|
/Cannot read properties of undefined \(reading 'crash'\)|Cannot read property 'crash' of undefined/g;
|
||||||
expect(error!.message).toMatch(regexp);
|
expect(error!.cause.message).toMatch(regexp);
|
||||||
expect(mockConsoleError).toBeCalledTimes(0);
|
expect(mockConsoleError).toBeCalledTimes(0);
|
||||||
expect(mockConsoleError).toBeCalledTimes(0);
|
expect(mockConsoleError).toBeCalledTimes(0);
|
||||||
});
|
});
|
||||||
@@ -183,14 +185,15 @@ describe("errors and promises", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let error: Error;
|
let error: OwlError;
|
||||||
try {
|
try {
|
||||||
await mount(App, fixture);
|
await mount(App, fixture);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e as Error;
|
error = e as OwlError;
|
||||||
}
|
}
|
||||||
expect(error!).toBeDefined();
|
expect(error!).toBeDefined();
|
||||||
expect(error!.message).toBe("boom");
|
expect(error!.cause).toBeDefined();
|
||||||
|
expect(error!.cause.message).toBe("boom");
|
||||||
expect(fixture.innerHTML).toBe("");
|
expect(fixture.innerHTML).toBe("");
|
||||||
expect(mockConsoleError).toBeCalledTimes(0);
|
expect(mockConsoleError).toBeCalledTimes(0);
|
||||||
expect(mockConsoleWarn).toBeCalledTimes(1);
|
expect(mockConsoleWarn).toBeCalledTimes(1);
|
||||||
@@ -264,6 +267,30 @@ describe("errors and promises", () => {
|
|||||||
expect(error!.message).toBe("Tokenizer error: could not tokenize `{ 'invalid: 5 }`");
|
expect(error!.message).toBe("Tokenizer error: could not tokenize `{ 'invalid: 5 }`");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("wrapped errors in async code are correctly caught", async () => {
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<div>abc</div>`;
|
||||||
|
setup() {
|
||||||
|
onWillStart(async () => {
|
||||||
|
await Promise.resolve();
|
||||||
|
throw new Error("boom in onWillStart");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let error: any;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture, { test: true });
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
expect(error!).toBeDefined();
|
||||||
|
expect(error!.message).toBe(
|
||||||
|
`The following error occurred in onWillStart: "boom in onWillStart"`
|
||||||
|
);
|
||||||
|
await new Promise((r) => setTimeout(r, 0)); // wait for the rejection event to bubble
|
||||||
|
});
|
||||||
|
|
||||||
test("an error in willPatch call will reject the render promise", async () => {
|
test("an error in willPatch call will reject the render promise", async () => {
|
||||||
class Root extends Component {
|
class Root extends Component {
|
||||||
static template = xml`<div><t t-esc="val"/></div>`;
|
static template = xml`<div><t t-esc="val"/></div>`;
|
||||||
@@ -320,16 +347,17 @@ describe("errors and promises", () => {
|
|||||||
static components = { Child };
|
static components = { Child };
|
||||||
}
|
}
|
||||||
|
|
||||||
let error: Error;
|
let error: OwlError;
|
||||||
try {
|
try {
|
||||||
await mount(App, fixture);
|
await mount(App, fixture);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e as Error;
|
error = e as OwlError;
|
||||||
}
|
}
|
||||||
expect(error!).toBeDefined();
|
expect(error!).toBeDefined();
|
||||||
|
expect(error!.cause).toBeDefined();
|
||||||
const regexp =
|
const regexp =
|
||||||
/Cannot read properties of undefined \(reading 'crash'\)|Cannot read property 'crash' of undefined/g;
|
/Cannot read properties of undefined \(reading 'crash'\)|Cannot read property 'crash' of undefined/g;
|
||||||
expect(error!.message).toMatch(regexp);
|
expect(error!.cause.message).toMatch(regexp);
|
||||||
expect(mockConsoleError).toBeCalledTimes(0);
|
expect(mockConsoleError).toBeCalledTimes(0);
|
||||||
expect(mockConsoleWarn).toBeCalledTimes(1);
|
expect(mockConsoleWarn).toBeCalledTimes(1);
|
||||||
});
|
});
|
||||||
@@ -339,7 +367,7 @@ describe("errors and promises", () => {
|
|||||||
static template = xml`<div><t t-if="flag" t-esc="this.will.crash"/></div>`;
|
static template = xml`<div><t t-if="flag" t-esc="this.will.crash"/></div>`;
|
||||||
flag = false;
|
flag = false;
|
||||||
setup() {
|
setup() {
|
||||||
onError((e) => (error = e));
|
onError(({ cause }) => (error = cause));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,16 +394,17 @@ describe("errors and promises", () => {
|
|||||||
static components = { Child };
|
static components = { Child };
|
||||||
}
|
}
|
||||||
|
|
||||||
let error: Error;
|
let error: OwlError;
|
||||||
try {
|
try {
|
||||||
await mount(Parent, fixture);
|
await mount(Parent, fixture);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e as Error;
|
error = e as OwlError;
|
||||||
}
|
}
|
||||||
expect(error!).toBeDefined();
|
expect(error!).toBeDefined();
|
||||||
|
expect(error!.cause).toBeDefined();
|
||||||
const regexp =
|
const regexp =
|
||||||
/Cannot read properties of undefined \(reading 'y'\)|Cannot read property 'y' of undefined/g;
|
/Cannot read properties of undefined \(reading 'y'\)|Cannot read property 'y' of undefined/g;
|
||||||
expect(error!.message).toMatch(regexp);
|
expect(error!.cause.message).toMatch(regexp);
|
||||||
expect(mockConsoleError).toBeCalledTimes(0);
|
expect(mockConsoleError).toBeCalledTimes(0);
|
||||||
expect(mockConsoleWarn).toBeCalledTimes(1);
|
expect(mockConsoleWarn).toBeCalledTimes(1);
|
||||||
});
|
});
|
||||||
@@ -482,6 +511,146 @@ describe("can catch errors", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("Errors have the right cause", async () => {
|
||||||
|
const err = new Error("test error");
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<t t-esc="state.value"/>`;
|
||||||
|
state = useState({ value: 1 });
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
onMounted(() => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let e: OwlError;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture, { test: true });
|
||||||
|
} catch (error) {
|
||||||
|
e = error as OwlError;
|
||||||
|
}
|
||||||
|
expect(e!.message).toBe(`The following error occurred in onMounted: "test error"`);
|
||||||
|
expect(e!.cause).toBe(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Errors in owl lifecycle are wrapped in dev mode: async hook", async () => {
|
||||||
|
const err = new Error("test error");
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<t t-esc="state.value"/>`;
|
||||||
|
state = useState({ value: 1 });
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
onWillStart(async () => {
|
||||||
|
await nextMicroTick();
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let e: OwlError;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture, { test: true });
|
||||||
|
} catch (error) {
|
||||||
|
e = error as OwlError;
|
||||||
|
}
|
||||||
|
expect(e!.message).toBe(`The following error occurred in onWillStart: "test error"`);
|
||||||
|
expect(e!.cause).toBe(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Errors in owl lifecycle are wrapped outside dev mode: sync hook", async () => {
|
||||||
|
const err = new Error("test error");
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<t t-esc="state.value"/>`;
|
||||||
|
state = useState({ value: 1 });
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
onMounted(() => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let e: OwlError;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture);
|
||||||
|
} catch (error) {
|
||||||
|
e = error as OwlError;
|
||||||
|
}
|
||||||
|
expect(e!.message).toBe(
|
||||||
|
`An error occured in the owl lifecycle (see this Error's "cause" property)`
|
||||||
|
);
|
||||||
|
expect(e!.cause).toBe(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Errors in owl lifecycle are wrapped out of dev mode: async hook", async () => {
|
||||||
|
const err = new Error("test error");
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<t t-esc="state.value"/>`;
|
||||||
|
state = useState({ value: 1 });
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
onWillStart(async () => {
|
||||||
|
await nextMicroTick();
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let e: OwlError;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture);
|
||||||
|
} catch (error) {
|
||||||
|
e = error as OwlError;
|
||||||
|
}
|
||||||
|
expect(e!.message).toBe(
|
||||||
|
`An error occured in the owl lifecycle (see this Error's "cause" property)`
|
||||||
|
);
|
||||||
|
expect(e!.cause).toBe(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Thrown values that are not errors are wrapped in dev mode", async () => {
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<t t-esc="state.value"/>`;
|
||||||
|
state = useState({ value: 1 });
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
onMounted(() => {
|
||||||
|
throw "This is not an error";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let e: OwlError;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture, { test: true });
|
||||||
|
} catch (error) {
|
||||||
|
e = error as OwlError;
|
||||||
|
}
|
||||||
|
expect(e!.message).toBe(
|
||||||
|
`Something that is not an Error was thrown in onMounted (see this Error's "cause" property)`
|
||||||
|
);
|
||||||
|
expect(e!.cause).toBe("This is not an error");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Thrown values that are not errors are wrapped outside dev mode", async () => {
|
||||||
|
class Root extends Component {
|
||||||
|
static template = xml`<t t-esc="state.value"/>`;
|
||||||
|
state = useState({ value: 1 });
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
onMounted(() => {
|
||||||
|
throw "This is not an error";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let e: OwlError;
|
||||||
|
try {
|
||||||
|
await mount(Root, fixture);
|
||||||
|
} catch (error) {
|
||||||
|
e = error as OwlError;
|
||||||
|
}
|
||||||
|
expect(e!.message).toBe(
|
||||||
|
`An error occured in the owl lifecycle (see this Error's "cause" property)`
|
||||||
|
);
|
||||||
|
expect(e!.cause).toBe("This is not an error");
|
||||||
|
});
|
||||||
|
|
||||||
test("can catch an error in the initial call of a component render function (parent mounted)", async () => {
|
test("can catch an error in the initial call of a component render function (parent mounted)", async () => {
|
||||||
class ErrorComponent extends Component {
|
class ErrorComponent extends Component {
|
||||||
static template = xml`<div>hey<t t-esc="state.this.will.crash"/></div>`;
|
static template = xml`<div>hey<t t-esc="state.this.will.crash"/></div>`;
|
||||||
@@ -1180,8 +1349,8 @@ describe("can catch errors", () => {
|
|||||||
class Catch extends Component {
|
class Catch extends Component {
|
||||||
static template = xml`<t t-slot="default" />`;
|
static template = xml`<t t-slot="default" />`;
|
||||||
setup() {
|
setup() {
|
||||||
onError((error) => {
|
onError(({ cause }) => {
|
||||||
this.props.onError(error);
|
this.props.onError(cause);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -171,4 +171,22 @@ describe("event handling", () => {
|
|||||||
// input is removed when component is destroyed => nothing should happen
|
// input is removed when component is destroyed => nothing should happen
|
||||||
expect([]).toBeLogged();
|
expect([]).toBeLogged();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("handler works when app is mounted in an iframe", async () => {
|
||||||
|
let clickCount = 0;
|
||||||
|
|
||||||
|
class Parent extends Component {
|
||||||
|
static template = xml`<span t-on-click="inc">click me</span>`;
|
||||||
|
inc() {
|
||||||
|
clickCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const iframe = document.createElement("iframe");
|
||||||
|
fixture.appendChild(iframe);
|
||||||
|
const iframeDoc = iframe.contentDocument!;
|
||||||
|
await mount(Parent, iframeDoc.body);
|
||||||
|
expect(clickCount).toBe(0);
|
||||||
|
iframeDoc.querySelector("span")!.click();
|
||||||
|
expect(clickCount).toBe(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -653,7 +653,7 @@ describe("hooks", () => {
|
|||||||
try {
|
try {
|
||||||
await mount(MyComponent, fixture);
|
await mount(MyComponent, fixture);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
expect(e.message).toBe("Intentional error");
|
expect(e.cause.message).toBe("Intentional error");
|
||||||
}
|
}
|
||||||
// no console.error because the error has been caught in this test
|
// no console.error because the error has been caught in this test
|
||||||
expect(console.error).toHaveBeenCalledTimes(0);
|
expect(console.error).toHaveBeenCalledTimes(0);
|
||||||
|
|||||||
@@ -1819,4 +1819,102 @@ describe("slots", () => {
|
|||||||
await nextTick();
|
await nextTick();
|
||||||
expect(fixture.innerHTML).toBe("<button>inc</button>[B][C][A][sub1] [sub2334]");
|
expect(fixture.innerHTML).toBe("<button>inc</button>[B][C][A][sub1] [sub2334]");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("slot in multiple locations", async () => {
|
||||||
|
class Child extends Component {
|
||||||
|
static template = xml`<div>child</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Slotter extends Component {
|
||||||
|
static components = { Child };
|
||||||
|
static template = xml`
|
||||||
|
<t t-if="props.location === 1">
|
||||||
|
<p><t t-slot="default"/></p>
|
||||||
|
</t>
|
||||||
|
<t t-if="props.location === 2">
|
||||||
|
<t t-slot="default"/>
|
||||||
|
</t>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Parent extends Component {
|
||||||
|
static components = { Child, Slotter };
|
||||||
|
static template = xml`
|
||||||
|
<Slotter location="state.location">
|
||||||
|
hello <Child/>
|
||||||
|
</Slotter>`;
|
||||||
|
state = useState({ location: 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = await mount(Parent, fixture);
|
||||||
|
expect(fixture.innerHTML).toBe("<p> hello <div>child</div></p>");
|
||||||
|
parent.state.location = 2;
|
||||||
|
await nextTick();
|
||||||
|
expect(fixture.innerHTML).toBe(" hello <div>child</div>");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("dynamic slot in multiple locations", async () => {
|
||||||
|
class Child extends Component {
|
||||||
|
static template = xml`<div>child</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Slotter extends Component {
|
||||||
|
static components = { Child };
|
||||||
|
static template = xml`
|
||||||
|
<t t-if="props.location === 1">
|
||||||
|
<p><t t-slot="{{'coffee'}}"/></p>
|
||||||
|
</t>
|
||||||
|
<t t-if="props.location === 2">
|
||||||
|
<t t-slot="{{'coffee'}}"/>
|
||||||
|
</t>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Parent extends Component {
|
||||||
|
static components = { Child, Slotter };
|
||||||
|
static template = xml`
|
||||||
|
<Slotter location="state.location">
|
||||||
|
<t t-set-slot="coffee">hello <Child/></t>
|
||||||
|
</Slotter>`;
|
||||||
|
state = useState({ location: 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = await mount(Parent, fixture);
|
||||||
|
expect(fixture.innerHTML).toBe("<p>hello <div>child</div></p>");
|
||||||
|
parent.state.location = 2;
|
||||||
|
await nextTick();
|
||||||
|
expect(fixture.innerHTML).toBe("hello <div>child</div>");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("slot in t-foreach locations", async () => {
|
||||||
|
class Child extends Component {
|
||||||
|
static template = xml`<div>child</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Slotter extends Component {
|
||||||
|
static components = { Child };
|
||||||
|
static template = xml`
|
||||||
|
<t t-foreach="props.list" t-as="elem" t-key="elem_index">
|
||||||
|
<p><t t-esc="elem"/><t t-slot="default"/></p>
|
||||||
|
</t>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Parent extends Component {
|
||||||
|
static components = { Child, Slotter };
|
||||||
|
static template = xml`
|
||||||
|
<Slotter list="state.list">
|
||||||
|
hello <Child/>
|
||||||
|
</Slotter>`;
|
||||||
|
state = useState({ list: [1] });
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = await mount(Parent, fixture);
|
||||||
|
expect(fixture.innerHTML).toBe("<p>1 hello <div>child</div></p>");
|
||||||
|
parent.state.list.push(2);
|
||||||
|
await nextTick();
|
||||||
|
expect(fixture.innerHTML).toBe(
|
||||||
|
"<p>1 hello <div>child</div></p><p>2 hello <div>child</div></p>"
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { OwlError } from "../../src/runtime/error_handling";
|
||||||
import { Component, mount, onMounted, useState, xml } from "../../src";
|
import { Component, mount, onMounted, useState, xml } from "../../src";
|
||||||
import { makeTestFixture, nextTick, snapshotEverything } from "../helpers";
|
import { makeTestFixture, nextTick, snapshotEverything } from "../helpers";
|
||||||
|
|
||||||
@@ -346,16 +347,17 @@ describe("style and class handling", () => {
|
|||||||
static template = xml`<Child class="'a'"/>`;
|
static template = xml`<Child class="'a'"/>`;
|
||||||
static components = { Child };
|
static components = { Child };
|
||||||
}
|
}
|
||||||
let error: Error;
|
let error: OwlError;
|
||||||
try {
|
try {
|
||||||
await mount(ParentWidget, fixture);
|
await mount(ParentWidget, fixture);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e as Error;
|
error = e as OwlError;
|
||||||
}
|
}
|
||||||
expect(error!).toBeDefined();
|
expect(error!).toBeDefined();
|
||||||
|
expect(error!.cause).toBeDefined();
|
||||||
const regexp =
|
const regexp =
|
||||||
/Cannot read properties of undefined \(reading 'crash'\)|Cannot read property 'crash' of undefined/g;
|
/Cannot read properties of undefined \(reading 'crash'\)|Cannot read property 'crash' of undefined/g;
|
||||||
expect(error!.message).toMatch(regexp);
|
expect(error!.cause.message).toMatch(regexp);
|
||||||
expect(fixture.innerHTML).toBe("");
|
expect(fixture.innerHTML).toBe("");
|
||||||
expect(mockConsoleWarn).toBeCalledTimes(1);
|
expect(mockConsoleWarn).toBeCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-1
@@ -19,6 +19,7 @@ import { helpers } from "../src/runtime/template_helpers";
|
|||||||
import { TemplateSet, globalTemplates } from "../src/runtime/template_set";
|
import { TemplateSet, globalTemplates } from "../src/runtime/template_set";
|
||||||
import { BDom } from "../src/runtime/blockdom";
|
import { BDom } from "../src/runtime/blockdom";
|
||||||
import { compile } from "../src/compiler";
|
import { compile } from "../src/compiler";
|
||||||
|
import { OwlError } from "../src/runtime/error_handling";
|
||||||
|
|
||||||
const mount = blockDom.mount;
|
const mount = blockDom.mount;
|
||||||
|
|
||||||
@@ -221,7 +222,7 @@ export async function editInput(input: HTMLInputElement | HTMLTextAreaElement, v
|
|||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
if (steps.length) {
|
if (steps.length) {
|
||||||
steps.splice(0);
|
steps.splice(0);
|
||||||
throw new Error("Remaining steps! Should be checked by a .toBeLogged() assertion!");
|
throw new OwlError("Remaining steps! Should be checked by a .toBeLogged() assertion!");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { OwlError } from "../../src/runtime/error_handling";
|
||||||
import {
|
import {
|
||||||
App,
|
App,
|
||||||
Component,
|
Component,
|
||||||
@@ -499,7 +500,7 @@ describe("Portal", () => {
|
|||||||
</div>`;
|
</div>`;
|
||||||
state = { error: false };
|
state = { error: false };
|
||||||
setup() {
|
setup() {
|
||||||
onError((e) => (error = e));
|
onError(({ cause }) => (error = cause));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addOutsideDiv(fixture);
|
addOutsideDiv(fixture);
|
||||||
@@ -958,14 +959,15 @@ describe("Portal: Props validation", () => {
|
|||||||
</t>
|
</t>
|
||||||
</div>`;
|
</div>`;
|
||||||
}
|
}
|
||||||
let error: Error;
|
let error: OwlError;
|
||||||
try {
|
try {
|
||||||
await mount(Parent, fixture, { dev: true });
|
await mount(Parent, fixture, { dev: true });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e as Error;
|
error = e as OwlError;
|
||||||
}
|
}
|
||||||
expect(error!).toBeDefined();
|
expect(error!).toBeDefined();
|
||||||
expect(error!.message).toBe(`' ' is not a valid selector`);
|
expect(error!.cause).toBeDefined();
|
||||||
|
expect(error!.cause.message).toBe(`' ' is not a valid selector`);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("target must be a valid selector 2", async () => {
|
test("target must be a valid selector 2", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user