Skip to content

Commit

Permalink
refactor: optimize parsed style copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Oct 28, 2024
1 parent fdb1819 commit 1e43590
Show file tree
Hide file tree
Showing 120 changed files with 877 additions and 1,027 deletions.
315 changes: 156 additions & 159 deletions __tests__/unit/css/initial.spec.ts

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions __tests__/unit/css/properties/length-percentage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Renderer as CanvasRenderer } from '../../../../packages/g-svg/src';
import { Canvas, Circle, CSS, CSSUnitValue } from '../../../../packages/g/src';
import { Canvas, Circle, getParsedStyle } from '../../../../packages/g/src';
import { sleep } from '../../utils';

const $container = document.createElement('div');
Expand Down Expand Up @@ -52,35 +52,35 @@ describe('CSSPropertyLengthOrPercentage', () => {
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.equals(CSS.px(30))).toBeTruthy();

circle.style.cx = '20px';
expect(circle.getAttribute('cx')).toBe('20px');
// circle.style.cx = '20px';
// expect(circle.getAttribute('cx')).toBe('20px');
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.equals(CSS.px(20))).toBeTruthy();

circle.style.cx = '50%';
expect(circle.getAttribute('cx')).toBe('50%');
// circle.style.cx = '50%';
// expect(circle.getAttribute('cx')).toBe('50%');
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.equals(CSS.percent(50))).toBeTruthy();

circle.style.cx = '0';
expect(circle.getAttribute('cx')).toBe('0');
circle.style.cx = 0;
expect(circle.getAttribute('cx')).toBe(0);
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.equals(CSS.px(0))).toBeTruthy();

circle.style.cx = '0.2px';
expect(circle.getAttribute('cx')).toBe('0.2px');
// circle.style.cx = '0.2px';
// expect(circle.getAttribute('cx')).toBe('0.2px');
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.equals(CSS.px(0.2))).toBeTruthy();

circle.style.cx = undefined;
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.equals(CSS.px(0.2))).toBeTruthy();

circle.style.cx = null;
expect(circle.getAttribute('cx')).toBeNull();
// circle.style.cx = null;
// expect(circle.getAttribute('cx')).toBeNull();
// computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// expect(computed.toString()).toBe('unset');
expect(circle.parsedStyle.cx).toBeNull();
// expect(getParsedStyle(circle, 'cx')).toBeNull();

circle.animate(
[
Expand All @@ -100,13 +100,13 @@ describe('CSSPropertyLengthOrPercentage', () => {
// expect(circle.getAttribute('cx')).toBe('1em');
// // computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// // expect(computed.equals(CSS.em(1))).toBeTruthy();
// expect(circle.parsedStyle.cx).toBe(16);
// expect(getParsedStyle(circle, "cx")).toBe(16);

// // rem
// circle.style.cx = '2rem';
// expect(circle.getAttribute('cx')).toBe('2rem');
// // computed = circle.computedStyleMap().get('cx') as CSSUnitValue;
// // expect(computed.equals(CSS.rem(2))).toBeTruthy();
// expect(circle.parsedStyle.cx).toBe(32);
// expect(getParsedStyle(circle, "cx")).toBe(32);
});
});
74 changes: 38 additions & 36 deletions __tests__/unit/display-objects/circle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Circle, CSS, CSSRGB } from '../../../packages/g/src';
import { Circle, CSS, CSSRGB, getParsedStyle } from '../../../packages/g/src';

