Skip to content

Commit a260174

Browse files
dmaildevelopit
authored andcommitted
Fix attributes diff on DOM node (preactjs#677)
* Fix render attributes diff with a DOM node * Add another demo the list (preactjs#681) Added the Periodic Weather PWA with the lastest of Preact to the list! ☀️
1 parent 5b6c21b commit a260174

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/vdom/diff.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,17 @@ function idiff(dom, vnode, context, mountAll, componentRoot) {
123123

124124

125125
let fc = out.firstChild,
126-
props = out[ATTR_KEY] || (out[ATTR_KEY] = {}),
126+
props,
127127
vchildren = vnode.children;
128128

129+
if (out[ATTR_KEY]) {
130+
props = out[ATTR_KEY];
131+
}
132+
else {
133+
props = out[ATTR_KEY] = {};
134+
for (let a=out.attributes, i=a.length; i--; ) props[a[i].name] = a[i].value;
135+
}
136+
129137
// Optimization: fast-path for elements containing a single TextNode:
130138
if (!hydrating && vchildren && vchildren.length===1 && typeof vchildren[0]==='string' && fc!=null && fc.splitText!==undefined && fc.nextSibling==null) {
131139
if (fc.nodeValue!=vchildren[0]) {

test/browser/render.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ describe('render()', () => {
406406
expect(scratch.innerHTML, 're-set').to.equal('<div>'+html+'</div>');
407407
});
408408

409-
it( 'should apply proper mutation for VNodes with dangerouslySetInnerHTML attr', () => {
409+
it('should apply proper mutation for VNodes with dangerouslySetInnerHTML attr', () => {
410410
class Thing extends Component {
411411
constructor(props, context) {
412412
super(props, context);
@@ -496,6 +496,20 @@ describe('render()', () => {
496496
expect(scratch.firstChild.lastChild).to.equal(a);
497497
});
498498

499+
it('should not merge attributes with node created by the DOM', () => {
500+
const html = (htmlString) => {
501+
const div = document.createElement('div');
502+
div.innerHTML = htmlString;
503+
return div.firstChild;
504+
};
505+
506+
const DOMElement = html`<div><a foo="bar"></a></div>`;
507+
const preactElement = <div><a></a></div>;
508+
509+
render(preactElement, scratch, DOMElement);
510+
expect(scratch).to.have.property('innerHTML', '<div><a></a></div>');
511+
});
512+
499513
it('should skip non-preact elements', () => {
500514
class Foo extends Component {
501515
render() {

0 commit comments

Comments
 (0)