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

Fix parsing modifiers with context of block/elem #289

Merged
merged 2 commits into from
Mar 28, 2018
Merged
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
17 changes: 7 additions & 10 deletions packages/import-notation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ parse('b:button m:theme=normal|action');
API
---

* [parse](#parsestr-ctx)
* [parse](#parsestr-scope)
* [stringify](#stringify)

### parse(str, ctx)
### parse(str, [scope])

Parameter | Type | Description
----------|----------|--------------------------------------------------------
`str` | `string` | BEM import notation check [notation section](#notation)
`ctx` | `object` | BEM entity name representation.
[`scope`] | `object` | BEM entity name representation.

Parses the string into BEM entities.

Expand All @@ -56,21 +56,18 @@ entity.block // → 'button'
entity.elem // → 'text'
```

#### ctx
#### scope

Context allows to extract portion of entities.

```js
var enties = parse('m:theme=normal', { block: 'button' });

// → [ { block: 'button', mod: { name: 'theme' } },
// → [ { block: 'button' },
Copy link
Collaborator

@Vittly Vittly Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we enclose ctx in [] in table above like [ctx] ? To mark it optional

// { block: 'button', mod: { name: 'theme' } },
// { block: 'button', mod: { name: 'theme', val: 'normal' } } ]
```

Note that, using context exludes `{ block: 'button'}` from result.

So `parse('m:theme=normal', { block: 'button' })` is not same as `parse('b:button m:theme=normal')`

### stringify

Parameter | Type | Description
Expand All @@ -84,7 +81,7 @@ Notation
--------

This section describes all possible syntax of BEM import strings.
Examples are provided in es6 syntax. Note that [parse](#parsestr-ctx) function only works with strings.
Examples are provided in es6 syntax. Note that [parse](#parsestr-scope) function only works with strings.

Right now order of fields is important, check [issue](https://github.com/bem-sdk-archive/bem-import-notation/issues/12):

Expand Down
15 changes: 8 additions & 7 deletions packages/import-notation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ const BemCellSet = hashSet(cell =>
*
* @public
* @param {String} importString - string Literal from import statement
* @param {BemEntity} [ctx] - entity to restore `block` part
* @param {BemEntity} [scope] - entity to restore `block`/`elem` base name
* it's needed for short syntax: `import 'e:elemOfThisBlock'`
* `import 'm:modOfThisBlock`
* @returns {BemCell[]}
*/
function parse(importString, ctx) {
function parse(importString, scope) {
const main = {};
ctx || (ctx = {});
scope || (scope = {});

return Array.from(importString.split(' ').reduce((acc, importToken) => {
const split = importToken.split(':'),
Expand All @@ -50,14 +50,15 @@ function parse(importString, ctx) {
acc.add(main);
} else if(type === 'e') {
main.elem = tail;
if(!main.block && ctx.elem !== tail) {
main.block = ctx.block;
if(!main.block && scope.elem !== tail) {
main.block = scope.block;
acc.add(main);
}
} else if(type === 'm' || type === 't') {
if(!main.block) {
main.block = ctx.block;
main.elem || ctx.elem && (main.elem = ctx.elem);
main.block = scope.block;
main.elem || scope.elem && (main.elem = scope.elem);
acc.add(main);
}

if(type === 'm') {
Expand Down
15 changes: 15 additions & 0 deletions packages/import-notation/test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ describe('block', () => {
describe('context is block', () => {
it('should extract blockMod', () => {
expect(p('m:autoclosable', { block : 'popup' })).to.eql([
{ block : 'popup' },
{ block : 'popup', mod : { name : 'autoclosable' } }
]);
});

it('should extract block with modifier', () => {
expect(p('m:autoclosable=yes', { block : 'popup' })).to.eql([
{ block : 'popup' },
{ block : 'popup', mod : { name : 'autoclosable' } },
{ block : 'popup', mod : { name : 'autoclosable', val : 'yes' } }
]);
});

it('should extract blockMod with several values', () => {
expect(p('m:theme=normal|action', { block : 'popup' })).to.eql([
{ block : 'popup' },
{ block : 'popup', mod : { name : 'theme' } },
{ block : 'popup', mod : { name : 'theme', val : 'normal' } },
{ block : 'popup', mod : { name : 'theme', val : 'action' } }
Expand All @@ -91,13 +94,15 @@ describe('block', () => {

it('should extract blockMod with several modifiers', () => {
expect(p('m:theme m:autoclosable', { block : 'popup' })).to.eql([
{ block : 'popup' },
{ block : 'popup', mod : { name : 'theme' } },
{ block : 'popup', mod : { name : 'autoclosable' } }
]);
});

it('should extract blockMods with several modifiers and several values', () => {
expect(p('m:theme=normal|action m:autoclosable=yes', { block : 'popup' })).to.eql([
{ block : 'popup' },
{ block : 'popup', mod : { name : 'theme' } },
{ block : 'popup', mod : { name : 'theme', val : 'normal' } },
{ block : 'popup', mod : { name : 'theme', val : 'action' } },
Expand Down Expand Up @@ -323,19 +328,22 @@ describe('elem', () => {
describe('context is elem', () => {
it('should extract elem with simple modifier', () => {
expect(p('m:pseudo', { block : 'button2', elem : 'text' })).to.eql([
{ block : 'button2', elem : 'text' },
{ block : 'button2', elem : 'text', mod : { name : 'pseudo' } }
]);
});

it('should extract elem with modifier', () => {
expect(p('m:pseudo=yes', { block : 'button2', elem : 'text' })).to.eql([
{ block : 'button2', elem : 'text' },
{ block : 'button2', elem : 'text', mod : { name : 'pseudo' } },
{ block : 'button2', elem : 'text', mod : { name : 'pseudo', val : 'yes' } }
]);
});

it('should extract elem with modifier and several values', () => {
expect(p('m:theme=normal|action', { block : 'button2', elem : 'text' })).to.eql([
{ block : 'button2', elem : 'text' },
{ block : 'button2', elem : 'text', mod : { name : 'theme' } },
{ block : 'button2', elem : 'text', mod : { name : 'theme', val : 'normal' } },
{ block : 'button2', elem : 'text', mod : { name : 'theme', val : 'action' } }
Expand All @@ -344,6 +352,7 @@ describe('elem', () => {

it('should extract elem with several modifiers', () => {
expect(p('m:theme m:autoclosable', { block : 'popup', elem : 'tail' })).to.eql([
{ block : 'popup', elem : 'tail' },
{ block : 'popup', elem : 'tail', mod : { name : 'theme' } },
{ block : 'popup', elem : 'tail', mod : { name : 'autoclosable' } }
]);
Expand All @@ -353,6 +362,7 @@ describe('elem', () => {
expect(
p('m:theme=normal|action m:autoclosable=yes', { block : 'popup', elem : 'tail' })
).to.eql([
{ block : 'popup', elem : 'tail' },
{ block : 'popup', elem : 'tail', mod : { name : 'theme' } },
{ block : 'popup', elem : 'tail', mod : { name : 'theme', val : 'normal' } },
{ block : 'popup', elem : 'tail', mod : { name : 'theme', val : 'action' } },
Expand Down Expand Up @@ -418,19 +428,22 @@ describe('elem', () => {
describe('context is current elem', () => {
it('should extract elem with simple modifier', () => {
expect(p('e:text m:pseudo', { block : 'button2', elem : 'text' })).to.eql([
{ block : 'button2', elem : 'text' },
{ block : 'button2', elem : 'text', mod : { name : 'pseudo' } }
]);
});

it('should extract elem with modifier', () => {
expect(p('e:text m:pseudo=yes', { block : 'button2', elem : 'text' })).to.eql([
{ block : 'button2', elem : 'text' },
{ block : 'button2', elem : 'text', mod : { name : 'pseudo' } },
{ block : 'button2', elem : 'text', mod : { name : 'pseudo', val : 'yes' } }
]);
});

it('should extract elem with modifier and several values', () => {
expect(p('e:text m:theme=normal|action', { block : 'button2', elem : 'text' })).to.eql([
{ block : 'button2', elem : 'text' },
{ block : 'button2', elem : 'text', mod : { name : 'theme' } },
{ block : 'button2', elem : 'text', mod : { name : 'theme', val : 'normal' } },
{ block : 'button2', elem : 'text', mod : { name : 'theme', val : 'action' } }
Expand All @@ -439,6 +452,7 @@ describe('elem', () => {

it('should extract elem with several modifiers', () => {
expect(p('e:tail m:theme m:autoclosable', { block : 'popup', elem : 'tail' })).to.eql([
{ block : 'popup', elem : 'tail' },
{ block : 'popup', elem : 'tail', mod : { name : 'theme' } },
{ block : 'popup', elem : 'tail', mod : { name : 'autoclosable' } }
]);
Expand All @@ -448,6 +462,7 @@ describe('elem', () => {
expect(
p('e:tail m:theme=normal|action m:autoclosable=yes', { block : 'popup', elem : 'tail' })
).to.eql([
{ block : 'popup', elem : 'tail' },
{ block : 'popup', elem : 'tail', mod : { name : 'theme' } },
{ block : 'popup', elem : 'tail', mod : { name : 'theme', val : 'normal' } },
{ block : 'popup', elem : 'tail', mod : { name : 'theme', val : 'action' } },
Expand Down