als-layout 3.0.1 → 4.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.
@@ -1,5 +1,15 @@
1
1
  ## Change Log
2
2
 
3
+ * V4.1.0
4
+ * Render removed
5
+ * V4.0.0
6
+ * All code rebuilded and refactored
7
+ * No als-simple-css for style
8
+ * No charset method
9
+ * minifying for style and inner scripts
10
+ * updated render version
11
+ * render as element's method instead layout's method
12
+
3
13
  * V3.0.0
4
14
  * render switched to als-render
5
15
  * updated bug with meta tags after body
@@ -13,7 +13,6 @@ Once you have your `Layout` instance, you can easily add or modify various eleme
13
13
 
14
14
  ```js
15
15
  const layout = new Layout()
16
- .charset() // default UTF-8
17
16
  .viewport() // default width=device-width, initial-scale=1.0
18
17
  .title('Test title') // adding/updating title and meta[og:title]
19
18
  .favicon('/favicon.png') // adding/updating link[rel=icon][type=image/x-icon] with new href
package/index.js CHANGED
@@ -1,3 +1,152 @@
1
- const Layout = require('./lib/layout')
1
+ const { Document, SingleNode, Node } = require('als-document');
2
+ const UglifyJS = require("uglify-js");
3
+ const uglifycss = require('uglifycss');
4
+
5
+ const onloadScript = /*js*/`document.addEventListener('DOMContentLoaded', function() {
6
+ const elements = document.querySelectorAll('[onload]');
7
+ elements.forEach(element => {
8
+ const onloadCode = element.getAttribute('onload');
9
+ const func = Function('"use strict"; return function() { ' + onloadCode + ' }');
10
+ func().call(element);
11
+ element.removeAttribute('onload');
12
+ });
13
+ });`;
14
+
15
+ class Layout extends Document {
16
+ get rawHtml() { return this.innerHTML }
17
+ get clone() { return new Layout(new Document(this), this.URL, this.minified) }
18
+ lang(lang) { this.html.setAttribute('lang', lang); return this }
19
+ version(v) { this.v = v; return this; }
20
+
21
+ constructor(html, host, minified = false) {
22
+ super(html, host);
23
+ this.minified = minified
24
+ this.root = this.html
25
+ }
26
+
27
+ onload() {
28
+ if (this.onloadAdded) return
29
+ this.script({}, onloadScript);
30
+ this.onloadAdded = true
31
+ return this
32
+ }
33
+
34
+ title(title) {
35
+ super.title // create title tag if not exists
36
+ super.title = title
37
+ this.meta({ property: 'og:title', content: title })
38
+ return this
39
+ }
40
+
41
+ description(description) {
42
+ this.meta({ name: 'description', content: description })
43
+ this.meta({ property: 'og:description', content: description })
44
+ this.meta({ property: 'twitter:description', content: description })
45
+ return this
46
+ }
47
+
48
+ favicon(href) {
49
+ const faviconElement = this.root.$('link[rel=icon][type=image/x-icon]')
50
+ if (faviconElement) faviconElement.setAttribute('href', href)
51
+ else this.head.insert(2, new SingleNode('link', { rel: 'icon', href, type: 'image/x-icon' }))
52
+ return this
53
+ }
54
+
55
+ meta(props) {
56
+ const entries = Object.entries(props)
57
+ const [name, value] = entries[0]
58
+ const selector = `meta[${name}="${value}"]`
59
+ const metaElement = this.root.$(selector)
60
+ if (metaElement) entries.forEach(([name, v]) => metaElement.setAttribute(name, props[name]))
61
+ else this.head.insert(2, new SingleNode('meta', props))
62
+ }
63
+
64
+ keywords(keywords = []) {
65
+ let keywordsElement = this.root.$('meta[name=keywords]')
66
+ if (!keywordsElement) keywordsElement = new SingleNode('meta', { name: 'keywords' })
67
+ const content = keywordsElement.getAttribute('content')
68
+ const existingKeywords = content ? content.split(',') : []
69
+ keywords.forEach(keyword => {
70
+ keyword = keyword.trim()
71
+ if (!existingKeywords.includes(keyword)) existingKeywords.push(keyword)
72
+ });
73
+ if (existingKeywords.length) {
74
+ keywordsElement.setAttribute('content', existingKeywords.join())
75
+ this.head.insert(2, keywordsElement)
76
+ }
77
+ return this
78
+ }
79
+
80
+ viewport(viewport = 'width=device-width, initial-scale=1.0') {
81
+ const element = this.root.$('meta[name="viewport"]')
82
+ if (element) element.setAttribute('content', viewport)
83
+ else this.head.insert(2, new SingleNode('meta', { name: 'viewport', content: viewport }))
84
+ return this
85
+ }
86
+
87
+ image(image, version = this.v) {
88
+ if (image && version) image += (image.includes('?') ? '&' : '?') + `v=${version}`
89
+ this.meta({ property: 'og:image', content: image })
90
+ this.meta({ name: 'twitter:image', content: image })
91
+ this.meta({ name: 'twitter:card', content: 'summary_large_image' })
92
+ return this
93
+ }
94
+
95
+ style(styles, minified = this.minified) {
96
+ if (typeof styles !== 'string') throw 'styles parameter should be string';
97
+ if (minified) styles = uglifycss.processString(styles);
98
+ let styleElement = this.root.$('style')
99
+ if (styleElement) styleElement.innerHTML = styleElement.innerHTML + '\n' + styles
100
+ else {
101
+ styleElement = new Node('style')
102
+ styleElement.innerHTML = styles
103
+ this.head.insert(2, styleElement)
104
+ }
105
+ return this
106
+ }
107
+
108
+ url(url, host = this.URL) {
109
+ try {
110
+ url = new URL(url, host).href.replace(/\/$/, '')
111
+ this.meta({ property: 'og:url', content: url })
112
+ const canonicalElement = this.root.$('link[rel="canonical"]')
113
+ if (canonicalElement) canonicalElement.setAttribute('href', url)
114
+ else this.head.insert(2, new SingleNode('link', { rel: 'canonical', href: url }))
115
+ } catch (error) {
116
+ console.log(`url ${url} with host ${host} is not valid url`)
117
+ }
118
+ return this
119
+ }
120
+
121
+ script(attrs = {}, innerHTML = '', head = true, version = this.v,minified = this.minified) {
122
+ if (typeof attrs !== 'object' || attrs === null || Array.isArray(attrs)) attrs = {}
123
+ if (attrs.src) {
124
+ const selector = `script[src="${attrs.src}"]`
125
+ if (attrs.src && this.root.$(selector)) return this
126
+ if (attrs.src && version) attrs.src += (attrs.src.includes('?') ? '&' : '?') + `v=${version}`
127
+ }
128
+ if (Object.keys(attrs).length || innerHTML) {
129
+ const script = new Node('script', attrs)
130
+ if (innerHTML) {
131
+ if (minified) innerHTML = UglifyJS.minify(innerHTML).code
132
+ script.innerHTML = innerHTML
133
+ }
134
+ if (head) this.head.insert(2, script)
135
+ else this.body.insert(3, script)
136
+ }
137
+ return this
138
+ }
139
+
140
+ link(href, version = this.v) {
141
+ if (!href || typeof href !== 'string') return this
142
+ if (href && version) href += (href.includes('?') ? '&' : '?') + `v=${version}`
143
+ const selector = `link[rel=stylesheet][href^="${href}"]`
144
+ let linkElement = this.root.$(selector)
145
+ if (linkElement) return
146
+ linkElement = new SingleNode('link', { rel: 'stylesheet', href })
147
+ this.head.insert(2, linkElement)
148
+ return this
149
+ }
150
+ }
2
151
 
3
152
  module.exports = Layout
package/package.json CHANGED
@@ -1,22 +1,18 @@
1
1
  {
2
2
  "name": "als-layout",
3
- "version": "3.0.1",
4
- "description": "Html layout constructor",
3
+ "version": "4.1.0",
5
4
  "main": "index.js",
6
- "directories": {
7
- "lib": "lib"
8
- },
9
5
  "scripts": {
10
6
  "test": "node --test --experimental-test-coverage",
11
7
  "report": "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info"
12
8
  },
13
9
  "keywords": [],
14
10
  "author": "Alex Sorkin",
15
- "license": "ISC",
11
+ "license": "MIT",
12
+ "description": "Html layout constructor",
16
13
  "dependencies": {
17
- "als-css-parser": "^0.5.0",
18
14
  "als-document": "^1.4.0",
19
- "als-render": "^0.2.4",
20
- "als-simple-css": "^9.1.0"
15
+ "uglify-js": "^3.19.2",
16
+ "uglifycss": "^0.0.29"
21
17
  }
22
18
  }
package/readme.md CHANGED
@@ -20,6 +20,16 @@ const Layout = require('als-layout')
20
20
 
21
21
  ## Change Log
22
22
 
23
+ * V4.1.0
24
+ * Render removed
25
+ * V4.0.0
26
+ * All code rebuilded and refactored
27
+ * No als-simple-css for style
28
+ * No charset method
29
+ * minifying for style and inner scripts
30
+ * updated render version
31
+ * render as element's method instead layout's method
32
+
23
33
  * V3.0.0
24
34
  * render switched to als-render
25
35
  * updated bug with meta tags after body
@@ -40,7 +50,6 @@ Once you have your `Layout` instance, you can easily add or modify various eleme
40
50
 
41
51
  ```js
