Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove <style> element from the minifyStyles plugin when it is empty #1373

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 16 additions & 8 deletions plugins/minifyStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ exports.fn = function(ast, options) {

elems.forEach(function(elem) {
if (elem.isElem('style')) {
// <style> element
var styleCss = elem.content[0].text || elem.content[0].cdata || [];
var DATA = styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0 ? 'cdata' : 'text';

elem.content[0][DATA] = csso.minify(styleCss, minifyOptionsForStylesheet).css;
// has <style> element
if (elem.isEmpty()) {
// removed empty <style> elements
var styleParentEl = elem.parentNode; // svg element
var styleIndex = styleParentEl.content.indexOf(elem)

console.log('style index', styleIndex);
styleParentEl.spliceContent(styleIndex, 1);
} else {
var styleCss = elem.content[0].text || elem.content[0].cdata || [];
var DATA = styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0 ? 'cdata' : 'text';

elem.content[0][DATA] = csso.minify(styleCss, minifyOptionsForStylesheet).css;
}
} else {
// style attribute
// has style attribute
var elemStyle = elem.attr('style').value;

elem.attr('style').value = csso.minifyBlock(elemStyle, minifyOptionsForAttribute).css;
Expand Down Expand Up @@ -73,8 +82,7 @@ function findStyleElems(ast) {
if (item.content) {
walk(item, styles);
}

if (item.isElem('style') && !item.isEmpty()) {
if (item.isElem('style')) {
styles.push(item);
} else if (item.isElem() && item.hasAttr('style')) {
styles.push(item);
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const regEOL = new RegExp(EOL, 'g');
const regFilename = /^(.*)\.(\d+)\.svg$/;
const { optimize } = require('../../lib/svgo.js');

describe('plugins tests', function() {
describe.only('plugins tests', function() {

FS.readdirSync(__dirname).forEach(function(file) {

Expand Down
4 changes: 2 additions & 2 deletions test/plugins/cleanupIDs.19.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion test/plugins/minifyStyles.02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/plugins/minifyStyles.11.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/plugins/minifyStyles.12.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions test/plugins/minifyStyles.13.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.