Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Need more examples?
1. Every **Declaration** starts on a new line
1. Every **Declaration** ends with a semicolon (;)
1. An empty line is placed after a **Block**, unless it’s the last in the surrounding **Block**
1. Unknown syntax is rendered as-is
1. Multiline tokens like **Selectors, Values, etc.** are rendered on a single line
1. Unknown syntax is rendered as-is, with multi-line formatting kept intact

## Acknowledgements

Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ function indent(size) {
* @returns A portion of the CSS
*/
function substr(node, css) {
if (node.loc) {
return css.substring(node.loc.start.offset, node.loc.end.offset)
if (node.loc === null) return ''
let str = css.substring(node.loc.start.offset, node.loc.end.offset)

// Single-line node, most common case
if (node.loc.start.line === node.loc.end.line) {
return str
}
return ''

// Multi-line nodes, not common
return str.split('\n').map(part => part.trim()).join(' ')
}

/**
* @param {import('css-tree').CssNode} node
* @param {string} css
* @returns A portion of the CSS
*/
function substr_raw(node, css) {
if (node.loc === null) return ''
return css.substring(node.loc.start.offset, node.loc.end.offset)
}

/**
Expand Down Expand Up @@ -178,7 +194,7 @@ function print_declaration(node, indent_level, css) {
* @returns {string} A formatted unknown CSS string
*/
function print_unknown(node, indent_level, css) {
return indent(indent_level) + substr(node, css).trim()
return indent(indent_level) + substr_raw(node, css).trim()
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,28 @@ test('empty input', () => {
assert.equal(actual, expected)
})

test('formats multiline tokens on a single line', () => {
let actual = format(`
a {
background: linear-gradient(
red,
10% blue,
20% green,100% yellow);
}

a.b
.c .d
.e .f {
color: green }
`)
let expected = `a {
background: linear-gradient( red, 10% blue, 20% green,100% yellow);
}

a.b .c .d .e .f {
color: green;
}`
assert.equal(actual, expected)
})

test.run();