42
52
  const layout = new Layout()
43
- .charset() // default UTF-8
44
53
  .viewport() // default width=device-width, initial-scale=1.0
45
54
  .title('Test title') // adding/updating title and meta[og:title]
46
55
  .favicon('/favicon.png') // adding/updating link[rel=icon][type=image/x-icon] with new href
@@ -137,48 +146,3 @@ In this example:
137
146
 
138
147
  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.
139
148
 
140
-
141
- ## Rendering
142
- Since version 3.0 , `als-layout` using `als-render` for rendering.
143
-
144
- ### Counter Example
145
- To demonstrate dynamic interaction, consider a counter that can be increased or decreased through user input:
146
-
147
- The App.js
148
- ```jsx
149
- const Counter = require('./counter')
150
- function App() {
151
- context.count = 0
152
- return (<Counter />)
153
- }
154
- module.exports = App
155
- ```
156
-
157
- counter.js
158
- ```jsx
159
- function Counter() {
160
- function change(m=1) {
161
- context.count += 1*m
162
- Counter.update()
163
- }
164
-
165
- return (<div>
166
- <button onclick={() => change(1)}>Increase</button>
167
- <span component="counter">{context.count}</span>
168
- <button onclick={() => change(-1)}>Decrease</button>
169
- </div>)
170
- }
171
-
172
- module.exports = Counter
173
- ```
174
-
175
- build.js
176
- ```js
177
- const fs = require('fs')
178
- const Layout = require('als-layout')
179
- const layout = new Layout()
180
- .title('Counter')
181
- .render('./App', 'body', {minified:true,update:true})
182
- fs.writeFileSync('counter.html', layout.rawHtml, 'utf-8') // Write the output to a file
183
- ```
184
-
@@ -1,7 +1,6 @@
1
1
  const assert = require('assert');
