[REF] qweb: put directives in static QWeb prop

so they can be altered by outside code.  This is mostly useful for tests.
This commit is contained in:
Géry Debongnie
2019-07-30 14:33:59 +02:00
parent f18c73e59f
commit f6369f9750
+18 -16
View File
@@ -64,15 +64,6 @@ const DISABLED_TAGS = ["input", "textarea", "button", "select", "option", "optgr
const lineBreakRE = /[\r\n]/; const lineBreakRE = /[\r\n]/;
const whitespaceRE = /\s+/g; const whitespaceRE = /\s+/g;
const DIRECTIVE_NAMES = {
name: 1,
att: 1,
attf: 1,
key: 1
};
const DIRECTIVES: Directive[] = [];
const NODE_HOOKS_PARAMS = { const NODE_HOOKS_PARAMS = {
create: "(_, n)", create: "(_, n)",
insert: "vn", insert: "vn",
@@ -150,6 +141,14 @@ export class QWeb extends EventBus {
static utils = UTILS; static utils = UTILS;
static components = Object.create(null); static components = Object.create(null);
static DIRECTIVE_NAMES: { [key: string]: 1 } = {
name: 1,
att: 1,
attf: 1,
key: 1
};
static DIRECTIVES: Directive[] = [];
h = h; h = h;
// dev mode enables better error messages or more costly validations // dev mode enables better error messages or more costly validations
static dev: boolean = false; static dev: boolean = false;
@@ -172,11 +171,14 @@ export class QWeb extends EventBus {
} }
static addDirective(directive: Directive) { static addDirective(directive: Directive) {
DIRECTIVES.push(directive); if (directive.name in QWeb.DIRECTIVE_NAMES) {
DIRECTIVE_NAMES[directive.name] = 1; throw new Error(`Directive "${directive.name} already registered`);
DIRECTIVES.sort((d1, d2) => d1.priority - d2.priority); }
QWeb.DIRECTIVES.push(directive);
QWeb.DIRECTIVE_NAMES[directive.name] = 1;
QWeb.DIRECTIVES.sort((d1, d2) => d1.priority - d2.priority);
if (directive.extraNames) { if (directive.extraNames) {
directive.extraNames.forEach(n => (DIRECTIVE_NAMES[n] = 1)); directive.extraNames.forEach(n => (QWeb.DIRECTIVE_NAMES[n] = 1));
} }
} }
@@ -413,16 +415,16 @@ export class QWeb extends EventBus {
let attrName = attributes[i].name; let attrName = attributes[i].name;
if (attrName.startsWith("t-")) { if (attrName.startsWith("t-")) {
let dName = attrName.slice(2).split(/-|\./)[0]; let dName = attrName.slice(2).split(/-|\./)[0];
if (!(dName in DIRECTIVE_NAMES)) { if (!(dName in QWeb.DIRECTIVE_NAMES)) {
throw new Error(`Unknown QWeb directive: '${attrName}'`); throw new Error(`Unknown QWeb directive: '${attrName}'`);
} }
} }
} }
const DIR_N = DIRECTIVES.length; const DIR_N = QWeb.DIRECTIVES.length;
const ATTR_N = attributes.length; const ATTR_N = attributes.length;
for (let i = 0; i < DIR_N; i++) { for (let i = 0; i < DIR_N; i++) {
let directive = DIRECTIVES[i]; let directive = QWeb.DIRECTIVES[i];
let fullName; let fullName;
let value; let value;
for (let j = 0; j < ATTR_N; j++) { for (let j = 0; j < ATTR_N; j++) {