Skip to content

Commit 5c727a3

Browse files
committed
🐛 fix: fix pseudo elt style issue and add test
1 parent 9fbe145 commit 5c727a3

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/parser/pseudoShape.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@ export const parsePseudoToShape = async (
1616
const { left, top, height, width } = bcr;
1717
let x = left;
1818
let y = top;
19-
const isContentBox = pseudoEl.boxSizing === 'content-box'
20-
const pseudoW = isContentBox ? parseFloat(pseudoEl.width) + parseFloat(pseudoEl.paddingLeft) + parseFloat(pseudoEl.paddingRight) + parseFloat(pseudoEl.borderLeftWidth) + parseFloat(pseudoEl.borderRightWidth) : parseFloat(pseudoEl.width);
21-
const pseudoH = isContentBox ? parseFloat(pseudoEl.height) + parseFloat(pseudoEl.paddingTop) + parseFloat(pseudoEl.paddingBottom) + parseFloat(pseudoEl.borderTopWidth) + parseFloat(pseudoEl.borderBottomWidth) : parseFloat(pseudoEl.height);
19+
const isContentBox = pseudoEl.boxSizing === 'content-box';
20+
21+
const pseudoW = isContentBox
22+
? parseFloat(pseudoEl.width) +
23+
parseFloat(pseudoEl.paddingLeft) +
24+
parseFloat(pseudoEl.paddingRight) +
25+
parseFloat(pseudoEl.borderLeftWidth) +
26+
parseFloat(pseudoEl.borderRightWidth)
27+
: parseFloat(pseudoEl.width);
28+
29+
const pseudoH = isContentBox
30+
? parseFloat(pseudoEl.height) +
31+
parseFloat(pseudoEl.paddingTop) +
32+
parseFloat(pseudoEl.paddingBottom) +
33+
parseFloat(pseudoEl.borderTopWidth) +
34+
parseFloat(pseudoEl.borderBottomWidth)
35+
: parseFloat(pseudoEl.height);
2236

2337
const rect = await parseToShape(node, pseudoEl);
2438

@@ -32,8 +46,8 @@ export const parsePseudoToShape = async (
3246
}
3347

3448
rect.frame = new Frame({
35-
width: pseudoW,
36-
height: pseudoH,
49+
width: pseudoW !== width ? pseudoW : width,
50+
height: pseudoH !== height ? pseudoH : height,
3751
x,
3852
y,
3953
});

tests/__tests__/parser/pseudoShape.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ describe('parsePseudoToShape', () => {
119119
border: 2px solid #fff;
120120
border-top: 0;
121121
border-left: 0;
122+
}
123+
124+
.with-padding-button-wrapper {
125+
background: #fff;
126+
border-color: #d9d9d9;
127+
border-style: solid;
128+
border-width: 1.02px 1px 1px 0;
129+
color: rgba(0, 0, 0, 0.85);
130+
cursor: pointer;
131+
display: inline-block;
132+
font-size: 14px;
133+
height: 32px;
134+
line-height: 30px;
135+
margin: 0;
136+
padding: 0 15px;
137+
position: relative;
138+
}
139+
140+
.with-padding-button-wrapper:first-child {
141+
border-left: 1px solid #d9d9d9;
142+
border-radius: 2px 0 0 2px;
143+
}
144+
145+
.with-padding-button-wrapper:not(:first-child):before {
146+
background-color: red;
147+
box-sizing: content-box;
148+
content: "";
149+
display: block;
150+
height: 100%;
151+
left: -1px;
152+
padding: 1px 0;
153+
position: absolute;
154+
top: -1px;
155+
width: 1px;
156+
}
122157
</style>
123158
`;
124159

@@ -140,6 +175,11 @@ describe('parsePseudoToShape', () => {
140175
<input type="checkbox" class="ant-checkbox-input" value="" checked="">
141176
<span id="checkbox" class="ant-checkbox-inner"/>
142177
</span>
178+
179+
<div >
180+
<div id="no-padding" class="with-padding-button-wrapper">button</div>
181+
<div id="with-padding" class="with-padding-button-wrapper">button</div>
182+
</div>
143183
</div>
144184
`;
145185
document.body.append(node);
@@ -177,4 +217,16 @@ describe('parsePseudoToShape', () => {
177217

178218
expect(checkbox.rotation).toBe(-45);
179219
});
220+
221+
it('带 padding 的伪类宽高解析正常', async () => {
222+
const node = document.getElementById('with-padding') as HTMLDivElement;
223+
const shape = (await parsePseudoToShape(node, 'before')) as Rectangle;
224+
const paddingBtn = shape.toSketchJSON();
225+
226+
expect(paddingBtn.frame.height).toBe(34);
227+
228+
const node1 = document.getElementById('no-padding') as HTMLDivElement;
229+
const shape1 = await parsePseudoToShape(node1, 'before');
230+
expect(shape1).toBeUndefined();
231+
});
180232
});

0 commit comments

Comments
 (0)