Skip to content

Commit e4cb499

Browse files
committed
Properly handle margin-left and margin-right specified on the input elements.
1 parent da21770 commit e4cb499

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

ts/output/common/Wrapper.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export const SPACE: StringMap = {
104104
* Padding and border data from the style attribute
105105
*/
106106
export type StyleData = {
107+
margin: [number, number, number, number];
107108
padding: [number, number, number, number];
108109
border: {
109110
width: [number, number, number, number];
@@ -596,9 +597,10 @@ export class CommonWrapper<
596597
if (!this.styleData) return bbox;
597598
const padding = this.styleData.padding;
598599
const border = this.styleData.border?.width || [0, 0, 0, 0];
600+
const margin = this.styleData.margin || [0, 0, 0, 0];
599601
const obox = bbox.copy();
600602
for (const [, i, side] of BBox.boxSides) {
601-
(obox as any)[side] += padding[i] + border[i];
603+
(obox as any)[side] += padding[i] + border[i] + margin[i];
602604
}
603605
return obox;
604606
}
@@ -748,7 +750,9 @@ export class CommonWrapper<
748750
if (!this.styleData) return;
749751
const border = this.styleData.border;
750752
const padding = this.styleData.padding;
751-
bbox.w += (border?.width?.[3] || 0) + (padding?.[3] || 0);
753+
const margin = this.styleData.margin;
754+
bbox.w +=
755+
(border?.width?.[3] || 0) + (padding?.[3] || 0) + (margin?.[3] || 0);
752756
}
753757

754758
/**
@@ -758,8 +762,11 @@ export class CommonWrapper<
758762
if (!this.styleData) return;
759763
const border = this.styleData.border;
760764
const padding = this.styleData.padding;
761-
bbox.h += (border?.width?.[0] || 0) + (padding?.[0] || 0);
762-
bbox.d += (border?.width?.[2] || 0) + (padding?.[2] || 0);
765+
const margin = this.styleData.margin;
766+
bbox.h +=
767+
(border?.width?.[0] || 0) + (padding?.[0] || 0) + (margin?.[0] || 0);
768+
bbox.d +=
769+
(border?.width?.[2] || 0) + (padding?.[2] || 0) + (margin?.[2] || 0);
763770
}
764771

765772
/**
@@ -769,7 +776,9 @@ export class CommonWrapper<
769776
if (!this.styleData) return;
770777
const border = this.styleData.border;
771778
const padding = this.styleData.padding;
772-
bbox.w += (border?.width?.[1] || 0) + (padding?.[1] || 0);
779+
const margin = this.styleData.margin;
780+
bbox.w +=
781+
(border?.width?.[1] || 0) + (padding?.[1] || 0) + (margin?.[1] || 0);
773782
}
774783

775784
/**
@@ -875,11 +884,13 @@ export class CommonWrapper<
875884
protected getStyleData() {
876885
if (!this.styles) return;
877886
const padding = Array(4).fill(0);
887+
const margin = Array(4).fill(0);
878888
const width = Array(4).fill(0);
879889
const style = Array(4);
880890
const color = Array(4);
881891
let hasPadding = false;
882892
let hasBorder = false;
893+
let hasMargin = false;
883894
for (const [name, i] of BBox.boxSides) {
884895
const key = 'border' + name;
885896
const w = this.styles.get(key + 'Width');
@@ -894,11 +905,17 @@ export class CommonWrapper<
894905
hasPadding = true;
895906
padding[i] = Math.max(0, this.length2em(p, 1));
896907
}
908+
const m = this.styles.get('margin' + name);
909+
if (m) {
910+
hasMargin = true;
911+
margin[i] = this.length2em(m, 1);
912+
}
897913
}
898914
this.styleData =
899-
hasPadding || hasBorder
915+
hasPadding || hasBorder || hasMargin
900916
? ({
901917
padding,
918+
margin,
902919
border: hasBorder ? { width, style, color } : null,
903920
} as StyleData)
904921
: null;

ts/output/svg/Wrapper.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class SvgWrapper<N, T, D> extends CommonWrapper<
239239
* @param {N[]} parents The HTML nodes in which the output is to be placed
240240
* @returns {N[]} The roots of the HTML tree for the node's output
241241
*/
242-
protected handleHref(parents: N[]) {
242+
protected handleHref(parents: N[]): N[] {
243243
const href = this.node.attributes.get('href');
244244
if (!href) return parents;
245245
let i = 0;
@@ -278,10 +278,17 @@ export class SvgWrapper<N, T, D> extends CommonWrapper<
278278
);
279279
}
280280
const padding = (this.styleData?.padding || [0, 0, 0, 0])[3];
281+
const margin = (this.styleData?.margin || [0, 0, 0, 0])[3];
281282
const border = (this.styleData?.border?.width || [0, 0, 0, 0])[3];
282283
if (padding || border) {
283284
this.dx = padding + border;
284285
}
286+
if (margin) {
287+
const transform = `translate(${this.fixed(margin)},0)`;
288+
this.dom.forEach((node) =>
289+
this.adaptor.setAttribute(node, 'transform', transform)
290+
);
291+
}
285292
}
286293

287294
/**
@@ -346,6 +353,7 @@ export class SvgWrapper<N, T, D> extends CommonWrapper<
346353
protected handleBorder() {
347354
const border = this.styleData?.border;
348355
if (!border) return;
356+
const margin = this.styleData?.margin ?? [0, 0, 0, 0];
349357
const f = SvgWrapper.borderFuzz;
350358
const adaptor = this.adaptor;
351359
let k = 0;
@@ -355,7 +363,9 @@ export class SvgWrapper<N, T, D> extends CommonWrapper<
355363
const L = !n || !k ? 1 : 0;
356364
const R = !n || k === n ? 1 : 0;
357365
const bbox = isEmbellished ? this.getOuterBBox() : this.getLineBBox(k++);
358-
const [h, d, w] = [bbox.h + f, bbox.d + f, bbox.w + f];
366+
const h = bbox.h - margin[0] + f;
367+
const d = bbox.d - margin[2] + f;
368+
const w = bbox.w - margin[1] - margin[3] + f;
359369
const outerRT = [w, h];
360370
const outerLT = [-f, h];
361371
const outerRB = [w, -d];

0 commit comments

Comments
 (0)