GeezerVison is showing github.com — :zap^Cfaster head and textarea · nullstack/nullstack@7758cca
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: client/hydrate.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { anchorableElement } from './anchorableNode'; | 2 | 2 |
| 3 | 3 | export default function hydrate(selector, node) { | 4 | | - if (node?.attributes?.['data-n'] !== undefined) { | 5 | | - node.element = document.querySelector(`[data-n="${node.attributes['data-n']}"]`) | | 4 | + if (node.head) { | | 5 | + node.element = document.getElementById(node.attributes.id) | 6 | 6 | return | 7 | 7 | } | 8 | 8 | if (node?.attributes?.html) { | |
Collapse file Copy file name to clipboardExpand all lines: client/index.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ export default class Nullstack { | 52 | 52 | updateParams(router.url); | 53 | 53 | client.currentInstance = null; | 54 | 54 | client.initializer = () => element(Starter); | 55 | | - client.selector = document.querySelector('#application'); | | 55 | + client.selector = document.getElementById('application'); | 56 | 56 | if (environment.mode === 'spa') { | 57 | 57 | scope.plugins = loadPlugins(scope); | 58 | 58 | worker.online = navigator.onLine; | |
Collapse file Copy file name to clipboardExpand all lines: client/render.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,7 @@ export default function render(node, options) { | 29 | 29 | for (let name in node.attributes) { | 30 | 30 | if (name === 'html') { | 31 | 31 | node.element.innerHTML = node.attributes[name]; | 32 | | - node.attributes['data-n'] === undefined && anchorableElement(node.element); | | 32 | + node.head || anchorableElement(node.element); | 33 | 33 | } else if (name.startsWith('on')) { | 34 | 34 | if (node.attributes[name] !== undefined) { | 35 | 35 | const eventName = name.substring(2); | |
Collapse file Copy file name to clipboardExpand all lines: client/rerender.js +8-9Lines changed: 8 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,15 +88,15 @@ function _rerender(current, next) { | 88 | 88 | const limit = next.children.length; | 89 | 89 | for (let i = limit - 1; i > -1; i--) { | 90 | 90 | const nextSelector = render(next.children[i]); | 91 | | - document.querySelector('head').appendChild(nextSelector) | | 91 | + document.head.appendChild(nextSelector) | 92 | 92 | } | 93 | 93 | return | 94 | 94 | } | 95 | 95 |
| 96 | 96 | if (current.type === 'head' && next.type !== 'head') { | 97 | 97 | const limit = current.children.length; | 98 | 98 | for (let i = limit - 1; i > -1; i--) { | 99 | | - document.querySelector(`[data-n="${current.children[i].attributes['data-n']}"]`).remove() | | 99 | + current.children[i].element.remove() | 100 | 100 | } | 101 | 101 | return | 102 | 102 | } | @@ -106,22 +106,21 @@ function _rerender(current, next) { | 106 | 106 | for (let i = limit - 1; i > -1; i--) { | 107 | 107 | if (isUndefined(current.children[i]) && !isFalse(next.children[i])) { | 108 | 108 | const nextSelector = render(next.children[i]); | 109 | | - document.querySelector('head').appendChild(nextSelector) | | 109 | + document.head.appendChild(nextSelector) | 110 | 110 | } else if (isUndefined(next.children[i]) && !isFalse(current.children[i])) { | 111 | 111 | current.children[i].element.remove() | 112 | 112 | } else if (!isFalse(current.children[i]) && !isFalse(next.children[i])) { | 113 | 113 | if (current.children[i].type === next.children[i].type) { | 114 | 114 | next.children[i].element = current.children[i].element | 115 | | - const element = document.querySelector(`[data-n="${next.children[i].attributes['data-n']}"]`) | 116 | | - updateAttributes(element, current.children[i], next.children[i]) | | 115 | + updateAttributes(current.children[i].element, current.children[i], next.children[i]) | 117 | 116 | } else { | 118 | | - document.querySelector(`[data-n="${current.children[i].attributes['data-n']}"]`).remove() | | 117 | + current.children[i].element.remove() | 119 | 118 | const nextSelector = render(next.children[i]); | 120 | | - document.querySelector('head').appendChild(nextSelector) | | 119 | + document.head.appendChild(nextSelector) | 121 | 120 | } | 122 | 121 | } else if (isFalse(current.children[i]) && !isFalse(next.children[i])) { | 123 | 122 | const nextSelector = render(next.children[i]); | 124 | | - document.querySelector('head').appendChild(nextSelector) | | 123 | + document.head.appendChild(nextSelector) | 125 | 124 | } else if (current.children[i].type) { | 126 | 125 | current.children[i].element.remove() | 127 | 126 | } | @@ -177,7 +176,7 @@ function _rerender(current, next) { | 177 | 176 | } | 178 | 177 |
| 179 | 178 | if (next.type == 'textarea') { | 180 | | - selector.value = next.children.reduce((t, c) => t + c.text, ''); | | 179 | + selector.value = next.children[0].text; | 181 | 180 | } | 182 | 181 |
| 183 | 182 | if (next.type == 'select') { | |
Collapse file Copy file name to clipboardExpand all lines: shared/generateTree.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -130,7 +130,8 @@ async function generateBranch(parent, node, depth, scope) { | 130 | 130 | if (node.type === 'head') { | 131 | 131 | for (let i = 0; i < branch.children.length; i++) { | 132 | 132 | if (branch.children[i].attributes) { | 133 | | - branch.children[i].attributes['data-n'] = depth + '-' + i | | 133 | + branch.children[i].head = true | | 134 | + branch.children[i].attributes.id = depth + '-' + i | 134 | 135 | } | 135 | 136 | } | 136 | 137 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/jest-puppeteer.config.js +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -// get environment variable | 2 | | -const CI = !!process.env.CI; | 3 | | - | 4 | | -const baseOptions = { | | 1 | +const defaultOptions = { | | 2 | + launch: { | | 3 | + headless: true, | | 4 | + }, | 5 | 5 | server: { | 6 | 6 | command: 'npm run start', | 7 | 7 | port: 6969, | @@ -22,7 +22,12 @@ const ciPipelineOptions = { | 22 | 22 | '--disable-gpu' | 23 | 23 | ] | 24 | 24 | }, | 25 | | - ...baseOptions | | 25 | + server: { | | 26 | + command: 'npm run start', | | 27 | + port: 6969, | | 28 | + launchTimeout: 25000 | | 29 | + }, | | 30 | + browserContext: 'incognito' | 26 | 31 | } | 27 | 32 |
| 28 | | -module.exports = CI ? ciPipelineOptions : baseOptions; | | 33 | +module.exports = process.env.CI ? ciPipelineOptions : defaultOptions; |
Collapse file Copy file name to clipboardExpand all lines: tests/src/DynamicHead.njs +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import Nullstack from 'nullstack'; | 3 | 3 | class DynamicHead extends Nullstack { | 4 | 4 |
| 5 | 5 | count = 0 | | 6 | + negativeCount = 0 | 6 | 7 |
| 7 | 8 | renderHead() { | 8 | 9 | const innerComponent = `[data-inner-component] { color: blue }` | @@ -30,21 +31,21 @@ class DynamicHead extends Nullstack { | 30 | 31 | </> | 31 | 32 | <Head /> | 32 | 33 | </head> | 33 | | - {this.count % 2 === 0 && | | 34 | + {this.count === 1 && | 34 | 35 | <head> | 35 | 36 | <style html={conditionalHead} data-conditional-head /> | 36 | 37 | </head> | 37 | 38 | } | 38 | 39 | <head> | 39 | | - {Array(this.count + 1).fill(<style html={dynamicLength} data-dynamic-length={this.count} />)} | | 40 | + {Array(this.count + 1 - this.negativeCount).fill(<style html={dynamicLength} data-dynamic-length={this.count} data-negative-count={this.negativeCount === 1} />)} | 40 | 41 | </head> | 41 | 42 | {this.count % 2 === 0 ? | 42 | 43 | <head> | 43 | 44 | <meta name="test" content="nullstack" data-ternary-head /> | 44 | 45 | </head> : <span data-ternary-span> not head </span> | 45 | 46 | } | 46 | 47 | <button onclick={{ count: this.count + 1 }} data-increment> inc {this.count} </button> | 47 | | - <button onclick={{ count: this.count - 1 }} data-decrement> dec {this.count} </button> | | 48 | + <button onclick={{ negativeCount: this.negativeCount + 1 }} data-decrement> dec {this.count} </button> | 48 | 49 | <span data-red-blue> data-red-blue </span> | 49 | 50 | <span data-prerender-conditional> data-prerender-conditional </span> | 50 | 51 | <span data-rerender-conditional> data-rerender-conditional </span> | |
Collapse file Copy file name to clipboardExpand all lines: tests/src/DynamicHead.test.js +15-3Lines changed: 15 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,9 +78,21 @@ describe('DynamicHead', () => { | 78 | 78 | await page.waitForSelector(`[data-dynamic-length="${i}"]`); | 79 | 79 | } | 80 | 80 | await page.click('[data-decrement]'); | 81 | | - await page.waitForSelector('[data-dynamic-length="2"]'); | 82 | | - await page.waitForTimeout(500) | 83 | | - const elements = await page.$$('[data-dynamic-length]'); | | 81 | + await page.waitForSelector('[data-negative-count]'); | | 82 | + const elements = await page.$$('[data-negative-count]'); | 84 | 83 | expect(elements.length).toEqual(2); | 85 | 84 | }); | | 85 | + | | 86 | + test('dynamic head elements can update attributes', async () => { | | 87 | + for (let i = 1; i < 3; i++) { | | 88 | + await page.click('[data-increment]'); | | 89 | + await page.waitForSelector(`[data-dynamic-length="${i}"]`); | | 90 | + } | | 91 | + await page.click('[data-decrement]'); | | 92 | + await page.waitForSelector('[data-negative-count]'); | | 93 | + await page.click('[data-decrement]'); | | 94 | + await page.waitForSelector('[data-dynamic-length]:not([data-negative-count])'); | | 95 | + const element = await page.$('[data-dynamic-length]:not([data-negative-count])'); | | 96 | + expect(element).toBeTruthy(); | | 97 | + }); | 86 | 98 | }); |
Collapse file Copy file name to clipboardExpand all lines: tests/src/StatefulComponent.njs +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,7 @@ class StatefulComponent extends Nullstack { | 49 | 49 | </> | 50 | 50 | <button onclick={{ visible: !this.visible }} data-toggle> Toggle </button> | 51 | 51 | {this.visible && <button onclick={undefined} data-undefined-event> button </button>} | | 52 | + <textarea> {this.count} {this.count} </textarea> | 52 | 53 | </form> | 53 | 54 | ) | 54 | 55 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/src/StatefulComponent.test.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,4 +64,16 @@ describe('StatefulComponent', () => { | 64 | 64 | expect(hasConsoleError).toBeFalsy(); | 65 | 65 | }); | 66 | 66 |
| | 67 | + test('textareas with multiple nodes become a single node', async () => { | | 68 | + const text = await page.$eval('textarea', (e) => e.value); | | 69 | + expect(text).toMatch(' 1 1 '); | | 70 | + }); | | 71 | + | | 72 | + test('textareas with multiple nodes can be updated', async () => { | | 73 | + await page.click('.increment-by-one'); | | 74 | + await page.waitForSelector('[data-count="2"]'); | | 75 | + const text = await page.$eval('textarea', (e) => e.value); | | 76 | + expect(text).toMatch(' 2 2 '); | | 77 | + }); | | 78 | + | 67 | 79 | }); |
|