2
2
  const { describe, it } = require('node:test')
3
- const Layout = require('../lib/layout');
4
- const { Root,cacheDoc } = require('als-document')
3
+ const Layout = require('../index');
5
4
 
6
5
  describe('Layout Initialization', () => {
7
6
  it('should create an instance of Layout', () => {
@@ -16,25 +15,14 @@ describe('Layout Initialization', () => {
16
15
 
17
16
  it('should allow setting a custom language', () => {
18
17
  const customLang = 'fr';
19
- const layout = new Layout(undefined, { lang: customLang });
18
+ const layout = new Layout(undefined).lang(customLang);
20
19
  layout.lang(customLang)
21
20
  assert(layout.html.getAttribute('lang') === customLang, `language is not set to ${customLang}`);
22
21
  });
23
22
 
24
- it('should initialize development mode as undefined or false', () => {
25
- const layout = new Layout();
26
- assert.strictEqual(layout.dev, undefined, 'dev is not undefined or false by default');
27
- });
28
-
29
- it('should initialize host as undefined', () => {
30
- const layout = new Layout();
31
- assert.strictEqual(layout.host, undefined, 'host is not undefined by default');
32
- });
33
-
34
23
  it('should allow setting a custom host', () => {
35
24
  const customHost = 'http://localhost';
36
25
  const layout = new Layout(undefined,customHost);
37
- console.log(layout.URL)
38
26
  assert.strictEqual(layout.URL, customHost, `host is not set to ${customHost}`);
39
27
  });
40
28
  });
@@ -0,0 +1,33 @@
1
+ const assert = require('assert');
2
+ const { describe, it,beforeEach } = require('node:test')
3
+ const Layout = require('../index');
4
+
5
+ describe('description and title', () => {
6
+ let layout;
7
+
8
+ beforeEach(() => layout = new Layout());
9
+
10
+ it('should add description correctly', () => {
11
+ const description = 'Test Description';
12
+ layout.description(description);
13
+ assert.strictEqual(layout.root.$('meta[name="description"]').getAttribute('content'), description, 'Description not set correctly');
14
+ assert.strictEqual(layout.root.$('meta[property="og:description"]').getAttribute('content'), description, 'Description not set correctly');
15
+ assert.strictEqual(layout.root.$('meta[property="twitter:description"]').getAttribute('content'), description, 'Description not set correctly');
16
+ });
17
+
18
+ it('should add title correctly', () => {
19
+ const title = 'Test Title';
20
+ layout.title(title);
21
+ assert(layout.root.$('title').innerHTML === title, 'Title not set correctly');
22
+ assert(layout.root.$('[property="og:title"]').getAttribute('content') === title, 'Title not set correctly');
23
+ });
24
+
25
+ it('should add title if no title tag', () => {
26
+ layout.$('title').remove()
27
+ const title = 'Test Title';
28
+ layout.title(title);
29
+ assert(layout.root.$('title').innerHTML === title, 'Title not set correctly');
30
+ assert(layout.root.$('[property="og:title"]').getAttribute('content') === title, 'Title not set correctly');
31
+ });
32
+
33
+ });
@@ -0,0 +1,36 @@
1
+ const assert = require('assert');
2
+ const { describe, it, beforeEach } = require('node:test')
3
+ const { SingleNode } = require('als-document')
4
+ const Layout = require('../index');
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
+ });
@@ -0,0 +1,42 @@
1
+ const assert = require('assert');
2
+ const { describe, it,beforeEach } = require('node:test')
3
+ const Layout = require('../index');
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
+ it('should handle cases where image URL already has query parameters', () => {
26
+ const imageUrl = 'test-image.jpg?existing=param';
27
+ const version = '456';
28
+ layout.image(imageUrl, version);
29
+ const expectedUrl = imageUrl + '&v=' + version;
30
+ const img = layout.root.$('meta[property="og:image"]').getAttribute('content')
31
+ assert(img === expectedUrl, 'Versioned image URL with existing parameters not set correctly');
32
+ });
33
+
34
+ it('should add version parameter to image URL if version is provided', () => {
35
+ const imageUrl = 'test-image.jpg';
36
+ const version = '123';
37
+ layout.image(imageUrl, version);
38
+ const expectedUrl = imageUrl + '?v=' + version;
39
+ assert.strictEqual(layout.root.$('meta[property="og:image"]').getAttribute('content'), expectedUrl, 'Versioned image URL not set correctly in og:image');
40
+ assert.strictEqual(layout.root.$('meta[name="twitter:image"]').getAttribute('content'), expectedUrl, 'Versioned image URL not set correctly in twitter:image');
41
+ });
42
+ });
@@ -1,7 +1,6 @@
1
1
  const assert = require('assert');
