Skip to content

Commit f5bc2dc

Browse files
beaucollinsbot-snapci
authored andcommitted
Internal Change
GitOrigin-RevId: 64a6d821f0a4f40991d130f5905a3461bc24553d
1 parent 7581a51 commit f5bc2dc

2 files changed

Lines changed: 73 additions & 17 deletions

File tree

‎src/valdi_modules/src/valdi/web_renderer/src/utils/parseAttributedText.ts‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,10 @@ class AttributedTextLayoutObserver implements ElementLayoutObserver {
271271
const style = attributedText.parts[i].style;
272272
const onLayout = style.onLayout;
273273
if (onLayout) {
274-
const outlineColor = style.outerOutlineColor ?? style.outlineColor;
275-
const outlineWidth = style.outerOutlineWidth ?? style.outlineWidth;
276274
this.measurements.push({
277275
onLayout,
278276
span: container.childNodes.item(i) as HTMLSpanElement,
279-
outlineWidth: outlineColor && outlineWidth ? outlineWidth : 0,
277+
outlineWidth: resolveOutline(style)?.width ?? 0,
280278
x: 0,
281279
y: 0,
282280
width: 0,
@@ -395,16 +393,24 @@ function applyTextDecoration(span: HTMLSpanElement, decoration: LabelTextDecorat
395393
}
396394
}
397395

398-
function applyOutline(
399-
span: HTMLSpanElement,
400-
color: string | undefined,
401-
width: number | undefined,
402-
context: AttributeApplierContext | undefined,
403-
): void {
404-
if (!color || !width) {
396+
// Web has a single text stroke, so when both pairs are complete the outer outline wins.
397+
// Colors and widths are only ever paired within the same (inner or outer) outline.
398+
function resolveOutline(style: StyleState): { color: string; width: number } | undefined {
399+
if (style.outerOutlineColor && style.outerOutlineWidth) {
400+
return { color: style.outerOutlineColor, width: style.outerOutlineWidth };
401+
}
402+
if (style.outlineColor && style.outlineWidth) {
403+
return { color: style.outlineColor, width: style.outlineWidth };
404+
}
405+
return undefined;
406+
}
407+
408+
function applyOutline(span: HTMLSpanElement, style: StyleState, context: AttributeApplierContext | undefined): void {
409+
const outline = resolveOutline(style);
410+
if (!outline) {
405411
return;
406412
}
407-
span.style.webkitTextStroke = `${width}px ${convertColor(color, context)}`;
413+
span.style.webkitTextStroke = `${outline.width}px ${convertColor(outline.color, context)}`;
408414
span.style.paintOrder = 'stroke fill';
409415
}
410416

@@ -492,12 +498,7 @@ function createStyledSpan(
492498
}
493499

494500
applyTextDecoration(span, style.textDecoration);
495-
applyOutline(
496-
span,
497-
style.outerOutlineColor ?? style.outlineColor,
498-
style.outerOutlineWidth ?? style.outlineWidth,
499-
context,
500-
);
501+
applyOutline(span, style, context);
501502

502503
if (style.onTap) {
503504
span.style.cursor = 'pointer';

‎src/valdi_modules/src/valdi/web_renderer/test/WebRendererCore.spec.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4699,6 +4699,61 @@ describe('web renderer core', () => {
46994699
expect(reported).toEqual({ x: 0, y: 0, width: 57, height: 21 });
47004700
});
47014701

4702+
describe('attributed text outlines', () => {
4703+
function outlineStyle(style: {
4704+
outlineColor?: string;
4705+
outlineWidth?: number;
4706+
outerOutlineColor?: string;
4707+
outerOutlineWidth?: number;
4708+
}): string {
4709+
const attributedText = new AttributedTextBuilder().append('28', style).build();
4710+
const container = renderAttributedText(ParsedAttributedText.parse(attributedText)) as unknown as FakeElement;
4711+
return container.childNodes.item(0)!.style.webkitTextStroke;
4712+
}
4713+
4714+
it('applies the inner outline when only an outer color is also set', () => {
4715+
expect(outlineStyle({ outerOutlineColor: '#fef3c7', outlineColor: '#f59e0b', outlineWidth: 1 })).toBe(
4716+
'1px #f59e0b',
4717+
);
4718+
});
4719+
4720+
it('applies the outer outline when its color and width are both set', () => {
4721+
expect(
4722+
outlineStyle({
4723+
outerOutlineColor: '#fef3c7',
4724+
outerOutlineWidth: 2,
4725+
outlineColor: '#f59e0b',
4726+
outlineWidth: 1,
4727+
}),
4728+
).toBe('2px #fef3c7');
4729+
});
4730+
4731+
it('never pairs an outer width with an inner color', () => {
4732+
expect(outlineStyle({ outerOutlineWidth: 2, outlineColor: '#f59e0b' })).toBeFalsy();
4733+
});
4734+
4735+
it('excludes only a complete outline pair from layout inflation', () => {
4736+
let reported: { width: number } | undefined;
4737+
const attributedText = new AttributedTextBuilder()
4738+
.append('28', {
4739+
onLayout: (_x, _y, width) => {
4740+
reported = { width };
4741+
},
4742+
outerOutlineColor: '#fef3c7',
4743+
outlineColor: '#f59e0b',
4744+
outlineWidth: 1,
4745+
})
4746+
.build();
4747+
const parsed = ParsedAttributedText.parse(attributedText);
4748+
const container = renderAttributedText(parsed) as unknown as FakeElement;
4749+
container.childNodes.item(0)!.rectWidth = 20;
4750+
4751+
dispatchAttributedTextLayouts(parsed, container as unknown as HTMLElement);
4752+
4753+
expect(reported).toEqual({ width: 18 });
4754+
});
4755+
});
4756+
47024757
it('keeps tappable attributed text interactive inside pointer-transparent labels', () => {
47034758
const attributedText = new AttributedTextBuilder().append('tap', { onTap: () => {} }).build();
47044759
const container = renderAttributedText(ParsedAttributedText.parse(attributedText)) as unknown as FakeElement;

0 commit comments

Comments
 (0)