diff --git a/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap b/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap index 86488701..a160379d 100644 --- a/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap +++ b/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap @@ -5,3 +5,6 @@ exports[`hast-util-to-babel-ast should correctly transform svg 1`] = ` /* Generator: Sketch 46.2 (44496) - http://www.bohemiancoding.com/sketch */ }{\\"Dismiss\\"}{\\"Created with Sketch.\\"};" `; + +exports[`hast-util-to-babel-ast should handle + 1`] = `";"`; diff --git a/packages/hast-util-to-babel-ast/src/getAttributes.js b/packages/hast-util-to-babel-ast/src/getAttributes.js index 564d3283..c21a0ee2 100644 --- a/packages/hast-util-to-babel-ast/src/getAttributes.js +++ b/packages/hast-util-to-babel-ast/src/getAttributes.js @@ -1,5 +1,5 @@ import * as t from '@babel/types' -import { isNumeric, kebabCase } from './util' +import { isNumeric, kebabCase, replaceLineBreaks } from './util' import stringToObjectStyle from './stringToObjectStyle' import { ATTRIBUTE_MAPPING, ELEMENT_ATTRIBUTE_MAPPING } from './mappings' @@ -19,7 +19,7 @@ function getKey(key, value, node) { function getValue(key, value) { // Handle className if (Array.isArray(value)) { - return t.stringLiteral(value.join(' ')) + return t.stringLiteral(replaceLineBreaks(value.join(' '))) } if (key === 'style') { @@ -30,7 +30,7 @@ function getValue(key, value) { return t.jsxExpressionContainer(t.numericLiteral(Number(value))) } - return t.stringLiteral(value) + return t.stringLiteral(replaceLineBreaks(value)) } const getAttributes = node => { diff --git a/packages/hast-util-to-babel-ast/src/index.test.js b/packages/hast-util-to-babel-ast/src/index.test.js index da265592..422bf379 100644 --- a/packages/hast-util-to-babel-ast/src/index.test.js +++ b/packages/hast-util-to-babel-ast/src/index.test.js @@ -1,3 +1,4 @@ +/* eslint-disable import/no-extraneous-dependencies */ import unified from 'unified' import parse from 'rehype-parse' import vfile from 'vfile' @@ -54,4 +55,21 @@ describe('hast-util-to-babel-ast', () => { `";"`, ) }) + + it('should handle \n\t', () => { + const code = ` + + + ` + + expect(transform(code)).toMatchSnapshot() + }) }) diff --git a/packages/hast-util-to-babel-ast/src/util.js b/packages/hast-util-to-babel-ast/src/util.js index fcd5bb88..daa106db 100644 --- a/packages/hast-util-to-babel-ast/src/util.js +++ b/packages/hast-util-to-babel-ast/src/util.js @@ -37,3 +37,9 @@ const KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g export function kebabCase(str) { return str.replace(KEBAB_REGEX, match => `-${match.toLowerCase()}`) } + +const LINE_BREAKS_REGEXP = /[\r\n\u0085\u2028\u2029]+/g + +export function replaceLineBreaks(str) { + return str.replace(LINE_BREAKS_REGEXP, ' ') +}