Skip to content
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

feat(fragment) - inherit section dataset #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export default async function decorate(block) {
if (fragment) {
const fragmentSection = fragment.querySelector(':scope .section');
if (fragmentSection) {
block.closest('.section').classList.add(...fragmentSection.classList);
const mainSection = block.closest('.section');
mainSection.classList.add(...fragmentSection.classList);
// in case of conflicts, the destination section's dataset wins
const mergedDataset = { ...fragmentSection.dataset, ...mainSection.dataset };
Object.keys(mergedDataset).forEach((key) => {
mainSection.dataset[key] = mergedDataset[key];
});
block.closest('.fragment-wrapper').replaceWith(...fragmentSection.childNodes);
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/blocks/fragment/fragment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('Fragment block', () => {
await sectionLoaded(section);
expect(section.textContent.trim()).to.equal('Hello world!');
expect(section.classList.contains('example-container')).to.be.true;
expect(section.classList.contains('example-style')).to.be.true;
expect(section.dataset.exampleData).to.equals('Example Value');
expect(document.querySelectorAll('.fragment').length).to.equal(1);
});
});
10 changes: 10 additions & 0 deletions test/blocks/fragment/test.plain.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@
</div>
</div>
</div>
<div class="section-metadata">
<div>
<div>Style</div>
<div>Example Style</div>
</div>
<div>
<div>Example Data</div>
<div>Example Value</div>
</div>
</div>
</div>