als-layout 7.0.0 → 7.1.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/index.mjs +89 -81
- package/layout.js +89 -81
- package/package.json +16 -4
- package/readme.md +186 -163
- package/src/layout.js +93 -85
- package/build.js +0 -8
- package/tests/constructor.test.js +0 -28
- package/tests/description.test.js +0 -33
- package/tests/favicon.test.js +0 -36
- package/tests/image.test.js +0 -25
- package/tests/integrative.test.js +0 -83
- package/tests/keywords.test.js +0 -47
- package/tests/link.test.js +0 -33
- package/tests/script.test.js +0 -42
- package/tests/style.test.js +0 -34
- package/tests/url.test.js +0 -42
- package/tests/viewport.test.js +0 -41
package/tests/favicon.test.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const { SingleNode } = require('als-document')
|
|
4
|
-
const Layout = require('../src/layout.js');
|
|
5
|
-
|
|
6
|
-
describe('Favicon tests', () => {
|
|
7
|
-
let layout;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
layout = new Layout();
|
|
11
|
-
// layout.head.insert(1, new SingleNode('title', {})); // Добавляем элемент title для проверки позиции
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('should update favicon href if link[rel="icon"] already exists', () => {
|
|
15
|
-
const oldHref = 'old-favicon.ico';
|
|
16
|
-
layout.head.insert(2, new SingleNode('link', { rel: 'icon', href: oldHref, type: 'image/x-icon' }));
|
|
17
|
-
const newHref = 'new-favicon.ico';
|
|
18
|
-
layout.favicon(newHref);
|
|
19
|
-
assert.strictEqual(layout.root.$('link[rel="icon"]').getAttribute('href'), newHref, 'Favicon href should be updated');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should insert favicon at the end of head if not present', () => {
|
|
23
|
-
const faviconHref = 'favicon.ico';
|
|
24
|
-
layout.favicon(faviconHref);
|
|
25
|
-
const element = layout.head.childNodes[layout.head.childNodes.length - 1]
|
|
26
|
-
assert.strictEqual(element.tagName, 'LINK', 'Favicon link should be at second position');
|
|
27
|
-
assert.strictEqual(element.getAttribute('href'), faviconHref, 'Favicon href not set correctly');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should add favicon correctly', () => {
|
|
31
|
-
const faviconHref = 'favicon.ico';
|
|
32
|
-
layout.favicon(faviconHref);
|
|
33
|
-
assert.strictEqual(layout.root.$('link[rel="icon"]').getAttribute('href'), faviconHref, 'Favicon not set correctly');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
});
|
package/tests/image.test.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it,beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
|
|
5
|
-
describe('Image tests', () => {
|
|
6
|
-
let layout;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
layout = new Layout();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should add twitter:card meta tag', () => {
|
|
13
|
-
const imageUrl = 'test-image.jpg';
|
|
14
|
-
layout.image(imageUrl);
|
|
15
|
-
assert.strictEqual(layout.root.$('meta[name="twitter:card"]').getAttribute('content'), 'summary_large_image', 'twitter:card not set correctly');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should add image correctly', () => {
|
|
19
|
-
const imageUrl = 'test-image.jpg';
|
|
20
|
-
layout.image(imageUrl);
|
|
21
|
-
assert.strictEqual(layout.root.$('meta[property="og:image"]').getAttribute('content'), imageUrl, 'Image not set correctly');
|
|
22
|
-
assert.strictEqual(layout.root.$('meta[name="twitter:image"]').getAttribute('content'), imageUrl, 'Image not set correctly');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
});
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
|
|
5
|
-
describe('Layout Integrative tests', () => {
|
|
6
|
-
let layout;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => layout = new Layout());
|
|
9
|
-
|
|
10
|
-
it('should integrate multiple elements correctly', () => {
|
|
11
|
-
layout.title('Test Title');
|
|
12
|
-
layout.description('Test Description');
|
|
13
|
-
layout.keywords(['keyword1', 'keyword2']);
|
|
14
|
-
|
|
15
|
-
assert(layout.root.$('title').innerHTML === 'Test Title', 'Title not integrated correctly');
|
|
16
|
-
assert(layout.root.$('meta[name="description"]').getAttribute('content') === 'Test Description', 'Description not integrated correctly');
|
|
17
|
-
assert(layout.root.$('meta[name="keywords"]').getAttribute('content') === 'keyword1,keyword2', 'Keywords not integrated correctly');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should update elements correctly when added multiple times', () => {
|
|
21
|
-
layout.title('First Title');
|
|
22
|
-
assert(layout.root.$('title').innerHTML === 'First Title', 'Title did not update correctly when added multiple times');
|
|
23
|
-
layout.title('Second Title');
|
|
24
|
-
assert(layout.root.$('title').innerHTML === 'Second Title', 'Title did not update correctly when added multiple times');
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('should remove elements correctly', () => {
|
|
28
|
-
layout.title('Test Title');
|
|
29
|
-
layout.root.$('title').remove();
|
|
30
|
-
assert(layout.root.$('title') === null, 'Title element was not removed correctly');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('HTML Structure Initialization', () => {
|
|
36
|
-
let layout;
|
|
37
|
-
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
layout = new Layout();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should initialize html element correctly', () => {
|
|
43
|
-
assert.strictEqual(layout.html.tagName, 'HTML', 'HTML element should be initialized');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should not recreate html element if it already exists', () => {
|
|
47
|
-
const existingHtml = layout.html; // вызовем один раз
|
|
48
|
-
assert.strictEqual(layout.html, existingHtml, 'HTML element should not be recreated');
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should initialize body element correctly', () => {
|
|
52
|
-
assert.strictEqual(layout.body.tagName, 'BODY', 'Body element should be initialized');
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('should not recreate body element if it already exists', () => {
|
|
56
|
-
const existingBody = layout.body; // вызовем один раз
|
|
57
|
-
assert.strictEqual(layout.body, existingBody, 'Body element should not be recreated');
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should initialize head element correctly', () => {
|
|
61
|
-
assert.strictEqual(layout.head.tagName, 'HEAD', 'Head element should be initialized');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should not recreate head element if it already exists', () => {
|
|
65
|
-
const existingHead = layout.head; // вызовем один раз
|
|
66
|
-
assert.strictEqual(layout.head, existingHead, 'Head element should not be recreated');
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('Clone Testing', () => {
|
|
71
|
-
let layout;
|
|
72
|
-
beforeEach(() => {
|
|
73
|
-
layout = new Layout();
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should clone the layout correctly', () => {
|
|
77
|
-
const clone = layout.clone;
|
|
78
|
-
// console.log(clone.constructor.name)
|
|
79
|
-
assert(clone instanceof Layout, 'Clone should be an instance of Layout');
|
|
80
|
-
assert.notStrictEqual(clone, layout, 'Clone should not be the same instance as the original');
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
package/tests/keywords.test.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
const { SingleNode } = require('als-document')
|
|
5
|
-
|
|
6
|
-
describe('Keywords tests', () => {
|
|
7
|
-
let layout;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
layout = new Layout();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should add new keywords to an existing meta tag', () => {
|
|
14
|
-
layout.head.insert(2, new SingleNode('meta', { name: 'keywords', content: 'initial' }));
|
|
15
|
-
const additionalKeywords = ['keyword1', 'keyword2'];
|
|
16
|
-
layout.keywords(additionalKeywords);
|
|
17
|
-
const expectedContent = 'initial,keyword1,keyword2';
|
|
18
|
-
assert.strictEqual(layout.root.$('meta[name="keywords"]').getAttribute('content'), expectedContent, 'Existing keywords not updated correctly');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('should not add duplicate keywords', () => {
|
|
22
|
-
layout.head.insert(2, new SingleNode('meta', { name: 'keywords', content: 'keyword1,keyword2' }));
|
|
23
|
-
const additionalKeywords = ['keyword2', 'keyword3'];
|
|
24
|
-
layout.keywords(additionalKeywords);
|
|
25
|
-
const expectedContent = 'keyword1,keyword2,keyword3';
|
|
26
|
-
assert.strictEqual(layout.root.$('meta[name="keywords"]').getAttribute('content'), expectedContent, 'Duplicate keywords were added');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should handle keywords with leading or trailing spaces', () => {
|
|
30
|
-
const messyKeywords = [' keyword1', 'keyword2 '];
|
|
31
|
-
layout.keywords(messyKeywords);
|
|
32
|
-
const expectedContent = 'keyword1,keyword2';
|
|
33
|
-
assert.strictEqual(layout.root.$('meta[name="keywords"]').getAttribute('content'), expectedContent, 'Keywords with spaces not trimmed correctly');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should add keywords correctly', () => {
|
|
37
|
-
const keywords = ['keyword1', 'keyword2'];
|
|
38
|
-
layout.keywords(keywords);
|
|
39
|
-
assert(layout.root.$('meta[name="keywords"]').getAttribute('content') === keywords.join(), 'Keywords not set correctly');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should handle empty keywords array', () => {
|
|
43
|
-
layout.keywords([]);
|
|
44
|
-
assert(!layout.root.$('meta[name="keywords"]'), 'Meta tag for empty keywords should not be created');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
});
|
package/tests/link.test.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
|
|
5
|
-
describe('Link', () => {
|
|
6
|
-
let layout;
|
|
7
|
-
beforeEach(() => layout = new Layout());
|
|
8
|
-
|
|
9
|
-
it('should add a new link element without version', () => {
|
|
10
|
-
const href = 'style.css';
|
|
11
|
-
layout.link(href);
|
|
12
|
-
assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), href, 'Link href should match the provided href');
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('should throw error for invalid href', () => {
|
|
16
|
-
assert.throws(() => layout.link(''));
|
|
17
|
-
assert.throws(() => layout.link(null));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should add link correctly', () => {
|
|
21
|
-
const href = 'style.css';
|
|
22
|
-
layout.link(href);
|
|
23
|
-
assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), href, 'Link not set correctly');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should not add a new link element if one already exists with the same href and no version', () => {
|
|
27
|
-
const href = 'style.css';
|
|
28
|
-
layout.link(href); // Добавление ссылки без версии
|
|
29
|
-
layout.link(href); // Повторное добавление той же ссылки без версии
|
|
30
|
-
assert.strictEqual(layout.root.$$(`link[rel="stylesheet"][href="${href}"]`).length, 1, 'Should not add duplicate link without version');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
})
|
package/tests/script.test.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
describe('Scripts', () => {
|
|
5
|
-
let layout;
|
|
6
|
-
|
|
7
|
-
beforeEach(() => layout = new Layout());
|
|
8
|
-
|
|
9
|
-
it('should not add script if attributes are not an object', () => {
|
|
10
|
-
layout.script("not-an-object");
|
|
11
|
-
assert.strictEqual(layout.root.$('script'), null, 'Script should not be added when attributes are not an object');
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('should not add script if src already exists', () => {
|
|
15
|
-
layout.script({ src: 'existingscript.js' }, 'console.log("test");');
|
|
16
|
-
layout.script({ src: 'existingscript.js' }, 'console.log("duplicate");');
|
|
17
|
-
assert.strictEqual(layout.root.$$(`script[src="existingscript.js"]`).length, 1, 'Duplicate script should not be added');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should handle empty innerHTML correctly', () => {
|
|
21
|
-
layout.script({ src: 'test.js' }, '');
|
|
22
|
-
assert.strictEqual(layout.root.$('script').innerHTML, '', 'Script innerHTML should be empty');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should add script correctly', () => {
|
|
26
|
-
const scriptContent = 'console.log("Hello, world!");';
|
|
27
|
-
layout.script({}, scriptContent);
|
|
28
|
-
assert.strictEqual(layout.root.$('script').innerHTML, scriptContent, 'Script content not set correctly');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('should not add script if no attributes and no innerHTML', () => {
|
|
32
|
-
layout.script({}, '');
|
|
33
|
-
assert.strictEqual(layout.root.$('script'), null, 'Script should not be added if there are no attributes and no innerHTML');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should add script after body when head is false', () => {
|
|
37
|
-
const scriptContent = 'console.log("Script in body");';
|
|
38
|
-
layout.script({ src: 'scriptbody.js' }, scriptContent, false);
|
|
39
|
-
assert.strictEqual(layout.body.next.innerHTML, scriptContent, 'Script should be added to body');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
})
|
package/tests/style.test.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
|
|
5
|
-
describe('Styles', () => {
|
|
6
|
-
let layout;
|
|
7
|
-
beforeEach(() => layout = new Layout());
|
|
8
|
-
|
|
9
|
-
it('should not add style if styles are invalid', () => {
|
|
10
|
-
assert.throws(() => layout.style(123))
|
|
11
|
-
// layout.style(123);
|
|
12
|
-
// assert.strictEqual(layout.root.$('style'), null, 'Style should not be added when styles are invalid');
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('should add style correctly', () => {
|
|
16
|
-
const styles = 'body { background-color: black; }';
|
|
17
|
-
layout.style(styles);
|
|
18
|
-
assert(layout.root.$('style').innerHTML.includes(styles), 'Styles not set correctly');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('should add styles to existing style tag', () => {
|
|
22
|
-
assert(layout.root.$$('style').length === 0)
|
|
23
|
-
const styles1 = 'body { background-color: black; }';
|
|
24
|
-
const styles2 = 'body { margin: 0; }';
|
|
25
|
-
layout.style(styles1);
|
|
26
|
-
assert(layout.root.$$('style').length === 1)
|
|
27
|
-
layout.style(styles2);
|
|
28
|
-
assert(layout.root.$$('style').length === 1)
|
|
29
|
-
const inner = layout.root.$('style').innerHTML
|
|
30
|
-
assert(inner.includes(styles1));
|
|
31
|
-
assert(inner.includes(styles2));
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
})
|
package/tests/url.test.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
|
|
5
|
-
describe('Url', () => {
|
|
6
|
-
let layout;
|
|
7
|
-
beforeEach(() => layout = new Layout());
|
|
8
|
-
|
|
9
|
-
it('should throw error if canonical URL if URL is invalid', () => {
|
|
10
|
-
assert.throws(() => layout.url('not-a-url', 'aa'));
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should add url correctly', () => {
|
|
14
|
-
const url = 'http://localhost';
|
|
15
|
-
layout.url(url);
|
|
16
|
-
assert(layout.root.$('link[rel="canonical"]').getAttribute('href') === url, 'Canonical URL not set correctly');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should add og:url meta correctly', () => {
|
|
20
|
-
const url = 'http://example.com';
|
|
21
|
-
const host = 'http://example.com';
|
|
22
|
-
layout.url(url, host);
|
|
23
|
-
assert.strictEqual(layout.root.$('meta[property="og:url"]').getAttribute('content'), url, 'og:url meta not set correctly');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should add canonical link if not already present', () => {
|
|
27
|
-
const url = 'http://example.com';
|
|
28
|
-
const host = 'http://example.com';
|
|
29
|
-
layout.url(url, host);
|
|
30
|
-
assert.strictEqual(layout.root.$('link[rel="canonical"]').getAttribute('href'), url, 'Canonical link should be added if not present');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should update existing canonical link', () => {
|
|
34
|
-
const initialUrl = 'http://example.com/initial';
|
|
35
|
-
const newUrl = 'http://example.com/new';
|
|
36
|
-
const host = 'http://example.com';
|
|
37
|
-
layout.url(initialUrl, host);
|
|
38
|
-
layout.url(newUrl, host);
|
|
39
|
-
assert.strictEqual(layout.root.$('link[rel="canonical"]').getAttribute('href'), newUrl, 'Canonical link should be updated with new URL');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
})
|
package/tests/viewport.test.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const { describe, it, beforeEach } = require('node:test')
|
|
3
|
-
const Layout = require('../src/layout.js');
|
|
4
|
-
|
|
5
|
-
describe('layout.viewport',() => {
|
|
6
|
-
let layout;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => layout = new Layout());
|
|
9
|
-
|
|
10
|
-
it('should update existing viewport meta correctly', () => {
|
|
11
|
-
// Добавляем изначальный viewport
|
|
12
|
-
const initialContent = 'width=device-width, initial-scale=1.0';
|
|
13
|
-
layout.viewport(initialContent);
|
|
14
|
-
|
|
15
|
-
// Обновляем viewport
|
|
16
|
-
const updatedContent = 'width=device-width, initial-scale=2.0';
|
|
17
|
-
layout.viewport(updatedContent);
|
|
18
|
-
|
|
19
|
-
// Проверяем, что содержимое meta обновлено
|
|
20
|
-
assert.strictEqual(layout.root.$('meta[name="viewport"]').getAttribute('content'), updatedContent, 'Existing viewport meta content should be updated');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should not add a second viewport meta if one already exists', () => {
|
|
24
|
-
// Добавляем изначальный viewport
|
|
25
|
-
const initialContent = 'width=device-width, initial-scale=1.0';
|
|
26
|
-
layout.viewport(initialContent);
|
|
27
|
-
|
|
28
|
-
// Пытаемся добавить ещё один
|
|
29
|
-
const secondContent = 'width=device-width, initial-scale=2.0';
|
|
30
|
-
layout.viewport(secondContent);
|
|
31
|
-
|
|
32
|
-
// Проверяем, что в документе только один элемент meta viewport
|
|
33
|
-
assert.strictEqual(layout.root.$$(`meta[name="viewport"]`).length, 1, 'Only one viewport meta should exist');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should add viewport correctly', () => {
|
|
37
|
-
const viewportContent = 'width=device-width, initial-scale=1.0';
|
|
38
|
-
layout.viewport(viewportContent);
|
|
39
|
-
assert.strictEqual(layout.root.$('meta[name="viewport"]').getAttribute('content'), viewportContent, 'Viewport not set correctly');
|
|
40
|
-
});
|
|
41
|
-
})
|