Skip to content

Commit

Permalink
chore: [#1079] Continues on implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Oct 24, 2024
1 parent 9042dde commit a94885e
Show file tree
Hide file tree
Showing 31 changed files with 4,460 additions and 234 deletions.
57 changes: 38 additions & 19 deletions packages/happy-dom/src/dom/dom-matrix/DOMMatrixReadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,17 +794,23 @@ export default class DOMMatrixReadOnly {
scaleX = scaleX === undefined ? 1 : Number(scaleX);
scaleY = scaleY === undefined ? scaleX : Number(scaleY);

this[PropertySymbol.translateSelf](originX, originY, originZ);
if (originX !== 0 || originY !== 0 || originZ !== 0) {
this[PropertySymbol.translateSelf](originX, originY, originZ);
}

// prettier-ignore
this[PropertySymbol.multiplySelf]((<typeof DOMMatrixReadOnly>this.constructor)[PropertySymbol.fromArray]([
scaleX, 0, 0, 0,
0, scaleY, 0, 0,
0, 0, scaleZ, 0,
0, 0, 0, 1,
]))
if (scaleX !== 1 || scaleY !== 1 || scaleZ !== 1) {
// prettier-ignore
this[PropertySymbol.multiplySelf]((<typeof DOMMatrixReadOnly>this.constructor)[PropertySymbol.fromArray]([
scaleX, 0, 0, 0,
0, scaleY, 0, 0,
0, 0, scaleZ, 0,
0, 0, 0, 1,
]));
}

this[PropertySymbol.translateSelf](-originX, -originY, -originZ);
if (originX !== 0 || originY !== 0 || originZ !== 0) {
this[PropertySymbol.translateSelf](-originX, -originY, -originZ);
}
}

/**
Expand All @@ -820,17 +826,23 @@ export default class DOMMatrixReadOnly {
* @param [originZ] Z-Axis scale.
*/
public [PropertySymbol.scale3dSelf](scale = 1, originX = 0, originY = 0, originZ = 0): void {
this[PropertySymbol.translateSelf](originX, originY, originZ);
if (originX !== 0 || originY !== 0 || originZ !== 0) {
this[PropertySymbol.translateSelf](originX, originY, originZ);
}

// prettier-ignore
this[PropertySymbol.multiplySelf]((<typeof DOMMatrixReadOnly>this.constructor)[PropertySymbol.fromArray]([
scale, 0, 0, 0,
0, scale, 0, 0,
0, 0, scale, 0,
0, 0, 0, 1,
]))
if (scale !== 1) {
// prettier-ignore
this[PropertySymbol.multiplySelf]((<typeof DOMMatrixReadOnly>this.constructor)[PropertySymbol.fromArray]([
scale, 0, 0, 0,
0, scale, 0, 0,
0, 0, scale, 0,
0, 0, 0, 1,
]));
}

this[PropertySymbol.translateSelf](-originX, -originY, -originZ);
if (originX !== 0 || originY !== 0 || originZ !== 0) {
this[PropertySymbol.translateSelf](-originX, -originY, -originZ);
}
}

/**
Expand All @@ -841,6 +853,10 @@ export default class DOMMatrixReadOnly {
* @param [scaleY] Y-Axis scale.
*/
public [PropertySymbol.scaleNonUniformSelf](scaleX = 1, scaleY = 1): void {
if (scaleX === 1 && scaleY === 1) {
return;
}

// prettier-ignore
this[PropertySymbol.multiplySelf]((<typeof DOMMatrixReadOnly>this.constructor)[PropertySymbol.fromArray]([
scaleX, 0, 0, 0,
Expand Down Expand Up @@ -972,7 +988,10 @@ export default class DOMMatrixReadOnly {
* @param y The Y component of the axis vector.
*/
public [PropertySymbol.rotateFromVectorSelf](x = 0, y = 0): void {
this[PropertySymbol.rotateSelf](x === 0 && y === 0 ? 0 : (Math.atan2(y, x) * 180) / Math.PI);
if (x === 0 && y === 0) {
return;
}
this[PropertySymbol.rotateSelf]((Math.atan2(y, x) * 180) / Math.PI);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default class SVGSVGElement extends SVGGraphicsElement {
* @returns Number.
*/
public createSVGNumber(): SVGNumber {
return new SVGNumber();
return new SVGNumber(PropertySymbol.illegalConstructor, this[PropertySymbol.window]);
}

/**
Expand Down
53 changes: 31 additions & 22 deletions packages/happy-dom/src/svg/SVGLengthList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class SVGLengthList {
* Clears all items from the list.
*/
public clear(): void {
this[PropertySymbol.ownerElement].removeAttribute(this[PropertySymbol.attributeName]);
this[PropertySymbol.setAttribute](null);
}

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ export default class SVGLengthList {
newItem[PropertySymbol.getAttribute] = () => newItem[PropertySymbol.attributeValue];
newItem[PropertySymbol.setAttribute] = () => {
this[PropertySymbol.cache].attributeValue = this[PropertySymbol.getItemList]()
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);
};
Expand Down Expand Up @@ -261,13 +261,13 @@ export default class SVGLengthList {
newItem[PropertySymbol.getAttribute] = () => newItem[PropertySymbol.attributeValue];
newItem[PropertySymbol.setAttribute] = () => {
this[PropertySymbol.cache].attributeValue = this[PropertySymbol.getItemList]()
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);
};

this[PropertySymbol.cache].attributeValue = items
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);

Expand Down Expand Up @@ -297,6 +297,10 @@ export default class SVGLengthList {
const items = this[PropertySymbol.getItemList]();
const existingIndex = items.indexOf(newItem);

if (existingIndex === index) {
return newItem;
}

if (existingIndex !== -1) {
items.splice(existingIndex, 1);
}
Expand All @@ -312,22 +316,24 @@ export default class SVGLengthList {
items[index][PropertySymbol.setAttribute] = null;
}

const replacedItem = items[index];

items[index] = newItem;

newItem[PropertySymbol.getAttribute] = () => newItem[PropertySymbol.attributeValue];
newItem[PropertySymbol.setAttribute] = () => {
this[PropertySymbol.cache].attributeValue = this[PropertySymbol.getItemList]()
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);
};

this[PropertySymbol.cache].attributeValue = items
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);

return newItem;
return replacedItem;
}

/**
Expand Down Expand Up @@ -380,7 +386,9 @@ export default class SVGLengthList {

items.splice(index, 1);

this[PropertySymbol.setAttribute](items.map((item) => item.valueAsString).join(' '));
this[PropertySymbol.setAttribute](
items.map((item) => item[PropertySymbol.attributeValue] ?? '0').join(' ')
);

return removedItem;
}
Expand Down Expand Up @@ -416,13 +424,13 @@ export default class SVGLengthList {
newItem[PropertySymbol.getAttribute] = () => newItem[PropertySymbol.attributeValue];
newItem[PropertySymbol.setAttribute] = () => {
this[PropertySymbol.cache].attributeValue = this[PropertySymbol.getItemList]()
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);
};

this[PropertySymbol.cache].attributeValue = items
.map((item) => item[PropertySymbol.attributeValue])
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
this[PropertySymbol.setAttribute](this[PropertySymbol.cache].attributeValue);

Expand Down Expand Up @@ -457,18 +465,19 @@ export default class SVGLengthList {
if (trimmed) {
const parts = trimmed.split(ATTRIBUTE_SEPARATOR_REGEXP);
for (let i = 0, max = parts.length; i < max; i++) {
let attributeValue = parts[i];
items.push(
new SVGLength(PropertySymbol.illegalConstructor, this[PropertySymbol.window], {
getAttribute: () => attributeValue,
setAttribute: (value: string) => {
attributeValue = value;
const newAttributeValue = items.map((item) => item.valueAsString).join(' ');
cache.attributeValue = newAttributeValue;
this[PropertySymbol.setAttribute](newAttributeValue);
}
})
);
const item = new SVGLength(PropertySymbol.illegalConstructor, this[PropertySymbol.window], {
getAttribute: () => item[PropertySymbol.attributeValue],
setAttribute: (value: string) => {
item[PropertySymbol.attributeValue] = value;
const newAttributeValue = items
.map((item) => item[PropertySymbol.attributeValue] ?? '0')
.join(' ');
cache.attributeValue = newAttributeValue;
this[PropertySymbol.setAttribute](newAttributeValue);
}
});
item[PropertySymbol.attributeValue] = parts[i];
items.push(item);
}
}

Expand Down
Loading

0 comments on commit a94885e

Please sign in to comment.