Skip to content

Commit 00a2625

Browse files
committed
fix(hast-util-to-babel-ast): replace line-breaks in attributes
Closes #219
1 parent 99711c4 commit 00a2625

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ exports[`hast-util-to-babel-ast should correctly transform svg 1`] = `
55
/* Generator: Sketch 46.2 (44496) - http://www.bohemiancoding.com/sketch */
66
}<title>{\\"Dismiss\\"}</title><desc>{\\"Created with Sketch.\\"}</desc><defs /><g id=\\"Blocks\\" stroke=\\"none\\" strokeWidth={1} fill=\\"none\\" fillRule=\\"evenodd\\" strokeLinecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" strokeWidth={2}><path d=\\"M51,37 L37,51\\" id=\\"Shape\\" /><path d=\\"M51,51 L37,37\\" id=\\"Shape\\" /></g></g></svg>;"
77
`;
8+
9+
exports[`hast-util-to-babel-ast should handle
10+
1`] = `"<svg viewBox=\\"0 0 32 32\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M25,5h-3V3c0-1.7-1.3-3-3-3H5C3.3,0,2,1.3,2,3v20c0,1.7,1.3,3,3,3h4v1c0,2.2,1.8,4,4,4h12c2.2,0,4-1.8,4-4V9 C29,6.8,27.2,5,25,5z M5,24c-0.6,0-1-0.5-1-1V3c0-0.6,0.4-1,1-1h14c0.5,0,1,0.4,1,1v2h-6.3H13H6.3c-0.6,0-1,0.4-1,1s0.4,1,1,1h3.2 C9.4,7.3,9.2,7.7,9.1,8C9.1,8,9,8,9,8H6.5c-0.6,0-1,0.4-1,1s0.4,1,1,1H9v3c-0.1,0-0.1,0-0.2,0H6.1c-0.6,0-1,0.4-1,1s0.4,1,1,1h2.7 c0.1,0,0.1,0,0.2,0V16c-0.1,0-0.1,0-0.2,0H6.1c-0.6,0-1,0.4-1,1c0,0.6,0.4,1,1,1h2.7c0.1,0,0.1,0,0.2,0V19c-0.1,0-0.1,0-0.2,0H6.1 c-0.6,0-1,0.4-1,1s0.4,1,1,1h2.7c0.1,0,0.1,0,0.2,0v3H5z M27,27c0,1.1-0.9,2-2,2H13c-1.1,0-2-0.9-2-2V9c0-1.1,0.9-2,2-2h0.7H25 c1.1,0,2,0.9,2,2V27z M25.2,19c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1s0.4-1,1-1h10.7C24.7,18,25.2,18.4,25.2,19z M25.2,22 c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1s0.4-1,1-1h10.7C24.7,21,25.2,21.4,25.2,22z M25.2,25c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1 s0.4-1,1-1h10.7C24.7,24,25.2,24.4,25.2,25z M12.3,11c0-0.6,0.4-1,1-1h7.3c0.6,0,1,0.4,1,1s-0.4,1-1,1h-7.3 C12.8,12,12.3,11.6,12.3,11z M16,13c0.6,0,1,0.4,1,1s-0.4,1-1,1h-2.5c-0.6,0-1-0.4-1-1s0.4-1,1-1H16z\\" /></svg>;"`;

packages/hast-util-to-babel-ast/src/getAttributes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as t from '@babel/types'
2-
import { isNumeric, kebabCase } from './util'
2+
import { isNumeric, kebabCase, replaceLineBreaks } from './util'
33
import stringToObjectStyle from './stringToObjectStyle'
44
import { ATTRIBUTE_MAPPING, ELEMENT_ATTRIBUTE_MAPPING } from './mappings'
55

