Skip to content

Commit

Permalink
Merge branch 'main' into multi-root-shared-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
developit authored Jan 2, 2024
2 parents 5f6d63e + e1f0d3e commit fe0df55
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
23 changes: 22 additions & 1 deletion compat/test/browser/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { render } from 'preact';
import {
setupScratch,
teardown,
createEvent
createEvent,
supportsPassiveEvents
} from '../../../test/_util/helpers';

import React, { createElement } from 'preact/compat';
Expand Down Expand Up @@ -309,4 +310,24 @@ describe('preact/compat events', () => {
scratch.firstChild.dispatchEvent(createEvent('focusout'));
expect(spy).to.be.calledOnce;
});

if (supportsPassiveEvents()) {
it('should use capturing for event props ending with *Capture', () => {
let click = sinon.spy();

render(
<div onTouchMoveCapture={click}>
<button type="button">Click me</button>
</div>,
scratch
);

expect(proto.addEventListener).to.have.been.calledOnce;
expect(proto.addEventListener).to.have.been.calledWithExactly(
'touchmove',
sinon.match.func,
true
);
});
}
});
9 changes: 9 additions & 0 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let oldBeforeRender = options._render;
let oldAfterDiff = options.diffed;
let oldCommit = options._commit;
let oldBeforeUnmount = options.unmount;
let oldRoot = options._root;

const RAF_TIMEOUT = 100;
let prevRaf;
Expand All @@ -35,6 +36,14 @@ options._diff = vnode => {
if (oldBeforeDiff) oldBeforeDiff(vnode);
};

options._root = (vnode, parentDom) => {
if (parentDom._children && parentDom._children._mask) {
vnode._mask = parentDom._children._mask;
}

if (oldRoot) oldRoot(vnode, parentDom);
};

/** @type {(vnode: import('./internal').VNode) => void} */
options._render = vnode => {
if (oldBeforeRender) oldBeforeRender(vnode);
Expand Down
23 changes: 23 additions & 0 deletions hooks/test/browser/useId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,27 @@ describe('useId', () => {
rerender();
expect(first).not.to.equal(scratch.innerHTML);
});

it('should return a unique id across invocations of render', () => {
const Id = () => {
const id = useId();
return <div>My id is {id}</div>;
};

const App = props => {
return (
<div>
<Id />
{props.secondId ? <Id /> : null}
</div>
);
};

render(createElement(App, { secondId: false }), scratch);
expect(scratch.innerHTML).to.equal('<div><div>My id is P0-0</div></div>');
render(createElement(App, { secondId: true }), scratch);
expect(scratch.innerHTML).to.equal(
'<div><div>My id is P0-0</div><div>My id is P0-1</div></div>'
);
});
});
6 changes: 5 additions & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ function insert(parentVNode, oldDom, parentDom) {
oldDom = parentVNode._dom;
}

return oldDom && oldDom.nextSibling;
do {
oldDom = oldDom && oldDom.nextSibling;
} while (oldDom != null && oldDom.nodeType === 8);

return oldDom;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function setProperty(dom, name, value, oldValue, isSvg) {
// Benchmark for comparison: https://esbench.com/bench/574c954bdb965b9a00965ac6
else if (name[0] === 'o' && name[1] === 'n') {
useCapture =
name !== (name = name.replace(/(PointerCapture)$|Capture$/, '$1'));
name !== (name = name.replace(/(PointerCapture)$|Capture$/i, '$1'));

// Infer correct casing for DOM built-in events:
if (name.toLowerCase() in dom) name = name.toLowerCase().slice(2);
Expand Down
11 changes: 10 additions & 1 deletion test/browser/hydrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ describe('hydrate()', () => {
beforeEach(() => {
scratch = setupScratch();
attributesSpy = spyOnElementAttributes();
clearLog();
});

afterEach(() => {
teardown(scratch);
clearLog();
});

it('should reuse existing DOM', () => {
Expand Down Expand Up @@ -92,6 +92,7 @@ describe('hydrate()', () => {
scratch
);
expect(scratch.innerHTML).to.equal('<p><i>0</i><b>1</b></p>');
expect(getLog()).to.deep.equal(['Comment.remove()']);
});

it('should reuse existing DOM when given components', () => {
Expand Down Expand Up @@ -458,5 +459,13 @@ describe('hydrate()', () => {
scratch.innerHTML = '<p>hello <!-- c -->foo</p>';
hydrate(<p>hello {'foo'}</p>, scratch);
expect(scratch.innerHTML).to.equal('<p>hello foo</p>');
expect(getLog()).to.deep.equal(['Comment.remove()']);
});

it('should skip over multiple comment nodes', () => {
scratch.innerHTML = '<p>hello <!-- a --><!-- b -->foo</p>';
hydrate(<p>hello {'foo'}</p>, scratch);
expect(scratch.innerHTML).to.equal('<p>hello foo</p>');
expect(getLog()).to.deep.equal(['Comment.remove()', 'Comment.remove()']);
});
});

0 comments on commit fe0df55

Please sign in to comment.