Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fe176f13a5 | |||
| 871dad6a13 | |||
| b620502a0f | |||
| 89cb00cc83 | |||
| 56041bc133 | |||
| e788e361c7 | |||
| 9d378b0e7b | |||
| fd3c194525 | |||
| ac9ccb81ca | |||
| 2b5cea944b | |||
| cf8039f643 | |||
| aec2373e6d | |||
| a9be149e1e | |||
| b365ea5c9c | |||
| b31fa81083 | |||
| 9b656fd9e4 | |||
| 17f4823b13 | |||
| d3bc101177 | |||
| 4287beae19 | |||
| a073c68685 | |||
| 7078d64049 | |||
| ccd31f12d9 | |||
| aff4019cde | |||
| 5ecb4809ff | |||
| c2daecc07b | |||
| fcda17c8e9 | |||
| aeed79c7e5 | |||
| 1c5b6f2573 | |||
| cd9b72158b | |||
| 7fc552e2f8 | |||
| e6768501cd | |||
| 6b2486473f | |||
| 7e687234bf | |||
| 968e96ad08 | |||
| 26c7856d5d | |||
| b8d09e523d | |||
| 04c2808701 | |||
| 15c2604df1 | |||
| 3e11fe6b12 | |||
| 20c6cacb4e | |||
| eb2b32ab60 | |||
| 2a223288d4 | |||
| 1272278225 | |||
| f502dd732e | |||
| 9c2d957525 | |||
| f8bb86820e | |||
| 0cde4b8737 | |||
| 66a801393f | |||
| e7f405cc97 | |||
| 55c48b2b12 | |||
| 2eb151c92d | |||
| 7952f31e63 | |||
| 11e4e67599 | |||
| 7b7a6de373 | |||
| b63d1e28b2 | |||
| c0667a11c6 | |||
| fddb1ec924 | |||
| e6c3b62ef0 | |||
| 97b69f164f | |||
| 33dfeb1b41 | |||
| dd292472b9 | |||
| 9b18b57fdf | |||
| 68f491cd32 | |||
| 7b3e39ba27 | |||
| 61fc3f4fdc | |||
| 7b454dae66 | |||
| 70101e4c66 | |||
| 5ef405293a | |||
| 9dcbbe54eb | |||
| e94428a186 | |||
| 941190dfa8 | |||
| a53e42518f | |||
| b7c37ca69a |
@@ -14,7 +14,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x, 14.x, 16.x]
|
||||
node-version: [20.x, 22.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
@@ -47,3 +47,4 @@ Utility/helpers:
|
||||
- [`status`](reference/component.md#status-helper): utility function to get the status of a component (new, mounted or destroyed)
|
||||
- [`validate`](reference/utils.md#validate): validates if an object satisfies a specified schema
|
||||
- [`whenReady`](reference/utils.md#whenready): utility function to execute code when DOM is ready
|
||||
- [`batched`](reference/utils.md#batched): utility function to batch function calls
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
- [API](#api)
|
||||
- [Configuration](#configuration)
|
||||
- [`mount` helper](#mount-helper)
|
||||
- [Roots](#roots)
|
||||
- [Loading templates](#loading-templates)
|
||||
|
||||
## Overview
|
||||
@@ -61,8 +62,13 @@ The `config` object is an object with some of the following keys:
|
||||
templates (see [translations](translations.md))
|
||||
- **`templates (string | xml document)`**: all the templates that will be used by
|
||||
the components created by the application.
|
||||
- **`getTemplate ((s: string) => Element | Function | string | void)`**: a function that will be called by owl when it
|
||||
needs a template. If undefined is returned, owl looks into the app templates.
|
||||
- **`warnIfNoStaticProps (boolean, default=false)`**: if true, Owl will log a warning
|
||||
whenever it encounters a component that does not provide a [static props description](props.md#props-validation).
|
||||
- **`customDirectives (object)`**: if given, the corresponding function on the object will be called
|
||||
on the template custom directives: `t-custom-*` (see [Custom Directives](templates.md#custom-directives)).
|
||||
- **`globalValues (object)`**: Global object of elements available at compilations.
|
||||
|
||||
## `mount` helper
|
||||
|
||||
@@ -90,6 +96,33 @@ Most of the time, the `mount` helper is more convenient, but whenever one needs
|
||||
a reference to the actual Owl App, then using the `App` class directly is
|
||||
possible.
|
||||
|
||||
## Roots
|
||||
|
||||
An application can have multiple roots. It is sometimes useful to instantiate
|
||||
sub components in places that are not managed by Owl, such as an html editor
|
||||
with dynamic content (the Knowledge application in Odoo).
|
||||
|
||||
To create a root, one can use the `createRoot` method, which takes two arguments:
|
||||
|
||||
- **`Component`**: a component class (Root component of the app)
|
||||
- **`config (optional)`**: a config object that may contain a `props` object or a
|
||||
`env` object.
|
||||
|
||||
The `createRoot` method returns an object with a `mount` method (same API as
|
||||
the `App.mount` method), and a `destroy` method.
|
||||
|
||||
```js
|
||||
const root = app.createRoot(MyComponent, { props: { someProps: true } });
|
||||
await root.mount(targetElement);
|
||||
|
||||
// later
|
||||
root.destroy();
|
||||
```
|
||||
|
||||
Note that, like with owl `App`, it is the responsibility of the code that created
|
||||
the root to properly destroy it (before it has been removed from the DOM!). Owl
|
||||
has no way of doing it itself.
|
||||
|
||||
## Loading templates
|
||||
|
||||
Most applications will need to load templates whenever they start. Here is
|
||||
|
||||
@@ -107,7 +107,7 @@ is necessary, since Owl needs to react to a change in state.
|
||||
### `useRef`
|
||||
|
||||
The `useRef` hook is useful when we need a way to interact with some inside part
|
||||
of a component, rendered by Owl. It only work on a html element tagged by the
|
||||
of a component, rendered by Owl. It only work on a HTML element tagged by the
|
||||
`t-ref` directive:
|
||||
|
||||
```xml
|
||||
@@ -227,12 +227,12 @@ function useSomething() {
|
||||
|
||||
This hook will run a callback when a component is mounted and patched, and
|
||||
will run a cleanup function before patching and before unmounting the
|
||||
the component (only if some dependencies have changed).
|
||||
component (only if some dependencies have changed).
|
||||
|
||||
It has almost the same API as the React `useEffect` hook, except that the dependencies
|
||||
are defined by a function instead of just the dependencies.
|
||||
|
||||
The `useEffect` hook takes two function: the effect function and the dependency
|
||||
The `useEffect` hook takes two functions: the effect function and the dependency
|
||||
function. The effect function perform some task and return (optionally) a cleanup
|
||||
function. The dependency function returns a list of dependencies, these dependencies
|
||||
are passed as parameters in the effect function . If any of these
|
||||
|
||||
@@ -140,6 +140,28 @@ class SomeComponent extends Component {
|
||||
The `.bind` suffix also implies `.alike`, so these props will not cause additional
|
||||
renderings.
|
||||
|
||||
## Translatable props
|
||||
|
||||
When you need to pass a user-facing string to a subcomponent, you likely want it
|
||||
to be translated. Unfortunately, because props are arbitrary expressions, it wouldn't
|
||||
be practical for Owl to find out which parts of the expression are strings and translate
|
||||
them, and it also makes it difficult for tooling to extract these strings to generate
|
||||
terms to translate. While you can work around this issue by doing the translation in
|
||||
JavaScript, or by using `t-set` with a body (the body of `t-set` is translated),
|
||||
and passing the variable as a prop, this is a sufficiently common use case that Owl
|
||||
provides a suffix for this purpose: `.translate`.
|
||||
|
||||
```xml
|
||||
<t t-name="ParentComponent">
|
||||
<Child someProp.translate="some message"/>
|
||||
</t>
|
||||
```
|
||||
|
||||
Note that the content of this attribute is _NOT_ treated as a JavaScript expression:
|
||||
it is treated as a string, as if it was an attribute on an HTML element, and translated
|
||||
before being passed to the component. If you need to interpolate some data into the
|
||||
string, you will still have to do this in JavaScript.
|
||||
|
||||
## Dynamic Props
|
||||
|
||||
The `t-props` directive can be used to specify totally dynamic props:
|
||||
@@ -238,7 +260,7 @@ class ComponentB extends owl.Component {
|
||||
count: {type: Number},
|
||||
messages: {
|
||||
type: Array,
|
||||
element: {type: Object, shape: {id: Boolean, text: String }
|
||||
element: {type: Object, shape: {id: Boolean, text: String }}
|
||||
},
|
||||
date: Date,
|
||||
combinedVal: [Number, Boolean],
|
||||
@@ -276,7 +298,8 @@ class ComponentB extends owl.Component {
|
||||
id: Number,
|
||||
name: {type: String, optional: true},
|
||||
url: String
|
||||
]}, // object, with keys id (number), name (string, optional) and url (string)
|
||||
}
|
||||
}, // object, with keys id (number), name (string, optional) and url (string)
|
||||
someObj3: {
|
||||
type: Object,
|
||||
values: { type: Array, element: String },
|
||||
|
||||
@@ -152,7 +152,7 @@ This may seem counter-intuitive, but it makes perfect sense in the context of co
|
||||
```js
|
||||
class DoubleCounter extends Component {
|
||||
static template = xml`
|
||||
<t t-esc="state.selected + ': ' + state[state.selected].value"/>
|
||||
<t t-esc="'selected: ' + state.selected + ', value: ' + state[state.selected]"/>
|
||||
<button t-on-click="() => this.state.count1++">increment count 1</button>
|
||||
<button t-on-click="() => this.state.count2++">increment count 2</button>
|
||||
<button t-on-click="changeCounter">Switch counter</button>
|
||||
@@ -193,7 +193,7 @@ to be able to opt out of creating them in the first place. This is the purpose o
|
||||
### `markRaw`
|
||||
|
||||
Marks an object so that it is ignored by the reactivity system, meaning that if this object is ever
|
||||
part of a of a reactive object, it will be returned as is, and no keys in that object will be
|
||||
part of a reactive object, it will be returned as is, and no keys in that object will be
|
||||
observed.
|
||||
|
||||
```js
|
||||
|
||||
@@ -133,7 +133,7 @@ Slots can define a default content, in case the parent did not define them:
|
||||
## Dynamic Slots
|
||||
|
||||
The `t-slot` directive is actually able to use any expressions, using string
|
||||
interplolation:
|
||||
interpolation:
|
||||
|
||||
```xml
|
||||
<t t-slot="{{current}}" />
|
||||
@@ -201,16 +201,17 @@ use this `Notebook` component:
|
||||
|
||||
```xml
|
||||
<Notebook>
|
||||
<t t-set-slot="page1" title="'Page 1'">
|
||||
<t t-set-slot="page1" title.translate="Page 1">
|
||||
<div>this is in the page 1</div>
|
||||
</t>
|
||||
<t t-set-slot="page2" title="'Page 2'" hidden="somevalue">
|
||||
<t t-set-slot="page2" title.translate="Page 2" hidden="somevalue">
|
||||
<div>this is in the page 2</div>
|
||||
</t>
|
||||
</Notebook>
|
||||
```
|
||||
|
||||
Slot params works like normal props, so one can use the `.bind` suffix to
|
||||
Slot params works like normal props, so one can use suffixes like `.translate`
|
||||
when a prop is a user facing string and should be translated, or `.bind` to
|
||||
bind a function if needed.
|
||||
|
||||
## Slot scopes
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
- [Sub Templates](#sub-templates)
|
||||
- [Dynamic Sub Templates](#dynamic-sub-templates)
|
||||
- [Debugging](#debugging)
|
||||
- [Custom Directives](#custom-directives)
|
||||
- [Fragments](#fragments)
|
||||
- [Inline templates](#inline-templates)
|
||||
- [Rendering svg](#rendering-svg)
|
||||
@@ -55,17 +56,19 @@ extensions.
|
||||
|
||||
For reference, here is a list of all standard QWeb directives:
|
||||
|
||||
| Name | Description |
|
||||
| ------------------------------ | --------------------------------------------------------------- |
|
||||
| `t-esc` | [Outputting safely a value](#outputting-data) |
|
||||
| `t-out` | [Outputting value, possibly without escaping](#outputting-data) |
|
||||
| `t-set`, `t-value` | [Setting variables](#setting-variables) |
|
||||
| `t-if`, `t-elif`, `t-else`, | [conditionally rendering](#conditionals) |
|
||||
| `t-foreach`, `t-as` | [Loops](#loops) |
|
||||
| `t-att`, `t-attf-*`, `t-att-*` | [Dynamic attributes](#dynamic-attributes) |
|
||||
| `t-call` | [Rendering sub templates](#sub-templates) |
|
||||
| `t-debug`, `t-log` | [Debugging](#debugging) |
|
||||
| `t-translation` | [Disabling the translation of a node](translations.md) |
|
||||
| Name | Description |
|
||||
| ------------------------------ | ----------------------------------------------------------------------- |
|
||||
| `t-esc` | [Outputting safely a value](#outputting-data) |
|
||||
| `t-out` | [Outputting value, possibly without escaping](#outputting-data) |
|
||||
| `t-set`, `t-value` | [Setting variables](#setting-variables) |
|
||||
| `t-if`, `t-elif`, `t-else`, | [conditionally rendering](#conditionals) |
|
||||
| `t-foreach`, `t-as` | [Loops](#loops) |
|
||||
| `t-att`, `t-attf-*`, `t-att-*` | [Dynamic attributes](#dynamic-attributes) |
|
||||
| `t-call` | [Rendering sub templates](#sub-templates) |
|
||||
| `t-debug`, `t-log` | [Debugging](#debugging) |
|
||||
| `t-translation` | [Disabling the translation of a node](translations.md) |
|
||||
| `t-translation-context` | [Context of translations within a node](translations.md) |
|
||||
| `t-translation-context-*` | [Context of translation for a specific node attribute](translations.md) |
|
||||
|
||||
The component system in Owl requires additional directives, to express various
|
||||
needs. Here is a list of all Owl specific directives:
|
||||
@@ -80,6 +83,7 @@ needs. Here is a list of all Owl specific directives:
|
||||
| `t-slot`, `t-set-slot`, `t-slot-scope` | [Rendering a slot](slots.md) |
|
||||
| `t-model` | [Form input bindings](input_bindings.md) |
|
||||
| `t-tag` | [Rendering nodes with dynamic tag name](#dynamic-tag-names) |
|
||||
| `t-custom-*` | [Rendering nodes with custom directives](#custom-directives) |
|
||||
|
||||
## QWeb Template Reference
|
||||
|
||||
@@ -189,6 +193,15 @@ The first `t-out` will act as a `t-esc` directive, which means that the content
|
||||
of `value1` will be escaped. However, since `value2` has been tagged as a markup,
|
||||
this will be injected as html.
|
||||
|
||||
`markup` can also be used as a tag function, allowing the interpolated values to
|
||||
be safely escaped:
|
||||
|
||||
```js
|
||||
const maliciousInput = "<script>alert('💥💥')</script>";
|
||||
// <b><script>alert('💥💥')</script></b>
|
||||
const value = markup`<b>${maliciousInput}</b>`;
|
||||
```
|
||||
|
||||
### Setting Variables
|
||||
|
||||
QWeb allows creating variables from within the template, to memoize a computation (to use it multiple times), give a piece of data a clearer name, ...
|
||||
@@ -588,6 +601,35 @@ will stop execution if the browser dev tools are open.
|
||||
|
||||
will print 42 to the console.
|
||||
|
||||
### Custom Directives
|
||||
|
||||
Owl 2 supports the declaration of custom directives. To use them, an Object of functions needs to be configured on the owl APP:
|
||||
|
||||
```js
|
||||
new App(..., {
|
||||
customDirectives: {
|
||||
test_directive: function (el, value) {
|
||||
el.setAttribute("t-on-click", value);
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
The functions will be called when a custom directive with the name of the
|
||||
function is found. The original element will be replaced with the one
|
||||
modified by the function.
|
||||
This :
|
||||
|
||||
```xml
|
||||
<div t-custom-test_directive="click" />
|
||||
```
|
||||
|
||||
will be replaced by :
|
||||
|
||||
```xml
|
||||
<div t-on-click="value"/>
|
||||
```
|
||||
|
||||
## Fragments
|
||||
|
||||
Owl 2 supports templates with an arbitrary number of root elements, or even just
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
# 🦉 Translations 🦉
|
||||
|
||||
If properly setup, Owl can translate all rendered templates. To do
|
||||
so, it needs a translate function, which takes a string and returns a string.
|
||||
so, it needs a translate function, which takes
|
||||
|
||||
- a string (the term to translate)
|
||||
- a string (the translation context of the term)
|
||||
and returns a string.
|
||||
|
||||
For example:
|
||||
|
||||
```js
|
||||
const translations = {
|
||||
hello: "bonjour",
|
||||
yes: "oui",
|
||||
no: "non",
|
||||
fr: {
|
||||
hello: "bonjour",
|
||||
yes: "oui",
|
||||
no: "non",
|
||||
},
|
||||
pt: {
|
||||
hello: "bom dia",
|
||||
yes: "sim",
|
||||
no: "não",
|
||||
},
|
||||
};
|
||||
const translateFn = (str) => translations[str] || str;
|
||||
const translateFn = (str, ctx) => translations[ctx]?.[str] || str;
|
||||
|
||||
const app = new App(Root, { templates, tranaslateFn });
|
||||
// ...
|
||||
@@ -27,6 +38,11 @@ Once setup, all rendered templates will be translated using `translateFn`:
|
||||
`placeholder`, `label` and `alt`,
|
||||
- translating text nodes can be disabled with the special attribute `t-translation`,
|
||||
if its value is `off`.
|
||||
- the translate function receives as second parameter a context that can be used
|
||||
to contextualized the translation. That context can be set globally on a node
|
||||
and its children by using `t-translation-context`. If a specific node
|
||||
attribute `x` needs another context, that context can be specified with a
|
||||
special directive `t-translation-context-x`.
|
||||
|
||||
So, with the above `translateFn`, the following templates:
|
||||
|
||||
@@ -46,6 +62,22 @@ will be rendered as:
|
||||
<input placeholder="bonjour" other="yes"/>
|
||||
```
|
||||
|
||||
and the following template:
|
||||
|
||||
```xml
|
||||
<div t-translation-context="fr" title="hello">hello</div>
|
||||
<div>Are you sure?</div>
|
||||
<input t-translation-context-placeholder="pt" placeholder="hello" other="yes"/>
|
||||
```
|
||||
|
||||
will be rendered as:
|
||||
|
||||
```xml
|
||||
<div title="bonjour">bonjour</div>
|
||||
<div>Are you sure?</div>
|
||||
<input placeholder="bom dia" other="yes"/>
|
||||
```
|
||||
|
||||
Note that the translation is done during the compilation of the template, not
|
||||
when it is rendered.
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ functions are all available in the `owl.utils` namespace.
|
||||
- [`loadFile`](#loadfile): loading a file (useful for templates)
|
||||
- [`EventBus`](#eventbus): a simple EventBus
|
||||
- [`validate`](#validate): a validation function
|
||||
- [`batched`](#batched): batch function calls
|
||||
|
||||
## `whenReady`
|
||||
|
||||
@@ -78,3 +79,22 @@ validate(
|
||||
// - 'id' is missing (should be a number),
|
||||
// - 'url' is missing (should be a boolean or list of numbers),
|
||||
```
|
||||
|
||||
## `batched`
|
||||
|
||||
The `batched` function creates a batched version of a callback so that multiple calls to it within the same microtick will only result in a single invocation of the original callback.
|
||||
|
||||
```js
|
||||
function hello() {
|
||||
console.log("hello");
|
||||
}
|
||||
|
||||
const batchedHello = batched(hello);
|
||||
batchedHello();
|
||||
// Nothing is logged
|
||||
batchedHello();
|
||||
// Still not logged
|
||||
|
||||
await Promise.resolve(); // Await the next microtick
|
||||
// "hello" is logged only once
|
||||
```
|
||||
|
||||
@@ -35,13 +35,13 @@ The components tab is separated into two sub windows: the components tree in the
|
||||
the component details in the right. The components tree will display all the different
|
||||
components that are present in the tab in the form of a tree. The root of this tree is
|
||||
actually the app which is not a component but can still be inspected by the devtools like
|
||||
one. There can also be multiple apps loaded in the page like in the following:
|
||||
one. There can also be multiple apps loaded in the page like in website:
|
||||
|
||||
<img src="screenshots/multi_apps.png"/>
|
||||
|
||||
There is a convenient search bar at the top of the components tree which will help finding
|
||||
the components tou want in the tree and also, an element picker can be used to directly select
|
||||
the component you want to focus on in the page which is especially useful when trying to find
|
||||
the component you want to focus on in the page which is especially useful when 1trying to find
|
||||
what you want. Just click on the elements picker icon and click on the element you want to focus
|
||||
on in the page and it will be selected in the devtools accordingly. Hovering any element in the
|
||||
page in this mode will highlight it and the same happens anytime in the components tree.
|
||||
@@ -64,31 +64,40 @@ as its env, props, observed states and all the other variables that are present
|
||||
While the props and the env are already present on the actual instance of the component and are
|
||||
pretty explicit by themselves, the observed state value is a bit more complicated to grasp.
|
||||
|
||||
The observed state is actually information about which variables are observed by the component
|
||||
which will trigger a rerender of the component when it is modified. The keys represent which part of the
|
||||
variable is actually observed and the target is the actual variable. For simplicity, the properties
|
||||
that are not observed by the component are greyed out while the others are in bold. This means that
|
||||
editing bold ones will trigger a rerender while the greyed out ones will not.
|
||||
The observed state is actually information about which variables are being observed by the component:
|
||||
when any property of a reactive object is being read by the component, the component will subscribe
|
||||
to this property which means it will listen to any change that can occur on the property and render
|
||||
when such a change occurs. This can be visualized easily within the devtools inside of the observed
|
||||
state section: observed properties of the reactive object(s) are displayed in bold while the others
|
||||
are greyed out. Do keep in mind that a greyed out property in the observed state of one component
|
||||
may be observed by another and the other way around is also possible. Here is an example for some
|
||||
user Field component:
|
||||
|
||||
<img src="screenshots/states.png"/>
|
||||
|
||||
In the given example, we have two keys/target pairs for two different variables. The first one indicates
|
||||
that adding or removing an element to the array will trigger a rerender since the length will have changed.
|
||||
Replacing the element at index 0, 1 or 2 will also have the same effect as implied by the keys. It doesn't
|
||||
mean that editing the properties of element at index 0, 1 or 2 will rerender the component though. It may
|
||||
be the case for some but this will be described in another keys/target pair. The second keys/target pair
|
||||
is actually the element at index 0 of the first pair. It only has id in the keys meaning that only the
|
||||
id property will actually trigger a rerender the component when modified. Be aware however that the other
|
||||
properties may be in the observed state of another component like a child one in this case. A greyed out
|
||||
property only implies it is not reactive for the selected component and not for the others.
|
||||
|
||||
The navigation inside the properties is also similar to the one in console variables: properties have
|
||||
Navigation inside the properties is also similar to the one in console variables: properties have
|
||||
their prototype displayed and getters will get their value when clicked on (...). It is also possible to
|
||||
send any property to the console using the right-click context menu on it and functions can be inspected
|
||||
in the sources tab as well.
|
||||
|
||||
<img src="screenshots/function_menu.png"/>
|
||||
|
||||
Using the right-click context menu on a property also allows to observe variables. Observed variables will
|
||||
be sent to a dedicated section of the details window and their value will be refreshed every 200ms. These
|
||||
variables are only shown when they are found and their access path will be kept in memory inside the
|
||||
browser so that it will always persist until the user decides to stop observing the variable. As in the
|
||||
browser's devtools, observed objects are displayed in reduced form and cannot be interacted with. It is
|
||||
still possible to send them to the console or remove them from the list using right-click.
|
||||
|
||||
<img src="screenshots/observe_variables.png"/>
|
||||
|
||||
The last section of the details window is filled with the component's lifecycle hooks. Using right click on
|
||||
them allows to place breakpoints inside the hook (either on its instance or class, hooks like mounted and
|
||||
willStart cannot have instance-based breakpoints because they will never trigger). Conditions in conditional
|
||||
breakpoints will be evaluated in the context of the component's definition.
|
||||
|
||||
<img src="screenshots/hooks.png"/>
|
||||
|
||||
There are several icons available to perform several of the actions described before in the components
|
||||
tree context menu and all these actions are also available by opening the menu by right-clicking on the
|
||||
component's name. Using the left click on the component's name will focus it in the components tree.
|
||||
@@ -96,8 +105,10 @@ component's name. Using the left click on the component's name will focus it in
|
||||
It is also possible to edit any of the leaf node properties. To do so, you must double click on the
|
||||
property's value and modify it using the freshly created input then press enter to apply the changes.
|
||||
Do note that the modified values should be written in JSON format in order to be valid (examples:
|
||||
89, "yes", undefined, null, \["hello", 15\], {"a": 1}, true, ...). Whether it has an impact on the
|
||||
component or not and whether it produces an error is the responsability of the user.
|
||||
89, "yes", undefined, null, \["hello", 15\], {"a": 1}, true, ...). Editing any value will produce a
|
||||
manual render of the component (or the root component of the application in the case of env values).
|
||||
Whether the edition has an impact on the component or not and whether it produces an error is the
|
||||
responsability of the user.
|
||||
|
||||
<img src="screenshots/edit.png"/>
|
||||
|
||||
@@ -117,7 +128,8 @@ are intercepted by the devtools using the record button.
|
||||
The second button is used to clear all the events that have been recorded. The select can be used to
|
||||
switch between the tree view (which shows the causality between renders) and the events log view which
|
||||
simply displays the events in the exact order they were triggered. In this view, you can expand the create,
|
||||
update and destroy events which reveals the component that initiated the event.
|
||||
update and destroy events which reveals the component that initiated the event. Also, a transition line will
|
||||
appear each time a new animation frame has been loaded between events.
|
||||
|
||||
<img src="screenshots/events_log.png"/>
|
||||
|
||||
@@ -131,20 +143,29 @@ There is also the Trace Renderings and Trace Subscriptions features. These featu
|
||||
recording of events and have no effect on the profiler tab. The Trace Renderings option is used to log in
|
||||
the console all the render events and allows to show their traceback information. Similarly, the Trace
|
||||
Subscriptions option logs all the properties that caused a render event and also allows to see the traceback
|
||||
of the modification
|
||||
of the modification.
|
||||
|
||||
<img src="screenshots/trace_rendering.png"/>
|
||||
<img src="screenshots/trace_subscriptions.png"/>
|
||||
|
||||
The Owl Devtools also allow to inspect iframes coded in Owl: when an Owl iframe is detected in the page,
|
||||
the iframe selector will appear next to the tabs. This allows to switch from an iframe to another easily.
|
||||
Be aware that switching iframes will clear all record events from the profiler tab. Iframes detection is
|
||||
currently not working in the firefox version, we are aware of this issue and will try to address it in the
|
||||
future.
|
||||
|
||||
<img src="screenshots/iframes.png"/>
|
||||
|
||||
## Options
|
||||
|
||||
The owl devtools extension has a dark mode feature which defaults to your general devtools settings and can
|
||||
be toggled using the sun/moon icon at the top-right corner of the tab. All the examples above were created
|
||||
with the dark mode enabled. There is also a refresh button to completely reset the owl devtools.
|
||||
be toggled using the sun/moon icon at the top-right corner of the tab. There is also a refresh button to
|
||||
completely reset the owl devtools.
|
||||
|
||||
<img src="screenshots/darkmode.png"/>
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
If the feedback from the page to the devtools seems to be cut, just close the devtools and refresh the page.
|
||||
If the feedback from the page to the devtools seems to be cut, you can first try to use the refresh
|
||||
button mentioned above but if it still doesn't seem to work, just close the devtools and refresh the page.
|
||||
This will eventually happen any time a tab stays opened for too long without being refreshed.
|
||||
|
||||
|
Before Width: | Height: | Size: 333 KiB After Width: | Height: | Size: 314 KiB |
|
Before Width: | Height: | Size: 210 KiB After Width: | Height: | Size: 193 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 197 KiB After Width: | Height: | Size: 266 KiB |
|
Before Width: | Height: | Size: 185 KiB After Width: | Height: | Size: 188 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 172 KiB |
|
Before Width: | Height: | Size: 166 KiB After Width: | Height: | Size: 200 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 311 KiB After Width: | Height: | Size: 346 KiB |
|
Before Width: | Height: | Size: 336 KiB After Width: | Height: | Size: 488 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 93 KiB |
|
Before Width: | Height: | Size: 146 KiB After Width: | Height: | Size: 182 KiB |
@@ -41,9 +41,6 @@ const loadFile = (path) => {
|
||||
* Make an iframe, with all the js, css and xml properly injected.
|
||||
*/
|
||||
function makeCodeIframe(js, css, xml) {
|
||||
// escape backticks in the xml so they don't close the template string
|
||||
const escapedXml = xml.replace(/`/g, '\\\`');
|
||||
|
||||
const iframe = document.createElement("iframe");
|
||||
iframe.onload = () => {
|
||||
const doc = iframe.contentDocument;
|
||||
@@ -55,6 +52,8 @@ function makeCodeIframe(js, css, xml) {
|
||||
|
||||
const script = doc.createElement("script");
|
||||
script.type = "module";
|
||||
// escape characters with special meaning in template literals
|
||||
const escapedXml = xml.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/, "\\${");
|
||||
script.textContent = `const TEMPLATES = \`${escapedXml}\`\n${js}`;
|
||||
doc.body.appendChild(script);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@odoo/owl",
|
||||
"version": "2.2.6",
|
||||
"version": "2.8.0",
|
||||
"description": "Odoo Web Library (OWL)",
|
||||
"main": "dist/owl.cjs.js",
|
||||
"module": "dist/owl.es.js",
|
||||
@@ -9,7 +9,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=12.18.3"
|
||||
"node": ">=20.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build:bundle": "rollup -c --failAfterWarnings",
|
||||
@@ -32,7 +32,10 @@
|
||||
"check-formatting": "prettier {src/*.ts,src/**/*.ts,tests/*.ts,tests/**/*.ts,doc/*.md,doc/**/*.md,tools/devtools/**/*.js} --check",
|
||||
"lint": "eslint src/**/*.ts tests/**/*.ts",
|
||||
"release": "node tools/release.js",
|
||||
"compile_templates": "node tools/compile_xml.js"
|
||||
"compile_templates": "node tools/compile_owl_templates.mjs"
|
||||
},
|
||||
"bin": {
|
||||
"compile_owl_templates": "tools/compile_owl_templates.mjs"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -46,6 +49,7 @@
|
||||
"homepage": "https://github.com/odoo/owl#readme",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/jsdom": "^21.1.7",
|
||||
"@types/node": "^14.11.8",
|
||||
"@typescript-eslint/eslint-plugin": "5.48.1",
|
||||
"@typescript-eslint/parser": "5.48.1",
|
||||
@@ -97,5 +101,8 @@
|
||||
"prettier": {
|
||||
"printWidth": 100,
|
||||
"endOfLine": "auto"
|
||||
},
|
||||
"dependencies": {
|
||||
"jsdom": "^25.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pkg from "./package.json";
|
||||
import git from "git-rev-sync";
|
||||
import typescript from 'rollup-plugin-typescript2';
|
||||
import typescript from "rollup-plugin-typescript2";
|
||||
import { terser } from "rollup-plugin-terser";
|
||||
import dts from "rollup-plugin-dts";
|
||||
|
||||
@@ -12,7 +12,7 @@ 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 = `
|
||||
__info__.date = '${new Date().toISOString()}';
|
||||
@@ -21,39 +21,37 @@ __info__.url = 'https://github.com/odoo/owl';
|
||||
`;
|
||||
|
||||
switch (process.argv[4]) {
|
||||
case "compiler":
|
||||
input = "src/compiler/index.ts",
|
||||
output = [
|
||||
getConfigForFormat('cjs', 'dist/compiler.js', ''),
|
||||
]
|
||||
case "compiler":
|
||||
(input = "src/compiler/index.ts"),
|
||||
(output = [getConfigForFormat("cjs", "dist/compiler.js", "")]);
|
||||
break;
|
||||
case "runtime":
|
||||
input = "src/runtime/index.ts";
|
||||
output = [
|
||||
getConfigForFormat('esm', addSuffix(ES_FILENAME, 'runtime'), outro),
|
||||
getConfigForFormat('cjs', addSuffix(CJS_FILENAME, 'runtime'), outro),
|
||||
getConfigForFormat('iife', addSuffix(IIFE_FILENAME, 'runtime'), outro),
|
||||
getConfigForFormat('iife', addSuffix(IIFE_FILENAME, 'runtime'), outro, true),
|
||||
]
|
||||
getConfigForFormat("esm", addSuffix(ES_FILENAME, "runtime"), outro),
|
||||
getConfigForFormat("cjs", addSuffix(CJS_FILENAME, "runtime"), outro),
|
||||
getConfigForFormat("iife", addSuffix(IIFE_FILENAME, "runtime"), outro),
|
||||
getConfigForFormat("iife", addSuffix(IIFE_FILENAME, "runtime"), outro, true),
|
||||
];
|
||||
break;
|
||||
default:
|
||||
input = "src/index.ts",
|
||||
output = [
|
||||
getConfigForFormat('esm', ES_FILENAME, outro),
|
||||
getConfigForFormat('cjs', CJS_FILENAME, outro),
|
||||
getConfigForFormat('iife', IIFE_FILENAME, outro),
|
||||
getConfigForFormat('iife', IIFE_FILENAME, outro, true),
|
||||
]
|
||||
}
|
||||
(input = "src/index.ts"),
|
||||
(output = [
|
||||
getConfigForFormat("esm", ES_FILENAME, outro),
|
||||
getConfigForFormat("cjs", CJS_FILENAME, outro),
|
||||
getConfigForFormat("iife", IIFE_FILENAME, outro),
|
||||
getConfigForFormat("iife", IIFE_FILENAME, outro, true),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate from a string depicting a path a new path for the minified version.
|
||||
* @param {string} pkgFileName file name
|
||||
*/
|
||||
function addSuffix(pkgFileName, suffix) {
|
||||
const parts = pkgFileName.split('.');
|
||||
const parts = pkgFileName.split(".");
|
||||
parts.splice(parts.length - 1, 0, suffix);
|
||||
return parts.join('.');
|
||||
return parts.join(".");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +69,7 @@ function getConfigForFormat(format, generatedFileName, outro, minified = false)
|
||||
outro: outro,
|
||||
freeze: false,
|
||||
plugins: minified ? [terser()] : [],
|
||||
indent: ' ', // indent with 4 spaces
|
||||
indent: " ", // indent with 4 spaces
|
||||
};
|
||||
}
|
||||
|
||||
@@ -81,9 +79,19 @@ export default [
|
||||
output,
|
||||
plugins: [
|
||||
typescript({
|
||||
useTsconfigDeclarationDir: true
|
||||
useTsconfigDeclarationDir: true,
|
||||
}),
|
||||
]
|
||||
],
|
||||
},
|
||||
{
|
||||
input: "src/compiler/standalone/index.ts",
|
||||
output: [{ file: "dist/compile_templates.mjs", format: "es" }],
|
||||
external: ["fs", "fs/promises", "path", "jsdom"],
|
||||
plugins: [
|
||||
typescript({
|
||||
useTsconfigDeclarationDir: true,
|
||||
}),
|
||||
],
|
||||
},
|
||||
{
|
||||
input: "dist/types/index.d.ts",
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export type customDirectives = Record<
|
||||
string,
|
||||
(node: Element, value: string, modifier: string[]) => void
|
||||
>;
|
||||
@@ -0,0 +1,38 @@
|
||||
import { OwlError } from "./owl_error";
|
||||
|
||||
/**
|
||||
* Parses an XML string into an XML document, throwing errors on parser errors
|
||||
* instead of returning an XML document containing the parseerror.
|
||||
*
|
||||
* @param xml the string to parse
|
||||
* @returns an XML document corresponding to the content of the string
|
||||
*/
|
||||
export function parseXML(xml: string): XMLDocument {
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(xml, "text/xml");
|
||||
if (doc.getElementsByTagName("parsererror").length) {
|
||||
let msg = "Invalid XML in template.";
|
||||
const parsererrorText = doc.getElementsByTagName("parsererror")[0].textContent;
|
||||
if (parsererrorText) {
|
||||
msg += "\nThe parser has produced the following error message:\n" + parsererrorText;
|
||||
const re = /\d+/g;
|
||||
const firstMatch = re.exec(parsererrorText);
|
||||
if (firstMatch) {
|
||||
const lineNumber = Number(firstMatch[0]);
|
||||
const line = xml.split("\n")[lineNumber - 1];
|
||||
const secondMatch = re.exec(parsererrorText);
|
||||
if (line && secondMatch) {
|
||||
const columnIndex = Number(secondMatch[0]) - 1;
|
||||
if (line[columnIndex]) {
|
||||
msg +=
|
||||
`\nThe error might be located at xml line ${lineNumber} column ${columnIndex}\n` +
|
||||
`${line}\n${"-".repeat(columnIndex - 1)}^`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new OwlError(msg);
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
ASTTOut,
|
||||
ASTTPortal,
|
||||
ASTTranslation,
|
||||
ASTTranslationContext,
|
||||
ASTTSet,
|
||||
ASTType,
|
||||
Attrs,
|
||||
@@ -35,7 +36,7 @@ type BlockType = "block" | "text" | "multi" | "list" | "html" | "comment";
|
||||
const whitespaceRE = /\s+/g;
|
||||
|
||||
export interface Config {
|
||||
translateFn?: (s: string) => string;
|
||||
translateFn?: (s: string, translationCtx: string) => string;
|
||||
translatableAttributes?: string[];
|
||||
dev?: boolean;
|
||||
}
|
||||
@@ -43,6 +44,7 @@ export interface Config {
|
||||
export interface CodeGenOptions extends Config {
|
||||
hasSafeContext?: boolean;
|
||||
name?: string;
|
||||
hasGlobalValues: boolean;
|
||||
}
|
||||
|
||||
// using a non-html document so that <inner/outer>HTML serializes as XML instead
|
||||
@@ -82,6 +84,14 @@ function isProp(tag: string, key: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a template literal that evaluates to str. You can add interpolation
|
||||
* sigils into the string if required
|
||||
*/
|
||||
function toStringExpression(str: string) {
|
||||
return `\`${str.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/, "\\${")}\``;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// BlockDescription
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -162,6 +172,7 @@ interface Context {
|
||||
forceNewBlock: boolean;
|
||||
isLast?: boolean;
|
||||
translate: boolean;
|
||||
translationCtx: string;
|
||||
tKeyExpr: string | null;
|
||||
nameSpace?: string;
|
||||
tModelSelectedExpr?: string;
|
||||
@@ -176,6 +187,7 @@ function createContext(parentCtx: Context, params?: Partial<Context>): Context {
|
||||
index: 0,
|
||||
forceNewBlock: true,
|
||||
translate: parentCtx.translate,
|
||||
translationCtx: parentCtx.translationCtx,
|
||||
tKeyExpr: null,
|
||||
nameSpace: parentCtx.nameSpace,
|
||||
tModelSelectedExpr: parentCtx.tModelSelectedExpr,
|
||||
@@ -242,7 +254,16 @@ class CodeTarget {
|
||||
}
|
||||
}
|
||||
|
||||
const TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"];
|
||||
const TRANSLATABLE_ATTRS = [
|
||||
"alt",
|
||||
"aria-label",
|
||||
"aria-placeholder",
|
||||
"aria-roledescription",
|
||||
"aria-valuetext",
|
||||
"label",
|
||||
"placeholder",
|
||||
"title",
|
||||
];
|
||||
const translationRE = /^(\s*)([\s\S]+?)(\s*)$/;
|
||||
|
||||
export class CodeGenerator {
|
||||
@@ -254,7 +275,7 @@ export class CodeGenerator {
|
||||
target = new CodeTarget("template");
|
||||
templateName?: string;
|
||||
dev: boolean;
|
||||
translateFn: (s: string) => string;
|
||||
translateFn: (s: string, translationCtx: string) => string;
|
||||
translatableAttributes: string[] = TRANSLATABLE_ATTRS;
|
||||
ast: AST;
|
||||
staticDefs: { id: string; expr: string }[] = [];
|
||||
@@ -278,6 +299,9 @@ export class CodeGenerator {
|
||||
this.dev = options.dev || false;
|
||||
this.ast = ast;
|
||||
this.templateName = options.name;
|
||||
if (options.hasGlobalValues) {
|
||||
this.helpers.add("__globals__");
|
||||
}
|
||||
}
|
||||
|
||||
generateCode(): string {
|
||||
@@ -291,6 +315,7 @@ export class CodeGenerator {
|
||||
forceNewBlock: false,
|
||||
isLast: true,
|
||||
translate: true,
|
||||
translationCtx: "",
|
||||
tKeyExpr: null,
|
||||
});
|
||||
// define blocks and utility functions
|
||||
@@ -311,14 +336,13 @@ export class CodeGenerator {
|
||||
mainCode.push(``);
|
||||
for (let block of this.blocks) {
|
||||
if (block.dom) {
|
||||
let xmlString = block.asXmlString();
|
||||
xmlString = xmlString.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
|
||||
let xmlString = toStringExpression(block.asXmlString());
|
||||
if (block.dynamicTagName) {
|
||||
xmlString = xmlString.replace(/^<\w+/, `<\${tag || '${block.dom.nodeName}'}`);
|
||||
xmlString = xmlString.replace(/\w+>$/, `\${tag || '${block.dom.nodeName}'}>`);
|
||||
mainCode.push(`let ${block.blockName} = tag => createBlock(\`${xmlString}\`);`);
|
||||
xmlString = xmlString.replace(/^`<\w+/, `\`<\${tag || '${block.dom.nodeName}'}`);
|
||||
xmlString = xmlString.replace(/\w+>`$/, `\${tag || '${block.dom.nodeName}'}>\``);
|
||||
mainCode.push(`let ${block.blockName} = tag => createBlock(${xmlString});`);
|
||||
} else {
|
||||
mainCode.push(`let ${block.blockName} = createBlock(\`${xmlString}\`);`);
|
||||
mainCode.push(`let ${block.blockName} = createBlock(${xmlString});`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -446,9 +470,9 @@ export class CodeGenerator {
|
||||
.join("");
|
||||
}
|
||||
|
||||
translate(str: string): string {
|
||||
translate(str: string, translationCtx: string): string {
|
||||
const match = translationRE.exec(str) as any;
|
||||
return match[1] + this.translateFn(match[2]) + match[3];
|
||||
return match[1] + this.translateFn(match[2], translationCtx) + match[3];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -490,6 +514,8 @@ export class CodeGenerator {
|
||||
return this.compileTSlot(ast, ctx);
|
||||
case ASTType.TTranslation:
|
||||
return this.compileTTranslation(ast, ctx);
|
||||
case ASTType.TTranslationContext:
|
||||
return this.compileTTranslationContext(ast, ctx);
|
||||
case ASTType.TPortal:
|
||||
return this.compileTPortal(ast, ctx);
|
||||
}
|
||||
@@ -515,7 +541,7 @@ export class CodeGenerator {
|
||||
const isNewBlock = !block || forceNewBlock;
|
||||
if (isNewBlock) {
|
||||
block = this.createBlock(block, "comment", ctx);
|
||||
this.insertBlock(`comment(\`${ast.value}\`)`, block, {
|
||||
this.insertBlock(`comment(${toStringExpression(ast.value)})`, block, {
|
||||
...ctx,
|
||||
forceNewBlock: forceNewBlock && !block,
|
||||
});
|
||||
@@ -531,7 +557,7 @@ export class CodeGenerator {
|
||||
|
||||
let value = ast.value;
|
||||
if (value && ctx.translate !== false) {
|
||||
value = this.translate(value);
|
||||
value = this.translate(value, ctx.translationCtx);
|
||||
}
|
||||
if (!ctx.inPreTag) {
|
||||
value = value.replace(whitespaceRE, " ");
|
||||
@@ -539,7 +565,7 @@ export class CodeGenerator {
|
||||
|
||||
if (!block || forceNewBlock) {
|
||||
block = this.createBlock(block, "text", ctx);
|
||||
this.insertBlock(`text(\`${value}\`)`, block, {
|
||||
this.insertBlock(`text(${toStringExpression(value)})`, block, {
|
||||
...ctx,
|
||||
forceNewBlock: forceNewBlock && !block,
|
||||
});
|
||||
@@ -620,7 +646,8 @@ export class CodeGenerator {
|
||||
}
|
||||
}
|
||||
} else if (this.translatableAttributes.includes(key)) {
|
||||
attrs[key] = this.translateFn(ast.attrs[key]);
|
||||
const attrTranslationCtx = ast.attrsTranslationCtx?.[key] || ctx.translationCtx;
|
||||
attrs[key] = this.translateFn(ast.attrs[key], attrTranslationCtx);
|
||||
} else {
|
||||
expr = `"${ast.attrs[key]}"`;
|
||||
attrName = key;
|
||||
@@ -774,7 +801,8 @@ export class CodeGenerator {
|
||||
expr = compileExpr(ast.expr);
|
||||
if (ast.defaultValue) {
|
||||
this.helpers.add("withDefault");
|
||||
expr = `withDefault(${expr}, \`${ast.defaultValue}\`)`;
|
||||
// FIXME: defaultValue is not translated
|
||||
expr = `withDefault(${expr}, ${toStringExpression(ast.defaultValue)})`;
|
||||
}
|
||||
}
|
||||
if (!block || forceNewBlock) {
|
||||
@@ -1039,7 +1067,7 @@ export class CodeGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
const key = `key + \`${this.generateComponentKey()}\``;
|
||||
const key = this.generateComponentKey();
|
||||
if (isDynamic) {
|
||||
const templateVar = generateId("template");
|
||||
if (!this.staticDefs.find((d) => d.id === "call")) {
|
||||
@@ -1091,11 +1119,13 @@ export class CodeGenerator {
|
||||
} else {
|
||||
let value: string;
|
||||
if (ast.defaultValue) {
|
||||
const defaultValue = ctx.translate ? this.translate(ast.defaultValue) : ast.defaultValue;
|
||||
const defaultValue = toStringExpression(
|
||||
ctx.translate ? this.translate(ast.defaultValue, ctx.translationCtx) : ast.defaultValue
|
||||
);
|
||||
if (ast.value) {
|
||||
value = `withDefault(${expr}, \`${defaultValue}\`)`;
|
||||
value = `withDefault(${expr}, ${defaultValue})`;
|
||||
} else {
|
||||
value = `\`${defaultValue}\``;
|
||||
value = defaultValue;
|
||||
}
|
||||
} else {
|
||||
value = expr;
|
||||
@@ -1106,12 +1136,12 @@ export class CodeGenerator {
|
||||
return null;
|
||||
}
|
||||
|
||||
generateComponentKey() {
|
||||
generateComponentKey(currentKey: string = "key") {
|
||||
const parts = [generateId("__")];
|
||||
for (let i = 0; i < this.target.loopLevel; i++) {
|
||||
parts.push(`\${key${i + 1}}`);
|
||||
}
|
||||
return parts.join("__");
|
||||
return `${currentKey} + \`${parts.join("__")}\``;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1125,8 +1155,18 @@ export class CodeGenerator {
|
||||
* "some-prop" "state" "'some-prop': ctx['state']"
|
||||
* "onClick.bind" "onClick" "onClick: bind(ctx, ctx['onClick'])"
|
||||
*/
|
||||
formatProp(name: string, value: string): string {
|
||||
value = this.captureExpression(value);
|
||||
formatProp(
|
||||
name: string,
|
||||
value: string,
|
||||
attrsTranslationCtx: { [name: string]: string } | null,
|
||||
translationCtx: string
|
||||
): string {
|
||||
if (name.endsWith(".translate")) {
|
||||
const attrTranslationCtx = attrsTranslationCtx?.[name] || translationCtx;
|
||||
value = toStringExpression(this.translateFn(value, attrTranslationCtx));
|
||||
} else {
|
||||
value = this.captureExpression(value);
|
||||
}
|
||||
if (name.includes(".")) {
|
||||
let [_name, suffix] = name.split(".");
|
||||
name = _name;
|
||||
@@ -1135,17 +1175,24 @@ export class CodeGenerator {
|
||||
value = `(${value}).bind(this)`;
|
||||
break;
|
||||
case "alike":
|
||||
case "translate":
|
||||
break;
|
||||
default:
|
||||
throw new OwlError("Invalid prop suffix");
|
||||
throw new OwlError(`Invalid prop suffix: ${suffix}`);
|
||||
}
|
||||
}
|
||||
name = /^[a-z_]+$/i.test(name) ? name : `'${name}'`;
|
||||
return `${name}: ${value || undefined}`;
|
||||
}
|
||||
|
||||
formatPropObject(obj: { [prop: string]: any }): string[] {
|
||||
return Object.entries(obj).map(([k, v]) => this.formatProp(k, v));
|
||||
formatPropObject(
|
||||
obj: { [prop: string]: any },
|
||||
attrsTranslationCtx: { [name: string]: string } | null,
|
||||
translationCtx: string
|
||||
): string[] {
|
||||
return Object.entries(obj).map(([k, v]) =>
|
||||
this.formatProp(k, v, attrsTranslationCtx, translationCtx)
|
||||
);
|
||||
}
|
||||
|
||||
getPropString(props: string[], dynProps: string | null): string {
|
||||
@@ -1162,7 +1209,9 @@ export class CodeGenerator {
|
||||
let { block } = ctx;
|
||||
// props
|
||||
const hasSlotsProp = "slots" in (ast.props || {});
|
||||
const props: string[] = ast.props ? this.formatPropObject(ast.props) : [];
|
||||
const props: string[] = ast.props
|
||||
? this.formatPropObject(ast.props, ast.propsTranslationCtx, ctx.translationCtx)
|
||||
: [];
|
||||
|
||||
// slots
|
||||
let slotDef: string = "";
|
||||
@@ -1186,7 +1235,13 @@ export class CodeGenerator {
|
||||
params.push(`__scope: "${scope}"`);
|
||||
}
|
||||
if (ast.slots[slotName].attrs) {
|
||||
params.push(...this.formatPropObject(ast.slots[slotName].attrs!));
|
||||
params.push(
|
||||
...this.formatPropObject(
|
||||
ast.slots[slotName].attrs!,
|
||||
ast.slots[slotName].attrsTranslationCtx,
|
||||
ctx.translationCtx
|
||||
)
|
||||
);
|
||||
}
|
||||
const slotInfo = `{${params.join(", ")}}`;
|
||||
slotStr.push(`'${slotName}': ${slotInfo}`);
|
||||
@@ -1214,7 +1269,6 @@ export class CodeGenerator {
|
||||
}
|
||||
|
||||
// cmap key
|
||||
const key = this.generateComponentKey();
|
||||
let expr: string;
|
||||
if (ast.isDynamic) {
|
||||
expr = generateId("Comp");
|
||||
@@ -1232,7 +1286,7 @@ export class CodeGenerator {
|
||||
this.insertAnchor(block);
|
||||
}
|
||||
|
||||
let keyArg = `key + \`${key}\``;
|
||||
let keyArg = this.generateComponentKey();
|
||||
if (ctx.tKeyExpr) {
|
||||
keyArg = `${ctx.tKeyExpr} + ${keyArg}`;
|
||||
}
|
||||
@@ -1305,16 +1359,17 @@ export class CodeGenerator {
|
||||
isMultiple = isMultiple || this.slotNames.has(ast.name);
|
||||
this.slotNames.add(ast.name);
|
||||
}
|
||||
const dynProps = ast.attrs ? ast.attrs["t-props"] : null;
|
||||
if (ast.attrs) {
|
||||
delete ast.attrs["t-props"];
|
||||
}
|
||||
const attrs = { ...ast.attrs };
|
||||
const dynProps = attrs["t-props"];
|
||||
delete attrs["t-props"];
|
||||
let key = this.target.loopLevel ? `key${this.target.loopLevel}` : "key";
|
||||
if (isMultiple) {
|
||||
key = `${key} + \`${this.generateComponentKey()}\``;
|
||||
key = this.generateComponentKey(key);
|
||||
}
|
||||
|
||||
const props = ast.attrs ? this.formatPropObject(ast.attrs) : [];
|
||||
const props = ast.attrs
|
||||
? this.formatPropObject(attrs, ast.attrsTranslationCtx, ctx.translationCtx)
|
||||
: [];
|
||||
const scope = this.getPropString(props, dynProps);
|
||||
if (ast.defaultContent) {
|
||||
const name = this.compileInNewTarget("defaultContent", ast.defaultContent, ctx);
|
||||
@@ -1347,6 +1402,15 @@ export class CodeGenerator {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
compileTTranslationContext(ast: ASTTranslationContext, ctx: Context): string | null {
|
||||
if (ast.content) {
|
||||
return this.compileAST(
|
||||
ast.content,
|
||||
Object.assign({}, ctx, { translationCtx: ast.translationCtx })
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
compileTPortal(ast: ASTTPortal, ctx: Context): string {
|
||||
if (!this.staticDefs.find((d) => d.id === "Portal")) {
|
||||
this.staticDefs.push({ id: "Portal", expr: `app.Portal` });
|
||||
@@ -1354,7 +1418,6 @@ export class CodeGenerator {
|
||||
|
||||
let { block } = ctx;
|
||||
const name = this.compileInNewTarget("slot", ast.content, ctx);
|
||||
const key = this.generateComponentKey();
|
||||
let ctxStr = "ctx";
|
||||
if (this.target.loopLevel || !this.hasSafeContext) {
|
||||
ctxStr = generateId("ctx");
|
||||
@@ -1368,7 +1431,8 @@ export class CodeGenerator {
|
||||
});
|
||||
|
||||
const target = compileExpr(ast.target);
|
||||
const blockString = `${id}({target: ${target},slots: {'default': {__render: ${name}.bind(this), __ctx: ${ctxStr}}}}, key + \`${key}\`, node, ctx, Portal)`;
|
||||
const key = this.generateComponentKey();
|
||||
const blockString = `${id}({target: ${target},slots: {'default': {__render: ${name}.bind(this), __ctx: ${ctxStr}}}}, ${key}, node, ctx, Portal)`;
|
||||
if (block) {
|
||||
this.insertAnchor(block);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { customDirectives } from "../common/types";
|
||||
import type { TemplateSet } from "../runtime/template_set";
|
||||
import type { BDom } from "../runtime/blockdom";
|
||||
import { CodeGenerator, Config } from "./code_generator";
|
||||
@@ -10,13 +11,17 @@ export type TemplateFunction = (app: TemplateSet, bdom: any, helpers: any) => Te
|
||||
|
||||
interface CompileOptions extends Config {
|
||||
name?: string;
|
||||
customDirectives?: customDirectives;
|
||||
hasGlobalValues: boolean;
|
||||
}
|
||||
export function compile(
|
||||
template: string | Element,
|
||||
options: CompileOptions = {}
|
||||
options: CompileOptions = {
|
||||
hasGlobalValues: false,
|
||||
}
|
||||
): TemplateFunction {
|
||||
// parsing
|
||||
const ast = parse(template);
|
||||
const ast = parse(template, options.customDirectives);
|
||||
|
||||
// some work
|
||||
const hasSafeContext =
|
||||
|
||||
@@ -28,7 +28,7 @@ import { OwlError } from "../common/owl_error";
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const RESERVED_WORDS =
|
||||
"true,false,NaN,null,undefined,debugger,console,window,in,instanceof,new,function,return,eval,void,Math,RegExp,Array,Object,Date".split(
|
||||
"true,false,NaN,null,undefined,debugger,console,window,in,instanceof,new,function,return,eval,void,Math,RegExp,Array,Object,Date,__globals__".split(
|
||||
","
|
||||
);
|
||||
|
||||
@@ -268,7 +268,7 @@ export function compileExprToArray(expr: string): Token[] {
|
||||
const localVars = new Set<string>();
|
||||
const tokens = tokenize(expr);
|
||||
let i = 0;
|
||||
let stack = []; // to track last opening [ or {
|
||||
let stack = []; // to track last opening (, [ or {
|
||||
|
||||
while (i < tokens.length) {
|
||||
let token = tokens[i];
|
||||
@@ -279,10 +279,12 @@ export function compileExprToArray(expr: string): Token[] {
|
||||
switch (token.type) {
|
||||
case "LEFT_BRACE":
|
||||
case "LEFT_BRACKET":
|
||||
case "LEFT_PAREN":
|
||||
stack.push(token.type);
|
||||
break;
|
||||
case "RIGHT_BRACE":
|
||||
case "RIGHT_BRACKET":
|
||||
case "RIGHT_PAREN":
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { OwlError } from "../common/owl_error";
|
||||
import type { customDirectives } from "../common/types";
|
||||
import { parseXML } from "../common/utils";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AST Type definition
|
||||
@@ -25,6 +27,7 @@ export const enum ASTType {
|
||||
TSlot,
|
||||
TCallBlock,
|
||||
TTranslation,
|
||||
TTranslationContext,
|
||||
TPortal,
|
||||
}
|
||||
|
||||
@@ -54,6 +57,7 @@ export interface ASTDomNode {
|
||||
tag: string;
|
||||
content: AST[];
|
||||
attrs: Attrs | null;
|
||||
attrsTranslationCtx: Attrs | null;
|
||||
ref: string | null;
|
||||
on: EventHandlers | null;
|
||||
model: TModelInfo | null;
|
||||
@@ -125,6 +129,7 @@ interface SlotDefinition {
|
||||
scope: string | null;
|
||||
on: EventHandlers | null;
|
||||
attrs: Attrs | null;
|
||||
attrsTranslationCtx: Attrs | null;
|
||||
}
|
||||
|
||||
export interface ASTComponent {
|
||||
@@ -134,6 +139,7 @@ export interface ASTComponent {
|
||||
dynamicProps: string | null;
|
||||
on: EventHandlers | null;
|
||||
props: { [name: string]: string } | null;
|
||||
propsTranslationCtx: { [name: string]: string } | null;
|
||||
slots: { [name: string]: SlotDefinition } | null;
|
||||
}
|
||||
|
||||
@@ -141,6 +147,7 @@ export interface ASTSlot {
|
||||
type: ASTType.TSlot;
|
||||
name: string;
|
||||
attrs: Attrs | null;
|
||||
attrsTranslationCtx: Attrs | null;
|
||||
on: EventHandlers | null;
|
||||
defaultContent: AST | null;
|
||||
}
|
||||
@@ -166,6 +173,12 @@ export interface ASTTranslation {
|
||||
content: AST | null;
|
||||
}
|
||||
|
||||
export interface ASTTranslationContext {
|
||||
type: ASTType.TTranslationContext;
|
||||
content: AST | null;
|
||||
translationCtx: string;
|
||||
}
|
||||
|
||||
export interface ASTTPortal {
|
||||
type: ASTType.TPortal;
|
||||
target: string;
|
||||
@@ -190,6 +203,7 @@ export type AST =
|
||||
| ASTLog
|
||||
| ASTDebug
|
||||
| ASTTranslation
|
||||
| ASTTranslationContext
|
||||
| ASTTPortal;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -197,23 +211,26 @@ export type AST =
|
||||
// -----------------------------------------------------------------------------
|
||||
const cache: WeakMap<Element, AST> = new WeakMap();
|
||||
|
||||
export function parse(xml: string | Element): AST {
|
||||
export function parse(xml: string | Element, customDir?: customDirectives): AST {
|
||||
const ctx = {
|
||||
inPreTag: false,
|
||||
customDirectives: customDir,
|
||||
};
|
||||
if (typeof xml === "string") {
|
||||
const elem = parseXML(`<t>${xml}</t>`).firstChild as Element;
|
||||
return _parse(elem);
|
||||
return _parse(elem, ctx);
|
||||
}
|
||||
let ast = cache.get(xml);
|
||||
if (!ast) {
|
||||
// we clone here the xml to prevent modifying it in place
|
||||
ast = _parse(xml.cloneNode(true) as Element);
|
||||
ast = _parse(xml.cloneNode(true) as Element, ctx);
|
||||
cache.set(xml, ast);
|
||||
}
|
||||
return ast;
|
||||
}
|
||||
|
||||
function _parse(xml: Element): AST {
|
||||
function _parse(xml: Element, ctx: ParsingContext): AST {
|
||||
normalizeXML(xml);
|
||||
const ctx = { inPreTag: false };
|
||||
return parseNode(xml, ctx) || { type: ASTType.Text, value: "" };
|
||||
}
|
||||
|
||||
@@ -221,6 +238,7 @@ interface ParsingContext {
|
||||
tModelInfo?: TModelInfo | null;
|
||||
nameSpace?: string;
|
||||
inPreTag: boolean;
|
||||
customDirectives?: customDirectives;
|
||||
}
|
||||
|
||||
function parseNode(node: Node, ctx: ParsingContext): AST | null {
|
||||
@@ -228,16 +246,18 @@ function parseNode(node: Node, ctx: ParsingContext): AST | null {
|
||||
return parseTextCommentNode(node, ctx);
|
||||
}
|
||||
return (
|
||||
parseTCustom(node, ctx) ||
|
||||
parseTDebugLog(node, ctx) ||
|
||||
parseTForEach(node, ctx) ||
|
||||
parseTIf(node, ctx) ||
|
||||
parseTPortal(node, ctx) ||
|
||||
parseTCall(node, ctx) ||
|
||||
parseTCallBlock(node, ctx) ||
|
||||
parseTTranslation(node, ctx) ||
|
||||
parseTTranslationContext(node, ctx) ||
|
||||
parseTEscNode(node, ctx) ||
|
||||
parseTOutNode(node, ctx) ||
|
||||
parseTKey(node, ctx) ||
|
||||
parseTTranslation(node, ctx) ||
|
||||
parseTSlot(node, ctx) ||
|
||||
parseComponent(node, ctx) ||
|
||||
parseDOMNode(node, ctx) ||
|
||||
@@ -276,6 +296,37 @@ function parseTextCommentNode(node: Node, ctx: ParsingContext): AST | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseTCustom(node: Element, ctx: ParsingContext): AST | null {
|
||||
if (!ctx.customDirectives) {
|
||||
return null;
|
||||
}
|
||||
const nodeAttrsNames = node.getAttributeNames();
|
||||
for (let attr of nodeAttrsNames) {
|
||||
if (attr === "t-custom" || attr === "t-custom-") {
|
||||
throw new OwlError("Missing custom directive name with t-custom directive");
|
||||
}
|
||||
if (attr.startsWith("t-custom-")) {
|
||||
const directiveName = attr.split(".")[0].slice(9);
|
||||
const customDirective = ctx.customDirectives[directiveName];
|
||||
if (!customDirective) {
|
||||
throw new OwlError(`Custom directive "${directiveName}" is not defined`);
|
||||
}
|
||||
const value = node.getAttribute(attr)!;
|
||||
const modifiers = attr.split(".").slice(1);
|
||||
node.removeAttribute(attr);
|
||||
try {
|
||||
customDirective(node, value, modifiers);
|
||||
} catch (error) {
|
||||
throw new OwlError(
|
||||
`Custom directive "${directiveName}" throw the following error: ${error}`
|
||||
);
|
||||
}
|
||||
return parseNode(node, ctx);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// debugging
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -330,6 +381,7 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
||||
|
||||
const nodeAttrsNames = node.getAttributeNames();
|
||||
let attrs: ASTDomNode["attrs"] = null;
|
||||
let attrsTranslationCtx: ASTDomNode["attrsTranslationCtx"] = null;
|
||||
let on: EventHandlers | null = null;
|
||||
let model: TModelInfo | null = null;
|
||||
|
||||
@@ -366,9 +418,9 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
||||
const isSelect = tagName === "select";
|
||||
const isCheckboxInput = isInput && typeAttr === "checkbox";
|
||||
const isRadioInput = isInput && typeAttr === "radio";
|
||||
const hasLazyMod = attr.includes(".lazy");
|
||||
const hasNumberMod = attr.includes(".number");
|
||||
const hasTrimMod = attr.includes(".trim");
|
||||
const hasLazyMod = hasTrimMod || attr.includes(".lazy");
|
||||
const hasNumberMod = attr.includes(".number");
|
||||
const eventType = isRadioInput ? "click" : isSelect || hasLazyMod ? "change" : "input";
|
||||
|
||||
model = {
|
||||
@@ -390,6 +442,10 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
||||
throw new OwlError(`Invalid attribute: '${attr}'`);
|
||||
} else if (attr === "xmlns") {
|
||||
ns = value;
|
||||
} else if (attr.startsWith("t-translation-context-")) {
|
||||
const attrName = attr.slice(22);
|
||||
attrsTranslationCtx = attrsTranslationCtx || {};
|
||||
attrsTranslationCtx[attrName] = value;
|
||||
} else if (attr !== "t-name") {
|
||||
if (attr.startsWith("t-") && !attr.startsWith("t-att")) {
|
||||
throw new OwlError(`Unknown QWeb directive: '${attr}'`);
|
||||
@@ -412,6 +468,7 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null {
|
||||
tag: tagName,
|
||||
dynamicTag,
|
||||
attrs,
|
||||
attrsTranslationCtx,
|
||||
on,
|
||||
ref,
|
||||
content: children,
|
||||
@@ -571,7 +628,15 @@ function parseTCall(node: Element, ctx: ParsingContext): AST | null {
|
||||
if (ast && ast.type === ASTType.TComponent) {
|
||||
return {
|
||||
...ast,
|
||||
slots: { default: { content: tcall, scope: null, on: null, attrs: null } },
|
||||
slots: {
|
||||
default: {
|
||||
content: tcall,
|
||||
scope: null,
|
||||
on: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -706,9 +771,14 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
let on: ASTComponent["on"] = null;
|
||||
|
||||
let props: ASTComponent["props"] = null;
|
||||
let propsTranslationCtx: ASTComponent["propsTranslationCtx"] = null;
|
||||
for (let name of node.getAttributeNames()) {
|
||||
const value = node.getAttribute(name)!;
|
||||
if (name.startsWith("t-")) {
|
||||
if (name.startsWith("t-translation-context-")) {
|
||||
const attrName = name.slice(22);
|
||||
propsTranslationCtx = propsTranslationCtx || {};
|
||||
propsTranslationCtx[attrName] = value;
|
||||
} else if (name.startsWith("t-")) {
|
||||
if (name.startsWith("t-on-")) {
|
||||
on = on || {};
|
||||
on[name.slice(5)] = value;
|
||||
@@ -740,14 +810,14 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
// be ignored)
|
||||
let el = slotNode.parentElement!;
|
||||
let isInSubComponent = false;
|
||||
while (el !== clone) {
|
||||
while (el && el !== clone) {
|
||||
if (el!.hasAttribute("t-component") || el!.tagName[0] === el!.tagName[0].toUpperCase()) {
|
||||
isInSubComponent = true;
|
||||
break;
|
||||
}
|
||||
el = el.parentElement!;
|
||||
}
|
||||
if (isInSubComponent) {
|
||||
if (isInSubComponent || !el) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -756,12 +826,17 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
const slotAst = parseNode(slotNode, ctx);
|
||||
let on: SlotDefinition["on"] = null;
|
||||
let attrs: Attrs | null = null;
|
||||
let attrsTranslationCtx: Attrs | null = null;
|
||||
let scope: string | null = null;
|
||||
for (let attributeName of slotNode.getAttributeNames()) {
|
||||
const value = slotNode.getAttribute(attributeName)!;
|
||||
if (attributeName === "t-slot-scope") {
|
||||
scope = value;
|
||||
continue;
|
||||
} else if (attributeName.startsWith("t-translation-context-")) {
|
||||
const attrName = attributeName.slice(22);
|
||||
attrsTranslationCtx = attrsTranslationCtx || {};
|
||||
attrsTranslationCtx[attrName] = value;
|
||||
} else if (attributeName.startsWith("t-on-")) {
|
||||
on = on || {};
|
||||
on[attributeName.slice(5)] = value;
|
||||
@@ -771,7 +846,7 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
}
|
||||
}
|
||||
slots = slots || {};
|
||||
slots[name] = { content: slotAst, on, attrs, scope };
|
||||
slots[name] = { content: slotAst, on, attrs, attrsTranslationCtx, scope };
|
||||
}
|
||||
|
||||
// default slot
|
||||
@@ -779,10 +854,25 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
slots = slots || {};
|
||||
// t-set-slot="default" has priority over content
|
||||
if (defaultContent && !slots.default) {
|
||||
slots.default = { content: defaultContent, on, attrs: null, scope: defaultSlotScope };
|
||||
slots.default = {
|
||||
content: defaultContent,
|
||||
on,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
scope: defaultSlotScope,
|
||||
};
|
||||
}
|
||||
}
|
||||
return { type: ASTType.TComponent, name, isDynamic, dynamicProps, props, slots, on };
|
||||
return {
|
||||
type: ASTType.TComponent,
|
||||
name,
|
||||
isDynamic,
|
||||
dynamicProps,
|
||||
props,
|
||||
propsTranslationCtx,
|
||||
slots,
|
||||
on,
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -796,12 +886,17 @@ function parseTSlot(node: Element, ctx: ParsingContext): AST | null {
|
||||
const name = node.getAttribute("t-slot")!;
|
||||
node.removeAttribute("t-slot");
|
||||
let attrs: Attrs | null = null;
|
||||
let attrsTranslationCtx: Attrs | null = null;
|
||||
let on: ASTComponent["on"] = null;
|
||||
for (let attributeName of node.getAttributeNames()) {
|
||||
const value = node.getAttribute(attributeName)!;
|
||||
if (attributeName.startsWith("t-on-")) {
|
||||
on = on || {};
|
||||
on[attributeName.slice(5)] = value;
|
||||
} else if (attributeName.startsWith("t-translation-context-")) {
|
||||
const attrName = attributeName.slice(22);
|
||||
attrsTranslationCtx = attrsTranslationCtx || {};
|
||||
attrsTranslationCtx[attrName] = value;
|
||||
} else {
|
||||
attrs = attrs || {};
|
||||
attrs[attributeName] = value;
|
||||
@@ -811,11 +906,16 @@ function parseTSlot(node: Element, ctx: ParsingContext): AST | null {
|
||||
type: ASTType.TSlot,
|
||||
name,
|
||||
attrs,
|
||||
attrsTranslationCtx,
|
||||
on,
|
||||
defaultContent: parseChildNodes(node, ctx),
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Translation
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
function parseTTranslation(node: Element, ctx: ParsingContext): AST | null {
|
||||
if (node.getAttribute("t-translation") !== "off") {
|
||||
return null;
|
||||
@@ -827,6 +927,23 @@ function parseTTranslation(node: Element, ctx: ParsingContext): AST | null {
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Translation Context
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
function parseTTranslationContext(node: Element, ctx: ParsingContext): AST | null {
|
||||
const translationCtx = node.getAttribute("t-translation-context");
|
||||
if (!translationCtx) {
|
||||
return null;
|
||||
}
|
||||
node.removeAttribute("t-translation-context");
|
||||
return {
|
||||
type: ASTType.TTranslationContext,
|
||||
content: parseNode(node, ctx),
|
||||
translationCtx,
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Portal
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -972,40 +1089,3 @@ function normalizeXML(el: Element) {
|
||||
normalizeTIf(el);
|
||||
normalizeTEscTOut(el);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an XML string into an XML document, throwing errors on parser errors
|
||||
* instead of returning an XML document containing the parseerror.
|
||||
*
|
||||
* @param xml the string to parse
|
||||
* @returns an XML document corresponding to the content of the string
|
||||
*/
|
||||
function parseXML(xml: string): XMLDocument {
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(xml, "text/xml");
|
||||
if (doc.getElementsByTagName("parsererror").length) {
|
||||
let msg = "Invalid XML in template.";
|
||||
const parsererrorText = doc.getElementsByTagName("parsererror")[0].textContent;
|
||||
if (parsererrorText) {
|
||||
msg += "\nThe parser has produced the following error message:\n" + parsererrorText;
|
||||
const re = /\d+/g;
|
||||
const firstMatch = re.exec(parsererrorText);
|
||||
if (firstMatch) {
|
||||
const lineNumber = Number(firstMatch[0]);
|
||||
const line = xml.split("\n")[lineNumber - 1];
|
||||
const secondMatch = re.exec(parsererrorText);
|
||||
if (line && secondMatch) {
|
||||
const columnIndex = Number(secondMatch[0]) - 1;
|
||||
if (line[columnIndex]) {
|
||||
msg +=
|
||||
`\nThe error might be located at xml line ${lineNumber} column ${columnIndex}\n` +
|
||||
`${line}\n${"-".repeat(columnIndex - 1)}^`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new OwlError(msg);
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// This file exports a function that allows compiling templates ahead of time.
|
||||
// It is used by the "compile_owl_template" command registered in the "bin"
|
||||
// section of owl's package.json
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
import { readdir, readFile, stat } from "fs/promises";
|
||||
import path from "path";
|
||||
import "./setup_jsdom";
|
||||
// Owl imports must be made after setting up jsdom in the global namespace
|
||||
import { compile } from "..";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// helpers
|
||||
// -----------------------------------------------------------------------------
|
||||
async function getXmlFiles(paths: string[]): Promise<string[]> {
|
||||
return (
|
||||
await Promise.all(
|
||||
paths.map(async (file) => {
|
||||
const stats = await stat(path.join(file));
|
||||
if (stats.isDirectory()) {
|
||||
return await getXmlFiles(
|
||||
(await readdir(file)).map((fileName) => path.join(file, fileName))
|
||||
);
|
||||
}
|
||||
if (file.endsWith(".xml")) {
|
||||
return file;
|
||||
}
|
||||
return [];
|
||||
})
|
||||
)
|
||||
).flat();
|
||||
}
|
||||
|
||||
// adapted from https://medium.com/@mhagemann/the-ultimate-way-to-slugify-a-url-string-in-javascript-b8e4a0d849e1
|
||||
const a = "·-_,:;";
|
||||
const p = new RegExp(a.split("").join("|"), "g");
|
||||
|
||||
function slugify(str: string) {
|
||||
return str
|
||||
.replace(/\//g, "") // remove /
|
||||
.replace(/\./g, "_") // Replace . with _
|
||||
.replace(p, (c) => "_") // Replace special characters
|
||||
.replace(/&/g, "_and_") // Replace & with ‘and’
|
||||
.replace(/[^\w\-]+/g, ""); // Remove all non-word characters
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// main
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
export async function compileTemplates(paths: string[]) {
|
||||
const files = await getXmlFiles(paths);
|
||||
process.stdout.write(`Processing ${files.length} files`);
|
||||
let xmlStrings = await Promise.all(files.map((file) => readFile(file, "utf8")));
|
||||
|
||||
const templates = [];
|
||||
const errors = [];
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const fileName = files[i];
|
||||
const fileContent = xmlStrings[i];
|
||||
process.stdout.write(`.`);
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(fileContent, "text/xml");
|
||||
for (const template of doc.querySelectorAll("[t-name]")) {
|
||||
const name = template.getAttribute("t-name");
|
||||
if (template.hasAttribute("owl")) {
|
||||
template.removeAttribute("owl");
|
||||
}
|
||||
const fnName = slugify(name!);
|
||||
try {
|
||||
const fn = compile(template).toString().replace("anonymous", fnName);
|
||||
templates.push(`"${name}": ${fn},\n`);
|
||||
} catch (e) {
|
||||
errors.push({ name, fileName, e });
|
||||
}
|
||||
}
|
||||
}
|
||||
process.stdout.write(`\n`);
|
||||
|
||||
for (let { name, fileName, e } of errors) {
|
||||
console.warn(`Error while compiling '${name}' (in file ${fileName})`);
|
||||
console.error(e);
|
||||
}
|
||||
console.log(`${templates.length} templates compiled`);
|
||||
|
||||
return `export const templates = {\n ${templates.join("\n")} \n}`;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import jsdom from "jsdom";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// add global DOM stuff for compiler. Needs to be in a separate file so rollup
|
||||
// doesn't hoist the owl imports above this block of code.
|
||||
// -----------------------------------------------------------------------------
|
||||
var document = new jsdom.JSDOM("", {});
|
||||
var window = document.window;
|
||||
global.document = window.document;
|
||||
global.window = window as unknown as Window & typeof globalThis;
|
||||
global.DOMParser = window.DOMParser;
|
||||
global.Element = window.Element;
|
||||
global.Node = window.Node;
|
||||
@@ -12,5 +12,7 @@ TemplateSet.prototype._compileTemplate = function _compileTemplate(
|
||||
dev: this.dev,
|
||||
translateFn: this.translateFn,
|
||||
translatableAttributes: this.translatableAttributes,
|
||||
customDirectives: this.customDirectives,
|
||||
hasGlobalValues: this.hasGlobalValues,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { version } from "../version";
|
||||
import { Component, ComponentConstructor, Props } from "./component";
|
||||
import { ComponentNode } from "./component_node";
|
||||
import { ComponentNode, saveCurrent } from "./component_node";
|
||||
import { nodeErrorHandlers, handleError } from "./error_handling";
|
||||
import { OwlError } from "../common/owl_error";
|
||||
import { Fiber, RootFiber, MountOptions } from "./fibers";
|
||||
@@ -16,25 +16,19 @@ export interface Env {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface AppConfig<P, E> extends TemplateSetConfig {
|
||||
name?: string;
|
||||
export interface RootConfig<P, E> {
|
||||
props?: P;
|
||||
env?: E;
|
||||
}
|
||||
|
||||
export interface AppConfig<P, E> extends TemplateSetConfig, RootConfig<P, E> {
|
||||
name?: string;
|
||||
test?: boolean;
|
||||
warnIfNoStaticProps?: boolean;
|
||||
}
|
||||
|
||||
let hasBeenLogged = false;
|
||||
|
||||
export const DEV_MSG = () => {
|
||||
const hash = (window as any).owl ? (window as any).owl.__info__.hash : "master";
|
||||
|
||||
return `Owl is running in 'dev' mode.
|
||||
|
||||
This is not suitable for production use.
|
||||
See https://github.com/odoo/owl/blob/${hash}/doc/reference/app.md#configuration for more information.`;
|
||||
};
|
||||
|
||||
const apps = new Set<App>();
|
||||
|
||||
declare global {
|
||||
@@ -49,6 +43,12 @@ declare global {
|
||||
}
|
||||
}
|
||||
|
||||
interface Root<P extends Props, E> {
|
||||
node: ComponentNode<P, E>;
|
||||
mount(target: HTMLElement | ShadowRoot, options?: MountOptions): Promise<Component<P, E>>;
|
||||
destroy(): void;
|
||||
}
|
||||
|
||||
window.__OWL_DEVTOOLS__ ||= { apps, Fiber, RootFiber, toRaw, reactive };
|
||||
|
||||
export class App<
|
||||
@@ -65,6 +65,7 @@ export class App<
|
||||
props: P;
|
||||
env: E;
|
||||
scheduler = new Scheduler();
|
||||
subRoots: Set<ComponentNode> = new Set();
|
||||
root: ComponentNode<P, E> | null = null;
|
||||
warnIfNoStaticProps: boolean;
|
||||
|
||||
@@ -78,7 +79,7 @@ export class App<
|
||||
}
|
||||
this.warnIfNoStaticProps = config.warnIfNoStaticProps || false;
|
||||
if (this.dev && !config.test && !hasBeenLogged) {
|
||||
console.info(DEV_MSG());
|
||||
console.info(`Owl is running in 'dev' mode.`);
|
||||
hasBeenLogged = true;
|
||||
}
|
||||
const env = config.env || {};
|
||||
@@ -91,14 +92,49 @@ export class App<
|
||||
target: HTMLElement | ShadowRoot,
|
||||
options?: MountOptions
|
||||
): Promise<Component<P, E> & InstanceType<T>> {
|
||||
App.validateTarget(target);
|
||||
if (this.dev) {
|
||||
validateProps(this.Root, this.props, { __owl__: { app: this } });
|
||||
const root = this.createRoot(this.Root, { props: this.props });
|
||||
this.root = root.node;
|
||||
this.subRoots.delete(root.node);
|
||||
return root.mount(target, options) as any;
|
||||
}
|
||||
|
||||
createRoot<Props extends object, SubEnv = any>(
|
||||
Root: ComponentConstructor<Props, E>,
|
||||
config: RootConfig<Props, SubEnv> = {}
|
||||
): Root<Props, SubEnv> {
|
||||
const props = config.props || ({} as Props);
|
||||
// hack to make sure the sub root get the sub env if necessary. for owl 3,
|
||||
// would be nice to rethink the initialization process to make sure that
|
||||
// we can create a ComponentNode and give it explicitely the env, instead
|
||||
// of looking it up in the app
|
||||
const env = this.env;
|
||||
if (config.env) {
|
||||
this.env = config.env as any;
|
||||
}
|
||||
const node = this.makeNode(this.Root, this.props);
|
||||
const prom = this.mountNode(node, target, options);
|
||||
this.root = node;
|
||||
return prom;
|
||||
|
||||
const restore = saveCurrent();
|
||||
const node = this.makeNode(Root, props);
|
||||
restore();
|
||||
if (config.env) {
|
||||
this.env = env;
|
||||
}
|
||||
this.subRoots.add(node);
|
||||
return {
|
||||
node,
|
||||
mount: (target: HTMLElement | ShadowRoot, options?: MountOptions) => {
|
||||
App.validateTarget(target);
|
||||
if (this.dev) {
|
||||
validateProps(Root, props, { __owl__: { app: this } });
|
||||
}
|
||||
const prom = this.mountNode(node, target, options);
|
||||
return prom;
|
||||
},
|
||||
destroy: () => {
|
||||
this.subRoots.delete(node);
|
||||
node.destroy();
|
||||
this.scheduler.processTasks();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
makeNode(Component: ComponentConstructor, props: any): ComponentNode {
|
||||
@@ -134,6 +170,9 @@ export class App<
|
||||
|
||||
destroy() {
|
||||
if (this.root) {
|
||||
for (let subroot of this.subRoots) {
|
||||
subroot.destroy();
|
||||
}
|
||||
this.root.destroy();
|
||||
this.scheduler.processTasks();
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export type ComponentConstructor<P extends Props = any, E = any> = (new (
|
||||
|
||||
export class Component<Props = any, Env = any> {
|
||||
static template: string = "";
|
||||
static props?: any;
|
||||
static props?: Schema;
|
||||
static defaultProps?: any;
|
||||
|
||||
props: Props;
|
||||
|
||||
@@ -10,6 +10,13 @@ import { batched, Callback } from "./utils";
|
||||
|
||||
let currentNode: ComponentNode | null = null;
|
||||
|
||||
export function saveCurrent() {
|
||||
let n = currentNode;
|
||||
return () => {
|
||||
currentNode = n;
|
||||
};
|
||||
}
|
||||
|
||||
export function getCurrent(): ComponentNode {
|
||||
if (!currentNode) {
|
||||
throw new OwlError("No active component (a hook function should only be called in 'setup')");
|
||||
|
||||
@@ -30,6 +30,13 @@ export function makeRootFiber(node: ComponentNode): Fiber {
|
||||
fibersInError.delete(current);
|
||||
fibersInError.delete(root);
|
||||
current.appliedToDom = false;
|
||||
if (current instanceof RootFiber) {
|
||||
// it is possible that this fiber is a fiber that crashed while being
|
||||
// mounted, so the mounted list is possibly corrupted. We restore it to
|
||||
// its normal initial state (which is empty list or a list with a mount
|
||||
// fiber.
|
||||
current.mounted = current instanceof MountFiber ? [current] : [];
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
@@ -152,6 +159,7 @@ export class RootFiber extends Fiber {
|
||||
const node = this.node;
|
||||
this.locked = true;
|
||||
let current: Fiber | undefined = undefined;
|
||||
let mountedFibers = this.mounted;
|
||||
try {
|
||||
// Step 1: calling all willPatch lifecycle hooks
|
||||
for (current of this.willPatch) {
|
||||
@@ -173,7 +181,6 @@ export class RootFiber extends Fiber {
|
||||
this.locked = false;
|
||||
|
||||
// Step 4: calling all mounted lifecycle hooks
|
||||
let mountedFibers = this.mounted;
|
||||
while ((current = mountedFibers.pop())) {
|
||||
current = current;
|
||||
if (current.appliedToDom) {
|
||||
@@ -194,6 +201,15 @@ export class RootFiber extends Fiber {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// if mountedFibers is not empty, this means that a crash occured while
|
||||
// calling the mounted hooks of some component. So, there may still be
|
||||
// some component that have been mounted, but for which the mounted hooks
|
||||
// have not been called. Here, we remove the willUnmount hooks for these
|
||||
// specific component to prevent a worse situation (willUnmount being
|
||||
// called even though mounted has not been called)
|
||||
for (let fiber of mountedFibers) {
|
||||
fiber.node.willUnmount = [];
|
||||
}
|
||||
this.locked = false;
|
||||
node.app.handleError({ fiber: current || this, error: e });
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ export { useComponent, useState } from "./component_node";
|
||||
export { status } from "./status";
|
||||
export { reactive, markRaw, toRaw } from "./reactivity";
|
||||
export { useEffect, useEnv, useExternalListener, useRef, useChildSubEnv, useSubEnv } from "./hooks";
|
||||
export { EventBus, whenReady, loadFile, markup } from "./utils";
|
||||
export { batched, EventBus, htmlEscape, whenReady, loadFile, markup } from "./utils";
|
||||
export {
|
||||
onWillStart,
|
||||
onMounted,
|
||||
|
||||
@@ -3,42 +3,50 @@ import { nodeErrorHandlers } from "./error_handling";
|
||||
import { OwlError } from "../common/owl_error";
|
||||
|
||||
const TIMEOUT = Symbol("timeout");
|
||||
const HOOK_TIMEOUT: { [key: string]: number } = {
|
||||
onWillStart: 3000,
|
||||
onWillUpdateProps: 3000,
|
||||
};
|
||||
function wrapError(fn: (...args: any[]) => any, hookName: string) {
|
||||
const error = new OwlError(`The following error occurred in ${hookName}: `) as Error & {
|
||||
const error = new OwlError() as Error & {
|
||||
cause: any;
|
||||
};
|
||||
const timeoutError = new OwlError(`${hookName}'s promise hasn't resolved after 3 seconds`);
|
||||
const timeoutError = new OwlError();
|
||||
const node = getCurrent();
|
||||
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)`;
|
||||
}
|
||||
error.message =
|
||||
cause instanceof Error
|
||||
? `The following error occurred in ${hookName}: "${cause.message}"`
|
||||
: `Something that is not an Error was thrown in ${hookName} (see this Error's "cause" property)`;
|
||||
throw error;
|
||||
};
|
||||
let result;
|
||||
try {
|
||||
const result = fn(...args);
|
||||
if (result instanceof Promise) {
|
||||
if (hookName === "onWillStart" || hookName === "onWillUpdateProps") {
|
||||
const fiber = node.fiber;
|
||||
Promise.race([
|
||||
result.catch(() => {}),
|
||||
new Promise((resolve) => setTimeout(() => resolve(TIMEOUT), 3000)),
|
||||
]).then((res) => {
|
||||
if (res === TIMEOUT && node.fiber === fiber) {
|
||||
console.warn(timeoutError);
|
||||
}
|
||||
});
|
||||
}
|
||||
return result.catch(onError);
|
||||
}
|
||||
return result;
|
||||
result = fn(...args);
|
||||
} catch (cause) {
|
||||
onError(cause);
|
||||
}
|
||||
if (!(result instanceof Promise)) {
|
||||
return result;
|
||||
}
|
||||
const timeout = HOOK_TIMEOUT[hookName];
|
||||
if (timeout) {
|
||||
const fiber = node.fiber;
|
||||
Promise.race([
|
||||
result.catch(() => {}),
|
||||
new Promise((resolve) => setTimeout(() => resolve(TIMEOUT), timeout)),
|
||||
]).then((res) => {
|
||||
if (res === TIMEOUT && node.fiber === fiber && node.status <= 2) {
|
||||
timeoutError.message = `${hookName}'s promise hasn't resolved after ${
|
||||
timeout / 1000
|
||||
} seconds`;
|
||||
console.log(timeoutError);
|
||||
}
|
||||
});
|
||||
}
|
||||
return result.catch(onError);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ export class Portal extends Component {
|
||||
type: String,
|
||||
},
|
||||
slots: true,
|
||||
};
|
||||
} as const;
|
||||
|
||||
setup() {
|
||||
const node: any = this.__owl__;
|
||||
|
||||
@@ -20,8 +20,9 @@ type CollectionRawType = "Set" | "Map" | "WeakMap";
|
||||
const objectToString = Object.prototype.toString;
|
||||
const objectHasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
|
||||
const SUPPORTED_RAW_TYPES = new Set(["Object", "Array", "Set", "Map", "WeakMap"]);
|
||||
const COLLECTION_RAWTYPES = new Set(["Set", "Map", "WeakMap"]);
|
||||
// Use arrays because Array.includes is faster than Set.has for small arrays
|
||||
const SUPPORTED_RAW_TYPES = ["Object", "Array", "Set", "Map", "WeakMap"];
|
||||
const COLLECTION_RAW_TYPES = ["Set", "Map", "WeakMap"];
|
||||
|
||||
/**
|
||||
* extract "RawType" from strings like "[object RawType]" => this lets us ignore
|
||||
@@ -45,7 +46,7 @@ function canBeMadeReactive(value: any): boolean {
|
||||
if (typeof value !== "object") {
|
||||
return false;
|
||||
}
|
||||
return SUPPORTED_RAW_TYPES.has(rawType(value));
|
||||
return SUPPORTED_RAW_TYPES.includes(rawType(value));
|
||||
}
|
||||
/**
|
||||
* Creates a reactive from the given object/callback if possible and returns it,
|
||||
@@ -220,7 +221,7 @@ export function reactive<T extends Target>(target: T, callback: Callback = NO_CA
|
||||
const reactivesForTarget = reactiveCache.get(target)!;
|
||||
if (!reactivesForTarget.has(callback)) {
|
||||
const targetRawType = rawType(target);
|
||||
const handler = COLLECTION_RAWTYPES.has(targetRawType)
|
||||
const handler = COLLECTION_RAW_TYPES.includes(targetRawType)
|
||||
? collectionsProxyHandler(target as Collection, callback, targetRawType as CollectionRawType)
|
||||
: basicProxyHandler<T>(callback);
|
||||
const proxy = new Proxy(target, handler as ProxyHandler<T>) as Reactive<T>;
|
||||
|
||||
@@ -16,6 +16,7 @@ export class Scheduler {
|
||||
frame: number = 0;
|
||||
delayedRenders: Fiber[] = [];
|
||||
cancelledNodes: Set<ComponentNode> = new Set();
|
||||
processing = false;
|
||||
|
||||
constructor() {
|
||||
this.requestAnimationFrame = Scheduler.requestAnimationFrame;
|
||||
@@ -53,6 +54,10 @@ export class Scheduler {
|
||||
}
|
||||
|
||||
processTasks() {
|
||||
if (this.processing) {
|
||||
return;
|
||||
}
|
||||
this.processing = true;
|
||||
this.frame = 0;
|
||||
for (let node of this.cancelledNodes) {
|
||||
node._destroy();
|
||||
@@ -66,6 +71,7 @@ export class Scheduler {
|
||||
this.tasks.delete(task);
|
||||
}
|
||||
}
|
||||
this.processing = false;
|
||||
}
|
||||
|
||||
processFiber(fiber: RootFiber) {
|
||||
@@ -87,7 +93,14 @@ export class Scheduler {
|
||||
if (!hasError) {
|
||||
fiber.complete();
|
||||
}
|
||||
this.tasks.delete(fiber);
|
||||
// at this point, the fiber should have been applied to the DOM, so we can
|
||||
// remove it from the task list. If it is not the case, it means that there
|
||||
// was an error and an error handler triggered a new rendering that recycled
|
||||
// the fiber, so in that case, we actually want to keep the fiber around,
|
||||
// otherwise it will just be ignored.
|
||||
if (fiber.appliedToDom) {
|
||||
this.tasks.delete(fiber);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,44 +4,19 @@ import { getCurrent } from "./component_node";
|
||||
import { Portal, portalTemplate } from "./portal";
|
||||
import { helpers } from "./template_helpers";
|
||||
import { OwlError } from "../common/owl_error";
|
||||
import { parseXML } from "../common/utils";
|
||||
import type { customDirectives } from "../common/types";
|
||||
|
||||
const bdom = { text, createBlock, list, multi, html, toggler, comment };
|
||||
|
||||
function parseXML(xml: string): Document {
|
||||
const parser = new DOMParser();
|
||||
|
||||
const doc = parser.parseFromString(xml, "text/xml");
|
||||
if (doc.getElementsByTagName("parsererror").length) {
|
||||
let msg = "Invalid XML in template.";
|
||||
const parsererrorText = doc.getElementsByTagName("parsererror")[0].textContent;
|
||||
if (parsererrorText) {
|
||||
msg += "\nThe parser has produced the following error message:\n" + parsererrorText;
|
||||
const re = /\d+/g;
|
||||
const firstMatch = re.exec(parsererrorText);
|
||||
if (firstMatch) {
|
||||
const lineNumber = Number(firstMatch[0]);
|
||||
const line = xml.split("\n")[lineNumber - 1];
|
||||
const secondMatch = re.exec(parsererrorText);
|
||||
if (line && secondMatch) {
|
||||
const columnIndex = Number(secondMatch[0]) - 1;
|
||||
if (line[columnIndex]) {
|
||||
msg +=
|
||||
`\nThe error might be located at xml line ${lineNumber} column ${columnIndex}\n` +
|
||||
`${line}\n${"-".repeat(columnIndex - 1)}^`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new OwlError(msg);
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
export interface TemplateSetConfig {
|
||||
dev?: boolean;
|
||||
translatableAttributes?: string[];
|
||||
translateFn?: (s: string) => string;
|
||||
templates?: string | Document;
|
||||
translateFn?: (s: string, translationCtx: string) => string;
|
||||
templates?: string | Document | Record<string, string>;
|
||||
getTemplate?: (s: string) => Element | Function | string | void;
|
||||
customDirectives?: customDirectives;
|
||||
globalValues?: object;
|
||||
}
|
||||
|
||||
export class TemplateSet {
|
||||
@@ -51,17 +26,31 @@ export class TemplateSet {
|
||||
dev: boolean;
|
||||
rawTemplates: typeof globalTemplates = Object.create(globalTemplates);
|
||||
templates: { [name: string]: Template } = {};
|
||||
translateFn?: (s: string) => string;
|
||||
getRawTemplate?: (s: string) => Element | Function | string | void;
|
||||
translateFn?: (s: string, translationCtx: string) => string;
|
||||
translatableAttributes?: string[];
|
||||
Portal = Portal;
|
||||
customDirectives: customDirectives;
|
||||
runtimeUtils: object;
|
||||
hasGlobalValues: boolean;
|
||||
|
||||
constructor(config: TemplateSetConfig = {}) {
|
||||
this.dev = config.dev || false;
|
||||
this.translateFn = config.translateFn;
|
||||
this.translatableAttributes = config.translatableAttributes;
|
||||
if (config.templates) {
|
||||
this.addTemplates(config.templates);
|
||||
if (config.templates instanceof Document || typeof config.templates === "string") {
|
||||
this.addTemplates(config.templates);
|
||||
} else {
|
||||
for (const name in config.templates) {
|
||||
this.addTemplate(name, config.templates[name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.getRawTemplate = config.getTemplate;
|
||||
this.customDirectives = config.customDirectives || {};
|
||||
this.runtimeUtils = { ...helpers, __globals__: config.globalValues || {} };
|
||||
this.hasGlobalValues = Boolean(config.globalValues && Object.keys(config.globalValues).length);
|
||||
}
|
||||
|
||||
addTemplate(name: string, template: string | Element) {
|
||||
@@ -100,7 +89,7 @@ export class TemplateSet {
|
||||
|
||||
getTemplate(name: string): Template {
|
||||
if (!(name in this.templates)) {
|
||||
const rawTemplate = this.rawTemplates[name];
|
||||
const rawTemplate = this.getRawTemplate?.(name) || this.rawTemplates[name];
|
||||
if (rawTemplate === undefined) {
|
||||
let extraInfo = "";
|
||||
try {
|
||||
@@ -117,7 +106,7 @@ export class TemplateSet {
|
||||
this.templates[name] = function (context, parent) {
|
||||
return templates[name].call(this, context, parent);
|
||||
};
|
||||
const template = templateFn(this, bdom, helpers);
|
||||
const template = templateFn(this, bdom, this.runtimeUtils);
|
||||
this.templates[name] = template;
|
||||
}
|
||||
return this.templates[name];
|
||||
|
||||
@@ -35,13 +35,43 @@ export function inOwnerDocument(el?: HTMLElement) {
|
||||
return rootNode instanceof ShadowRoot && el.ownerDocument.contains(rootNode.host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given element is contained in a specific root documnet:
|
||||
* either directly or with a shadow root in between or in an iframe.
|
||||
*/
|
||||
function isAttachedToDocument(
|
||||
element: HTMLElement | ShadowRoot,
|
||||
documentElement: Document
|
||||
): boolean {
|
||||
let current: Node = element;
|
||||
const shadowRoot = documentElement.defaultView!.ShadowRoot;
|
||||
while (current) {
|
||||
if (current === documentElement) {
|
||||
return true;
|
||||
}
|
||||
if (current.parentNode) {
|
||||
current = current.parentNode;
|
||||
} else if (current instanceof shadowRoot && current.host) {
|
||||
current = current.host;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function validateTarget(target: HTMLElement | ShadowRoot) {
|
||||
// Get the document and HTMLElement corresponding to the target to allow mounting in iframes
|
||||
const document = target && target.ownerDocument;
|
||||
if (document) {
|
||||
const HTMLElement = document.defaultView!.HTMLElement;
|
||||
if (!document.defaultView) {
|
||||
throw new OwlError(
|
||||
"Cannot mount a component: the target document is not attached to a window (defaultView is missing)"
|
||||
);
|
||||
}
|
||||
const HTMLElement = document.defaultView.HTMLElement;
|
||||
if (target instanceof HTMLElement || target instanceof ShadowRoot) {
|
||||
if (!document.body.contains(target instanceof HTMLElement ? target : target.host)) {
|
||||
if (!isAttachedToDocument(target, document)) {
|
||||
throw new OwlError("Cannot mount a component on a detached dom node");
|
||||
}
|
||||
return;
|
||||
@@ -81,10 +111,50 @@ export async function loadFile(url: string): Promise<string> {
|
||||
*/
|
||||
export class Markup extends String {}
|
||||
|
||||
export function htmlEscape(str: any): Markup {
|
||||
if (str instanceof Markup) {
|
||||
return str;
|
||||
}
|
||||
if (str === undefined) {
|
||||
return markup("");
|
||||
}
|
||||
if (typeof str === "number") {
|
||||
return markup(String(str));
|
||||
}
|
||||
[
|
||||
["&", "&"],
|
||||
["<", "<"],
|
||||
[">", ">"],
|
||||
["'", "'"],
|
||||
['"', """],
|
||||
["`", "`"],
|
||||
].forEach((pairs) => {
|
||||
str = String(str).replace(new RegExp(pairs[0], "g"), pairs[1]);
|
||||
});
|
||||
return markup(str);
|
||||
}
|
||||
|
||||
/*
|
||||
* Marks a value as safe, that is, a value that can be injected as HTML directly.
|
||||
* It should be used to wrap the value passed to a t-out directive to allow a raw rendering.
|
||||
*
|
||||
* If called as a tag function, the interpolated strings are escaped.
|
||||
*/
|
||||
export function markup(value: any) {
|
||||
return new Markup(value);
|
||||
export function markup(strings: TemplateStringsArray, ...placeholders: unknown[]): Markup;
|
||||
export function markup(value: string): Markup;
|
||||
export function markup(
|
||||
valueOrStrings: string | TemplateStringsArray,
|
||||
...placeholders: unknown[]
|
||||
): Markup {
|
||||
if (!Array.isArray(valueOrStrings)) {
|
||||
return new Markup(valueOrStrings);
|
||||
}
|
||||
const strings = valueOrStrings;
|
||||
let acc = "";
|
||||
let i = 0;
|
||||
for (; i < placeholders.length; ++i) {
|
||||
acc += strings[i] + htmlEscape(placeholders[i]);
|
||||
}
|
||||
acc += strings[i];
|
||||
return new Markup(acc);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
import { OwlError } from "../common/owl_error";
|
||||
import { toRaw } from "./reactivity";
|
||||
|
||||
type BaseType =
|
||||
| typeof String
|
||||
| typeof Boolean
|
||||
| typeof Number
|
||||
| typeof Date
|
||||
| typeof Object
|
||||
| typeof Array
|
||||
| true
|
||||
| "*";
|
||||
type BaseType = { new (...args: any[]): any } | true | "*";
|
||||
|
||||
interface TypeInfo {
|
||||
type?: TypeDescription;
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
// do not modify manually. This file is generated by the release script.
|
||||
export const version = "2.2.6";
|
||||
export const version = "2.8.0";
|
||||
|
||||
@@ -43,6 +43,48 @@ exports[`app app: clear scheduler tasks and destroy cancelled nodes immediately
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`app can add functions to the bdom 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { __globals__ } = helpers;
|
||||
|
||||
let block1 = createBlock(\`<div class=\\"my-div\\" block-handler-0=\\"click\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let hdlr1 = [()=>__globals__.plop('click'), ctx];
|
||||
return block1([hdlr1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`app can call processTask twice in a row without crashing 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b2 = text(\`parent\`);
|
||||
const b3 = comp1({}, key + \`__1\`, node, this, null);
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`app can call processTask twice in a row without crashing 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`app can configure an app with props 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -57,6 +99,19 @@ exports[`app can configure an app with props 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`app can load templates from an object name-string 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div class=\\"hello\\">hello</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`app can mount app in an iframe 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`destroy a subroot while another component is mounted in main app 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`ChildB\`, true, false, false, []);
|
||||
const comp2 = app.createComponent(\`ChildA\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let b2, b3;
|
||||
if (ctx['state'].flag) {
|
||||
b2 = comp1({}, key + \`__1\`, node, this, null);
|
||||
} else {
|
||||
b3 = comp2({}, key + \`__2\`, node, this, null);
|
||||
}
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`destroy a subroot while another component is mounted in main app 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block3 = createBlock(\`<div block-ref=\\"0\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b2 = text(\`a\`);
|
||||
let ref1 = (el) => this.__owl__.setRef((\`elem\`), el);
|
||||
const b3 = block3([ref1]);
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`destroy a subroot while another component is mounted in main app 3`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`c\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`destroy a subroot while another component is mounted in main app 4`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`b\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot by default, env is the same in sub root 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>main app</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot by default, env is the same in sub root 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>sub root</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot can create a root in a setup function, then use a hook 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`a\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot can create a root in a setup function, then use a hook 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`c\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot can mount subroot 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>main app</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot can mount subroot 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>sub root</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot can mount subroot inside own dom 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>main app</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot can mount subroot inside own dom 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>sub root</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot env can be specified for sub roots 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>main app</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot env can be specified for sub roots 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>sub root</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot subcomponents can be destroyed, and it properly cleanup the subroots 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>main app</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`subroot subcomponents can be destroyed, and it properly cleanup the subroots 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>sub root</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -1,4 +1,4 @@
|
||||
import { App, Component, mount, onWillStart, useState, xml } from "../../src";
|
||||
import { App, Component, mount, onWillPatch, onWillStart, useState, xml } from "../../src";
|
||||
import { status } from "../../src/runtime/status";
|
||||
import {
|
||||
makeTestFixture,
|
||||
@@ -167,4 +167,56 @@ describe("app", () => {
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("can load templates from an object name-string", async () => {
|
||||
const templates = {
|
||||
hello: `<div class="hello">hello</div>`,
|
||||
world: `<div>world</div>`,
|
||||
};
|
||||
class SomeComponent extends Component {
|
||||
static template = "hello";
|
||||
}
|
||||
|
||||
const app = new App(SomeComponent, { templates });
|
||||
await app.mount(fixture);
|
||||
expect(fixture.querySelector(".hello")).toBeDefined();
|
||||
// Only the "hello" template is used, so the "world" template is not yet loaded
|
||||
expect(Object.keys(app.templates)).toEqual(["hello"]);
|
||||
expect(Object.keys(app.rawTemplates)).toEqual(["hello", "world"]);
|
||||
});
|
||||
|
||||
test("can call processTask twice in a row without crashing", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<div/>`;
|
||||
setup() {
|
||||
onWillPatch(() => app.scheduler.processTasks());
|
||||
}
|
||||
}
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`parent<Child/>`;
|
||||
static components = { Child };
|
||||
}
|
||||
|
||||
const app = new App(SomeComponent);
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("parent<div></div>");
|
||||
});
|
||||
|
||||
test("can add functions to the bdom", async () => {
|
||||
const steps: string[] = [];
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div t-on-click="() => __globals__.plop('click')" class="my-div"/>`;
|
||||
}
|
||||
const app = new App(SomeComponent, {
|
||||
globalValues: {
|
||||
plop: (string: any) => {
|
||||
steps.push(string);
|
||||
},
|
||||
},
|
||||
});
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
||||
fixture.querySelector("div")!.click();
|
||||
expect(steps).toEqual(["click"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
import { App, Component, onMounted, onWillDestroy, useRef, useState, xml } from "../../src";
|
||||
import { status } from "../../src/runtime/status";
|
||||
import { makeTestFixture, nextTick, snapshotEverything } from "../helpers";
|
||||
|
||||
let fixture: HTMLElement;
|
||||
|
||||
snapshotEverything();
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = makeTestFixture();
|
||||
});
|
||||
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div>main app</div>`;
|
||||
}
|
||||
|
||||
class SubComponent extends Component {
|
||||
static template = xml`<div>sub root</div>`;
|
||||
}
|
||||
|
||||
describe("subroot", () => {
|
||||
test("can mount subroot", async () => {
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div>main app</div>");
|
||||
const subRoot = app.createRoot(SubComponent);
|
||||
const subcomp = await subRoot.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div>main app</div><div>sub root</div>");
|
||||
|
||||
app.destroy();
|
||||
expect(fixture.innerHTML).toBe("");
|
||||
expect(status(comp)).toBe("destroyed");
|
||||
expect(status(subcomp)).toBe("destroyed");
|
||||
});
|
||||
|
||||
test("can mount subroot inside own dom", async () => {
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div>main app</div>");
|
||||
const subRoot = app.createRoot(SubComponent);
|
||||
const subcomp = await subRoot.mount(fixture.querySelector("div")!);
|
||||
expect(fixture.innerHTML).toBe("<div>main app<div>sub root</div></div>");
|
||||
|
||||
app.destroy();
|
||||
expect(fixture.innerHTML).toBe("");
|
||||
expect(status(comp)).toBe("destroyed");
|
||||
expect(status(subcomp)).toBe("destroyed");
|
||||
});
|
||||
|
||||
test("by default, env is the same in sub root", async () => {
|
||||
let env, subenv;
|
||||
class SC extends SomeComponent {
|
||||
setup() {
|
||||
env = this.env;
|
||||
}
|
||||
}
|
||||
class Sub extends SubComponent {
|
||||
setup() {
|
||||
subenv = this.env;
|
||||
}
|
||||
}
|
||||
|
||||
const app = new App(SC);
|
||||
await app.mount(fixture);
|
||||
const subRoot = app.createRoot(Sub);
|
||||
await subRoot.mount(fixture);
|
||||
|
||||
expect(env).toBeDefined();
|
||||
expect(subenv).toBeDefined();
|
||||
expect(env).toBe(subenv);
|
||||
});
|
||||
|
||||
test("env can be specified for sub roots", async () => {
|
||||
const env1 = { env1: true };
|
||||
const env2 = {};
|
||||
let someComponentEnv: any, subComponentEnv: any;
|
||||
class SC extends SomeComponent {
|
||||
setup() {
|
||||
someComponentEnv = this.env;
|
||||
}
|
||||
}
|
||||
class Sub extends SubComponent {
|
||||
setup() {
|
||||
subComponentEnv = this.env;
|
||||
}
|
||||
}
|
||||
|
||||
const app = new App(SC, { env: env1 });
|
||||
await app.mount(fixture);
|
||||
const subRoot = app.createRoot(Sub, { env: env2 });
|
||||
await subRoot.mount(fixture);
|
||||
|
||||
// because env is different in app => it is given a sub object, frozen and all
|
||||
// not sure it is a good idea, but it's the way owl 2 works. maybe we should
|
||||
// avoid doing anything with the main env and let user code do it if they
|
||||
// want. in that case, we can change the test here to assert that they are equal
|
||||
expect(someComponentEnv).not.toBe(env1);
|
||||
expect(someComponentEnv!.env1).toBe(true);
|
||||
expect(subComponentEnv).toBe(env2);
|
||||
});
|
||||
|
||||
test("subcomponents can be destroyed, and it properly cleanup the subroots", async () => {
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div>main app</div>");
|
||||
const root = app.createRoot(SubComponent);
|
||||
const subcomp = await root.mount(fixture.querySelector("div")!);
|
||||
expect(fixture.innerHTML).toBe("<div>main app<div>sub root</div></div>");
|
||||
|
||||
root.destroy();
|
||||
expect(fixture.innerHTML).toBe("<div>main app</div>");
|
||||
expect(status(comp)).not.toBe("destroyed");
|
||||
expect(status(subcomp)).toBe("destroyed");
|
||||
});
|
||||
|
||||
test("can create a root in a setup function, then use a hook", async () => {
|
||||
class C extends Component {
|
||||
static template = xml`c`;
|
||||
}
|
||||
|
||||
class A extends Component {
|
||||
static template = xml`a`;
|
||||
state: any;
|
||||
setup() {
|
||||
app.createRoot(C);
|
||||
this.state = useState({ value: 1 });
|
||||
}
|
||||
}
|
||||
|
||||
const app = new App(A);
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("a");
|
||||
});
|
||||
});
|
||||
|
||||
test("destroy a subroot while another component is mounted in main app", async () => {
|
||||
class C extends Component {
|
||||
static template = xml`c`;
|
||||
}
|
||||
|
||||
class ChildA extends Component {
|
||||
static template = xml`a<div t-ref="elem"></div>`;
|
||||
ref: any;
|
||||
setup() {
|
||||
this.ref = useRef("elem");
|
||||
let root = app.createRoot(C);
|
||||
onMounted(() => {
|
||||
root.mount(this.ref.el);
|
||||
});
|
||||
onWillDestroy(() => {
|
||||
root.destroy();
|
||||
});
|
||||
}
|
||||
}
|
||||
class ChildB extends Component {
|
||||
static template = xml`b`;
|
||||
}
|
||||
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
<t t-if="state.flag"><ChildB/></t>
|
||||
<t t-else=""><ChildA/></t>
|
||||
`;
|
||||
static components = { ChildA, ChildB };
|
||||
state = useState({ flag: false });
|
||||
}
|
||||
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("a<div></div>");
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("a<div>c</div>");
|
||||
comp.state.flag = true;
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("b");
|
||||
});
|
||||
@@ -1,5 +1,38 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`comments comment node with backslash at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return comment(\` \\\\\\\\ \`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`comments comment node with backtick at top-level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return comment(\` \\\\\` \`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`comments comment node with interpolation sigil at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return comment(\` \\\\\${very cool} \`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`comments only a comment 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -341,6 +341,39 @@ exports[`simple templates, mostly static template with t tag with multiple conte
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`simple templates, mostly static text node with backslash at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`\\\\\\\\\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`simple templates, mostly static text node with backtick at top-level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`\\\\\`\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`simple templates, mostly static text node with interpolation sigil at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`\\\\\${very cool}\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`simple templates, mostly static two t-escs next to each other 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`t-custom can use t-custom directive on a node 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div class=\\"my-div\\" block-handler-0=\\"click\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let hdlr1 = [ctx['click'], ctx];
|
||||
return block1([hdlr1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-custom can use t-custom directive with modifiers on a node 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div class=\\"my-div\\" block-handler-0=\\"click\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let hdlr1 = [ctx['click'], ctx];
|
||||
return block1([hdlr1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -1,5 +1,41 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`t-esc default with backslash at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { withDefault } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(withDefault(undefined, \`\\\\\\\\\`));
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-esc default with backtick at top-level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { withDefault } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(withDefault(undefined, \`\\\\\`\`));
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-esc default with interpolation sigil at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { withDefault } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(withDefault(undefined, \`\\\\\${very cool}\`));
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-esc div with falsy values 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -1,5 +1,50 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`t-set body with backslash at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { isBoundary, withDefault, setContextValue } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
ctx = Object.create(ctx);
|
||||
ctx[isBoundary] = 1
|
||||
setContextValue(ctx, \\"value\\", \`\\\\\\\\\`);
|
||||
return text(ctx['value']);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-set body with backtick at top-level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { isBoundary, withDefault, setContextValue } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
ctx = Object.create(ctx);
|
||||
ctx[isBoundary] = 1
|
||||
setContextValue(ctx, \\"value\\", \`\\\\\`\`);
|
||||
return text(ctx['value']);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-set body with interpolation sigil at top level 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { isBoundary, withDefault, setContextValue } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
ctx = Object.create(ctx);
|
||||
ctx[isBoundary] = 1
|
||||
setContextValue(ctx, \\"value\\", \`\\\\\${very cool}\`);
|
||||
return text(ctx['value']);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-set evaluate value expression 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`t-slot compile t-props correctly multiple time 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { callSlot } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return callSlot(ctx, node, key, 'default', false, Object.assign({}, {a:1}));
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -101,3 +101,55 @@ exports[`loading templates can load a few templates from an XMLDocument 2`] = `
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`loading templates getTemplate: element returned (2) 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>Hello World!</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`loading templates getTemplate: element returned 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>Hello World!</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`loading templates getTemplate: template string returned 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>Hello World!</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`loading templates getTemplate: undefined returned 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>Hello World!</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
@@ -1,5 +1,128 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`translation context body of t-sets are translated in context 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { isBoundary, withDefault, setContextValue } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
ctx = Object.create(ctx);
|
||||
ctx[isBoundary] = 1
|
||||
setContextValue(ctx, \\"label\\", \`traduit\`);
|
||||
const b2 = text(ctx['label']);
|
||||
return multi([b2]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context default slot params and content translated in context 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { callSlot } = helpers;
|
||||
|
||||
let block1 = createBlock(\`<div><block-child-0/></div>\`);
|
||||
|
||||
function defaultContent1(ctx, node, key = \\"\\") {
|
||||
return text(\` foo \`);
|
||||
}
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b3 = callSlot(ctx, node, key, 'default', false, {param: \`param\`,title: \`título\`}, defaultContent1.bind(this));
|
||||
return block1([], [b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context props with modifier .translate are translated in context 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`ChildComponent\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return comp1({text: \`jeu\`}, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context props with modifier .translate are translated in context 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<span><block-text-0/></span>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let txt1 = ctx['props'].text;
|
||||
return block1([txt1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context slot attrs and text contents are translated in context 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { capture, markRaw } = helpers;
|
||||
const comp1 = app.createComponent(\`ChildComponent\`, true, true, false, []);
|
||||
|
||||
function slot1(ctx, node, key = \\"\\") {
|
||||
return text(\`jeu\`);
|
||||
}
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const ctx1 = capture(ctx);
|
||||
return comp1({slots: markRaw({'a': {__render: slot1.bind(this), __ctx: ctx1, title: \`título\`}})}, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context slot attrs and text contents are translated in context 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { callSlot } = helpers;
|
||||
|
||||
let block1 = createBlock(\`<div><block-child-0/></div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b2 = callSlot(ctx, node, key, 'a', false, {});
|
||||
return block1([], [b2]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context translation of attributes in context 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div title=\\"titre\\" label=\\"game\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation context translation of text in context 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block2 = createBlock(\`<div>word</div>\`);
|
||||
let block3 = createBlock(\`<div>mot</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b2 = block2();
|
||||
const b3 = block3();
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation support body of t-sets are translated 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -26,4 +26,19 @@ describe("comments", () => {
|
||||
</div>`;
|
||||
expect(renderToString(template)).toBe("<div><span>true</span></div>");
|
||||
});
|
||||
|
||||
test("comment node with backslash at top level", () => {
|
||||
const template = "<!-- \\ -->";
|
||||
expect(renderToString(template)).toBe("<!-- \\ -->");
|
||||
});
|
||||
|
||||
test("comment node with backtick at top-level", () => {
|
||||
const template = "<!-- ` -->";
|
||||
expect(renderToString(template)).toBe("<!-- ` -->");
|
||||
});
|
||||
|
||||
test("comment node with interpolation sigil at top level", () => {
|
||||
const template = "<!-- ${very cool} -->";
|
||||
expect(renderToString(template)).toBe("<!-- ${very cool} -->");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -174,6 +174,9 @@ describe("expression evaluation", () => {
|
||||
expect(compileExpr("list.data.map((data) => data)")).toBe(
|
||||
"ctx['list'].data.map((_data)=>_data)"
|
||||
);
|
||||
expect(compileExpr("(ev) => { myFunc(v1, v2, ev.target.value); }")).toBe(
|
||||
"(_ev)=>{ctx['myFunc'](ctx['v1'],ctx['v2'],_ev.target.value);}"
|
||||
);
|
||||
});
|
||||
test.skip("arrow functions: not yet supported", () => {
|
||||
// e is added to localvars in inline_expression but not removed after the arrow func body
|
||||
|
||||
@@ -43,6 +43,7 @@ describe("qweb parser", () => {
|
||||
dynamicTag: null,
|
||||
content: [],
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -70,6 +71,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -84,6 +86,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -98,6 +101,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -109,6 +113,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -128,6 +133,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -139,6 +145,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -156,6 +163,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -181,6 +189,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -201,6 +210,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -223,6 +233,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -246,6 +257,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -262,6 +274,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: { class: "abc" },
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -280,6 +293,7 @@ describe("qweb parser", () => {
|
||||
height: "90px",
|
||||
width: "100px",
|
||||
},
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
attrs: {
|
||||
@@ -290,6 +304,7 @@ describe("qweb parser", () => {
|
||||
stroke: "green",
|
||||
"stroke-width": "1",
|
||||
},
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
@@ -312,6 +327,7 @@ describe("qweb parser", () => {
|
||||
parse(`<g><circle cx="50" cy="50" r="4" stroke="green" stroke-width="1" fill="yellow"/></g>`)
|
||||
).toEqual({
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
attrs: {
|
||||
@@ -322,6 +338,7 @@ describe("qweb parser", () => {
|
||||
stroke: "green",
|
||||
"stroke-width": "1",
|
||||
},
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
@@ -348,6 +365,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
content: [
|
||||
@@ -356,6 +374,7 @@ describe("qweb parser", () => {
|
||||
tag: "pre",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
content: [],
|
||||
@@ -391,6 +410,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -413,6 +433,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -455,6 +476,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -469,6 +491,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -489,6 +512,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -530,6 +554,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -607,6 +632,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -626,6 +652,7 @@ describe("qweb parser", () => {
|
||||
tag: "h1",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -639,6 +666,7 @@ describe("qweb parser", () => {
|
||||
tag: "h2",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -685,6 +713,7 @@ describe("qweb parser", () => {
|
||||
{
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
@@ -705,6 +734,7 @@ describe("qweb parser", () => {
|
||||
{
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
@@ -742,6 +772,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -811,6 +842,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -853,6 +885,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -887,6 +920,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -920,6 +954,7 @@ describe("qweb parser", () => {
|
||||
"t-att-selected": "category.id==options.active_category_id",
|
||||
"t-att-value": "category.id",
|
||||
},
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -940,6 +975,7 @@ describe("qweb parser", () => {
|
||||
).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -987,6 +1023,7 @@ describe("qweb parser", () => {
|
||||
ref: null,
|
||||
model: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
ns: null,
|
||||
content: [{ type: ASTType.TEsc, expr: "item", defaultValue: "" }],
|
||||
},
|
||||
@@ -1010,6 +1047,7 @@ describe("qweb parser", () => {
|
||||
name: "Comp",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
slots: null,
|
||||
on: null,
|
||||
},
|
||||
@@ -1099,6 +1137,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -1139,6 +1178,7 @@ describe("qweb parser", () => {
|
||||
tag: "button",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: { click: "add" },
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -1175,6 +1215,7 @@ describe("qweb parser", () => {
|
||||
tag: "select",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
content: [
|
||||
@@ -1183,6 +1224,7 @@ describe("qweb parser", () => {
|
||||
tag: "option",
|
||||
dynamicTag: null,
|
||||
attrs: { value: "1" },
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
content: [],
|
||||
@@ -1212,6 +1254,7 @@ describe("qweb parser", () => {
|
||||
tag: "select",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
content: [
|
||||
@@ -1220,6 +1263,7 @@ describe("qweb parser", () => {
|
||||
tag: "option",
|
||||
dynamicTag: null,
|
||||
attrs: { "t-att-value": "valueVar" },
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
content: [],
|
||||
@@ -1251,6 +1295,7 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: null,
|
||||
isDynamic: false,
|
||||
@@ -1263,6 +1308,7 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: { a: "1", b: "'b'" },
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: null,
|
||||
@@ -1275,6 +1321,7 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: "state",
|
||||
props: { a: "1" },
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: null,
|
||||
@@ -1287,6 +1334,7 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: { click: "someMethod" },
|
||||
slots: null,
|
||||
@@ -1329,12 +1377,14 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
@@ -1350,12 +1400,14 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
attrs: { param: "param" },
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
@@ -1370,6 +1422,7 @@ describe("qweb parser", () => {
|
||||
isDynamic: false,
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
@@ -1381,6 +1434,7 @@ describe("qweb parser", () => {
|
||||
tag: "span",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -1392,6 +1446,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -1401,6 +1456,7 @@ describe("qweb parser", () => {
|
||||
],
|
||||
},
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
@@ -1415,9 +1471,11 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
on: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
slots: {
|
||||
mySlot: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
@@ -1434,9 +1492,16 @@ describe("qweb parser", () => {
|
||||
isDynamic: false,
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: {
|
||||
name: { content: { type: ASTType.Text, value: "foo" }, attrs: null, on: null, scope: null },
|
||||
name: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -1448,11 +1513,13 @@ describe("qweb parser", () => {
|
||||
isDynamic: false,
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: {
|
||||
name: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
attrs: { param: "param" },
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
@@ -1469,12 +1536,14 @@ describe("qweb parser", () => {
|
||||
isDynamic: false,
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: {
|
||||
name: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
on: { click: "doStuff" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
scope: null,
|
||||
},
|
||||
},
|
||||
@@ -1493,16 +1562,24 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
content: { type: ASTType.Text, value: " " },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
name: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
name: { content: { type: ASTType.Text, value: "foo" }, attrs: null, on: null, scope: null },
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -1518,11 +1595,24 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
a: { content: { type: ASTType.Text, value: "foo" }, attrs: null, on: null, scope: null },
|
||||
b: { content: { type: ASTType.Text, value: "bar" }, attrs: null, on: null, scope: null },
|
||||
a: {
|
||||
content: { type: ASTType.Text, value: "foo" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
b: {
|
||||
content: { type: ASTType.Text, value: "bar" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -1533,6 +1623,7 @@ describe("qweb parser", () => {
|
||||
name: "myComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: true,
|
||||
on: null,
|
||||
slots: null,
|
||||
@@ -1545,6 +1636,7 @@ describe("qweb parser", () => {
|
||||
name: "mycomponent",
|
||||
dynamicProps: null,
|
||||
props: { a: "1", b: "'b'" },
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: true,
|
||||
on: null,
|
||||
slots: null,
|
||||
@@ -1557,6 +1649,7 @@ describe("qweb parser", () => {
|
||||
name: "mycomponent",
|
||||
dynamicProps: "state",
|
||||
props: { a: "1" },
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: true,
|
||||
on: null,
|
||||
slots: null,
|
||||
@@ -1587,12 +1680,14 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
content: { body: null, name: "subTemplate", type: ASTType.TCall, context: null },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
scope: null,
|
||||
on: null,
|
||||
},
|
||||
@@ -1613,11 +1708,13 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
content: {
|
||||
@@ -1626,11 +1723,13 @@ describe("qweb parser", () => {
|
||||
name: "Child",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: {
|
||||
brol: {
|
||||
content: { type: ASTType.Text, value: "coucou" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
scope: null,
|
||||
on: null,
|
||||
},
|
||||
@@ -1654,11 +1753,13 @@ describe("qweb parser", () => {
|
||||
name: "MyComponent",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
isDynamic: false,
|
||||
on: null,
|
||||
slots: {
|
||||
default: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
content: {
|
||||
@@ -1667,11 +1768,13 @@ describe("qweb parser", () => {
|
||||
name: "Child",
|
||||
dynamicProps: null,
|
||||
props: null,
|
||||
propsTranslationCtx: null,
|
||||
on: null,
|
||||
slots: {
|
||||
brol: {
|
||||
content: { type: ASTType.Text, value: "coucou" },
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
scope: null,
|
||||
},
|
||||
@@ -1691,6 +1794,7 @@ describe("qweb parser", () => {
|
||||
type: ASTType.TSlot,
|
||||
name: "default",
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
defaultContent: null,
|
||||
});
|
||||
@@ -1701,6 +1805,7 @@ describe("qweb parser", () => {
|
||||
type: ASTType.TSlot,
|
||||
name: "header",
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
defaultContent: { type: ASTType.Text, value: "default content" },
|
||||
});
|
||||
@@ -1711,6 +1816,7 @@ describe("qweb parser", () => {
|
||||
type: ASTType.TSlot,
|
||||
name: "default",
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: { "click.prevent": "doSomething" },
|
||||
defaultContent: null,
|
||||
});
|
||||
@@ -1728,6 +1834,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -1746,6 +1853,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
model: null,
|
||||
@@ -1765,6 +1873,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: "name",
|
||||
model: null,
|
||||
@@ -1779,6 +1888,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: "name",
|
||||
model: null,
|
||||
@@ -1795,6 +1905,7 @@ describe("qweb parser", () => {
|
||||
tag: "div",
|
||||
dynamicTag: null,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
on: null,
|
||||
ref: "name",
|
||||
model: null,
|
||||
@@ -1831,6 +1942,7 @@ describe("qweb parser", () => {
|
||||
body: {
|
||||
content: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
type: ASTType.Text,
|
||||
@@ -1859,6 +1971,190 @@ describe("qweb parser", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('t-translation="off": interaction with t-esc', async () => {
|
||||
expect(parse(`<span t-esc="a" t-translation="off"/>`)).toEqual({
|
||||
type: ASTType.TTranslation,
|
||||
content: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
defaultValue: "",
|
||||
expr: "a",
|
||||
type: ASTType.TEsc,
|
||||
},
|
||||
],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
ns: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
tag: "span",
|
||||
type: ASTType.DomNode,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('t-translation="off": interaction with t-out', async () => {
|
||||
expect(parse(`<span t-out="a" t-translation="off"/>`)).toEqual({
|
||||
type: ASTType.TTranslation,
|
||||
content: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
body: null,
|
||||
expr: "a",
|
||||
type: ASTType.TOut,
|
||||
},
|
||||
],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
ns: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
tag: "span",
|
||||
type: ASTType.DomNode,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// t-translation-context
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test('t-translation-context="fr"', async () => {
|
||||
expect(parse(`<t t-translation-context="fr">word</t>`)).toEqual({
|
||||
type: ASTType.TTranslationContext,
|
||||
content: {
|
||||
type: ASTType.Text,
|
||||
value: "word",
|
||||
},
|
||||
translationCtx: "fr",
|
||||
});
|
||||
|
||||
expect(parse(`<div t-translation-context="fr">word</div>`)).toEqual({
|
||||
content: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
type: 0,
|
||||
value: "word",
|
||||
},
|
||||
],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
ns: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
tag: "div",
|
||||
type: ASTType.DomNode,
|
||||
},
|
||||
translationCtx: "fr",
|
||||
type: ASTType.TTranslationContext,
|
||||
});
|
||||
});
|
||||
|
||||
test("t-translation-context: interaction with t-esc", async () => {
|
||||
expect(parse(`<span t-esc="a" t-translation-context="fr"/>`)).toEqual({
|
||||
type: ASTType.TTranslationContext,
|
||||
content: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
defaultValue: "",
|
||||
expr: "a",
|
||||
type: ASTType.TEsc,
|
||||
},
|
||||
],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
ns: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
tag: "span",
|
||||
type: ASTType.DomNode,
|
||||
},
|
||||
translationCtx: "fr",
|
||||
});
|
||||
});
|
||||
|
||||
test("t-translation-context: interaction with t-out", async () => {
|
||||
expect(parse(`<span t-out="a" t-translation-context="fr"/>`)).toEqual({
|
||||
type: ASTType.TTranslationContext,
|
||||
content: {
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [
|
||||
{
|
||||
body: null,
|
||||
expr: "a",
|
||||
type: ASTType.TOut,
|
||||
},
|
||||
],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
ns: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
tag: "span",
|
||||
type: ASTType.DomNode,
|
||||
},
|
||||
translationCtx: "fr",
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// t-translation-context-attr
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test('t-translation-context="fr" and t-translation-context-title="pt" for a div attr title', async () => {
|
||||
expect(
|
||||
parse(
|
||||
`<div t-translation-context="fr" title="hello" t-translation-context-title="pt">word</div>`
|
||||
)
|
||||
).toEqual({
|
||||
content: {
|
||||
attrs: { title: "hello" },
|
||||
attrsTranslationCtx: { title: "pt" },
|
||||
content: [
|
||||
{
|
||||
type: 0,
|
||||
value: "word",
|
||||
},
|
||||
],
|
||||
dynamicTag: null,
|
||||
model: null,
|
||||
ns: null,
|
||||
on: null,
|
||||
ref: null,
|
||||
tag: "div",
|
||||
type: ASTType.DomNode,
|
||||
},
|
||||
translationCtx: "fr",
|
||||
type: ASTType.TTranslationContext,
|
||||
});
|
||||
});
|
||||
|
||||
test('t-translation-context-title="fr" for component prop title', async () => {
|
||||
expect(parse(`<Comp title="hello" t-translation-context-title="fr" />`)).toEqual({
|
||||
dynamicProps: null,
|
||||
isDynamic: false,
|
||||
name: "Comp",
|
||||
on: null,
|
||||
props: {
|
||||
title: "hello",
|
||||
},
|
||||
propsTranslationCtx: {
|
||||
title: "fr",
|
||||
},
|
||||
slots: null,
|
||||
type: ASTType.TComponent,
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// t-model
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1866,6 +2162,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<input t-model="state.stuff" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -1886,6 +2183,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<input t-model="state['stuff']" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -1906,6 +2204,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<input t-model.lazy.trim.number="state.stuff" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -1927,6 +2226,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<textarea t-model="state.stuff" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -1947,6 +2247,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<input type="checkbox" t-model="state.stuff" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: { type: "checkbox" },
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -1967,6 +2268,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<input type="radio" t-model="state.stuff" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: { type: "radio" },
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -1987,6 +2289,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<input type="radio" t-model.lazy.trim.number="state.stuff" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: { type: "radio" },
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
@@ -2012,6 +2315,7 @@ describe("qweb parser", () => {
|
||||
expect(parse(`<div t-tag="theTag" />`)).toEqual({
|
||||
type: ASTType.DomNode,
|
||||
attrs: null,
|
||||
attrsTranslationCtx: null,
|
||||
content: [],
|
||||
on: null,
|
||||
ref: null,
|
||||
|
||||
@@ -154,4 +154,19 @@ describe("simple templates, mostly static", () => {
|
||||
</div>`;
|
||||
expect(renderToString(template, { a: "a", b: "b", c: "c" })).toBe("<div>abLoadingc</div>");
|
||||
});
|
||||
|
||||
test("text node with backslash at top level", () => {
|
||||
const template = "\\";
|
||||
expect(renderToString(template)).toBe("\\");
|
||||
});
|
||||
|
||||
test("text node with backtick at top-level", () => {
|
||||
const template = "`";
|
||||
expect(renderToString(template)).toBe("`");
|
||||
});
|
||||
|
||||
test("text node with interpolation sigil at top level", () => {
|
||||
const template = "${very cool}";
|
||||
expect(renderToString(template)).toBe("${very cool}");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { App, Component, xml } from "../../src";
|
||||
import { makeTestFixture, snapshotEverything } from "../helpers";
|
||||
|
||||
let fixture: HTMLElement;
|
||||
|
||||
snapshotEverything();
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = makeTestFixture();
|
||||
});
|
||||
|
||||
describe("t-custom", () => {
|
||||
test("can use t-custom directive on a node", async () => {
|
||||
const steps: string[] = [];
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div t-custom-plop="click" class="my-div"/>`;
|
||||
click() {
|
||||
steps.push("clicked");
|
||||
}
|
||||
}
|
||||
const app = new App(SomeComponent, {
|
||||
customDirectives: {
|
||||
plop: (node, value) => {
|
||||
node.setAttribute("t-on-click", value);
|
||||
},
|
||||
},
|
||||
});
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
||||
fixture.querySelector("div")!.click();
|
||||
expect(steps).toEqual(["clicked"]);
|
||||
});
|
||||
|
||||
test("can use t-custom directive with modifiers on a node", async () => {
|
||||
const steps: string[] = [];
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div t-custom-plop.mouse.stop="click" class="my-div"/>`;
|
||||
click() {
|
||||
steps.push("clicked");
|
||||
}
|
||||
}
|
||||
const app = new App(SomeComponent, {
|
||||
customDirectives: {
|
||||
plop: (node, value, modifiers) => {
|
||||
node.setAttribute("t-on-click", value);
|
||||
for (let mod of modifiers) {
|
||||
steps.push(mod);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
||||
fixture.querySelector("div")!.click();
|
||||
expect(steps).toEqual(["mouse", "stop", "clicked"]);
|
||||
});
|
||||
});
|
||||
@@ -121,4 +121,19 @@ describe("t-esc", () => {
|
||||
mount(bdom, fixture);
|
||||
expect(fixture.querySelector("span")!.textContent).toBe("<p>escaped</p>");
|
||||
});
|
||||
|
||||
test("default with backslash at top level", () => {
|
||||
const template = '<t t-esc="undefined">\\</t>';
|
||||
expect(renderToString(template)).toBe("\\");
|
||||
});
|
||||
|
||||
test("default with backtick at top-level", () => {
|
||||
const template = '<t t-esc="undefined">`</t>';
|
||||
expect(renderToString(template)).toBe("`");
|
||||
});
|
||||
|
||||
test("default with interpolation sigil at top level", () => {
|
||||
const template = '<t t-esc="undefined">${very cool}</t>';
|
||||
expect(renderToString(template)).toBe("${very cool}");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,6 +54,21 @@ describe("t-set", () => {
|
||||
expect(renderToString(template)).toBe("ok");
|
||||
});
|
||||
|
||||
test("body with backslash at top level", () => {
|
||||
const template = '<t t-set="value">\\</t><t t-esc="value"/>';
|
||||
expect(renderToString(template)).toBe("\\");
|
||||
});
|
||||
|
||||
test("body with backtick at top-level", () => {
|
||||
const template = '<t t-set="value">`</t><t t-esc="value"/>';
|
||||
expect(renderToString(template)).toBe("`");
|
||||
});
|
||||
|
||||
test("body with interpolation sigil at top level", () => {
|
||||
const template = '<t t-set="value">${very cool}</t><t t-esc="value"/>';
|
||||
expect(renderToString(template)).toBe("${very cool}");
|
||||
});
|
||||
|
||||
test("set from body literal (with t-if/t-else", () => {
|
||||
const template = `
|
||||
<t>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { parseXML } from "../../src/common/utils";
|
||||
import { compile } from "../../src/compiler";
|
||||
|
||||
describe("t-slot", () => {
|
||||
test("compile t-props correctly multiple time", () => {
|
||||
const template = `<t t-slot="default" t-props="{ a: 1 }"/>`;
|
||||
const parsedTemplate = parseXML(template).firstChild as Element;
|
||||
|
||||
const fn1 = compile(parsedTemplate);
|
||||
expect(fn1.toString()).toMatchSnapshot();
|
||||
|
||||
const fn2 = compile(parsedTemplate);
|
||||
expect(fn2.toString()).toBe(fn1.toString());
|
||||
});
|
||||
});
|
||||
@@ -78,4 +78,62 @@ describe("loading templates", () => {
|
||||
context.addTemplates(xml);
|
||||
expect(Object.keys(context.rawTemplates)).toEqual([]);
|
||||
});
|
||||
|
||||
test("getTemplate: element returned", () => {
|
||||
const context = new TestContext({
|
||||
getTemplate: (name) => {
|
||||
if (name === "main") {
|
||||
const data = `<div>Hello World!</div>`;
|
||||
const xml = new DOMParser().parseFromString(data, "text/xml");
|
||||
return xml.firstChild as Element;
|
||||
}
|
||||
return;
|
||||
},
|
||||
});
|
||||
const result = context.renderToString("main");
|
||||
expect(result).toBe("<div>Hello World!</div>");
|
||||
});
|
||||
|
||||
test("getTemplate: element returned (2)", () => {
|
||||
const context = new TestContext({
|
||||
getTemplate: (name) => {
|
||||
if (name === "main") {
|
||||
const doc = new Document();
|
||||
const div = doc.createElement("div");
|
||||
div.append(doc.createTextNode("Hello World!"));
|
||||
return div;
|
||||
}
|
||||
return;
|
||||
},
|
||||
});
|
||||
const result = context.renderToString("main");
|
||||
expect(result).toBe("<div>Hello World!</div>");
|
||||
});
|
||||
|
||||
test("getTemplate: template string returned", () => {
|
||||
const context = new TestContext({
|
||||
getTemplate: (name) => {
|
||||
if (name === "main") {
|
||||
return `<div>Hello World!</div>`;
|
||||
}
|
||||
return;
|
||||
},
|
||||
});
|
||||
const result = context.renderToString("main");
|
||||
expect(result).toBe("<div>Hello World!</div>");
|
||||
});
|
||||
|
||||
test("getTemplate: undefined returned", () => {
|
||||
const context = new TestContext({
|
||||
getTemplate: () => {},
|
||||
});
|
||||
const data = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<templates id="template" xml:space="preserve">
|
||||
<div t-name="main">Hello World!</div>
|
||||
</templates>`;
|
||||
const xml = new DOMParser().parseFromString(data, "text/xml");
|
||||
context.addTemplates(xml);
|
||||
const result = context.renderToString("main");
|
||||
expect(result).toBe("<div>Hello World!</div>");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -86,7 +86,7 @@ describe("translation support", () => {
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe("<div> mot </div>");
|
||||
expect(translateFn).toHaveBeenCalledWith("word");
|
||||
expect(translateFn).toHaveBeenCalledWith("word", "");
|
||||
});
|
||||
|
||||
test("translation works, even if initial string has inner consecutive white space", async () => {
|
||||
@@ -97,7 +97,7 @@ describe("translation support", () => {
|
||||
const translateFn = jest.fn((expr: string) => (expr === "some word" ? "un mot" : expr));
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(translateFn).toHaveBeenCalledWith("some word");
|
||||
expect(translateFn).toHaveBeenCalledWith("some word", "");
|
||||
expect(fixture.innerHTML).toBe("<div>un mot</div>");
|
||||
});
|
||||
|
||||
@@ -171,3 +171,126 @@ describe("translation support", () => {
|
||||
expect(fixture.innerHTML).toBe("translated");
|
||||
});
|
||||
});
|
||||
|
||||
describe("translation context", () => {
|
||||
test("translation of text in context", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
<div>word</div>
|
||||
<div t-translation-context="fr">word</div>
|
||||
`;
|
||||
}
|
||||
|
||||
const translateFn = jest.fn((expr: string, translationCtx: string) =>
|
||||
translationCtx === "fr" ? (expr === "word" ? "mot" : expr) : expr
|
||||
);
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe("<div>word</div><div>mot</div>");
|
||||
expect(translateFn).toHaveBeenCalledWith("word", "");
|
||||
expect(translateFn).toHaveBeenCalledWith("word", "fr");
|
||||
});
|
||||
test("translation of attributes in context", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
<div t-translation-context="en" t-translation-context-title="fr" title="title" label="game"/>
|
||||
`;
|
||||
}
|
||||
|
||||
const translateFn = jest.fn((expr: string, translationCtx: string) =>
|
||||
translationCtx === "fr" ? (expr === "title" ? "titre" : expr) : expr
|
||||
);
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe(`<div title="titre" label="game"></div>`);
|
||||
expect(translateFn).toHaveBeenCalledWith("title", "fr");
|
||||
expect(translateFn).toHaveBeenCalledWith("game", "en");
|
||||
});
|
||||
test("body of t-sets are translated in context", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
<t t-set="label" t-translation-context="fr">untranslated</t>
|
||||
<t t-esc="label"/>`;
|
||||
}
|
||||
|
||||
const translateFn = jest.fn((expr: string, translationCtx: string) =>
|
||||
translationCtx === "fr" ? "traduit" : expr
|
||||
);
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe("traduit");
|
||||
expect(translateFn).toHaveBeenCalledWith("untranslated", "fr");
|
||||
});
|
||||
test("props with modifier .translate are translated in context", async () => {
|
||||
class ChildComponent extends Component {
|
||||
static props = ["text"];
|
||||
static template = xml`<span t-esc="props.text"/>`;
|
||||
}
|
||||
|
||||
class SomeComponent extends Component {
|
||||
static components = { ChildComponent };
|
||||
static template = xml`
|
||||
<ChildComponent text.translate="game" t-translation-context-text.translate="fr" />`;
|
||||
}
|
||||
|
||||
const translateFn = jest.fn((expr: string, translationCtx: string) =>
|
||||
translationCtx === "fr" ? "jeu" : expr
|
||||
);
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe("<span>jeu</span>");
|
||||
expect(translateFn).toHaveBeenCalledWith("game", "fr");
|
||||
});
|
||||
test("slot attrs and text contents are translated in context", async () => {
|
||||
class ChildComponent extends Component {
|
||||
static template = xml`
|
||||
<div t-translation-context="ja">
|
||||
<t t-slot="a"/>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
class SomeComponent extends Component {
|
||||
static components = { ChildComponent };
|
||||
static template = xml`
|
||||
<ChildComponent t-translation-context="fr">
|
||||
<t t-set-slot="a" title.translate="title" t-translation-context-title.translate="pt">game</t>
|
||||
</ChildComponent>
|
||||
`;
|
||||
}
|
||||
|
||||
const translateFn = jest.fn((expr: string, translationCtx: string) =>
|
||||
translationCtx === "fr" ? "jeu" : translationCtx === "pt" ? "título" : expr
|
||||
);
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe("<div>jeu</div>");
|
||||
expect(translateFn).toHaveBeenCalledWith("game", "fr");
|
||||
expect(translateFn).toHaveBeenCalledWith("title", "pt");
|
||||
});
|
||||
test("default slot params and content translated in context", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
<t
|
||||
t-slot="default"
|
||||
t-translation-context="fr"
|
||||
param.translate="param"
|
||||
title.translate="title"
|
||||
t-translation-context-title.translate="pt"
|
||||
>
|
||||
foo
|
||||
</t>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
const translateFn = jest.fn((expr: string, translationCtx: string) =>
|
||||
translationCtx === "pt" ? "título" : expr
|
||||
);
|
||||
|
||||
await mount(SomeComponent, fixture, { translateFn });
|
||||
expect(fixture.innerHTML).toBe("<div> foo </div>");
|
||||
expect(translateFn).toHaveBeenCalledWith("foo", "fr");
|
||||
expect(translateFn).toHaveBeenCalledWith("param", "fr");
|
||||
expect(translateFn).toHaveBeenCalledWith("title", "pt");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,7 +48,7 @@ describe("basic validation", () => {
|
||||
test("compilation error", () => {
|
||||
const template = `<div t-att-class="a b">test</div>`;
|
||||
expect(() => renderToString(template))
|
||||
.toThrow(`Failed to compile anonymous template: Unexpected identifier
|
||||
.toThrow(`Failed to compile anonymous template: Unexpected identifier 'ctx'
|
||||
|
||||
generated code:
|
||||
function(app, bdom, helpers) {
|
||||
|
||||
@@ -97,6 +97,19 @@ exports[`basics a component cannot be mounted in a detached node (even if node i
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics a component cannot be mounted in a detached node 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics a component inside a component 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -261,6 +274,19 @@ exports[`basics can mount a simple component with props 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics cannot mount on a documentFragment 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>content</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics child can be updated 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -1002,6 +1028,19 @@ exports[`basics three level of components with collapsing root nodes 3`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics throws if mounting on target=null 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<span>simple vnode</span>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics two child components 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -320,6 +320,50 @@ exports[`can catch errors can catch an error in a component render function 3`]
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors can catch an error in onmounted 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(null, false, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let b2, b3;
|
||||
b2 = text(\`Main\`);
|
||||
if (ctx['state'].ok) {
|
||||
const Comp1 = ctx['component'];
|
||||
b3 = toggler(Comp1, comp1({}, (Comp1).name + key + \`__1\`, node, this, Comp1));
|
||||
}
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors can catch an error in onmounted 3`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>Error!!!</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors can catch an error in onmounted 4`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div>perfect</div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors can catch an error in the constructor call of a component render function 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -1147,6 +1191,135 @@ exports[`can catch errors error in mounted on a component with a sibling (proper
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(null, false, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const Comp1 = ctx['component'];
|
||||
return toggler(Comp1, comp1({}, (Comp1).name + key + \`__1\`, node, this, Comp1));
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, []);
|
||||
const comp2 = app.createComponent(\`Boom\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b2 = text(\`parent\`);
|
||||
const b3 = comp1({}, key + \`__1\`, node, this, null);
|
||||
const b4 = comp2({}, key + \`__2\`, node, this, null);
|
||||
return multi([b2, b3, b4]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery 3`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`abc\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery 4`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`boom\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery 5`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`def\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery, variation 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(null, false, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let b2, b3;
|
||||
b2 = text(\`R\`);
|
||||
if (ctx['state'].gogogo) {
|
||||
const Comp1 = ctx['component'];
|
||||
b3 = toggler(Comp1, comp1({}, (Comp1).name + key + \`__1\`, node, this, Comp1));
|
||||
}
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery, variation 3`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, []);
|
||||
const comp2 = app.createComponent(\`Boom\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const b2 = text(\`parent\`);
|
||||
const b3 = comp1({}, key + \`__1\`, node, this, null);
|
||||
const b4 = comp2({}, key + \`__2\`, node, this, null);
|
||||
return multi([b2, b3, b4]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery, variation 4`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`abc\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery, variation 5`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`boom\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors error in onMounted, graceful recovery, variation 6`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(\`def\`);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can catch errors onError in class inheritance is called if rethrown 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -683,7 +683,7 @@ exports[`lifecycle hooks sub widget (inside sub node): hooks are correctly calle
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`lifecycle hooks timeout in onWillStart emits a warning 1`] = `
|
||||
exports[`lifecycle hooks timeout in onWillStart doesn't emit a console log if app is destroyed 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
@@ -696,7 +696,20 @@ exports[`lifecycle hooks timeout in onWillStart emits a warning 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`lifecycle hooks timeout in onWillUpdateProps emits a warning 1`] = `
|
||||
exports[`lifecycle hooks timeout in onWillStart emits a console log 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<span/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return block1();
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`lifecycle hooks timeout in onWillUpdateProps emits a console log 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
@@ -710,7 +723,7 @@ exports[`lifecycle hooks timeout in onWillUpdateProps emits a warning 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`lifecycle hooks timeout in onWillUpdateProps emits a warning 2`] = `
|
||||
exports[`lifecycle hooks timeout in onWillUpdateProps emits a console log 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
@@ -66,6 +66,29 @@ exports[`.alike suffix in a simple case 2`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`.translate props are translated 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return comp1({message: \`translated message\`}, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`.translate props are translated 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(ctx['props'].message);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`basics accept ES6-like syntax for props (with getters) 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -412,6 +435,29 @@ exports[`can bind function prop with bind suffix 2`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can use .translate suffix 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return comp1({message: \`some message\`}, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`can use .translate suffix 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(ctx['props'].message);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`do not crash when binding anonymous function prop with bind suffix 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -167,6 +167,45 @@ exports[`props validation can specify that additional props are allowed (object)
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`props validation can use custom class as type 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, [\\"customObj\\"]);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const props1 = {customObj: ctx['customObj']};
|
||||
helpers.validateProps(\`Child\`, props1, this);
|
||||
return comp1(props1, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`props validation can use custom class as type 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(ctx['props'].customObj.val);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`props validation can use custom class as type: validation failure 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
const comp1 = app.createComponent(\`Child\`, true, false, false, [\\"customObj\\"]);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const props1 = {customObj: ctx['customObj']};
|
||||
helpers.validateProps(\`Child\`, props1, this);
|
||||
return comp1(props1, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`props validation can validate a prop with multiple types 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -885,6 +924,20 @@ exports[`props validation props: list of strings 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`props validation validate props for root component 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
let block1 = createBlock(\`<div><block-text-0/></div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
let txt1 = ctx['message'];
|
||||
return block1([txt1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`props validation validate simple types 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -1,5 +1,30 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`slots .translate slot props are translated 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, true, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const ctx1 = capture(ctx);
|
||||
return comp1({slots: markRaw({'default': {message: \`translated message\`}})}, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots .translate slot props are translated 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(ctx['props'].slots.default.message);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots can define a default content 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -201,6 +226,31 @@ exports[`slots can render only empty slot 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots can use .translate suffix on slot props 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, true, false, []);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const ctx1 = capture(ctx);
|
||||
return comp1({slots: markRaw({'default': {message: \`some message\`}})}, key + \`__1\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots can use .translate suffix on slot props 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return text(ctx['props'].slots.default.message);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots can use component in default-content of t-slot 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
@@ -1066,6 +1116,45 @@ exports[`slots multiple slots containing components 3`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots named slot inside named slot in t-component 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { capture, markRaw } = helpers;
|
||||
const comp1 = app.createComponent(null, false, true, false, []);
|
||||
const comp2 = app.createComponent(\`Child\`, true, true, false, []);
|
||||
|
||||
function slot1(ctx, node, key = \\"\\") {
|
||||
const b2 = text(\` outer \`);
|
||||
const ctx2 = capture(ctx);
|
||||
const Comp1 = ctx['Child'];
|
||||
const b4 = toggler(Comp1, comp1({slots: markRaw({'brol': {__render: slot2.bind(this), __ctx: ctx2}})}, (Comp1).name + key + \`__1\`, node, this, Comp1));
|
||||
return multi([b2, b4]);
|
||||
}
|
||||
|
||||
function slot2(ctx, node, key = \\"\\") {
|
||||
return text(ctx['value']);
|
||||
}
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const ctx1 = capture(ctx);
|
||||
return comp2({slots: markRaw({'brol': {__render: slot1.bind(this), __ctx: ctx1}})}, key + \`__2\`, node, this, null);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots named slot inside named slot in t-component 2`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { callSlot } = helpers;
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
return callSlot(ctx, node, key, 'brol', false, {});
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`slots named slot inside slot 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -44,7 +44,26 @@ exports[`t-model directive .trim modifier 1`] = `
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { toNumber } = helpers;
|
||||
|
||||
let block1 = createBlock(\`<div><input block-property-0=\\"value\\" block-handler-1=\\"input\\"/><span><block-text-2/></span></div>\`);
|
||||
let block1 = createBlock(\`<div><input block-property-0=\\"value\\" block-handler-1=\\"change\\"/><span><block-text-2/></span></div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const bExpr1 = ctx['state'];
|
||||
const expr1 = 'text';
|
||||
let prop1 = bExpr1[expr1];
|
||||
let hdlr1 = [(ev) => { bExpr1[expr1] = ev.target.value.trim(); }];
|
||||
let txt1 = ctx['state'].text;
|
||||
return block1([prop1, hdlr1, txt1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`t-model directive .trim modifier implies .lazy modifier 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||
let { toNumber } = helpers;
|
||||
|
||||
let block1 = createBlock(\`<div><input block-property-0=\\"value\\" block-handler-1=\\"change\\"/><span><block-text-2/></span></div>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const bExpr1 = ctx['state'];
|
||||
|
||||
@@ -157,7 +157,7 @@ describe("basics", () => {
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
const expectedErrorMessage = `Failed to compile anonymous template: Unexpected identifier
|
||||
const expectedErrorMessage = `Failed to compile anonymous template: Unexpected identifier 'ctx'
|
||||
|
||||
generated code:
|
||||
function(app, bdom, helpers) {
|
||||
@@ -182,7 +182,7 @@ function(app, bdom, helpers) {
|
||||
static components = { Child };
|
||||
static template = xml`<Child/>`;
|
||||
}
|
||||
const expectedErrorMessage = `Failed to compile anonymous template: Unexpected identifier
|
||||
const expectedErrorMessage = `Failed to compile anonymous template: Unexpected identifier 'ctx'
|
||||
|
||||
generated code:
|
||||
function(app, bdom, helpers) {
|
||||
@@ -564,6 +564,82 @@ describe("can catch errors", () => {
|
||||
expect(mockConsoleWarn).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("can catch an error in onmounted", async () => {
|
||||
class ErrorComponent extends Component {
|
||||
static template = xml`<div>Error!!!</div>`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
onMounted(() => {
|
||||
throw new Error("error");
|
||||
});
|
||||
}
|
||||
}
|
||||
class PerfectComponent extends Component {
|
||||
static template = xml`<div>perfect</div>`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
class Main extends Component {
|
||||
static template = xml`Main<t t-if="state.ok" t-component="component"/>`;
|
||||
component: any;
|
||||
state: any;
|
||||
setup() {
|
||||
this.state = useState({ ok: false });
|
||||
useLogLifecycle();
|
||||
this.component = ErrorComponent;
|
||||
onError(() => {
|
||||
this.component = PerfectComponent;
|
||||
this.render();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const app = await mount(Main, fixture);
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"Main:setup",
|
||||
"Main:willStart",
|
||||
"Main:willRender",
|
||||
"Main:rendered",
|
||||
"Main:mounted",
|
||||
]
|
||||
`);
|
||||
expect(fixture.innerHTML).toBe("Main");
|
||||
(app as any).state.ok = true;
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("Main<div>Error!!!</div>");
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"Main:willRender",
|
||||
"ErrorComponent:setup",
|
||||
"ErrorComponent:willStart",
|
||||
"Main:rendered",
|
||||
"ErrorComponent:willRender",
|
||||
"ErrorComponent:rendered",
|
||||
"Main:willPatch",
|
||||
"ErrorComponent:mounted",
|
||||
"Main:willRender",
|
||||
"PerfectComponent:setup",
|
||||
"PerfectComponent:willStart",
|
||||
"Main:rendered",
|
||||
]
|
||||
`);
|
||||
await nextTick();
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"PerfectComponent:willRender",
|
||||
"PerfectComponent:rendered",
|
||||
"Main:willPatch",
|
||||
"ErrorComponent:willUnmount",
|
||||
"ErrorComponent:willDestroy",
|
||||
"PerfectComponent:mounted",
|
||||
"Main:patched",
|
||||
]
|
||||
`);
|
||||
expect(fixture.innerHTML).toBe("Main<div>perfect</div>");
|
||||
});
|
||||
|
||||
test("calling a hook outside setup should crash", async () => {
|
||||
class Root extends Component {
|
||||
static template = xml`<t t-esc="state.value"/>`;
|
||||
@@ -1602,4 +1678,198 @@ describe("can catch errors", () => {
|
||||
`);
|
||||
expect(fixture.innerHTML).toBe("2");
|
||||
});
|
||||
|
||||
test("error in onMounted, graceful recovery", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`abc`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
class OtherChild extends Component {
|
||||
static template = xml`def`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
class Boom extends Component {
|
||||
static template = xml`boom`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
onMounted(() => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`parent<Child/><Boom/>`;
|
||||
static components = { Child, Boom };
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
class Root extends Component {
|
||||
static template = xml`<t t-component="component"/>`;
|
||||
|
||||
component: any = Parent;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
onError(() => {
|
||||
logStep("error");
|
||||
this.component = OtherChild;
|
||||
this.render();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await mount(Root, fixture);
|
||||
expect(fixture.innerHTML).toBe("def");
|
||||
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"Root:setup",
|
||||
"Root:willStart",
|
||||
"Root:willRender",
|
||||
"Parent:setup",
|
||||
"Parent:willStart",
|
||||
"Root:rendered",
|
||||
"Parent:willRender",
|
||||
"Child:setup",
|
||||
"Child:willStart",
|
||||
"Boom:setup",
|
||||
"Boom:willStart",
|
||||
"Parent:rendered",
|
||||
"Child:willRender",
|
||||
"Child:rendered",
|
||||
"Boom:willRender",
|
||||
"Boom:rendered",
|
||||
"Boom:mounted",
|
||||
"error",
|
||||
"Root:willRender",
|
||||
"OtherChild:setup",
|
||||
"OtherChild:willStart",
|
||||
"Root:rendered",
|
||||
"OtherChild:willRender",
|
||||
"OtherChild:rendered",
|
||||
"OtherChild:mounted",
|
||||
"Root:mounted",
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("error in onMounted, graceful recovery, variation", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`abc`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
class OtherChild extends Component {
|
||||
static template = xml`def`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
class Boom extends Component {
|
||||
static template = xml`boom`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
onMounted(() => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`parent<Child/><Boom/>`;
|
||||
static components = { Child, Boom };
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
class Root extends Component {
|
||||
static template = xml`R<t t-if="state.gogogo" t-component="component"/>`;
|
||||
|
||||
component: any = Parent;
|
||||
state = useState({ gogogo: false });
|
||||
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
onError(() => {
|
||||
logStep("error");
|
||||
this.component = OtherChild;
|
||||
this.render();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const root = await mount(Root, fixture);
|
||||
expect(fixture.innerHTML).toBe("R");
|
||||
|
||||
// standard mounting process
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"Root:setup",
|
||||
"Root:willStart",
|
||||
"Root:willRender",
|
||||
"Root:rendered",
|
||||
"Root:mounted",
|
||||
]
|
||||
`);
|
||||
|
||||
root.state.gogogo = true;
|
||||
await nextTick();
|
||||
|
||||
expect(fixture.innerHTML).toBe("Rparentabcboom");
|
||||
// rerender, root creates sub components, it crashes, tries to recover
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"Root:willRender",
|
||||
"Parent:setup",
|
||||
"Parent:willStart",
|
||||
"Root:rendered",
|
||||
"Parent:willRender",
|
||||
"Child:setup",
|
||||
"Child:willStart",
|
||||
"Boom:setup",
|
||||
"Boom:willStart",
|
||||
"Parent:rendered",
|
||||
"Child:willRender",
|
||||
"Child:rendered",
|
||||
"Boom:willRender",
|
||||
"Boom:rendered",
|
||||
"Root:willPatch",
|
||||
"Boom:mounted",
|
||||
"error",
|
||||
"Root:willRender",
|
||||
"OtherChild:setup",
|
||||
"OtherChild:willStart",
|
||||
"Root:rendered",
|
||||
]
|
||||
`);
|
||||
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("Rdef");
|
||||
|
||||
expect(steps.splice(0)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
"OtherChild:willRender",
|
||||
"OtherChild:rendered",
|
||||
"Root:willPatch",
|
||||
"Child:willDestroy",
|
||||
"Boom:willUnmount",
|
||||
"Boom:willDestroy",
|
||||
"Parent:willDestroy",
|
||||
"OtherChild:mounted",
|
||||
"Root:patched",
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { App, Component, mount, onMounted, onWillStart, useState, xml } from "../../src";
|
||||
import {
|
||||
App,
|
||||
Component,
|
||||
mount,
|
||||
useState,
|
||||
xml,
|
||||
onWillPatch,
|
||||
onWillUnmount,
|
||||
onPatched,
|
||||
@@ -7,7 +11,9 @@ import {
|
||||
onWillRender,
|
||||
onWillDestroy,
|
||||
onRendered,
|
||||
} from "../../src/runtime/lifecycle_hooks";
|
||||
onMounted,
|
||||
onWillStart,
|
||||
} from "../../src";
|
||||
import { status } from "../../src/runtime/status";
|
||||
import {
|
||||
elem,
|
||||
@@ -106,10 +112,10 @@ describe("lifecycle hooks", () => {
|
||||
await mount(Test, fixture);
|
||||
});
|
||||
|
||||
test("timeout in onWillStart emits a warning", async () => {
|
||||
const { warn } = console;
|
||||
let warnArgs: any[];
|
||||
console.warn = jest.fn((...args) => (warnArgs = args));
|
||||
test("timeout in onWillStart emits a console log", async () => {
|
||||
const { log } = console;
|
||||
let logArgs: any[];
|
||||
console.log = jest.fn((...args) => (logArgs = args));
|
||||
const { setTimeout } = window;
|
||||
let timeoutCbs: any = {};
|
||||
let timeoutId = 0;
|
||||
@@ -117,27 +123,63 @@ describe("lifecycle hooks", () => {
|
||||
timeoutCbs[++timeoutId] = cb;
|
||||
return timeoutId;
|
||||
}) as any;
|
||||
class Test extends Component {
|
||||
static template = xml`<span/>`;
|
||||
setup() {
|
||||
onWillStart(() => new Promise(() => {}));
|
||||
try {
|
||||
class Test extends Component {
|
||||
static template = xml`<span/>`;
|
||||
setup() {
|
||||
onWillStart(() => new Promise(() => {}));
|
||||
}
|
||||
}
|
||||
mount(Test, fixture, { test: true });
|
||||
nextTick();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
}
|
||||
await nextMicroTick();
|
||||
await nextMicroTick();
|
||||
expect(console.log).toHaveBeenCalledTimes(1);
|
||||
expect(logArgs![0]!.message).toBe("onWillStart's promise hasn't resolved after 3 seconds");
|
||||
} finally {
|
||||
console.log = log;
|
||||
window.setTimeout = setTimeout;
|
||||
}
|
||||
mount(Test, fixture, { test: true });
|
||||
nextTick();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
}
|
||||
await nextMicroTick();
|
||||
await nextMicroTick();
|
||||
expect(console.warn).toHaveBeenCalledTimes(1);
|
||||
expect(warnArgs![0]!.message).toBe("onWillStart's promise hasn't resolved after 3 seconds");
|
||||
console.warn = warn;
|
||||
window.setTimeout = setTimeout;
|
||||
});
|
||||
|
||||
test("timeout in onWillUpdateProps emits a warning", async () => {
|
||||
test("timeout in onWillStart doesn't emit a console log if app is destroyed", async () => {
|
||||
const { log } = console;
|
||||
console.log = jest.fn();
|
||||
const { setTimeout } = window;
|
||||
let timeoutCbs: any = {};
|
||||
let timeoutId = 0;
|
||||
window.setTimeout = ((cb: any) => {
|
||||
timeoutCbs[++timeoutId] = cb;
|
||||
return timeoutId;
|
||||
}) as any;
|
||||
try {
|
||||
class Test extends Component {
|
||||
static template = xml`<span/>`;
|
||||
setup() {
|
||||
onWillStart(() => new Promise(() => {}));
|
||||
}
|
||||
}
|
||||
const app = new App(Test, { test: true });
|
||||
app.mount(fixture);
|
||||
app.destroy();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
}
|
||||
await nextMicroTick();
|
||||
await nextMicroTick();
|
||||
expect(console.log).toHaveBeenCalledTimes(0);
|
||||
} finally {
|
||||
console.log = log;
|
||||
window.setTimeout = setTimeout;
|
||||
}
|
||||
});
|
||||
|
||||
test("timeout in onWillUpdateProps emits a console log", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml``;
|
||||
setup() {
|
||||
@@ -151,9 +193,9 @@ describe("lifecycle hooks", () => {
|
||||
}
|
||||
const parent = await mount(Parent, fixture, { test: true });
|
||||
|
||||
const { warn } = console;
|
||||
let warnArgs: any[];
|
||||
console.warn = jest.fn((...args) => (warnArgs = args));
|
||||
const { log } = console;
|
||||
let logArgs: any[];
|
||||
console.log = jest.fn((...args) => (logArgs = args));
|
||||
const { setTimeout } = window;
|
||||
let timeoutCbs: any = {};
|
||||
let timeoutId = 0;
|
||||
@@ -162,25 +204,28 @@ describe("lifecycle hooks", () => {
|
||||
return timeoutId;
|
||||
}) as any;
|
||||
|
||||
parent.state.prop = 2;
|
||||
let tick = nextTick();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
try {
|
||||
parent.state.prop = 2;
|
||||
let tick = nextTick();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
}
|
||||
await tick;
|
||||
tick = nextTick();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
}
|
||||
await tick;
|
||||
expect(console.log).toHaveBeenCalledTimes(1);
|
||||
expect(logArgs![0]!.message).toBe(
|
||||
"onWillUpdateProps's promise hasn't resolved after 3 seconds"
|
||||
);
|
||||
} finally {
|
||||
console.log = log;
|
||||
window.setTimeout = setTimeout;
|
||||
}
|
||||
await tick;
|
||||
tick = nextTick();
|
||||
for (const id in timeoutCbs) {
|
||||
timeoutCbs[id]();
|
||||
delete timeoutCbs[id];
|
||||
}
|
||||
await tick;
|
||||
expect(console.warn).toHaveBeenCalledTimes(1);
|
||||
expect(warnArgs![0]!.message).toBe(
|
||||
"onWillUpdateProps's promise hasn't resolved after 3 seconds"
|
||||
);
|
||||
console.warn = warn;
|
||||
window.setTimeout = setTimeout;
|
||||
});
|
||||
|
||||
test("mounted hook is called if mounted in DOM", async () => {
|
||||
|
||||
@@ -299,6 +299,34 @@ test("bound functions are considered 'alike'", async () => {
|
||||
expect(fixture.innerHTML).toBe("3child");
|
||||
});
|
||||
|
||||
test("can use .translate suffix", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<t t-esc="props.message"/>`;
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<Child message.translate="some message"/>`;
|
||||
static components = { Child };
|
||||
}
|
||||
|
||||
await mount(Parent, fixture);
|
||||
expect(fixture.innerHTML).toBe("some message");
|
||||
});
|
||||
|
||||
test(".translate props are translated", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<t t-esc="props.message"/>`;
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<Child message.translate="some message"/>`;
|
||||
static components = { Child };
|
||||
}
|
||||
|
||||
await mount(Parent, fixture, { translateFn: () => "translated message" });
|
||||
expect(fixture.innerHTML).toBe("translated message");
|
||||
});
|
||||
|
||||
test("throw if prop uses an unknown suffix", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<t t-esc="props.val"/>`;
|
||||
@@ -311,7 +339,7 @@ test("throw if prop uses an unknown suffix", async () => {
|
||||
|
||||
await expect(async () => {
|
||||
await mount(Parent, fixture);
|
||||
}).rejects.toThrowError("Invalid prop suffix");
|
||||
}).rejects.toThrowError("Invalid prop suffix: somesuffix");
|
||||
});
|
||||
|
||||
test(".alike suffix in a simple case", async () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { makeTestFixture, nextAppError, nextTick, snapshotEverything } from "../helpers";
|
||||
import { Component, onError, xml, mount, OwlError, useState } from "../../src";
|
||||
import { App, DEV_MSG } from "../../src/runtime/app";
|
||||
import { App } from "../../src/runtime/app";
|
||||
import { validateProps } from "../../src/runtime/template_helpers";
|
||||
import { Schema } from "../../src/runtime/validation";
|
||||
|
||||
@@ -13,7 +13,7 @@ let mockConsoleWarn: any;
|
||||
|
||||
beforeAll(() => {
|
||||
console.info = (message: any) => {
|
||||
if (message === DEV_MSG()) {
|
||||
if (message === `Owl is running in 'dev' mode.`) {
|
||||
return;
|
||||
}
|
||||
info(message);
|
||||
@@ -594,7 +594,7 @@ describe("props validation", () => {
|
||||
|
||||
test("props: can be defined with a boolean", async () => {
|
||||
class SubComp extends Component {
|
||||
static props = { message: true };
|
||||
static props = { message: true } as const;
|
||||
}
|
||||
expect(() => {
|
||||
validateProps(SubComp as any, {});
|
||||
@@ -636,7 +636,7 @@ describe("props validation", () => {
|
||||
|
||||
test("props: extra props cause an error, part 2", async () => {
|
||||
class SubComp extends Component {
|
||||
static props = { message: true };
|
||||
static props = { message: true } as const;
|
||||
}
|
||||
expect(() => {
|
||||
validateProps(SubComp as any, { message: 1, flag: true });
|
||||
@@ -829,6 +829,52 @@ describe("props validation", () => {
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe("Invalid props for component 'Child': 'message' is missing");
|
||||
});
|
||||
|
||||
test("can use custom class as type", async () => {
|
||||
class CustomClass {
|
||||
val = "hey";
|
||||
}
|
||||
class Child extends Component {
|
||||
static props = { customObj: CustomClass };
|
||||
static template = xml`<t t-esc="props.customObj.val"/>`;
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`<Child customObj="customObj" />`;
|
||||
customObj = new CustomClass();
|
||||
}
|
||||
|
||||
const app = new App(Parent, { test: true });
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("hey");
|
||||
});
|
||||
|
||||
test("can use custom class as type: validation failure", async () => {
|
||||
class CustomClass {}
|
||||
class Child extends Component {
|
||||
static props = { customObj: CustomClass };
|
||||
static template = xml`<div>hey</div>`;
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`<Child customObj="customObj" />`;
|
||||
customObj = {};
|
||||
}
|
||||
|
||||
const app = new App(Parent, { test: true });
|
||||
let error: OwlError | undefined;
|
||||
const mountProm = app.mount(fixture).catch((e: Error) => (error = e));
|
||||
await expect(nextAppError(app)).resolves.toThrow(
|
||||
"Invalid props for component 'Child': 'customObj' is not a customclass"
|
||||
);
|
||||
await mountProm;
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Invalid props for component 'Child': 'customObj' is not a customclass"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -179,6 +179,34 @@ describe("slots", () => {
|
||||
expect(fixture.innerHTML).toBe("<span>default empty</span>");
|
||||
});
|
||||
|
||||
test("can use .translate suffix on slot props", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<t t-esc="props.slots.default.message"/>`;
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<Child><t t-set-slot="default" message.translate="some message"/></Child>`;
|
||||
static components = { Child };
|
||||
}
|
||||
|
||||
await mount(Parent, fixture);
|
||||
expect(fixture.innerHTML).toBe("some message");
|
||||
});
|
||||
|
||||
test(".translate slot props are translated", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<t t-esc="props.slots.default.message"/>`;
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<Child><t t-set-slot="default" message.translate="some message"/></Child>`;
|
||||
static components = { Child };
|
||||
}
|
||||
|
||||
await mount(Parent, fixture, { translateFn: () => "translated message" });
|
||||
expect(fixture.innerHTML).toBe("translated message");
|
||||
});
|
||||
|
||||
test("default slot with slot scope: shorthand syntax", async () => {
|
||||
let child: any;
|
||||
class Child extends Component {
|
||||
@@ -1673,6 +1701,31 @@ describe("slots", () => {
|
||||
expect(fixture.innerHTML).toBe("<div><div><p>Ablip</p><div><p>Bblip</p></div></div></div>");
|
||||
});
|
||||
|
||||
test("named slot inside named slot in t-component", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<t t-slot="brol"/>`;
|
||||
}
|
||||
class Parent extends Component {
|
||||
static template = xml`
|
||||
<Child>
|
||||
<t t-set-slot="brol">
|
||||
outer
|
||||
<t t-component="Child">
|
||||
<t t-set-slot="brol">
|
||||
<t t-esc="value"/>
|
||||
</t>
|
||||
</t>
|
||||
</t>
|
||||
</Child>`;
|
||||
static components = { Child };
|
||||
Child = Child;
|
||||
value = "inner";
|
||||
}
|
||||
await mount(Parent, fixture);
|
||||
|
||||
expect(fixture.innerHTML).toBe(" outer inner");
|
||||
});
|
||||
|
||||
test("can render only empty slot", async () => {
|
||||
class Parent extends Component {
|
||||
static template = xml`<t t-slot="default"/>`;
|
||||
|
||||
@@ -331,6 +331,32 @@ describe("t-model directive", () => {
|
||||
expect(fixture.innerHTML).toBe("<div><input><span>test</span></div>");
|
||||
});
|
||||
|
||||
test(".trim modifier implies .lazy modifier", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
<input t-model.trim="state.text"/>
|
||||
<span><t t-esc="state.text"/></span>
|
||||
</div>
|
||||
`;
|
||||
state = useState({ text: "" });
|
||||
}
|
||||
const comp = await mount(SomeComponent, fixture);
|
||||
|
||||
expect(fixture.innerHTML).toBe("<div><input><span></span></div>");
|
||||
|
||||
const input = fixture.querySelector("input")!;
|
||||
input.value = "test ";
|
||||
input.dispatchEvent(new Event("input"));
|
||||
await nextTick();
|
||||
expect(comp.state.text).toBe("");
|
||||
expect(fixture.innerHTML).toBe("<div><input><span></span></div>");
|
||||
input.dispatchEvent(new Event("change"));
|
||||
await nextTick();
|
||||
expect(comp.state.text).toBe("test");
|
||||
expect(fixture.innerHTML).toBe("<div><input><span>test</span></div>");
|
||||
});
|
||||
|
||||
test(".number modifier", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
useState,
|
||||
} from "../../src";
|
||||
import { xml } from "../../src/";
|
||||
import { DEV_MSG } from "../../src/runtime/app";
|
||||
import { elem, makeTestFixture, nextAppError, nextTick, snapshotEverything } from "../helpers";
|
||||
|
||||
let fixture: HTMLElement;
|
||||
@@ -30,7 +29,7 @@ snapshotEverything();
|
||||
|
||||
beforeAll(() => {
|
||||
console.info = (message: any) => {
|
||||
if (message === DEV_MSG()) {
|
||||
if (message === `Owl is running in 'dev' mode.`) {
|
||||
return;
|
||||
}
|
||||
info(message);
|
||||
|
||||
@@ -27,6 +27,58 @@ exports[`shadow_dom can mount app 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`shadow_dom can mount app in closed shadow dom 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[`shadow_dom can mount app inside a separate HTML document 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[`shadow_dom can mount app inside a shadow child element 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[`shadow_dom can mount app inside an element in a shadow root inside 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[`shadow_dom useRef hook 1`] = `
|
||||
"function anonymous(app, bdom, helpers
|
||||
) {
|
||||
|
||||
@@ -29,6 +29,24 @@ describe("shadow_dom", () => {
|
||||
expect(status(comp)).toBe("destroyed");
|
||||
});
|
||||
|
||||
test("can mount app in closed shadow dom", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div class="my-div"/>`;
|
||||
}
|
||||
|
||||
const container = document.createElement("div");
|
||||
fixture.appendChild(container);
|
||||
const shadow = container.attachShadow({ mode: "closed" });
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(shadow);
|
||||
const div = shadow.querySelector(".my-div");
|
||||
expect(div).not.toBe(null);
|
||||
expect(shadow.contains(div)).toBe(true);
|
||||
app.destroy();
|
||||
expect(shadow.contains(div)).toBe(false);
|
||||
expect(status(comp)).toBe("destroyed");
|
||||
});
|
||||
|
||||
test("can bind event handler", async () => {
|
||||
let a = 1;
|
||||
class SomeComponent extends Component {
|
||||
@@ -64,4 +82,73 @@ describe("shadow_dom", () => {
|
||||
await mountedProm;
|
||||
expect(comp!.div.el).toBe(shadow.querySelector(".my-div"));
|
||||
});
|
||||
|
||||
test("can mount app inside a shadow child element", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div class="my-div"/>`;
|
||||
}
|
||||
const shadow = fixture.attachShadow({ mode: "open" });
|
||||
const shadowDiv = document.createElement("div");
|
||||
shadow.append(shadowDiv);
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(shadowDiv);
|
||||
const div = shadow.querySelector(".my-div");
|
||||
expect(div).not.toBe(null);
|
||||
expect(shadow.contains(div)).toBe(true);
|
||||
app.destroy();
|
||||
expect(shadow.contains(div)).toBe(false);
|
||||
expect(status(comp)).toBe("destroyed");
|
||||
});
|
||||
|
||||
test("can mount app inside a separate HTML document", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div class="my-div"/>`;
|
||||
}
|
||||
|
||||
const separateDoc = document.implementation.createHTMLDocument();
|
||||
const container = separateDoc.createElement("div");
|
||||
separateDoc.body.appendChild(container);
|
||||
|
||||
const app = new App(SomeComponent);
|
||||
let error: Error;
|
||||
try {
|
||||
await app.mount(container);
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Cannot mount a component: the target document is not attached to a window (defaultView is missing)"
|
||||
);
|
||||
});
|
||||
|
||||
test("can mount app inside an element in a shadow root inside an iframe", async () => {
|
||||
class SomeComponent extends Component {
|
||||
static template = xml`<div class="my-div"/>`;
|
||||
}
|
||||
|
||||
const iframe = document.createElement("iframe");
|
||||
fixture.appendChild(iframe);
|
||||
|
||||
const iframeDoc = iframe.contentDocument!;
|
||||
const container = iframeDoc.createElement("div");
|
||||
iframeDoc.body.appendChild(container);
|
||||
|
||||
const shadow = container.attachShadow({ mode: "open" });
|
||||
|
||||
const shadowTarget = iframeDoc.createElement("div");
|
||||
shadow.appendChild(shadowTarget);
|
||||
|
||||
const app = new App(SomeComponent);
|
||||
const comp = await app.mount(shadowTarget);
|
||||
|
||||
const div = shadow.querySelector(".my-div");
|
||||
expect(div).not.toBe(null);
|
||||
expect(shadow.contains(div)).toBe(true);
|
||||
expect(iframeDoc.body.contains(container)).toBe(true);
|
||||
|
||||
app.destroy();
|
||||
expect(shadow.contains(div)).toBe(false);
|
||||
expect(status(comp)).toBe("destroyed");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { batched, EventBus } from "../src/runtime/utils";
|
||||
import { batched, EventBus, htmlEscape, markup } from "../src/runtime/utils";
|
||||
import { nextMicroTick } from "./helpers";
|
||||
|
||||
describe("event bus behaviour", () => {
|
||||
@@ -71,3 +71,94 @@ describe("batched", () => {
|
||||
expect(n).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
const Markup = markup("").constructor;
|
||||
describe("markup", () => {
|
||||
test("string is flagged as safe", () => {
|
||||
const html = markup("<blink>Hello</blink>");
|
||||
expect(html).toBeInstanceOf(Markup);
|
||||
});
|
||||
describe("htmlEscape", () => {
|
||||
test("htmlEscape escapes text", () => {
|
||||
const res = htmlEscape("<p>test</p>");
|
||||
expect(res.toString()).toBe("<p>test</p>");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("htmlEscape keeps html markup", () => {
|
||||
const res = htmlEscape(markup("<p>test</p>"));
|
||||
expect(res.toString()).toBe("<p>test</p>");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("htmlEscape produces empty string on undefined", () => {
|
||||
const res = htmlEscape(undefined);
|
||||
expect(res.toString()).toBe("");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("htmlEscape produces string from number", () => {
|
||||
const res = htmlEscape(10);
|
||||
expect(res.toString()).toBe("10");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("htmlEscape produces string from boolean", () => {
|
||||
const res = htmlEscape(false);
|
||||
expect(res.toString()).toBe("false");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("htmlEscape correctly escapes various links", () => {
|
||||
expect(htmlEscape("<a>this is a link</a>").toString()).toBe(
|
||||
"<a>this is a link</a>"
|
||||
);
|
||||
expect(htmlEscape(`<a href="https://www.odoo.com">odoo<a>`).toString()).toBe(
|
||||
`<a href="https://www.odoo.com">odoo<a>`
|
||||
);
|
||||
expect(htmlEscape(`<a href='https://www.odoo.com'>odoo<a>`).toString()).toBe(
|
||||
`<a href='https://www.odoo.com'>odoo<a>`
|
||||
);
|
||||
expect(htmlEscape("<a href='https://www.odoo.com'>Odoo`s website<a>").toString()).toBe(
|
||||
`<a href='https://www.odoo.com'>Odoo`s website<a>`
|
||||
);
|
||||
});
|
||||
test("htmlEscape doesn't escape already escaped content", () => {
|
||||
const res = htmlEscape("<p>test</p>");
|
||||
expect(res.toString()).toBe("<p>test</p>");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
const res2 = htmlEscape(res);
|
||||
expect(res2.toString()).toBe("<p>test</p>");
|
||||
expect(res2).toBeInstanceOf(Markup);
|
||||
expect(res2).toBe(res);
|
||||
});
|
||||
test("htmlEscape returns markup even for only-safe text", () => {
|
||||
const res = htmlEscape("safe");
|
||||
expect(res.toString()).toBe("safe");
|
||||
expect(res).toBeInstanceOf(Markup);
|
||||
});
|
||||
});
|
||||
describe("tag function", () => {
|
||||
test("interpolated values are escaped", () => {
|
||||
const maliciousInput = "<script>alert('💥💥')</script>";
|
||||
const html = markup`<b>${maliciousInput}</b>`;
|
||||
expect(html.toString()).toBe("<b><script>alert('💥💥')</script></b>");
|
||||
expect(html).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("interpolated markups aren't escaped", () => {
|
||||
const shouldBeEscaped = "<script>alert('should be escaped')</script>";
|
||||
const shouldnt = markup("<b>this is safe</b>");
|
||||
const html = markup`<div>${shouldBeEscaped} ${shouldnt}</div>`;
|
||||
expect(html.toString()).toBe(
|
||||
"<div><script>alert('should be escaped')</script> <b>this is safe</b></div>"
|
||||
);
|
||||
expect(html).toBeInstanceOf(Markup);
|
||||
});
|
||||
test("quotes in interpolated values are escaped", () => {
|
||||
const imgUrl = `lol" onerror="alert('xss')`;
|
||||
const html = markup`<img src="${imgUrl}">`;
|
||||
expect(html.toString()).toBe(`<img src="lol" onerror="alert('xss')">`);
|
||||
});
|
||||
test("already escaped content is not escaped again", () => {
|
||||
const res = htmlEscape("<p>test</p>");
|
||||
expect(res.toString()).toBe("<p>test</p>");
|
||||
const html = markup`${res}`;
|
||||
expect(html.toString()).toBe("<p>test</p>");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// this is the "compile_owl_templates" command that owl makes available when
|
||||
// installed as a node_module.
|
||||
import { existsSync, mkdirSync, writeFileSync } from "fs";
|
||||
import { dirname } from "path";
|
||||
import { compileTemplates } from "../dist/compile_templates.mjs";
|
||||
import { parseArgs } from "util";
|
||||
|
||||
const { values, positionals } = parseArgs({
|
||||
allowPositionals: true,
|
||||
options: {
|
||||
output: {
|
||||
type: "string",
|
||||
short: "o",
|
||||
default: "templates.js",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (positionals.length) {
|
||||
const result = await compileTemplates(positionals);
|
||||
const outputPath = values.output;
|
||||
const dir = dirname(outputPath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
writeFileSync(outputPath, result);
|
||||
} else {
|
||||
console.log("Please provide a path");
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const jsdom = require("jsdom");
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// add global DOM stuff for compiler
|
||||
// -----------------------------------------------------------------------------
|
||||
var document = new jsdom.JSDOM("", {});
|
||||
var window = document.window;
|
||||
global.document = window.document;
|
||||
global.window = window;
|
||||
global.DOMParser = window.DOMParser;
|
||||
global.Element = window.Element;
|
||||
global.Node = window.Node;
|
||||
// this needs to be below the jsdom stuff
|
||||
const { compile } = require("../dist/compiler.js");
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// helpers
|
||||
// -----------------------------------------------------------------------------
|
||||
async function getXmlFiles(dir) {
|
||||
let xmls = [];
|
||||
const files = await fs.promises.readdir(dir);
|
||||
const filesStats = await Promise.all(files.map((file) => fs.promises.stat(path.join(dir, file))));
|
||||
for (let i in files) {
|
||||
const name = path.join(dir, files[i]);
|
||||
if (filesStats[i].isDirectory()) {
|
||||
xmls = xmls.concat(await getXmlFiles(name));
|
||||
} else {
|
||||
if (name.endsWith(".xml")) {
|
||||
xmls.push(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return xmls;
|
||||
}
|
||||
|
||||
function writeToFile(filepath, data) {
|
||||
if (!fs.existsSync(path.dirname(filepath))) {
|
||||
fs.mkdirSync(path.dirname(filepath), { recursive: true });
|
||||
}
|
||||
fs.writeFile(filepath, data, (err) => {
|
||||
if (err) {
|
||||
process.stdout.write(`Error while writing file ${filepath}: ${err}`);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// adapted from https://medium.com/@mhagemann/the-ultimate-way-to-slugify-a-url-string-in-javascript-b8e4a0d849e1
|
||||
const a = "·-_,:;";
|
||||
const p = new RegExp(a.split("").join("|"), "g");
|
||||
|
||||
function slugify(str) {
|
||||
return str
|
||||
.replace(/\//g, "") // remove /
|
||||
.replace(/\./g, "_") // Replace . with _
|
||||
.replace(p, (c) => '_') // Replace special characters
|
||||
.replace(/&/g, "_and_") // Replace & with ‘and’
|
||||
.replace(/[^\w\-]+/g, "") // Remove all non-word characters
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// main
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
async function compileTemplates(files) {
|
||||
process.stdout.write(`Processing ${files.length} files`);
|
||||
let xmlStrings = await Promise.all(files.map((file) => fs.promises.readFile(file, "utf8")));
|
||||
|
||||
const templates = [];
|
||||
const errors = [];
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const fileName = files[i];
|
||||
const fileContent = xmlStrings[i];
|
||||
process.stdout.write(`.`);
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(fileContent, "text/xml");
|
||||
for (const template of doc.querySelectorAll("[t-name]")) {
|
||||
const name = template.getAttribute("t-name");
|
||||
if (template.hasAttribute("owl")) {
|
||||
template.removeAttribute("owl")
|
||||
}
|
||||
const fnName = slugify(name);
|
||||
try {
|
||||
const fn = compile(template).toString().replace('anonymous', fnName);
|
||||
templates.push(`"${name}": ${fn},\n`);
|
||||
} catch (e) {
|
||||
errors.push({ name, fileName, e });
|
||||
}
|
||||
}
|
||||
}
|
||||
process.stdout.write(`\n`);
|
||||
|
||||
for (let { name, fileName, e } of errors) {
|
||||
console.warn(`Error while compiling '${name}' (in file ${fileName})`);
|
||||
console.error(e);
|
||||
}
|
||||
console.log(`${templates.length} templates compiled`);
|
||||
|
||||
return `export const templates = {\n ${templates.join("\n")} \n}`;
|
||||
}
|
||||
|
||||
const templatesPath = process.argv[2];
|
||||
if (templatesPath && templatesPath.length) {
|
||||
getXmlFiles(templatesPath).then(async (files) => {
|
||||
const result = await compileTemplates(files);
|
||||
writeToFile("templates.js", result);
|
||||
});
|
||||
} else {
|
||||
console.log("Please provide a path");
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Owl devtools",
|
||||
"version": "1.2.2",
|
||||
"version": "1.3.0",
|
||||
"manifest_version": 3,
|
||||
"description": "Chrome devtools extension for Odoo Owl framework",
|
||||
"icons": {
|
||||
@@ -14,7 +14,7 @@
|
||||
"default_popup": "popup_app/popup.html"
|
||||
},
|
||||
"permissions": ["scripting", "storage"],
|
||||
"host_permissions": ["http://*/*", "https://*/*"],
|
||||
"host_permissions": ["http://*/*", "https://*/*", "file://*"],
|
||||
"content_security_policy": {
|
||||
"script-src": "self",
|
||||
"object-src": "self"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Owl devtools",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "Firefox devtools extension for Odoo Owl framework",
|
||||
"manifest_version": 2,
|
||||
"browser_specific_settings": {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { useStore } from "../store/store";
|
||||
|
||||
const { Component, useEffect, useRef } = owl;
|
||||
|
||||
export class ContextMenu extends Component {
|
||||
static template = "devtools.ContextMenu";
|
||||
static props = {
|
||||
items: Array,
|
||||
};
|
||||
setup() {
|
||||
this.store = useStore();
|
||||
this.contextMenu = useRef("contextmenu");
|
||||
useEffect(
|
||||
(position) => {
|
||||
const menu = this.contextMenu.el;
|
||||
const menuWidth = menu.offsetWidth;
|
||||
const menuHeight = menu.offsetHeight;
|
||||
let { x, y } = position;
|
||||
if (x + menuWidth > window.innerWidth) {
|
||||
x = window.innerWidth - menuWidth;
|
||||
}
|
||||
if (y + menuHeight > window.innerHeight) {
|
||||
y = window.innerHeight - menuHeight;
|
||||
}
|
||||
menu.style.left = x + "px";
|
||||
// Need 25px offset because of the main navbar from the browser devtools
|
||||
menu.style.top = y + "px";
|
||||
},
|
||||
() => [this.store.contextMenu?.position]
|
||||
);
|
||||
}
|
||||
onClickItem(action) {
|
||||
action();
|
||||
this.store.contextMenu = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<templates xml:space="preserve">
|
||||
<t t-name="devtools.ContextMenu">
|
||||
<div class="custom-menu" t-ref="contextmenu">
|
||||
<ul class="my-1">
|
||||
<li t-foreach="props.items" t-as="item" t-key="item_index" t-if="item.show" t-esc="item.title" t-on-click.stop="() => this.onClickItem(item.action)" class="custom-menu-item py-1 px-4"/>
|
||||
</ul>
|
||||
</div>
|
||||
</t>
|
||||
</templates>
|
||||
@@ -1,4 +1,4 @@
|
||||
const { Component, useRef, useEffect } = owl;
|
||||
const { Component } = owl;
|
||||
import { useStore } from "../../../store/store";
|
||||
import { ObjectTreeElement } from "./object_tree_element/object_tree_element";
|
||||
|
||||
@@ -7,23 +7,64 @@ export class DetailsWindow extends Component {
|
||||
static components = { ObjectTreeElement };
|
||||
setup() {
|
||||
this.store = useStore();
|
||||
this.contextMenu = useRef("contextmenu");
|
||||
this.contextMenuId = this.store.contextMenu.id++;
|
||||
this.contextMenuEvent;
|
||||
// Open the context menu when the ids match
|
||||
useEffect(
|
||||
(menuId) => {
|
||||
if (menuId === this.contextMenuId) {
|
||||
this.store.contextMenu.open(this.contextMenuEvent, this.contextMenu.el);
|
||||
}
|
||||
}
|
||||
|
||||
get contextMenuItems() {
|
||||
return [
|
||||
{
|
||||
title: "Inspect source code",
|
||||
show: true,
|
||||
action: () => this.store.inspectComponent("source", this.store.activeComponent.path),
|
||||
},
|
||||
() => [this.store.contextMenu.activeMenu]
|
||||
);
|
||||
{
|
||||
title: "Store as global variable",
|
||||
show: this.store.activeComponent.path.length !== 1,
|
||||
action: () =>
|
||||
this.store.logObjectInConsole([
|
||||
...this.store.activeComponent.path,
|
||||
{ type: "item", value: "component" },
|
||||
]),
|
||||
},
|
||||
{
|
||||
title: "Inspect in Elements tab",
|
||||
show: this.store.activeComponent.path.length !== 1,
|
||||
action: () => this.store.inspectComponent("DOM", this.store.activeComponent.path),
|
||||
},
|
||||
{
|
||||
title: "Force rerender",
|
||||
show: this.store.activeComponent.path.length !== 1,
|
||||
action: () => this.store.refreshComponent(this.store.activeComponent.path),
|
||||
},
|
||||
{
|
||||
title: "Store observed states as global variable",
|
||||
show: this.store.activeComponent.path.length !== 1,
|
||||
action: () =>
|
||||
this.store.logObjectInConsole([
|
||||
...this.store.activeComponent.path,
|
||||
{ type: "item", value: "subscriptions" },
|
||||
]),
|
||||
},
|
||||
{
|
||||
title: "Inspect compiled template",
|
||||
show: this.store.activeComponent.path.length !== 1,
|
||||
action: () =>
|
||||
this.store.inspectComponent("compiled template", this.store.activeComponent.path),
|
||||
},
|
||||
{
|
||||
title: "Log raw template",
|
||||
show: this.store.activeComponent.path.length !== 1,
|
||||
action: () => this.store.inspectComponent("raw template", this.store.activeComponent.path),
|
||||
},
|
||||
{
|
||||
title: "Store as global variable",
|
||||
show: this.store.activeComponent.path.length === 1,
|
||||
action: () => this.store.logObjectInConsole([...this.store.activeComponent.path]),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
openMenu(ev) {
|
||||
this.contextMenuEvent = ev;
|
||||
this.store.contextMenu.activeMenu = this.contextMenuId;
|
||||
this.store.openContextMenu(ev, this.contextMenuItems);
|
||||
}
|
||||
|
||||
toggleCategory(ev, category) {
|
||||
|
||||