thi-ng / umbrella

⛱ Broadly scoped ecosystem & mono-repository of 199 TypeScript projects (and ~180 examples) for general purpose, functional, data driven development
https://thi.ng
Apache License 2.0
3.36k stars 149 forks source link

[hiccup-svg] Pass width and height with unit identifier not allowed anymore #312

Closed dmnsgn closed 2 years ago

dmnsgn commented 3 years ago

svg spec width and height attribute are of data type \<length> so, if I get this right, the recent use of numericAttribs() (https://github.com/thi-ng/umbrella/commit/d6cb9929d274c83e89670e9140bba1cb172a0deb) seem to have enforced a numerical value to be passed as attribute? That means we're getting TypeError: x.toFixed is not a function from ff() when trying to pass a value with unit identifier:

svg.svg({ width "1000px" })
postspectacular commented 3 years ago

That's a good catch, thanks @dmnsgn! I guess a good fix would be to update numericAttribs() to also check each of the selected attrib values if there're numeric to begin with and only if so format them with ff(). Otherwise the function would/should just leave them as is...

export const numericAttribs = (attribs: any, ...ids: string[]) => {
    let v: any;
    for (let id of ids) {
        typeof (v = attribs[id]) === "number" && (attribs[id] = ff(v));
    }
    return attribs;
};

numericAttribs({ width: 1.123456, height: "12px"}, "width","height")
// { width: '1.12', height: '12px' }
dmnsgn commented 3 years ago

Started a PR with your suggested change here if you need it: https://github.com/thi-ng/umbrella/pull/313

postspectacular commented 2 years ago

Thanks again, @dmnsgn - your fix is already released, plus I've just added some more changes/updates to the related logic (still unreleased): bf06b655393f1d08f8bab6d451ca8171ee71796f