Skip to content

Commit

Permalink
Initial implementation of reversed attribute merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Aug 20, 2019
1 parent 8e4a226 commit 0b3290f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const defaultOptions: Options = {
'hidden', 'ismap', 'loop', 'multiple', 'muted', 'novalidate', 'readonly',
'required', 'reversed', 'selected', 'typemustmatch'
],
'output.reverseAttributes': false,
'output.selfClosingStyle': 'html',
'output.field': (index, placeholder) => placeholder,

Expand All @@ -52,7 +53,7 @@ export const defaultOptions: Options = {
'stylesheet.floatUnit': 'em',
'stylesheet.unitAliases': { e: 'em', p: '%', x: 'ex', r: 'rem' },
'stylesheet.json': false,
'stylesheet.json-double-quotes': false,
'stylesheet.jsonDoubleQuotes': false,
'stylesheet.fuzzySearchMinScore': 0.3
};

Expand Down
5 changes: 4 additions & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export interface Options {
/** A list of boolean attributes */
'output.booleanAttributes': string[];

/** Reverses attribute merging directions when resolving snippets */
'output.reverseAttributes': boolean;

/** Style of self-closing tags: html (`<br>`), xml (`<br/>`) or xhtml (`<br />`) */
'output.selfClosingStyle': 'html' | 'xml' | 'xhtml';

Expand Down Expand Up @@ -204,7 +207,7 @@ export interface Options {
'stylesheet.json': boolean;

/** Use double quotes for JSON values */
'stylesheet.json-double-quotes': boolean;
'stylesheet.jsonDoubleQuotes': boolean;

/**
* A float number between 0 and 1 to pick fuzzy-matched abbreviations.
Expand Down
9 changes: 6 additions & 3 deletions src/markup/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parse, { AbbreviationNode } from '@emmetio/abbreviation';
import parse, { AbbreviationNode, AbbreviationAttribute } from '@emmetio/abbreviation';
import { walk, findDeepest, isNode, Container } from './utils';
import { Config } from '../config';

Expand Down Expand Up @@ -37,9 +37,12 @@ export default function resolveSnippets(node: AbbreviationNode, parentAncestors:

// Add attributes from current node into every top-level node of parsed abbreviation
if (child.attributes) {
// TODO add option with attribute merge direction
for (const topNode of abbr.children) {
topNode.attributes = (topNode.attributes || []).concat(child.attributes);
const from: AbbreviationAttribute[] = topNode.attributes || [];
const to: AbbreviationAttribute[] = child.attributes || [];
topNode.attributes = config.options['output.reverseAttributes']
? to.concat(from)
: from.concat(to);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stylesheet/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,5 @@ function toCamelCase(str: string): string {
}

function getQuote(config: Config): string {
return config.options['stylesheet.json-double-quotes'] ? '"' : '\'';
return config.options['stylesheet.jsonDoubleQuotes'] ? '"' : '\'';
}
5 changes: 5 additions & 0 deletions test/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ describe('Expand Abbreviation', () => {
'<ul>\n\t<li class="item1"></li>\n\t<!-- /.item1 -->\n\t<li class="item2"></li>\n\t<!-- /.item2 -->\n</ul>');
});

it('reverse attributes merge', () => {
equal(expand('a.test'), '<a href="" class="test"></a>');
equal(expand('a.test', { options: { 'output.reverseAttributes': true } }), '<a class="test" href=""></a>');
});

// it.only('debug', () => {
// equal(expand('link:css'), '<link rel="stylesheet" href="style.css">');
// });
Expand Down

5 comments on commit 0b3290f

@violetflash
Copy link

Choose a reason for hiding this comment

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

it('reverse attributes merge', () => {
equal(expand('a.test'), '');
equal(expand('a.test', { options: { 'output.reverseAttributes': true } }), '');
});

How can i use this?

@sergeche
Copy link
Member Author

Choose a reason for hiding this comment

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

Um, what do you mean?

@violetflash
Copy link

@violetflash violetflash commented on 0b3290f May 18, 2020

Choose a reason for hiding this comment

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

Um, what do you mean?

как использовать этот реверс? a.test разворачивается в <a href="" class="test"></a>

@sergeche
Copy link
Member Author

Choose a reason for hiding this comment

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

Передать параметром в метод expand. Эту версию Emmet пока поддерживают новый Sublime Text, Nova и CodeMirror

@violetflash
Copy link

Choose a reason for hiding this comment

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

Передать параметром в метод expand. Эту версию Emmet пока поддерживают новый Sublime Text, Nova и CodeMirror

Понятно, спасибо!

Please sign in to comment.