Skip to content

Commit a72ca0e

Browse files
committed
add an option to disable strict name checking (closes #16)
1 parent 747e8b1 commit a72ca0e

4 files changed

Lines changed: 67 additions & 13 deletions

File tree

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Let's say we have a base template:
88

99
`base.html`
10-
```html
10+
```xml
1111
<html>
1212
<head>
1313
<title><block name="title"> — Github</block></title>
@@ -46,7 +46,7 @@ posthtml([require('posthtml-extend')({
4646
```
4747

4848
The final HTML will be:
49-
```html
49+
```xml
5050
<html>
5151
<head>
5252
<title>How to use posthtml-extend</title>
@@ -76,7 +76,7 @@ posthtml([require('posthtml-extend')()]).process(html).then(function (result) {
7676
```
7777

7878
The final HTML will be:
79-
```html
79+
```xml
8080
<html>
8181
<head>
8282
<title>How to use posthtml-extend — Github</title>
@@ -88,8 +88,15 @@ The final HTML will be:
8888
</html>
8989
```
9090

91-
### Plugins
92-
You can also include [other PostHTML plugins](http://posthtml.github.io/posthtml-plugins/) in your templates.
91+
## Options
92+
93+
### encoding
94+
95+
The encoding of the parent template. Default: "utf8".
96+
97+
### plugins
98+
99+
You can include [other PostHTML plugins](http://posthtml.github.io/posthtml-plugins/) in your templates.
93100
Here is an example of using [posthtml-expressions](https://github.com/posthtml/posthtml-expressions), which allows to use variables and conditions:
94101

95102
```js
@@ -115,7 +122,7 @@ posthtml([require('posthtml-extend')(options)]).process(html).then(function (res
115122
```
116123

117124
The final HTML will be:
118-
```html
125+
```xml
119126
<html>
120127
<head>
121128
<title>How to use posthtml-extend — Github</title>
@@ -126,3 +133,24 @@ The final HTML will be:
126133
</body>
127134
</html>
128135
```
136+
137+
### root
138+
139+
The path to the root template directory. Default: "./".
140+
141+
### strict
142+
143+
Whether the plugin should disallow undeclared block names. Default: true.
144+
145+
By default, posthtml-extend raises an exception if an undeclared block name is encountered. This can be useful for troubleshooting (i.e. detecting typos in block names), but
146+
there are cases where "forward declaring" a block name as an extension point for downstream templates is useful, so this restriction can be lifted by setting the `strict`
147+
option to a false value:
148+
149+
```javascript
150+
const extend = require('posthtml-extend');
151+
152+
const root = './src/html';
153+
const options = { root, strict: false };
154+
155+
posthtml([extends(options)]).then(result => console.log(result.html));
156+
```

lib/extend.es6

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default (options = {}) => {
1515
options.encoding = options.encoding || 'utf8';
1616
options.root = options.root || './';
1717
options.plugins = options.plugins || [];
18+
options.strict = Object.prototype.hasOwnProperty.call(options, 'strict') ? !!options.strict : true;
1819

1920
tree = handleExtendsNodes(tree, options, tree.messages);
2021

@@ -41,7 +42,7 @@ function handleExtendsNodes(tree, options, messages) {
4142
let layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
4243

4344
extendsNode.tag = false;
44-
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode);
45+
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict);
4546
messages.push({
4647
type: 'dependency',
4748
file: layoutPath,
@@ -59,7 +60,7 @@ function applyPluginsToTree(tree, plugins) {
5960
}
6061

6162

62-
function mergeExtendsAndLayout(layoutTree, extendsNode) {
63+
function mergeExtendsAndLayout(layoutTree, extendsNode, strictNames) {
6364
const layoutBlockNodes = getBlockNodes(layoutTree);
6465
const extendsBlockNodes = getBlockNodes(extendsNode.content);
6566

@@ -79,8 +80,10 @@ function mergeExtendsAndLayout(layoutTree, extendsNode) {
7980
delete extendsBlockNodes[layoutBlockName];
8081
}
8182

82-
for (let extendsBlockName of Object.keys(extendsBlockNodes)) {
83-
throw getError(errors.UNEXPECTED_BLOCK, extendsBlockName);
83+
if (strictNames) {
84+
for (let extendsBlockName of Object.keys(extendsBlockNodes)) {
85+
throw getError(errors.UNEXPECTED_BLOCK, extendsBlockName);
86+
}
8487
}
8588

8689
return layoutTree;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"author": "Kirill Maltsev <maltsevkirill@gmail.com>",
77
"license": "MIT",
88
"scripts": {
9-
"compile": "rm -f lib/*.js && node_modules/.bin/babel -d lib/ lib/",
10-
"lint": "node_modules/.bin/eslint *.js lib/*.es6 test/",
9+
"compile": "rm -f lib/*.js && babel -d lib/ lib/",
10+
"lint": "eslint *.js lib/*.es6 test/",
1111
"pretest": "npm run lint && npm run compile",
12-
"test": "node_modules/.bin/_mocha --compilers js:babel-core/register --check-leaks",
12+
"test": "_mocha --compilers js:babel-core/register --check-leaks",
1313
"prepublish": "npm run compile"
1414
},
1515
"keywords": [

test/extend.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ describe('Extend', () => {
200200
'[posthtml-extend] Unexpected block "head"'
201201
);
202202
});
203+
204+
205+
it('should not throw an error for an unexpected <block> if `strict` is false', () => {
206+
mfs.writeFileSync('./layout.html', '<h1>Welcome to 3 Pages!</h1><block name="body"></block>');
207+
mfs.writeFileSync(
208+
'./page1.html',
209+
`<extends src="layout.html">
210+
<block name="body">
211+
<form name="login">
212+
<block name="submit-button">
213+
<input type="submit" disabled="disabled" />
214+
</block>
215+
</form>
216+
</block>
217+
</extends>`
218+
);
219+
220+
const page3 = '<extends src="page1.html"><block name="submit-button"><input type="submit" /></block></extends>';
221+
const want = '<h1>Welcome to 3 Pages!</h1><form name="login"><input type="submit"></form>';
222+
223+
return init(page3, { strict: false })
224+
.then(html => expect(html).toBe(want));
225+
});
203226
});
204227

205228
describe('Messages', () => {

0 commit comments

Comments
 (0)