2
2
  const { describe, it, beforeEach } = require('node:test')
3
- const Layout = require('../lib/layout');
4
- const { buildFromCache } = require('als-document')
3
+ const Layout = require('../index');
5
4
 
6
5
  describe('Layout Integrative tests', () => {
7
6
  let layout;
@@ -0,0 +1,47 @@
1
+ const assert = require('assert');
2
+ const { describe, it, beforeEach } = require('node:test')
3
+ const Layout = require('../index');
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
+ });
@@ -0,0 +1,83 @@
1
+ const assert = require('assert');
2
+ const { describe, it, beforeEach } = require('node:test')
3
+ const Layout = require('../index');
4
+
5
+
6
+ describe('Link', () => {
7
+ let layout;
8
+ beforeEach(() => layout = new Layout());
9
+
10
+ it('should add a new link element without version', () => {
11
+ const href = 'style.css';
12
+ layout.link(href);
13
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), href, 'Link href should match the provided href');
14
+ });
15
+
16
+ it('should add a new link element with version', () => {
17
+ const href = 'style.css';
18
+ const version = '1.0';
19
+ layout.link(href, version);
20
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), `${href}?v=${version}`, 'Link href should include version query parameter');
21
+ });
22
+
23
+ it('should not add a link if one with the same href and version already exists', () => {
24
+ const href = 'style.css';
25
+ const version = '1.0';
26
+ layout.link(href, version);
27
+ layout.link(href, version);
28
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"][href="${href}?v=${version}"]`).length, 1, 'Should not add duplicate link with the same version');
29
+ });
30
+
31
+ it('should handle invalid href or version correctly', () => {
32
+ layout.link('', '1.0');
33
+ layout.link(null, '1.0');
34
+ // layout.link('style.css', '');
35
+ // layout.link('style.css', null);
36
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]'), null, 'Should not add a link when href or version are invalid');
37
+ });
38
+
39
+ it('should add link correctly', () => {
40
+ const href = 'style.css';
41
+ layout.link(href);
42
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), href, 'Link not set correctly');
43
+ });
44
+
45
+ it('should not add a new link element if one already exists with the same href and no version', () => {
46
+ const href = 'style.css';
47
+ layout.link(href); // Добавление ссылки без версии
48
+ layout.link(href); // Повторное добавление той же ссылки без версии
49
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"][href="${href}"]`).length, 1, 'Should not add duplicate link without version');
50
+ });
51
+
52
+ it('should not add a link if href is undefined or null', () => {
53
+ layout.link(undefined, '1.0');
54
+ layout.link(null);
55
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]'), null, 'Should not add a link when href is undefined or null');
56
+ });
57
+
58
+ it('should handle different versions for the same href', () => {
59
+ const href = 'style.css';
60
+ const version1 = '1.0';
61
+ const version2 = '1.1';
62
+ layout.link(href, version1);
63
+ layout.link(href, version2);
64
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"]`).length, 2, 'Should add different links for different versions');
65
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"][href="${href}?v=${version1}"]`).length, 1, 'First version link should exist');
66
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"][href="${href}?v=${version2}"]`).length, 1, 'Second version link should exist');
67
+ });
68
+
69
+ it('should correctly add version when href already has parameters', () => {
70
+ const href = 'style.css?param=value';
71
+ const version = '1.0';
72
+ layout.link(href, version);
73
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), `${href}&v=${version}`, 'Href should include version appended with &');
74
+ });
75
+
76
+ it('should not add a link when one with a similar href prefix exists', () => {
77
+ const href = 'style.css';
78
+ layout.link(href);
79
+ layout.link(href + '?param=value', '1.0');
80
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"]`).length, 2, 'Should recognize different full hrefs as different links');
81
+ });
82
+
83
+ })
@@ -0,0 +1,57 @@
1
+ const assert = require('assert');
2
+ const { describe, it, beforeEach } = require('node:test')
3
+ const Layout = require('../index');
4
+
5
+ describe('Scripts', () => {
6
+ let layout;
7
+
8
+ beforeEach(() => layout = new Layout());
9
+
10
+ it('should not add script if attributes are not an object', () => {
11
+ layout.script("not-an-object");
12
+ assert.strictEqual(layout.root.$('script'), null, 'Script should not be added when attributes are not an object');
13
+ });
14
+
15
+ it('should not add script if src already exists', () => {
16
+ layout.script({ src: 'existingscript.js' }, 'console.log("test");');
17
+ layout.script({ src: 'existingscript.js' }, 'console.log("duplicate");');
18
+ assert.strictEqual(layout.root.$$(`script[src="existingscript.js"]`).length, 1, 'Duplicate script should not be added');
19
+ });
20
+
21
+ it('should handle empty innerHTML correctly', () => {
22
+ layout.script({ src: 'test.js' }, '');
23
+ assert.strictEqual(layout.root.$('script').innerHTML, '', 'Script innerHTML should be empty');
24
+ });
25
+
26
+ it('should add script correctly', () => {
27
+ const scriptContent = 'console.log("Hello, world!");';
28
+ layout.script({}, scriptContent);
29
+ assert.strictEqual(layout.root.$('script').innerHTML, scriptContent, 'Script content not set correctly');
30
+ });
31
+
32
+ it('should add version to script src', () => {
33
+ const src = 'script.js';
34
+ const version = '1.0';
35
+ layout.script({ src }, '', true, version);
36
+ assert.strictEqual(layout.root.$('script').getAttribute('src'), `${src}?v=${version}`, 'Script src should include version');
37
+ });
38
+
39
+ it('should not add script if no attributes and no innerHTML', () => {
40
+ layout.script({}, '');
41
+ assert.strictEqual(layout.root.$('script'), null, 'Script should not be added if there are no attributes and no innerHTML');
42
+ });
43
+
44
+ it('should add script after body when head is false', () => {
45
+ const scriptContent = 'console.log("Script in body");';
46
+ layout.script({ src: 'scriptbody.js' }, scriptContent, false);
47
+ assert.strictEqual(layout.body.next.innerHTML, scriptContent, 'Script should be added to body');
48
+ });
49
+
50
+ it('should append version parameter correctly when src already has parameters', () => {
51
+ const srcWithParams = 'script.js?existing=param';
52
+ const version = '1.0';
53
+ layout.script({ src: srcWithParams }, '', true, version);
54
+ assert.strictEqual(layout.root.$('script').getAttribute('src'), `${srcWithParams}&v=${version}`, 'Version parameter should be appended with &');
55
+ });
56
+
57
+ })