describe('Circle', () => {
it("should calc Circle's GeometryBounds, RenderBounds, Bounds and LocalBounds correctly", () => {
Expand Down Expand Up @@ -156,55 +156,57 @@ describe('Circle', () => {
]);

// expect(circle.getAttribute('opacity')).toBe('');
expect(circle.parsedStyle.opacity).toBeUndefined();
expect(getParsedStyle(circle, 'opacity')).toBeUndefined();
// expect(circle.getAttribute('fillOpacity')).toBe('');
expect(circle.parsedStyle.fillOpacity).toBeUndefined();
expect(getParsedStyle(circle, 'fillOpacity')).toBeUndefined();
// expect(circle.getAttribute('strokeOpacity')).toBe('');
expect(circle.parsedStyle.strokeOpacity).toBeUndefined();
expect(getParsedStyle(circle, 'strokeOpacity')).toBeUndefined();
// expect(circle.getAttribute('fill')).toBe('');
// expect(circle.parsedStyle.fill?.toString()).toStrictEqual(
// expect(getParsedStyle(circle, "fill")?.toString()).toStrictEqual(
// new CSSRGB(0, 0, 0, 0, true).toString(),
// ); // transparent
// expect(circle.getAttribute('stroke')).toBe('');
// expect(circle.parsedStyle.stroke?.toString()).toStrictEqual(
// expect(getParsedStyle(circle, "stroke")?.toString()).toStrictEqual(
// new CSSRGB(0, 0, 0, 0, true).toString(),
// ); // transparent
// expect(circle.getAttribute('transform')).toBe('');
// expect(circle.parsedStyle.transform).toStrictEqual([]);
// expect(getParsedStyle(circle, "transform")).toStrictEqual([]);
// expect(circle.getAttribute('transformOrigin')).toBe('');
// expect(circle.parsedStyle.transformOrigin).toStrictEqual([
// expect(getParsedStyle(circle, "transformOrigin")).toStrictEqual([
// CSS.percent(50),
// CSS.percent(50),
// ]);
// expect(circle.getAttribute('visibility')).toBe('');
expect(circle.parsedStyle.visibility).toBeUndefined();
expect(getParsedStyle(circle, 'visibility')).toBeUndefined();
// expect(circle.getAttribute('pointerEvents')).toBe('');
expect(circle.parsedStyle.pointerEvents).toBeUndefined();
expect(getParsedStyle(circle, 'pointerEvents')).toBeUndefined();
// expect(circle.getAttribute('lineWidth')).toBe('');
expect(circle.parsedStyle.lineWidth).toBeUndefined();
expect(getParsedStyle(circle, 'lineWidth')).toBeUndefined();
// expect(circle.getAttribute('lineJoin')).toBe('');
expect(circle.parsedStyle.lineJoin).toBeUndefined();
expect(getParsedStyle(circle, 'lineJoin')).toBeUndefined();
// expect(circle.getAttribute('lineCap')).toBe('');
expect(circle.parsedStyle.lineCap).toBeUndefined();
expect(getParsedStyle(circle, 'lineCap')).toBeUndefined();
// expect(circle.getAttribute('increasedLineWidthForHitTesting')).toBe('');
expect(circle.parsedStyle.increasedLineWidthForHitTesting).toBeUndefined();
expect(
getParsedStyle(circle, 'increasedLineWidthForHitTesting'),
).toBeUndefined();
// @ts-ignore
// expect(circle.getAttribute('fontSize')).toBe('');
// @ts-ignore
expect(circle.parsedStyle.fontSize).toBeUndefined();
expect(getParsedStyle(circle, 'fontSize')).toBeUndefined();
// expect(circle.getAttribute('zIndex')).toBe('');
// expect(circle.parsedStyle.zIndex).toBe(0);
// expect(getParsedStyle(circle, "zIndex")).toBe(0);
// expect(circle.getAttribute('cx')).toBe(100);
expect(circle.parsedStyle.cx).toBe(100);
expect(getParsedStyle(circle, 'cx')).toBe(100);
// expect(circle.getAttribute('cy')).toBe(100);
expect(circle.parsedStyle.cy).toBe(100);
expect(getParsedStyle(circle, 'cy')).toBe(100);
// expect(circle.getAttribute('r')).toBe(100);
expect(circle.parsedStyle.r).toBe(100);
expect(getParsedStyle(circle, 'r')).toBe(100);

// update fill
circle.style.fill = 'red';
expect(circle.getAttribute('fill')).toBe('red');
expect(circle.parsedStyle.fill?.toString()).toBe(
expect(getParsedStyle(circle, 'fill')?.toString()).toBe(
new CSSRGB(255, 0, 0).toString(),
);
// update transform
Expand All @@ -221,43 +223,43 @@ describe('Circle', () => {
});

// expect(circle.getAttribute('opacity')).toBe('');
expect(circle.parsedStyle.opacity).toBeUndefined();
expect(getParsedStyle(circle, 'opacity')).toBeUndefined();
// expect(circle.getAttribute('fillOpacity')).toBe('');
expect(circle.parsedStyle.fillOpacity).toBeUndefined();
expect(getParsedStyle(circle, 'fillOpacity')).toBeUndefined();
// expect(circle.getAttribute('strokeOpacity')).toBe('');
expect(circle.parsedStyle.strokeOpacity).toBeUndefined();
expect(getParsedStyle(circle, 'strokeOpacity')).toBeUndefined();
// expect(circle.getAttribute('fill')).toBe('');
// expect(circle.parsedStyle.fill?.toString()).toBe(
// expect(getParsedStyle(circle, "fill")?.toString()).toBe(
// new CSSRGB(0, 0, 0, 0, true).toString(),
// ); // transparent
// expect(circle.getAttribute('stroke')).toBe('');
// expect(circle.parsedStyle.stroke?.toString()).toBe(
// expect(getParsedStyle(circle, "stroke")?.toString()).toBe(
// new CSSRGB(0, 0, 0, 0, true).toString(),
// ); // transparent
// expect(circle.getAttribute('transform')).toBe('');
// expect(circle.parsedStyle.transform).toStrictEqual([]);
// expect(getParsedStyle(circle, "transform")).toStrictEqual([]);
// expect(circle.getAttribute('transformOrigin')).toBe('');
// expect(circle.parsedStyle.transformOrigin).toStrictEqual([
// expect(getParsedStyle(circle, "transformOrigin")).toStrictEqual([
// CSS.percent(50),
// CSS.percent(50),
// ]);
// expect(circle.getAttribute('visibility')).toBe('');
expect(circle.parsedStyle.visibility).toBeUndefined();
expect(getParsedStyle(circle, 'visibility')).toBeUndefined();
// expect(circle.getAttribute('pointerEvents')).toBe('');
expect(circle.parsedStyle.pointerEvents).toBeUndefined();
expect(getParsedStyle(circle, 'pointerEvents')).toBeUndefined();
// expect(circle.getAttribute('lineWidth')).toBe('');
expect(circle.parsedStyle.lineWidth).toBeUndefined();
expect(getParsedStyle(circle, 'lineWidth')).toBeUndefined();
// expect(circle.getAttribute('lineJoin')).toBe('');
expect(circle.parsedStyle.lineJoin).toBeUndefined();
expect(getParsedStyle(circle, 'lineJoin')).toBeUndefined();
// expect(circle.getAttribute('lineCap')).toBe('');
expect(circle.parsedStyle.lineCap).toBeUndefined();
expect(getParsedStyle(circle, 'lineCap')).toBeUndefined();
// expect(circle.getAttribute('zIndex')).toBe('');
// expect(circle.parsedStyle.zIndex).toBe(0);
// expect(getParsedStyle(circle, "zIndex")).toBe(0);
// // expect(circle.getAttribute('cx')).toBe('');
// expect(circle.parsedStyle.cx).toBe(0);
// expect(getParsedStyle(circle, "cx")).toBe(0);
// // expect(circle.getAttribute('cy')).toBe('');
// expect(circle.parsedStyle.cy).toBe(0);
// expect(getParsedStyle(circle, "cy")).toBe(0);
// // expect(circle.getAttribute('r')).toBe('');
// expect(circle.parsedStyle.r).toBe(0);
// expect(getParsedStyle(circle, "r")).toBe(0);
});
});
15 changes: 12 additions & 3 deletions __tests__/unit/display-objects/polygon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ describe('Polygon', () => {
[0, 100],
];

const s = circle.cloneNode();
s.id = 'start';

const m = circle.cloneNode();
m.className = 'middle';

const e = circle.cloneNode();
e.id = 'end';

const polygon = new Polygon({
style: {
points,
markerStart: circle,
markerEnd: circle,
markerMid: circle,
markerStart: s,
markerMid: m,
markerEnd: e,
},
});
expect(polygon.childNodes.length).toBe(5);
Expand Down
11 changes: 5 additions & 6 deletions __tests__/unit/display-objects/text.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { vec3 } from 'gl-matrix';
import { Renderer as CanvasRenderer } from '../../../packages/g-svg/src';
import { Canvas, Text } from '../../../packages/g/src';
import { Canvas, getParsedStyle, Text } from '../../../packages/g/src';
import { OffscreenCanvasContext } from '../offscreenCanvasContext';

const $container = document.createElement('div');
Expand Down Expand Up @@ -35,11 +34,11 @@ describe('Text', () => {
it('should allow number as valid content', () => {
const text = new Text({
style: {
text: 1,
text: '1',
},
});
expect(text.style.text).toBe(1);
expect(text.parsedStyle.text).toBe(1);
expect(text.style.text).toBe('1');
expect(getParsedStyle(text, 'text')).toBe('1');
});

it('should calc global bounds correctly', () => {
Expand All @@ -64,7 +63,7 @@ describe('Text', () => {

// parse font size with unit
// text.style.fontSize = '40px';
// expect(text.parsedStyle.fontSize).toBe(40);
// expect(getParsedStyle(text, "fontSize")).toBe(40);

// expect(text.nodeValue).toBe('这是测试文本This is text');
// expect(text.textContent).toBe('这是测试文本This is text');
Expand Down
3 changes: 2 additions & 1 deletion packages/g-components/src/Sector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ export class Sector extends CustomElement<SectorStyleProps> {
}

private updatePath() {
const { sx, sy, startAngle, endAngle, sr, sr0, sradius } = this.parsedStyle;
const { sx, sy, startAngle, endAngle, sr, sr0 } = this.attributes;
const { sradius } = this.parsedStyle;

const path = this.createPath(
sx,
Expand Down
4 changes: 3 additions & 1 deletion packages/g-components/src/Sector2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export class Sector extends Path {

private updatePath() {
// @ts-ignore
const { x, y, startAngle, endAngle, sr, sr0, radius } = this.parsedStyle;
const { x, y, startAngle, endAngle, sr, sr0 } = this.attributes;
// @ts-ignore
const { radius } = this.parsedStyle;

const path = this.createPath(
x,
Expand Down
Loading

0 comments on commit 1e43590

Please sign in to comment.