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
Changes from 1 commit
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
Next Next commit
feat(fragment) - inherit section dataset
andreituicu committed Nov 20, 2023
commit 7ef493ed5ae79c3236b54bfb19d76ea3ba67a78b
8 changes: 7 additions & 1 deletion blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
@@ -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);
// destination file metadata overrides the fragment metadata in case of conflicts
const mergedDataset = { ...fragmentSection.dataset, ...mainSection.dataset };
Object.keys(mergedDataset).forEach((key) => {
mainSection.dataset[key] = mergedDataset[key];
});
block.closest('.fragment-wrapper').replaceWith(...fragmentSection.childNodes);
}
}
2 changes: 2 additions & 0 deletions test/blocks/fragment/fragment.test.js
Original file line number Diff line number Diff line change
@@ -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
@@ -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>