Skip to content

Commit

Permalink
fix(posthtml#220): should not minify json with SRI
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 8, 2023
1 parent 9681eaa commit 447079d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/modules/minifyJson.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ const rNodeAttrsTypeJson = /(\/|\+)json/;

export function onContent() {
return (content, node) => {
// Skip SRI
if (node.attrs && 'integrity' in node.attrs) {
return content;
}

if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) {
try {
// cast minified JSON to an array
Expand Down
19 changes: 19 additions & 0 deletions test/modules/minifyJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ describe('minifyJson', () => {
);
});

it('should skip JSON inside <script> tags with SRI', () => {
const fixtures = `<script type="application/json" integrity="example">
{
"test": 5
}
</script>
<script type="application/ld+json" integrity="example">
{
"test": 6
}
</script>`;

return init(
fixtures,
fixtures,
options
);
});

it('should skip <script> tags with non-JSON mime type', () => {
return init(
'<script>{"test": 5}</script>',
Expand Down

0 comments on commit 447079d

Please sign in to comment.