als-layout 1.3.1 → 1.3.3

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,7 +1,7 @@
1
1
  const { SingleNode } = require('als-document')
2
2
  function addLink(href, version, layout) {
3
3
  if(!href || typeof href !== 'string') return layout
4
- const selector = `link[rel=stylesheet][href="${href}"]`
4
+ const selector = `link[rel=stylesheet][href^="${href}"]`
5
5
  let linkElement = layout.root.$(selector)
6
6
  if (linkElement) return
7
7
  if (href && version) href += href.includes('?') ? '&' : '?' + `v=${version}`
@@ -10,5 +10,4 @@ function addLink(href, version, layout) {
10
10
  return layout
11
11
  }
12
12
 
13
-
14
13
  module.exports = addLink
@@ -6,7 +6,7 @@ function checkStyles(styles, minified) {
6
6
  else if(typeof styles === 'string') {
7
7
  styles = styles.trim()
8
8
  if (minified) {
9
- const cssParser = new CssParser(rawCss);
9
+ const cssParser = new CssParser(styles);
10
10
  styles = cssParser.minified()
11
11
  }
12
12
  } else return null
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "als-layout",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Html layout constructor",
5
5
  "main": "index.js",
6
6
  "directories": {
7
7
  "lib": "lib"
8
8
  },
9
9
  "scripts": {
10
- "test": "node ./tests/run-tests.js"
10
+ "test": "node --test --experimental-test-coverage"
11
11
  },
12
12
  "keywords": [],
13
13
  "author": "Alex Sorkin",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
16
  "als-css-parser": "^0.5.0",
17
- "als-document": "^1.1.1",
17
+ "als-document": "^1.1.2",
18
18
  "als-simple-css": "^9.1.0"
19
19
  }
20
20
  }
@@ -1,6 +1,6 @@
1
1
  const assert = require('assert');
2
2
  const Layout = require('../lib/layout');
3
- const { describe, it,beforeEach } = require('node:test')
3
+ const { describe, it, beforeEach } = require('node:test')
4
4
  const Simple = require('als-simple-css')
5
5
  describe('Elements Integration', () => {
6
6
  let layout;
@@ -78,8 +78,8 @@ describe('Elements Integration', () => {
78
78
  assert(layout.root.$('style').innerHTML.includes(styles), 'Styles not set correctly');
79
79
  });
80
80
 
81
- it('Should add simple styles',() => {
82
- const styles = [{body:{bgc:'black'}}]
81
+ it('Should add simple styles', () => {
82
+ const styles = [{ body: { bgc: 'black' } }]
83
83
  const css = new Simple(styles).stylesheet()
84
84
  layout.style(styles);
85
85
  assert(layout.root.$('style').innerHTML.includes(css), 'Styles not set correctly');
@@ -97,5 +97,91 @@ describe('Elements Integration', () => {
97
97
  assert(inner.includes(styles1));
98
98
  assert(inner.includes(styles2));
99
99
  });
100
-
101
- });
100
+
101
+ });
102
+
103
+ describe('Scripts', () => {
104
+ let layout;
105
+
106
+ beforeEach(() => layout = new Layout());
107
+
108
+ it('should not add script if attributes are not an object', () => {
109
+ layout.script("not-an-object");
110
+ assert.strictEqual(layout.root.$('script'), null, 'Script should not be added when attributes are not an object');
111
+ });
112
+
113
+ it('should not add script if src already exists', () => {
114
+ layout.script({ src: 'existingscript.js' }, 'console.log("test");');
115
+ layout.script({ src: 'existingscript.js' }, 'console.log("duplicate");');
116
+ assert.strictEqual(layout.root.$$(`script[src="existingscript.js"]`).length, 1, 'Duplicate script should not be added');
117
+ });
118
+
119
+ it('should handle empty innerHTML correctly', () => {
120
+ layout.script({ src: 'test.js' }, '');
121
+ assert.strictEqual(layout.root.$('script').innerHTML, '', 'Script innerHTML should be empty');
122
+ });
123
+
124
+ })
125
+
126
+ describe('Styles', () => {
127
+ let layout;
128
+ beforeEach(() => layout = new Layout());
129
+
130
+ it('should not add style if styles are invalid', () => {
131
+ layout.style(123);
132
+ assert.strictEqual(layout.root.$('style'), null, 'Style should not be added when styles are invalid');
133
+ });
134
+
135
+ it('should add minified style', () => {
136
+ const styles = 'body { color: blue; } div { font-size: 12px; }';
137
+ layout.style(styles, true);
138
+ console.log(layout.root.$('style').innerHTML)
139
+ assert.strictEqual(layout.root.$('style').innerHTML, 'body{color:blue}div{font-size:12px}', 'Styles should be minified');
140
+ });
141
+ })
142
+
143
+ describe('Url', () => {
144
+ let layout;
145
+ beforeEach(() => layout = new Layout());
146
+
147
+ it('should not add canonical URL if URL is invalid', () => {
148
+ layout.url('not-a-url', 'aa');
149
+ assert.strictEqual(layout.root.$('link[rel="canonical"]'), null, 'Canonical URL should not be added for invalid URLs');
150
+ });
151
+
152
+ })
153
+
154
+ describe('Link', () => {
155
+ let layout;
156
+ beforeEach(() => layout = new Layout());
157
+
158
+ it('should add a new link element without version', () => {
159
+ const href = 'style.css';
160
+ layout.link(href);
161
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), href, 'Link href should match the provided href');
162
+ });
163
+
164
+ it('should add a new link element with version', () => {
165
+ const href = 'style.css';
166
+ const version = '1.0';
167
+ layout.link(href, version);
168
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]').getAttribute('href'), `${href}?v=${version}`, 'Link href should include version query parameter');
169
+ });
170
+
171
+ it('should not add a link if one with the same href and version already exists', () => {
172
+ const href = 'style.css';
173
+ const version = '1.0';
174
+ layout.link(href, version);
175
+ layout.link(href, version);
176
+ assert.strictEqual(layout.root.$$(`link[rel="stylesheet"][href="${href}?v=${version}"]`).length, 1, 'Should not add duplicate link with the same version');
177
+ });
178
+
179
+ it('should handle invalid href or version correctly', () => {
180
+ layout.link('', '1.0');
181
+ layout.link(null, '1.0');
182
+ // layout.link('style.css', '');
183
+ // layout.link('style.css', null);
184
+ assert.strictEqual(layout.root.$('link[rel="stylesheet"]'), null, 'Should not add a link when href or version are invalid');
185
+ });
186
+
187
+ })