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

Support for recursive extends (adjusted) #24

Open
wants to merge 4 commits into
base: master
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
12 changes: 10 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ module.exports = function reshapeLayouts (options = {}) {
function handleExtendsNodes (tree, options, ctx) {
return tree.reduce((m, node) => {
// if it's not an "extends" tag, move on
if (node.name !== 'extends') { m.push(node); return m }
if (node.name !== 'extends') {
if (Array.isArray(node.content)) {
node.content = handleExtendsNodes(node.content, options, ctx)
m.push(node)
} else {
m.push(node)
}
return m
}

// if the extends tag has no "src", throw an error
if (!node.attrs || !node.attrs.src) {
Expand All @@ -51,7 +59,7 @@ function handleExtendsNodes (tree, options, ctx) {
}

// merge the contents of the current node into the layout
m = m.concat(mergeExtendsAndLayout(layoutTree, node.content, ctx))
m = m.concat(mergeExtendsAndLayout(layoutTree, handleExtendsNodes(node.content, options, ctx), ctx))
return m
}, [])
}
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ test('uses the correct source location for layouts and templates', (t) => {
}
})

test('recursive layout extension', (t) => {
mfs.writeFileSync('./layout.html', '<div class="container"><block name="content"><p>default</p></block></div>')
mfs.writeFileSync('./component.html', '<div><p>this is a very interesting component</p><block name="content">this is default content</block></div>')

return init(`<extends src="layout.html">
<block name="content">
<p>blah</p>
<extends src="component.html">
<block name="content">
Hello!
</block>
</extends>
</block>
</extends>
`).then((html) => {
t.truthy((html) === cleanHtml(`<div class="container"><p>blah</p><div><p>this is a very interesting component</p>
Hello!
</div></div>`))
})
})

function assertError (t, promise, expectedErrorMessage) {
return promise
.catch((error) => error.message)
Expand Down