@@ -19,7 +19,7 @@ function getKey(key, value, node) {
1919
function getValue(key, value) {
2020
// Handle className
2121
if (Array.isArray(value)) {
22-
return t.stringLiteral(value.join(' '))
22+
return t.stringLiteral(replaceLineBreaks(value.join(' ')))
2323
}
2424

2525
if (key === 'style') {
@@ -30,7 +30,7 @@ function getValue(key, value) {
3030
return t.jsxExpressionContainer(t.numericLiteral(Number(value)))
3131
}
3232

33-
return t.stringLiteral(value)
33+
return t.stringLiteral(replaceLineBreaks(value))
3434
}
3535

3636
const getAttributes = node => {

packages/hast-util-to-babel-ast/src/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
12
import unified from 'unified'
23
import parse from 'rehype-parse'
34
import vfile from 'vfile'
@@ -54,4 +55,21 @@ describe('hast-util-to-babel-ast', () => {
5455
`"<svg data-hidden=\\"true\\" />;"`,
5556
)
5657
})
58+
59+
it('should handle \n\t', () => {
60+
const code = `<svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
61+
<path d="M25,5h-3V3c0-1.7-1.3-3-3-3H5C3.3,0,2,1.3,2,3v20c0,1.7,1.3,3,3,3h4v1c0,2.2,1.8,4,4,4h12c2.2,0,4-1.8,4-4V9
62+
C29,6.8,27.2,5,25,5z M5,24c-0.6,0-1-0.5-1-1V3c0-0.6,0.4-1,1-1h14c0.5,0,1,0.4,1,1v2h-6.3H13H6.3c-0.6,0-1,0.4-1,1s0.4,1,1,1h3.2
63+
C9.4,7.3,9.2,7.7,9.1,8C9.1,8,9,8,9,8H6.5c-0.6,0-1,0.4-1,1s0.4,1,1,1H9v3c-0.1,0-0.1,0-0.2,0H6.1c-0.6,0-1,0.4-1,1s0.4,1,1,1h2.7
64+
c0.1,0,0.1,0,0.2,0V16c-0.1,0-0.1,0-0.2,0H6.1c-0.6,0-1,0.4-1,1c0,0.6,0.4,1,1,1h2.7c0.1,0,0.1,0,0.2,0V19c-0.1,0-0.1,0-0.2,0H6.1
65+
c-0.6,0-1,0.4-1,1s0.4,1,1,1h2.7c0.1,0,0.1,0,0.2,0v3H5z M27,27c0,1.1-0.9,2-2,2H13c-1.1,0-2-0.9-2-2V9c0-1.1,0.9-2,2-2h0.7H25
66+
c1.1,0,2,0.9,2,2V27z M25.2,19c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1s0.4-1,1-1h10.7C24.7,18,25.2,18.4,25.2,19z M25.2,22
67+
c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1s0.4-1,1-1h10.7C24.7,21,25.2,21.4,25.2,22z M25.2,25c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1
68+
s0.4-1,1-1h10.7C24.7,24,25.2,24.4,25.2,25z M12.3,11c0-0.6,0.4-1,1-1h7.3c0.6,0,1,0.4,1,1s-0.4,1-1,1h-7.3
69+
C12.8,12,12.3,11.6,12.3,11z M16,13c0.6,0,1,0.4,1,1s-0.4,1-1,1h-2.5c-0.6,0-1-0.4-1-1s0.4-1,1-1H16z"/>
70+
</svg>
71+
`
72+
73+
expect(transform(code)).toMatchSnapshot()
74+
})
5775
})

packages/hast-util-to-babel-ast/src/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ const KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g
3737
export function kebabCase(str) {
3838
return str.replace(KEBAB_REGEX, match => `-${match.toLowerCase()}`)
3939
}
40+
41+
const LINE_BREAKS_REGEXP = /[\r\n\u0085\u2028\u2029]+/g
42+
43+
export function replaceLineBreaks(str) {
44+
return str.replace(LINE_BREAKS_REGEXP, ' ')
45+
}

0 commit comments

Comments
 (0)