als-layout 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build-readme.js +8 -0
- package/docs/#.md +27 -0
- package/docs/0-change-log.md +16 -0
- package/docs/1-basic-usage.md +49 -0
- package/docs/2-cloning.md +19 -0
- package/docs/3-advanced-usage.md +43 -0
- package/docs/4-render.md +71 -0
- package/lib/elements/script.js +1 -1
- package/lib/layout.js +23 -58
- package/lib/onload.js +9 -0
- package/lib/{build-app.js → render/build-app.js} +8 -4
- package/lib/{component-hierarchy.js → render/component-hierarchy.js} +8 -0
- package/lib/render/update.js +19 -0
- package/package.json +2 -2
- package/readme.md +145 -43
- package/tests/build-app.test.js +16 -5
- package/tests/component-hierarchy.test.js +1 -1
- package/tests/constructor.test.js +5 -12
- package/tests/elements.test.js +14 -8
- package/tests/layout.test.js +55 -10
- package/lib/build-root.js +0 -15
- package/lib/data/languages.js +0 -2
- package/tests/build-root.test.js +0 -45
package/readme.md
CHANGED
|
@@ -1,22 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# als-layout Documentation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Library Description
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### What is it?
|
|
6
|
+
`als-layout` is a JavaScript library designed to simplify and enhance the process of constructing and managing web page layouts. It provides a comprehensive API for modifying HTML documents dynamically, allowing developers to add, update, and manipulate various elements such as meta tags, styles, scripts, and more.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
-
|
|
8
|
+
### Why is it needed?
|
|
9
|
+
Managing HTML document structures and their contents can often become repetitive and error-prone when done manually. `als-modular` offers a structured and reusable approach, reducing development time and improving code maintainability.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
### What can you do with it and where can it be used?
|
|
12
|
+
The `als-layout` library is versatile, suitable for:
|
|
13
|
+
- Building dynamic web pages that require frequent updates to their metadata, styles, or scripts.
|
|
14
|
+
- Creating templating systems where multiple page layouts share similar structures but differ in content or styling.
|
|
15
|
+
- Developing web applications that need to dynamically adjust their UI based on user interactions or data changes.
|
|
16
|
+
This library is particularly useful in environments where rapid development and modular design are prioritized.
|
|
17
|
+
|
|
18
|
+
## Installation and Adding
|
|
19
|
+
To use the `als-layout` library in your project, you can install it via npm and then include it in your JavaScript files:
|
|
11
20
|
|
|
12
21
|
```bash
|
|
13
22
|
npm i als-layout
|
|
14
23
|
```
|
|
15
24
|
|
|
25
|
+
```js
|
|
26
|
+
const Layout = require('als-layout')
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Change Log
|
|
30
|
+
* V2.2.0
|
|
31
|
+
* fixed empty update function if no components for $App
|
|
32
|
+
* if component function return string, it will element.innerHTML = string
|
|
33
|
+
* Now each Layout extends Document (als-document)
|
|
34
|
+
* layout.$ and layout.$$
|
|
35
|
+
* Also $App.$ and $App.$$ on backend too
|
|
36
|
+
* no langs validation any more
|
|
37
|
+
* lang method instead
|
|
38
|
+
* no layout.cached
|
|
39
|
+
* charset meta tag allready exists by default
|
|
40
|
+
* V2.1.0
|
|
41
|
+
* updated als-document version
|
|
42
|
+
* [part] attribute for static components
|
|
43
|
+
* onload() method
|
|
44
|
+
* bugs fixed
|
|
16
45
|
## Basic Usage
|
|
17
46
|
|
|
47
|
+
### Initialization
|
|
48
|
+
To start using `als-layout`, you first need to create a new instance of `Layout`. This instance will serve as the foundation for building and modifying your web page.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const Layout = require('als-layout');
|
|
52
|
+
const layout = new Layout();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Adding Different Elements
|
|
56
|
+
Once you have your `Layout` instance, you can easily add or modify various elements of your web page. Here are some examples of how you can use the library to customize your layout:
|
|
57
|
+
|
|
18
58
|
```js
|
|
19
|
-
const Layout = require('als-layout')
|
|
20
59
|
const layout = new Layout()
|
|
21
60
|
.charset() // default UTF-8
|
|
22
61
|
.viewport() // default width=device-width, initial-scale=1.0
|
|
@@ -36,91 +75,154 @@ const layout = new Layout()
|
|
|
36
75
|
// Accessors for document parts
|
|
37
76
|
layout.body // getter for body element (if not exists, created)
|
|
38
77
|
layout.head // getter for head element (if not exists, created)
|
|
39
|
-
layout.html // getter for html
|
|
78
|
+
layout.html // getter for html Element (if not exists, created)
|
|
40
79
|
|
|
41
80
|
// Outputs
|
|
42
|
-
layout.rawHtml // raw
|
|
43
|
-
layout.
|
|
44
|
-
|
|
81
|
+
layout.rawHtml // raw HTML of the document
|
|
82
|
+
layout.clone // creates a new layout object clone for current object
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### onload
|
|
86
|
+
|
|
87
|
+
By adding onload attribute, you can run scripts for each element after dom content has loaded.
|
|
88
|
+
|
|
89
|
+
Example how it works:
|
|
90
|
+
```js
|
|
91
|
+
const layout = new Layout().charset().viewport().title('On load').onload()
|
|
92
|
+
layout.body.innerHTML = /*html*/`<div onload="this.innerHTML = 'new content'">original content</div>`
|
|
93
|
+
```
|
|
94
|
+
## Cloning Functionality
|
|
95
|
+
|
|
96
|
+
### What is Cloning and Why is it Necessary?
|
|
97
|
+
Cloning in the `als-layout` library refers to creating a complete, independent copy of the existing `Layout` instance. This functionality is crucial when you need to generate multiple pages or versions of a page from a single base layout without affecting the original setup.
|
|
98
|
+
|
|
99
|
+
### How to Use Cloning
|
|
100
|
+
To clone a `Layout` instance, simply use the `clone` method. This method creates a new `Layout` instance with the same properties and settings as the original, allowing for independent modifications without interference.
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
const newLayout = layout.clone;
|
|
45
104
|
```
|
|
46
105
|
|
|
106
|
+
### Benefits of Cloning
|
|
107
|
+
- **Efficiency:** Cloning is highly efficient, especially for creating pages with similar structures but different content or styles. It avoids the overhead of reinitializing and reconfiguring a new `Layout` instance from scratch.
|
|
108
|
+
- **Speed:** Cloning is fast, typically taking less than 20ms even for large pages. This makes it ideal for high-performance web applications that need to dynamically generate content.
|
|
109
|
+
- **Isolation:** Changes made to a cloned `Layout` do not affect the original, ensuring that each instance can be modified independently based on specific requirements.
|
|
110
|
+
|
|
111
|
+
Cloning is particularly useful in scenarios where templates or base layouts are used repeatedly with slight variations, providing a robust and scalable solution for web page generation.
|
|
112
|
+
|
|
113
|
+
|
|
47
114
|
## Advanced Usage
|
|
48
115
|
|
|
116
|
+
The `als-layout` library allows for sophisticated manipulation of web page layouts, providing robust tools for creating dynamic and complex web pages. Below is an advanced example demonstrating various capabilities of the library:
|
|
117
|
+
|
|
49
118
|
```js
|
|
50
|
-
const Layout = require('
|
|
119
|
+
const Layout = require('als-layout')
|
|
51
120
|
|
|
121
|
+
// Starting with a basic HTML template and specifying the host for URL methods
|
|
52
122
|
const raw = /*html*/`<html></html>`
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
lang:'fr' // for <html lang="fr"></html>
|
|
56
|
-
}
|
|
57
|
-
const layout = new Layout(raw, options)
|
|
123
|
+
const host = 'http://example.com';
|
|
124
|
+
const layout = new Layout(raw, host).lang('fr')
|
|
58
125
|
console.log(layout.rawHtml)
|
|
59
|
-
// <!DOCTYPE html><html lang="fr"><head></head><body></body></html>
|
|
126
|
+
// <!DOCTYPE html><html lang="fr"><head></p></head><body></body></html>
|
|
60
127
|
|
|
128
|
+
// Cloning the initial layout to create a specialized page
|
|
61
129
|
const homePage = layout.clone
|
|
130
|
+
homeAutoReload = layout.clone
|
|
62
131
|
homePage.title('Home page')
|
|
63
132
|
homePage.body.innerHTML = /*html*/`<h1>Home page</h1>`
|
|
64
133
|
console.log(homePage.rawHtml)
|
|
65
134
|
// <!DOCTYPE html><html lang="fr"><head><title>Home page</title><meta property="og:title" content="Home page"></head><body><h1>Home page</h1></body></html>
|
|
135
|
+
|
|
136
|
+
// Adding script that reloads the page every minute
|
|
137
|
+
homeAutoReload.script({}, 'setTimeout(function() { window.location.reload(); }, 60000);', false)
|
|
138
|
+
console.log(homeAutoReload.rawHtml)
|
|
139
|
+
// <!DOCTYPE html><html lang="fr"><head><title>Automatic Reload Page</title><meta property="og:title" content="Automatic Reload Page"></head><body><script>setTimeout(function() { window.location.reload(); }, 60000);</script></body></html>
|
|
140
|
+
|
|
141
|
+
// Demonstrating dynamic stylesheet linkage with versioning
|
|
142
|
+
const styleVersion = '1.1';
|
|
143
|
+
homePage.link('/css/main.css', styleVersion)
|
|
144
|
+
console.log(homePage.rawHtml)
|
|
145
|
+
// Includes link to the stylesheet with version parameter to ensure fresh cache
|
|
66
146
|
```
|
|
67
147
|
|
|
148
|
+
In this example:
|
|
149
|
+
- We start with a basic HTML template and use the `lang` method to set the language.
|
|
150
|
+
- We use the `clone` method to create two versions of the base layout: one for the home page and another that automatically reloads every minute.
|
|
151
|
+
- We manipulate the `body` of the `homePage` to include custom HTML.
|
|
152
|
+
- We add a script to `homeAutoReload` that sets up an automatic page reload, showcasing how to insert JavaScript dynamically.
|
|
153
|
+
- We dynamically add a versioned link to a stylesheet in the `homePage`, demonstrating control over caching and resource management.
|
|
154
|
+
|
|
155
|
+
This advanced example illustrates how `als-layout` can be used to handle complex scenarios and requirements in web development, enhancing the flexibility and power at your disposal.
|
|
156
|
+
|
|
157
|
+
|
|
68
158
|
## Rendering
|
|
69
159
|
|
|
70
|
-
Each
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
layout
|
|
74
|
-
layout.utils
|
|
75
|
-
layout.actions
|
|
76
|
-
```
|
|
160
|
+
Each instance of `Layout` comes equipped with a `render` method that compiles the HTML structure and embeds a JavaScript object to manage the page dynamically. This object, known as `window.$App`, allows for real-time interaction and updates within the page.
|
|
161
|
+
|
|
162
|
+
### Structure
|
|
163
|
+
The layout object houses several key properties that facilitate dynamic content management:
|
|
77
164
|
|
|
78
|
-
|
|
165
|
+
- `layout.data`: Stores the data that can be used across the page, such as state variables or configuration settings.
|
|
166
|
+
- `layout.components`: Holds functions that define the behavior and rendering logic for components identified by specific attributes in the HTML.
|
|
167
|
+
- `layout.utils`: Contains utility functions that can be used throughout the page for common tasks.
|
|
168
|
+
- `layout.actions`: Methods that can be triggered by user interaction, often modifying `layout.data` and updating the page accordingly.
|
|
169
|
+
|
|
170
|
+
### The Render Method
|
|
171
|
+
The `render` method processes all elements with a `component` attribute, dynamically generating their content and behavior based on the defined components. After rendering, the page includes the `window.$App` JavaScript object, which provides methods and properties to interact with the page elements and data:
|
|
79
172
|
|
|
80
173
|
```js
|
|
81
174
|
window.$App = {
|
|
82
|
-
data,
|
|
83
|
-
components,
|
|
84
|
-
utils,
|
|
85
|
-
actions,
|
|
86
|
-
$(selector, parent=document),
|
|
87
|
-
$$(selector, parent=document),
|
|
88
|
-
update(element)
|
|
175
|
+
data, // Access to the layout data object
|
|
176
|
+
components, // Access to components functions
|
|
177
|
+
utils, // Access to utility functions
|
|
178
|
+
actions, // Access to action functions
|
|
179
|
+
$(selector, parent = document), // Equivalent to querySelector
|
|
180
|
+
$$(selector, parent = document), // Equivalent to querySelectorAll
|
|
181
|
+
update(element) { // Updates the element if it has a component attribute
|
|
182
|
+
// Update logic here
|
|
183
|
+
}
|
|
89
184
|
}
|
|
90
185
|
```
|
|
91
186
|
|
|
92
|
-
|
|
93
|
-
|
|
187
|
+
### Counter Example
|
|
188
|
+
To demonstrate dynamic interaction, consider a counter that can be increased or decreased through user input:
|
|
94
189
|
|
|
95
190
|
```js
|
|
96
191
|
const fs = require('fs')
|
|
97
192
|
const Layout = require('als-layout')
|
|
98
193
|
|
|
99
|
-
|
|
100
|
-
layout
|
|
194
|
+
// Create and configure the layout
|
|
195
|
+
const layout = new Layout().title('Counter')
|
|
196
|
+
layout.data.counter = 0 // Initialize counter data
|
|
197
|
+
|
|
198
|
+
// Define a component for displaying the counter
|
|
101
199
|
layout.components.counter = function(element, $App) {
|
|
102
200
|
element.innerHTML = `${$App.data.counter}`
|
|
103
201
|
}
|
|
104
202
|
|
|
203
|
+
// Define actions for increasing and decreasing the counter
|
|
105
204
|
layout.actions = {
|
|
106
|
-
increase: () => { $App.data.counter++; $App.update($App.$('[component=counter]')) },
|
|
107
|
-
decrease: () => { $
|
|
205
|
+
increase: () => { $App.data.counter++; $App.update($App.$('[component=counter]')); },
|
|
206
|
+
decrease: () => { $We render the html page to measure and write to a document.app.data.counter--; $Page updates are shown in real-time on the rendered HTML.$App.update($App.$('[component=counter]')); }
|
|
108
207
|
}
|
|
109
208
|
|
|
209
|
+
// Add buttons and the counter display to the body
|
|
110
210
|
layout.body.innerHTML = /*html*/`
|
|
111
211
|
<button onclick="$App.actions.increase()">Increase</button>
|
|
112
212
|
<span component="counter"></span>
|
|
113
213
|
<button onclick="$App.actions.decrease()">Decrease</button>
|
|
114
214
|
`
|
|
215
|
+
|
|
216
|
+
// Measure render time and generate HTML
|
|
115
217
|
const time1 = performance.now()
|
|
116
218
|
const rawHtml = layout.render()
|
|
117
219
|
const time2 = performance.now()
|
|
118
220
|
console.log(`${time2 - time1}ms`) // e.g., 1.0649ms
|
|
119
221
|
|
|
222
|
+
// Write the output to a file
|
|
120
223
|
fs.writeFileSync('counter.html', rawHtml, 'utf-8')
|
|
121
224
|
```
|
|
122
225
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
```
|
|
226
|
+
### Advanced Rendering Details
|
|
227
|
+
- **Component Indexing:** Each component is assigned a `componentIndex` during rendering, providing a unique index within its parent component.
|
|
228
|
+
- **Part Attribute:** Using the `part` attribute in a component prevents it from being added to `$App.components`, unless it is nested within another component.
|
package/tests/build-app.test.js
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
1
1
|
const assert = require('assert');
|
|
2
2
|
const { describe, it } = require('node:test');
|
|
3
|
-
const build$App = require('../lib/build-app');
|
|
3
|
+
const build$App = require('../lib/render/build-app');
|
|
4
4
|
|
|
5
5
|
describe('build-$App Functionality', () => {
|
|
6
|
+
|
|
6
7
|
it('should handle empty input arrays correctly', () => {
|
|
7
8
|
const done = [];
|
|
8
9
|
const layout = {
|
|
9
10
|
components: {},
|
|
10
|
-
update: () => { }
|
|
11
11
|
};
|
|
12
12
|
const result = build$App(done, layout);
|
|
13
|
-
assert.strictEqual(result, 'window.$App = {browser:true,data:undefined,components:{},actions:{},utils:{},update:
|
|
13
|
+
assert.strictEqual(result, 'window.$App = {browser:true,data:undefined,components:{},actions:{},utils:{},update:()=>{},$:function $(selector,parent = document) {return parent.querySelector(selector)},$$:function $$(selector,parent = document) {return [...parent.querySelectorAll(selector)]}}');
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
it('should process multiple components correctly', () => {
|
|
17
|
+
const called = []
|
|
17
18
|
const done = [
|
|
18
|
-
{
|
|
19
|
-
|
|
19
|
+
{
|
|
20
|
+
getAttribute: (attr) => attr === 'component' ? 'comp1' : null,
|
|
21
|
+
removeAttribute(att) {called.push(att)},
|
|
22
|
+
$$(){return []},
|
|
23
|
+
ancestors:2
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
getAttribute: (attr) => attr === 'component' ? 'comp2' : null,
|
|
27
|
+
removeAttribute(att) {called.push(att)},
|
|
28
|
+
$$(){return []},
|
|
29
|
+
ancestors:2
|
|
30
|
+
}
|
|
20
31
|
];
|
|
21
32
|
const layout = {
|
|
22
33
|
components: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { parseHTML } = require('als-document')
|
|
2
2
|
const assert = require('assert');
|
|
3
3
|
const { describe, it } = require('node:test')
|
|
4
|
-
const componentHierarchy = require('../lib/component-hierarchy')
|
|
4
|
+
const componentHierarchy = require('../lib/render/component-hierarchy')
|
|
5
5
|
|
|
6
6
|
function shuffleArray(array) {
|
|
7
7
|
for (let i = array.length - 1; i > 0; i--) {
|
|
@@ -17,7 +17,8 @@ describe('Layout Initialization', () => {
|
|
|
17
17
|
it('should allow setting a custom language', () => {
|
|
18
18
|
const customLang = 'fr';
|
|
19
19
|
const layout = new Layout(undefined, { lang: customLang });
|
|
20
|
-
|
|
20
|
+
layout.lang(customLang)
|
|
21
|
+
assert(layout.html.getAttribute('lang') === customLang, `language is not set to ${customLang}`);
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
it('should initialize development mode as undefined or false', () => {
|
|
@@ -32,16 +33,8 @@ describe('Layout Initialization', () => {
|
|
|
32
33
|
|
|
33
34
|
it('should allow setting a custom host', () => {
|
|
34
35
|
const customHost = 'http://localhost';
|
|
35
|
-
const layout = new Layout(undefined,
|
|
36
|
-
|
|
36
|
+
const layout = new Layout(undefined,customHost);
|
|
37
|
+
console.log(layout.URL)
|
|
38
|
+
assert.strictEqual(layout.URL, customHost, `host is not set to ${customHost}`);
|
|
37
39
|
});
|
|
38
|
-
|
|
39
|
-
it('check method',() => {
|
|
40
|
-
const cached = cacheDoc(new Root())
|
|
41
|
-
const layout = new Layout(cached)
|
|
42
|
-
const root = layout.root
|
|
43
|
-
assert(root.$('html') !== null)
|
|
44
|
-
assert(root.$('head') !== null)
|
|
45
|
-
assert(root.$('body') !== null)
|
|
46
|
-
})
|
|
47
40
|
});
|
package/tests/elements.test.js
CHANGED
|
@@ -18,11 +18,10 @@ describe('Charset tests', () => {
|
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
it('should insert charset at second position if not present', () => {
|
|
21
|
-
layout.head.insert(1, new SingleNode('title', {}));
|
|
22
21
|
const charset = 'UTF-8';
|
|
23
22
|
layout.charset(charset);
|
|
24
|
-
assert.strictEqual(layout.head.childNodes[
|
|
25
|
-
assert.strictEqual(layout.head.childNodes[
|
|
23
|
+
assert.strictEqual(layout.head.childNodes[0].tagName, 'META', 'Meta should be at second position');
|
|
24
|
+
assert.strictEqual(layout.head.childNodes[0].getAttribute('charset'), charset, 'Charset not set correctly');
|
|
26
25
|
});
|
|
27
26
|
|
|
28
27
|
it('should add charset correctly', () => {
|
|
@@ -37,7 +36,7 @@ describe('Favicon tests', () => {
|
|
|
37
36
|
|
|
38
37
|
beforeEach(() => {
|
|
39
38
|
layout = new Layout();
|
|
40
|
-
layout.head.insert(1, new SingleNode('title', {})); // Добавляем элемент title для проверки позиции
|
|
39
|
+
// layout.head.insert(1, new SingleNode('title', {})); // Добавляем элемент title для проверки позиции
|
|
41
40
|
});
|
|
42
41
|
|
|
43
42
|
it('should update favicon href if link[rel="icon"] already exists', () => {
|
|
@@ -51,8 +50,8 @@ describe('Favicon tests', () => {
|
|
|
51
50
|
it('should insert favicon at second position if not present', () => {
|
|
52
51
|
const faviconHref = 'favicon.ico';
|
|
53
52
|
layout.favicon(faviconHref);
|
|
54
|
-
assert.strictEqual(layout.head.childNodes[
|
|
55
|
-
assert.strictEqual(layout.head.childNodes[
|
|
53
|
+
assert.strictEqual(layout.head.childNodes[0].tagName, 'LINK', 'Favicon link should be at second position');
|
|
54
|
+
assert.strictEqual(layout.head.childNodes[0].getAttribute('href'), faviconHref, 'Favicon href not set correctly');
|
|
56
55
|
});
|
|
57
56
|
|
|
58
57
|
it('should add favicon correctly', () => {
|
|
@@ -266,7 +265,7 @@ describe('Scripts', () => {
|
|
|
266
265
|
it('should add script after body when head is false', () => {
|
|
267
266
|
const scriptContent = 'console.log("Script in body");';
|
|
268
267
|
layout.script({ src: 'scriptbody.js' }, scriptContent, false);
|
|
269
|
-
assert.strictEqual(layout.body.next
|
|
268
|
+
assert.strictEqual(layout.body.next.innerHTML, scriptContent, 'Script should be added to body');
|
|
270
269
|
});
|
|
271
270
|
|
|
272
271
|
it('should append version parameter correctly when src already has parameters', () => {
|
|
@@ -329,7 +328,6 @@ describe('Url', () => {
|
|
|
329
328
|
layout.url('not-a-url', 'aa');
|
|
330
329
|
assert.strictEqual(layout.root.$('link[rel="canonical"]'), null, 'Canonical URL should not be added for invalid URLs');
|
|
331
330
|
});
|
|
332
|
-
|
|
333
331
|
|
|
334
332
|
it('should add url correctly', () => {
|
|
335
333
|
const url = 'http://localhost';
|
|
@@ -427,4 +425,12 @@ describe('description and title', () => {
|
|
|
427
425
|
assert(layout.root.$('[property="og:title"]').getAttribute('content') === title, 'Title not set correctly');
|
|
428
426
|
});
|
|
429
427
|
|
|
428
|
+
it('should add title if no title tag', () => {
|
|
429
|
+
layout.$('title').remove()
|
|
430
|
+
const title = 'Test Title';
|
|
431
|
+
layout.title(title);
|
|
432
|
+
assert(layout.root.$('title').innerHTML === title, 'Title not set correctly');
|
|
433
|
+
assert(layout.root.$('[property="og:title"]').getAttribute('content') === title, 'Title not set correctly');
|
|
434
|
+
});
|
|
435
|
+
|
|
430
436
|
});
|
package/tests/layout.test.js
CHANGED
|
@@ -31,6 +31,12 @@ describe('Layout Integrative tests', () => {
|
|
|
31
31
|
assert(layout.root.$('title') === null, 'Title element was not removed correctly');
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
+
it('onload', () => {
|
|
35
|
+
layout.onload()
|
|
36
|
+
const script = layout.root.$('script')
|
|
37
|
+
assert(script.innerHTML.includes(`document.addEventListener('DOMContentLoaded'`))
|
|
38
|
+
})
|
|
39
|
+
|
|
34
40
|
});
|
|
35
41
|
|
|
36
42
|
describe('HTML Structure Initialization', () => {
|
|
@@ -41,7 +47,7 @@ describe('HTML Structure Initialization', () => {
|
|
|
41
47
|
});
|
|
42
48
|
|
|
43
49
|
it('should initialize html element correctly', () => {
|
|
44
|
-
assert.strictEqual(layout.html.tagName, '
|
|
50
|
+
assert.strictEqual(layout.html.tagName, 'HTML', 'HTML element should be initialized');
|
|
45
51
|
});
|
|
46
52
|
|
|
47
53
|
it('should not recreate html element if it already exists', () => {
|
|
@@ -50,7 +56,7 @@ describe('HTML Structure Initialization', () => {
|
|
|
50
56
|
});
|
|
51
57
|
|
|
52
58
|
it('should initialize body element correctly', () => {
|
|
53
|
-
assert.strictEqual(layout.body.tagName, '
|
|
59
|
+
assert.strictEqual(layout.body.tagName, 'BODY', 'Body element should be initialized');
|
|
54
60
|
});
|
|
55
61
|
|
|
56
62
|
it('should not recreate body element if it already exists', () => {
|
|
@@ -59,7 +65,7 @@ describe('HTML Structure Initialization', () => {
|
|
|
59
65
|
});
|
|
60
66
|
|
|
61
67
|
it('should initialize head element correctly', () => {
|
|
62
|
-
assert.strictEqual(layout.head.tagName, '
|
|
68
|
+
assert.strictEqual(layout.head.tagName, 'HEAD', 'Head element should be initialized');
|
|
63
69
|
});
|
|
64
70
|
|
|
65
71
|
it('should not recreate head element if it already exists', () => {
|
|
@@ -124,22 +130,60 @@ describe('Component Updating', () => {
|
|
|
124
130
|
assert(data.test === 'hello')
|
|
125
131
|
})
|
|
126
132
|
|
|
133
|
+
it('should add part to $App if it is component`s descendant', () => {
|
|
134
|
+
layout.body.innerHTML = /*html*/`
|
|
135
|
+
<div component="parent">
|
|
136
|
+
<div component="child" part></div>
|
|
137
|
+
</div>`;
|
|
138
|
+
layout.components.parent = (element, $App) => {};
|
|
139
|
+
layout.components.child = (element, $App) => {};
|
|
140
|
+
|
|
141
|
+
const rawHtml = layout.render();
|
|
142
|
+
const script = rawHtml.match(/<[^>]*?script.*?>[^<]*<\/[^>]*?script.*?>/g)[0]
|
|
143
|
+
.replace('<script>window.$App = ', '')
|
|
144
|
+
.replace('</script>', '')
|
|
145
|
+
const fn = new Function(`return ${script}`)
|
|
146
|
+
|
|
147
|
+
const {components} = fn()
|
|
148
|
+
assert(components.parent !== undefined)
|
|
149
|
+
assert(components.child !== undefined)
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should not add part to $App if it has no component ancestor', () => {
|
|
153
|
+
layout.body.innerHTML = /*html*/`
|
|
154
|
+
<div component="comp1"></div>
|
|
155
|
+
<div component="comp2" part></div>
|
|
156
|
+
`;
|
|
157
|
+
layout.components.comp1 = (element, $App) => {};
|
|
158
|
+
layout.components.comp2 = (element, $App) => {};
|
|
159
|
+
|
|
160
|
+
const rawHtml = layout.render();
|
|
161
|
+
const script = rawHtml.match(/<[^>]*?script.*?>[^<]*<\/[^>]*?script.*?>/g)[0]
|
|
162
|
+
.replace('<script>window.$App = ', '')
|
|
163
|
+
.replace('</script>', '')
|
|
164
|
+
const fn = new Function(`return ${script}`)
|
|
165
|
+
|
|
166
|
+
const {components} = fn()
|
|
167
|
+
assert(components.comp1 !== undefined)
|
|
168
|
+
assert(components.comp2 === undefined)
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('If no components, should add empty update function to $App', () => {
|
|
172
|
+
const rawHtml = layout.render();
|
|
173
|
+
assert( rawHtml.includes('update:()=>{}'));
|
|
174
|
+
});
|
|
175
|
+
|
|
127
176
|
});
|
|
128
177
|
|
|
129
|
-
describe('
|
|
178
|
+
describe('Clone Testing', () => {
|
|
130
179
|
let layout;
|
|
131
180
|
beforeEach(() => {
|
|
132
181
|
layout = new Layout();
|
|
133
182
|
});
|
|
134
183
|
|
|
135
|
-
it('should retrieve cached version of the layout', () => {
|
|
136
|
-
const cachedVersion = layout.cached;
|
|
137
|
-
assert.doesNotThrow(() => JSON.stringify(cachedVersion)) // should be object without recursions
|
|
138
|
-
assert(buildFromCache(cachedVersion).innerHTML === layout.rawHtml)
|
|
139
|
-
});
|
|
140
|
-
|
|
141
184
|
it('should clone the layout correctly', () => {
|
|
142
185
|
const clone = layout.clone;
|
|
186
|
+
console.log(clone.constructor.name)
|
|
143
187
|
assert(clone instanceof Layout, 'Clone should be an instance of Layout');
|
|
144
188
|
assert.notStrictEqual(clone, layout, 'Clone should not be the same instance as the original');
|
|
145
189
|
});
|
|
@@ -158,3 +202,4 @@ describe('Version method tests', () => {
|
|
|
158
202
|
assert.strictEqual(layout.v, version, 'Version should be set correctly');
|
|
159
203
|
});
|
|
160
204
|
});
|
|
205
|
+
|
package/lib/build-root.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const { buildFromCache, Root, parseHTML, cacheDoc } = require('als-document')
|
|
2
|
-
|
|
3
|
-
function buildRoot(item) {
|
|
4
|
-
const root =
|
|
5
|
-
(item instanceof Root) ? buildFromCache(cacheDoc(item))
|
|
6
|
-
: (typeof item === 'string') ? parseHTML(item)
|
|
7
|
-
: (typeof item === 'object' && item.tagName) ? buildFromCache(item)
|
|
8
|
-
: new Root()
|
|
9
|
-
|
|
10
|
-
const firstNode = root.childNodes[0]
|
|
11
|
-
if(!firstNode || firstNode.tagName !== '!DOCTYPE') root.insert(1,/*html*/`<!DOCTYPE html>`)
|
|
12
|
-
return root
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = buildRoot
|