-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial API for updating attributes and properties. #332
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Copyright 2017 The Incremental DOM Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { getData } from './node_data.js'; | ||
import { currentElement } from './core.js'; | ||
import { calculateDiff } from './diff.js'; | ||
import { | ||
applyAttr, | ||
applyProp | ||
} from './attributes.js'; | ||
import { | ||
queueChange, | ||
flush | ||
} from './changes.js'; | ||
import { truncateArray } from './util.js'; | ||
|
||
|
||
/** @type {!Array<*>} */ | ||
const attributes = []; | ||
|
||
/** @type {!Array<*>} */ | ||
const properties = []; | ||
|
||
|
||
/** | ||
* Buffers an attribute, saving it to be applied later. | ||
* @param {string} name | ||
* @param {*} value | ||
*/ | ||
const bufferAttribute = function(name, value) { | ||
attributes.push(name); | ||
attributes.push(value); | ||
}; | ||
|
||
|
||
/** | ||
* Buffers an attribute, saving it to be applied later. | ||
* @param {string} name | ||
* @param {*} value | ||
*/ | ||
const bufferProperty = function(name, value) { | ||
properties.push(name); | ||
properties.push(value); | ||
}; | ||
|
||
|
||
/** | ||
* @type {function(!Element, string, *)} | ||
*/ | ||
const queueAttribute = queueChange.bind(null, applyAttr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
||
|
||
/** | ||
* @type {function(!Element, string, *)} | ||
*/ | ||
const queueProperty = queueChange.bind(null, applyProp); | ||
|
||
|
||
/** | ||
* Queues up any diffs for the current element and buffered attributes. These | ||
* can be apply by calling `flush()`; | ||
*/ | ||
const queueUpdates = function() { | ||
const node = currentElement(); | ||
const data = getData(node); | ||
const attributesArr = data.attrsArr; | ||
const propertiesArr = data.propertiesArr; | ||
|
||
calculateDiff(attributesArr, attributes, node, queueAttribute); | ||
calculateDiff(propertiesArr, properties, node, queueProperty); | ||
truncateArray(attributes, 0); | ||
truncateArray(properties, 0); | ||
}; | ||
|
||
|
||
/** | ||
* Immediately applies any diffs for the current element and buffered | ||
* attributes. | ||
*/ | ||
const applyUpdates = function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: rename it to |
||
queueUpdates(); | ||
flush(); | ||
}; | ||
|
||
/** */ | ||
export { | ||
applyUpdates, | ||
bufferAttribute, | ||
bufferProperty, | ||
queueAttribute, | ||
queueProperty, | ||
queueUpdates, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright 2017 The Incremental DOM Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
patch, | ||
open, | ||
close, | ||
applyUpdates, | ||
bufferAttribute, | ||
bufferProperty | ||
} from '../../index.js'; | ||
|
||
describe('updates', () => { | ||
let container; | ||
|
||
function render({attributes, properties} = {}) { | ||
open('div'); | ||
for (const key in properties) { | ||
bufferProperty(key, properties[key]); | ||
} | ||
for (const key in attributes) { | ||
bufferAttribute(key, attributes[key]); | ||
} | ||
applyUpdates(); | ||
close('div'); | ||
} | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
it('should add new attributes', () => { | ||
patch(container, () => render({ | ||
attributes: { | ||
'data-expanded': 'hello' | ||
} | ||
})); | ||
const el = container.childNodes[0]; | ||
|
||
expect(el.getAttribute('data-expanded')).to.equal('hello'); | ||
}); | ||
|
||
it('should remove attributes', () => { | ||
patch(container, () => render({ | ||
attributes: { | ||
'data-expanded': 'hello' | ||
} | ||
})); | ||
patch(container, () => render()); | ||
const el = container.childNodes[0]; | ||
|
||
expect(el.hasAttribute('data-expanded')).to.be.false; | ||
}); | ||
|
||
it('should add new properties', () => { | ||
patch(container, () => render({ | ||
properties: { | ||
'prop': 'hello' | ||
} | ||
})); | ||
const el = container.childNodes[0]; | ||
|
||
expect(el.prop).to.equal('hello'); | ||
}); | ||
|
||
it('should remove properties', () => { | ||
patch(container, () => render({ | ||
properties: { | ||
'prop': 'hello' | ||
} | ||
})); | ||
patch(container, () => render()); | ||
const el = container.childNodes[0]; | ||
|
||
expect(el.prop).to.be.undefined; | ||
}); | ||
|
||
it('should add both attributes and properties', () => { | ||
patch(container, () => render({ | ||
attributes: { | ||
'data-expanded': 'hello' | ||
}, | ||
properties: { | ||
'prop': 'hello' | ||
} | ||
})); | ||
const el = container.childNodes[0]; | ||
|
||
expect(el.getAttribute('data-expanded')).to.equal('hello'); | ||
expect(el.prop).to.equal('hello'); | ||
}); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we exposing the queues here?