Skip to content

Commit

Permalink
Merge pull request #61 from posthtml/fix-locals-content
Browse files Browse the repository at this point in the history
parse options locals in <content>
  • Loading branch information
Scrum authored Jan 28, 2021
2 parents a6f5235 + 6d01304 commit 60f9c0d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const fs = require('fs');
const path = require('path');
const isJSON = require('is-json');
const {merge} = require('lodash');
const posthtml = require('posthtml');
const render = require('posthtml-render');
const {merge, isEmpty} = require('lodash');
const match = require('posthtml-match-helper');
const expressions = require('posthtml-expressions');

Expand All @@ -18,7 +18,7 @@ const expressions = require('posthtml-expressions');
function processNodeContentWithPosthtml(node, options) {
return function (content) {
return processWithPostHtml(options.plugins, path.join(path.dirname(options.from), node.attrs[options.attribute]), content, [
parseLocals(node.attrs.locals, options.locals)
parseLocals(options.locals, node.attrs.locals)
]);
};
}
Expand All @@ -28,13 +28,13 @@ function processNodeContentWithPosthtml(node, options) {
* @param {String} locals [string to parse as locals object]
* @return {Function} [Function containing evaluated locals, or empty object]
*/
function parseLocals(attributeLocals = {}, optionLocals = {}) {
function parseLocals(optionLocals, attributeLocals) {
try {
const locals = merge(optionLocals, JSON.parse(attributeLocals));

return expressions({locals});
} catch {
return () => {};
return expressions({locals: optionLocals});
}
}

Expand Down Expand Up @@ -77,7 +77,11 @@ function parse(options) {
node.attrs &&
isJSON(node.attrs.locals)
) {
return parseLocals(node.attrs.locals, options.locals)(node.content);
return parseLocals(options.locals, node.attrs.locals)(node.content);
}

if (!isEmpty(options.locals)) {
return parseLocals(options.locals)(node.content);
}

return node.content || '';
Expand Down
23 changes: 16 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ test('Must parse locals if locals prop is passed and it contains a valid JSON st

test('Must not parse locals if locals prop is passed but is not a valid JSON string', async t => {
const actual = `<div class="test"><module href="./test/locals.spec.html" locals="test">Test</module></div>`;
const expected = `<div class="test"><button type="button">foo is: {{ foo }} - Test</button></div>`;
const expected = `<div class="test"><button type="button">foo is: undefined - Test</button></div>`;

const html = await posthtml().use(plugin()).process(actual).then(result => clean(result.html));

Expand All @@ -106,7 +106,7 @@ test('Must not parse locals if locals prop is passed but is not a valid JSON str

test('Must not try to parse locals if locals prop is missing', async t => {
const actual = `<div class="test"><module href="./test/locals.spec.html">Test</module></div>`;
const expected = `<div class="test"><button type="button">foo is: {{ foo }} - Test</button></div>`;
const expected = `<div class="test"><button type="button">foo is: undefined - Test</button></div>`;

const html = await posthtml().use(plugin()).process(actual).then(result => clean(result.html));

Expand All @@ -115,7 +115,7 @@ test('Must not try to parse locals if locals prop is missing', async t => {

test('Must not parse locals if locals prop is passed but is empty', async t => {
const actual = `<div class="test"><module href="./test/locals.spec.html" locals="">Test</module></div>`;
const expected = `<div class="test"><button type="button">foo is: {{ foo }} - Test</button></div>`;
const expected = `<div class="test"><button type="button">foo is: undefined - Test</button></div>`;

const html = await posthtml().use(plugin()).process(actual).then(result => clean(result.html));

Expand All @@ -140,16 +140,25 @@ test('Must use custom attribute name if it was provided in options', async t =>
t.is(html, expected);
});

test('Must parse locals passed to <content>', async t => {
const actual = `<div class="test"><module href="./test/locals.spec.html" locals='{"foo": "bar"}'>{{ foo }}</module></div>`;
const expected = `<div class="test"><button type="button">foo is: bar - bar</button></div>`;
test('Must parse attribute locals passed to <content>', async t => {
const actual = `<module href="./test/locals.option.spec.html" locals='{"inlineFoo": "bar"}'>{{ optionFoo }}</module>`;
const expected = `<div> Locals attribute: bar Locals option: undefined undefined</div>`;

const html = await posthtml().use(plugin()).process(actual).then(result => clean(result.html));

t.is(html, expected);
});

test('Must parse locals passed as option', async t => {
test('Must parse options locals passed to <content>', async t => {
const actual = `<module href="./test/locals.option.spec.html">{{ optionFoo }}</module>`;
const expected = `<div> Locals attribute: undefined Locals option: optionBar optionBar</div>`;

const html = await posthtml().use(plugin({locals: {optionFoo: 'optionBar'}})).process(actual).then(result => clean(result.html));

t.is(html, expected);
});

test('Must parse all locals', async t => {
const actual = `<module href="./test/locals.option.spec.html" locals='{"inlineFoo": "inlineBar"}'>{{ optionFoo }}</module>`;
const expected = `<div> Locals attribute: inlineBar Locals option: optionBar optionBar</div>`;

Expand Down

0 comments on commit 60f9c0d

Please sign in to comment.