Skip to content

Commit

Permalink
feat: remove overflow:visible (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 authored Nov 22, 2024
1 parent 2afbbbb commit a3c32e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
36 changes: 31 additions & 5 deletions plugins/removeUnknownsAndDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const allowedAttributesPerElement = new Map();
* @type {Map<string, Map<string, string>>}
*/
const attributesDefaultsPerElement = new Map();
const preserveOverflowElements = new Set([
'foreignObject',
'image',
'marker',
'pattern',
'svg',
'symbol',
'text',
]);

for (const [name, config] of Object.entries(elems)) {
/**
Expand Down Expand Up @@ -86,17 +95,28 @@ for (const [name, config] of Object.entries(elems)) {
}

/**
* @param {string} name
* @param {import('../lib/types.js').XastElement} element
* @param {string} propName
* @param {string|undefined} value
* @param {Map<string,string>|undefined} defaults
* @returns {boolean}
*/
function isDefaultPropertyValue(name, value, defaults) {
function isDefaultPropertyValue(element, propName, value, defaults) {
if (defaults === undefined) {
return false;
}
const defaultVals = defaults.get(name);
return value === defaultVals;
const defaultVals = defaults.get(propName);
if (value === defaultVals) {
return true;
}
if (
propName === 'overflow' &&
value === 'visible' &&
!preserveOverflowElements.has(element.name)
) {
return true;
}
return false;
}

/**
Expand Down Expand Up @@ -247,7 +267,12 @@ export const fn = (root, params, info) => {
origVal !== null &&
(origVal === newVal ||
(newVal === undefined &&
isDefaultPropertyValue(p, origVal, attributesDefaults)))
isDefaultPropertyValue(
element,
p,
origVal,
attributesDefaults,
)))
) {
propsToDelete.push(p);
}
Expand Down Expand Up @@ -315,6 +340,7 @@ export const fn = (root, params, info) => {
// -- has the same value as the parent.
// (b) not inheritable, and a default value.
const isDefault = isDefaultPropertyValue(
element,
name,
value.toString(),
attributesDefaults,
Expand Down
13 changes: 13 additions & 0 deletions test/plugins/removeUnknownsAndDefaults.34.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Remove overflow:visible where possible.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<path style="overflow:visible;opacity:.5" d="m1 8v64h64v-64h-64zm1 1h62v62h-62v-62z"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<path style="opacity:.5" d="m1 8v64h64v-64h-64zm1 1h62v62h-62v-62z"/>
</svg>

0 comments on commit a3c32e4

Please sign in to comment.