Skip to content

Commit

Permalink
Provide better tests for SVG attributes (#990)
Browse files Browse the repository at this point in the history
* Add tests for xlink attributes

* Create xlink tests programmatically instead

* More attributes are different in SVGOM

* address review feedback

* Fix typescript error

* Update test-builder/elements.ts

Co-authored-by: Queen Vinyl Da.i'gyu-Kazotetsu <vinyldarkscratch@gmail.com>

---------

Co-authored-by: Queen Vinyl Da.i'gyu-Kazotetsu <vinyldarkscratch@gmail.com>
  • Loading branch information
Elchi3 and queengooborg committed Dec 20, 2023
1 parent cf38a2c commit 742fc84
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions test-builder/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,53 @@ const build = async (specElements, customElements) => {
].reduce((acc, cv) => ({...acc, ...cv}), {})
: data.attributes;

for (const [attrName, attrProp] of Object.entries(attributes)) {
for (const [attrName, attrProp] of Object.entries(attributes) as [
string,
string,
][]) {
const customAttrTest = await getCustomTest(
`${bcdPath}.${attrName}`,
"${category}.elements",
true,
);

const defaultAttrCode = `(function() {
var instance = ${defaultConstructCode};
return !!instance && '${attrProp}' in instance;
})()`;
let attrCode = `(function() {
var instance = ${defaultConstructCode};
return !!instance && '${attrProp}' in instance;
})()`;

// Some SVG attribute names are reflected differently in SVGOM
if (category === "svg") {
const replacements = {
baseFrequency: "baseFrequencyX",
in: "in1",
kernelUnitLength: "kernelUnitLengthX",
order: "orderX",
radius: "radiusX",
stdDeviation: "stdDeviationX",
};

if (attrProp in replacements) {
attrCode = attrCode.replace(
`'${attrProp}'`,
`'${replacements[attrProp]}'`,
);
}

// All xlink attributes need special handling
if (attrProp.startsWith("xlink_")) {
const xlinkAttr = attrProp.replace("xlink_", "");
attrCode = `(function() {
var instance = ${defaultConstructCode};
instance.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:${xlinkAttr}', 'test');
return !!instance && instance.getAttributeNS('http://www.w3.org/1999/xlink', '${xlinkAttr}') === 'test';
})()`;
}
}

tests[`${bcdPath}.${attrName}`] = compileTest({
raw: {
code: customAttrTest.test || defaultAttrCode,
code: customAttrTest.test || attrCode,
},
exposure: ["Window"],
});
Expand Down

0 comments on commit 742fc84

Please sign in to comment.