Skip to content

Commit

Permalink
Merge pull request #131 from posthtml/feat/safe-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin authored Feb 24, 2024
2 parents faaa401 + 14de8f7 commit 3ce3224
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const postcss = require('postcss')
const safe = require('postcss-safe-parser')
const mergeLonghand = require('postcss-merge-longhand')

module.exports = (options = {}) => tree => {
Expand All @@ -14,7 +15,7 @@ module.exports = (options = {}) => tree => {
}

if (node.attrs && node.attrs.style) {
const {css} = postcss().use(mergeLonghand).process(`div { ${node.attrs.style} }`)
const {css} = postcss().use(mergeLonghand).process(`div { ${node.attrs.style} }`, {parser: safe})
node.attrs.style = css.replace(/div {\s|\s}$/gm, '')
}

Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
"postcss",
"inline-styles",
"inline-css",
"inline-css",
"merge-styles",
"merge-css",
"longhand",
"shorthand"
],
"dependencies": {
"postcss": "^8.1.10",
"postcss-merge-longhand": "^6.0.0",
"postcss-safe-parser": "^7.0.0",
"posthtml": "^0.16.4"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions test/expected/parser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div style="margin: 0 auto">margin</div>
1 change: 1 addition & 0 deletions test/fixtures/parser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div style="margin-top: 0; margin-bottom: 0 margin-left: auto; margin-right: auto">margin</div>
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ test('Preserves other properties', t => {
test('Works with tags option', t => {
return process(t, 'tags', {tags: ['div', 'p']})
})

test('Works with bad CSS', t => {
return process(t, 'parser')
})

0 comments on commit 3ce3224

Please sign in to comment.