diff --git a/lib/extend.es6 b/lib/extend.es6
index 4535683..049cd4c 100644
--- a/lib/extend.es6
+++ b/lib/extend.es6
@@ -19,11 +19,14 @@ export default (options = {}) => {
tree = handleExtendsNodes(tree, options, tree.messages);
const blockNodes = getBlockNodes(tree);
+ let blockTree;
+
for (let blockName of Object.keys(blockNodes)) {
let blockNode = blockNodes[blockName];
blockNode.tag = false;
- blockNode.content = blockNode.content || [];
+ blockTree = blockNode.content = blockNode.content || [];
blockNodes[blockName] = blockNode;
+ trimTree(blockTree);
}
return tree;
@@ -32,6 +35,26 @@ export default (options = {}) => {
const api = new Api();
+function trimTree(tree) {
+ let firstNode = tree[0];
+ let lastNode = tree[tree.length - 1];
+
+ if (typeof firstNode === 'string') {
+ tree[0] = firstNode = firstNode.replace(/^\s+/, '');
+
+ // make first and last node refer to the same string if they are the same node
+ if (tree.length === 1) {
+ lastNode = firstNode;
+ }
+ }
+
+ if (typeof lastNode === 'string') {
+ tree[tree.length - 1] = lastNode = lastNode.replace(/\s+$/, '');
+ }
+
+ return tree;
+}
+
function handleExtendsNodes(tree, options, messages) {
api.match.call(applyPluginsToTree(tree, options.plugins), {tag: 'extends'}, extendsNode => {
if (! extendsNode.attrs || ! extendsNode.attrs.src) {
@@ -42,6 +65,8 @@ function handleExtendsNodes(tree, options, messages) {
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
let layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
+ trimTree(layoutTree);
+
extendsNode.tag = false;
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode);
messages.push({
@@ -72,11 +97,11 @@ function mergeExtendsAndLayout(layoutTree, extendsNode) {
}
let layoutBlockNode = layoutBlockNodes[layoutBlockName];
- layoutBlockNode.content = mergeContent(
+ layoutBlockNode.content = trimTree(mergeContent(
extendsBlockNode.content,
layoutBlockNode.content,
getBlockType(extendsBlockNode)
- );
+ ));
delete extendsBlockNodes[layoutBlockName];
}
diff --git a/test/expect/result_trim-whitespace.html b/test/expect/result_trim-whitespace.html
new file mode 100644
index 0000000..a31f7ac
--- /dev/null
+++ b/test/expect/result_trim-whitespace.html
@@ -0,0 +1,7 @@
+No line breaks are inserted, because "block" and "extend" are control tags... ...which can't dictate vertical indentation.
+
+I am indented according to the file which should control it — base.html.
+
+
+
+^^^ 3 empty lines above me (and no empty lines below), as per base.html.
\ No newline at end of file
diff --git a/test/extend.js b/test/extend.js
index 1346207..63bafba 100644
--- a/test/extend.js
+++ b/test/extend.js
@@ -16,6 +16,14 @@ let mfs = {
this._files[fullPath] = content;
}
};
+const join = require('path').join;
+const readSync = require('fs').readFileSync;
+const fixture = (file) => {
+ return readSync(join(__dirname, 'fixtures', `${file}.html`), 'utf8');
+};
+const result = (file) => {
+ return readSync(join(__dirname, 'expect', `${file}.html`), 'utf8');
+};
const extend = proxyquire('../lib/extend', {
fs: mfs
@@ -200,6 +208,15 @@ describe('Extend', () => {
'[posthtml-extend] Unexpected block "head"'
);
});
+
+ it('should remove all whitespace at the start/end of the first/last text node', () => {
+ mfs.writeFileSync( './base.html', fixture('base_trim-whitespace'));
+ mfs.writeFileSync( './layout.html', fixture('layout_trim-whitespace'));
+
+ return init(fixture('test_trim-whitespace')).then(html => {
+ expect(html).toBe(result('result_trim-whitespace'));
+ });
+ });
});
describe('Messages', () => {
diff --git a/test/fixtures/base_trim-whitespace.html b/test/fixtures/base_trim-whitespace.html
new file mode 100644
index 0000000..392fa1e
--- /dev/null
+++ b/test/fixtures/base_trim-whitespace.html
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/test/fixtures/layout_trim-whitespace.html b/test/fixtures/layout_trim-whitespace.html
new file mode 100644
index 0000000..869e552
--- /dev/null
+++ b/test/fixtures/layout_trim-whitespace.html
@@ -0,0 +1,8 @@
+
+
+
+ ^^^ 3 empty lines above me (and no empty lines below), as per base.html.
+
+
+
+
\ No newline at end of file
diff --git a/test/fixtures/test_trim-whitespace.html b/test/fixtures/test_trim-whitespace.html
new file mode 100644
index 0000000..cbbc27c
--- /dev/null
+++ b/test/fixtures/test_trim-whitespace.html
@@ -0,0 +1,39 @@
+
+
+
+
+
+ No line breaks
+
+
+
+
+
+ are inserted,
+
+
+
+ because "block" and "extend" are control tags...
+
+
+ ...which can't dictate vertical indentation.
+
+
+
+
+
+
+
+
+
+
+
+
+ I am indented according to the file which should control it — base.html.
+
+
+
+
+
+
+
\ No newline at end of file