Skip to content

Commit 74a7eff

Browse files
committed
Modification - Lib - Use setAttribute for font size in autoFontSize util instead of style.fontSize
1 parent 9f9c04e commit 74a7eff

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/lib.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,8 +2783,7 @@ export function autoFontSize({
27832783
if (!el || !currentFontSize) return 0;
27842784

27852785
let fontSize = currentFontSize;
2786-
2787-
el.style.fontSize = fontSize;
2786+
el.setAttribute('font-size', fontSize);
27882787

27892788
const { x, y, width: W, height: H } = bounds;
27902789
const cLeft = x + padding;
@@ -2807,7 +2806,7 @@ export function autoFontSize({
28072806

28082807
while (tries-- > 0 && fontSize > minFontSize) {
28092808
fontSize--;
2810-
el.style.fontSize = fontSize;
2809+
el.setAttribute('font-size', fontSize);
28112810
er = el.getBBox();
28122811
if (
28132812
er.x >= cLeft + padding &&
@@ -2820,7 +2819,7 @@ export function autoFontSize({
28202819
}
28212820
if (fontSize < minFontSize) {
28222821
fontSize = 0;
2823-
el.style.fontSize = 0;
2822+
el.setAttribute('font-size', fontSize);
28242823
}
28252824
return fontSize;
28262825
}

src/useAutoSizeLabelsInsideViewbox.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function useAutoSizeLabelsInsideViewbox({
6565
let current = startSize;
6666
let tries = 0;
6767
while (tries < attempts) {
68-
el.style.fontSize = `${current}px`;
68+
el.setAttribute('font-size', current);
6969
if (fitsWithinBounds(el, bounds, padding)) break;
7070
if (current <= minSize) break;
7171
current -= 0.5;
@@ -110,8 +110,7 @@ export function useAutoSizeLabelsInsideViewbox({
110110
});
111111

112112
const final = shrinkToFit(el, bounds, initial, minSize, 120, 1);
113-
114-
el.style.fontSize = `${final}px`;
113+
el.setAttribute('font-size', final);
115114
sizeRef.value = final;
116115
});
117116
});

tests/lib.test.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3803,14 +3803,21 @@ describe('autoFontSize', () => {
38033803
}) {
38043804
const el = {
38053805
style: { fontSize: '' },
3806+
setAttribute(name, value) {
3807+
if (name === 'font-size') {
3808+
this.style.fontSize = String(value)
3809+
} else {
3810+
// no-op for other attributes in this test context
3811+
}
3812+
},
38063813
getBBox() {
38073814
const size = parseInt(this.style.fontSize, 10) || 0
38083815
const box = elementBBoxes[size]
38093816
if (!box) throw new Error(`no bbox for fontSize ${size}`)
3810-
return box;
3817+
return box
38113818
},
38123819
getCTM() {
3813-
return ctm;
3820+
return ctm
38143821
},
38153822
};
38163823
return { el, bounds };
@@ -3835,7 +3842,7 @@ describe('autoFontSize', () => {
38353842
});
38363843

38373844
const result = autoFontSize({
3838-
el,
3845+
el: el,
38393846
bounds,
38403847
currentFontSize: 14,
38413848
minFontSize: 6,
@@ -3844,8 +3851,8 @@ describe('autoFontSize', () => {
38443851
});
38453852

38463853
expect(result).toBe(14);
3847-
expect(parseInt(el.style.fontSize, 10)).toBe(14);
3848-
})
3854+
expect(parseInt((el).style.fontSize, 10)).toBe(14);
3855+
});
38493856

38503857
test('shrinks down until fits', () => {
38513858
const { el, bounds } = makeMocks({
@@ -3858,7 +3865,7 @@ describe('autoFontSize', () => {
38583865
});
38593866

38603867
const result = autoFontSize({
3861-
el,
3868+
el: el,
38623869
bounds,
38633870
currentFontSize: 14,
38643871
minFontSize: 8,
@@ -3867,13 +3874,12 @@ describe('autoFontSize', () => {
38673874
});
38683875

38693876
expect(result).toBe(12);
3870-
expect(parseInt(el.style.fontSize, 10)).toBe(12);
3871-
})
3877+
expect(parseInt((el).style.fontSize, 10)).toBe(12);
3878+
});
38723879

38733880
test('stops at minFontSize if still overflowing', () => {
3874-
// simulate overflow at every size down to minFontSize=6
38753881
const elementBBoxes = {};
3876-
for (let s = 10; s >= 6; s--) {
3882+
for (let s = 10; s >= 6; s -= 1) {
38773883
elementBBoxes[s] = { x: 0, y: 0, width: s * 20, height: 10 }
38783884
}
38793885
const { el, bounds } = makeMocks({
@@ -3882,17 +3888,16 @@ describe('autoFontSize', () => {
38823888
});
38833889

38843890
const result = autoFontSize({
3885-
el,
3891+
el: el,
38863892
bounds,
38873893
currentFontSize: 10,
38883894
minFontSize: 6,
38893895
attempts: 10,
38903896
padding: 0,
38913897
});
38923898

3893-
// it never fits, but we stop at the minimum rather than hide
3894-
expect(result).toBe(6);
3895-
expect(parseInt(el.style.fontSize, 10)).toBe(6);
3899+
expect(result).toBe(6)
3900+
expect(parseInt((el).style.fontSize, 10)).toBe(6)
38963901
});
38973902
});
38983903

@@ -4130,7 +4135,7 @@ describe('buildInterLineAreas', () => {
41304135
{ x: 0, y: 0, value: 1 },
41314136
{ x: 4, y: 0, value: 1 },
41324137
{ x: 5, y: 0, value: null },
4133-
{ x: 6, y: 0, value: 1 },
4138+
{ x: 6, y: 0, value: 1 },
41344139
];
41354140
const B = [
41364141
{ x: 0, y: 10, value: 1 },

0 commit comments

Comments
 (0)