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 21, 2024
1 parent 70d6786 commit 9042dde
Show file tree
Hide file tree
Showing 13 changed files with 1,293 additions and 101 deletions.
46 changes: 32 additions & 14 deletions packages/happy-dom/src/svg/SVGAnimatedTransformList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,59 @@ export default class SVGAnimatedTransformList {
}

/**
* Returns base value.
* Returns animated value.
*
* @returns Base value.
* @returns Animated value.
*/
public get baseVal(): SVGTransformList {
if (!this[PropertySymbol.baseVal]) {
this[PropertySymbol.baseVal] = new SVGTransformList(
public get animVal(): SVGTransformList {
if (!this[PropertySymbol.animVal]) {
this[PropertySymbol.animVal] = new SVGTransformList(
PropertySymbol.illegalConstructor,
this[PropertySymbol.window],
{
readOnly: true,
getAttribute: this[PropertySymbol.getAttribute],
setAttribute: () => {}
setAttribute: this[PropertySymbol.setAttribute]
}
);
}
return this[PropertySymbol.baseVal];
return this[PropertySymbol.animVal];
}

/**
* Returns animated value.
*
* @returns Animated value.
* @param value Animated value.
*/
public get animVal(): SVGTransformList {
if (!this[PropertySymbol.animVal]) {
this[PropertySymbol.animVal] = new SVGTransformList(
public set animVal(_value) {
// Do nothing
}

/**
* Returns base value.
*
* @returns Base value.
*/
public get baseVal(): SVGTransformList {
if (!this[PropertySymbol.baseVal]) {
this[PropertySymbol.baseVal] = new SVGTransformList(
PropertySymbol.illegalConstructor,
this[PropertySymbol.window],
{
readOnly: true,
getAttribute: this[PropertySymbol.getAttribute],
setAttribute: this[PropertySymbol.setAttribute]
setAttribute: () => {}
}
);
}
return this[PropertySymbol.animVal];
return this[PropertySymbol.baseVal];
}

/**
* Returns base value.
*
* @param value Base value.
*/
public set baseVal(_value) {
// Do nothing
}
}
120 changes: 60 additions & 60 deletions packages/happy-dom/src/svg/SVGLength.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as PropertySymbol from '../PropertySymbol.js';
import SVGUnitTypeEnum from './SVGUnitTypeEnum.js';
import SVGLengthTypeEnum from './SVGLengthTypeEnum.js';
import BrowserWindow from '../window/BrowserWindow.js';

const ATTRIBUTE_REGEXP = /^(\d+|\d+\.\d+)(px|em|ex|cm|mm|in|pt|pc|%|)$/;
Expand All @@ -11,17 +11,17 @@ const ATTRIBUTE_REGEXP = /^(\d+|\d+\.\d+)(px|em|ex|cm|mm|in|pt|pc|%|)$/;
*/
export default class SVGLength {
// Static properties
public static SVG_LENGTHTYPE_UNKNOWN = SVGUnitTypeEnum.unknown;
public static SVG_LENGTHTYPE_NUMBER = SVGUnitTypeEnum.number;
public static SVG_LENGTHTYPE_PERCENTAGE = SVGUnitTypeEnum.percentage;
public static SVG_LENGTHTYPE_EMS = SVGUnitTypeEnum.ems;
public static SVG_LENGTHTYPE_EXS = SVGUnitTypeEnum.exs;
public static SVG_LENGTHTYPE_PX = SVGUnitTypeEnum.px;
public static SVG_LENGTHTYPE_CM = SVGUnitTypeEnum.cm;
public static SVG_LENGTHTYPE_MM = SVGUnitTypeEnum.mm;
public static SVG_LENGTHTYPE_IN = SVGUnitTypeEnum.in;
public static SVG_LENGTHTYPE_PT = SVGUnitTypeEnum.pt;
public static SVG_LENGTHTYPE_PC = SVGUnitTypeEnum.pc;
public static SVG_LENGTHTYPE_UNKNOWN = SVGLengthTypeEnum.unknown;
public static SVG_LENGTHTYPE_NUMBER = SVGLengthTypeEnum.number;
public static SVG_LENGTHTYPE_PERCENTAGE = SVGLengthTypeEnum.percentage;
public static SVG_LENGTHTYPE_EMS = SVGLengthTypeEnum.ems;
public static SVG_LENGTHTYPE_EXS = SVGLengthTypeEnum.exs;
public static SVG_LENGTHTYPE_PX = SVGLengthTypeEnum.px;
public static SVG_LENGTHTYPE_CM = SVGLengthTypeEnum.cm;
public static SVG_LENGTHTYPE_MM = SVGLengthTypeEnum.mm;
public static SVG_LENGTHTYPE_IN = SVGLengthTypeEnum.in;
public static SVG_LENGTHTYPE_PT = SVGLengthTypeEnum.pt;
public static SVG_LENGTHTYPE_PC = SVGLengthTypeEnum.pc;

// Internal properties
public [PropertySymbol.window]: BrowserWindow;
Expand Down Expand Up @@ -67,43 +67,43 @@ export default class SVGLength {
*
* @returns Unit type.
*/
public get unitType(): SVGUnitTypeEnum {
public get unitType(): SVGLengthTypeEnum {
const attributeValue = this[PropertySymbol.getAttribute]
? this[PropertySymbol.getAttribute]()
: this[PropertySymbol.attributeValue];
? this[PropertySymbol.getAttribute]() || ''
: this[PropertySymbol.attributeValue] || '';
const match = attributeValue.match(ATTRIBUTE_REGEXP);

if (!match) {
return SVGUnitTypeEnum.unknown;
return SVGLengthTypeEnum.unknown;
}

if (isNaN(parseFloat(match[1]))) {
return SVGUnitTypeEnum.unknown;
return SVGLengthTypeEnum.unknown;
}

switch (match[2]) {
case '':
return SVGUnitTypeEnum.number;
return SVGLengthTypeEnum.number;
case 'px':
return SVGUnitTypeEnum.px;
return SVGLengthTypeEnum.px;
case 'cm':
return SVGUnitTypeEnum.cm;
return SVGLengthTypeEnum.cm;
case 'mm':
return SVGUnitTypeEnum.mm;
return SVGLengthTypeEnum.mm;
case 'in':
return SVGUnitTypeEnum.in;
return SVGLengthTypeEnum.in;
case 'pt':
return SVGUnitTypeEnum.pt;
return SVGLengthTypeEnum.pt;
case 'pc':
return SVGUnitTypeEnum.pc;
return SVGLengthTypeEnum.pc;
case 'em':
case 'ex':
case '%':
throw new this[PropertySymbol.window].TypeError(
`Failed to execute 'value' on 'SVGLength': Could not resolve relative length.`
);
default:
return SVGUnitTypeEnum.unknown;
return SVGLengthTypeEnum.unknown;
}
}

Expand All @@ -114,8 +114,8 @@ export default class SVGLength {
*/
public get value(): number {
const attributeValue = this[PropertySymbol.getAttribute]
? this[PropertySymbol.getAttribute]()
: this[PropertySymbol.attributeValue];
? this[PropertySymbol.getAttribute]() || ''
: this[PropertySymbol.attributeValue] || '';
const match = attributeValue.match(ATTRIBUTE_REGEXP);

if (!match) {
Expand Down Expand Up @@ -176,37 +176,37 @@ export default class SVGLength {
let unitType = '';
let valueInSpecifiedUnits = value;
switch (this.unitType) {
case SVGUnitTypeEnum.number:
case SVGLengthTypeEnum.number:
valueInSpecifiedUnits = value;
unitType = '';
break;
case SVGUnitTypeEnum.px:
case SVGLengthTypeEnum.px:
valueInSpecifiedUnits = value;
unitType = 'px';
break;
case SVGUnitTypeEnum.cm:
case SVGLengthTypeEnum.cm:
valueInSpecifiedUnits = (value / 96) * 2.54;
unitType = 'cm';
break;
case SVGUnitTypeEnum.mm:
case SVGLengthTypeEnum.mm:
valueInSpecifiedUnits = (value / 96) * 25.4;
unitType = 'mm';
break;
case SVGUnitTypeEnum.in:
case SVGLengthTypeEnum.in:
valueInSpecifiedUnits = value / 96;
unitType = 'in';
break;
case SVGUnitTypeEnum.pt:
case SVGLengthTypeEnum.pt:
valueInSpecifiedUnits = value / 72;
unitType = 'pt';
break;
case SVGUnitTypeEnum.pc:
case SVGLengthTypeEnum.pc:
valueInSpecifiedUnits = value / 6;
unitType = 'pc';
break;
case SVGUnitTypeEnum.percentage:
case SVGUnitTypeEnum.ems:
case SVGUnitTypeEnum.exs:
case SVGLengthTypeEnum.percentage:
case SVGLengthTypeEnum.ems:
case SVGLengthTypeEnum.exs:
throw new this[PropertySymbol.window].TypeError(
`Failed to set the 'value' property on 'SVGLength': Could not resolve relative length.`
);
Expand Down Expand Up @@ -271,32 +271,32 @@ export default class SVGLength {

let unit = '';
switch (unitType) {
case SVGUnitTypeEnum.number:
case SVGLengthTypeEnum.number:
unit = '';
break;
case SVGUnitTypeEnum.px:
case SVGLengthTypeEnum.px:
unit = 'px';
break;
case SVGUnitTypeEnum.cm:
case SVGLengthTypeEnum.cm:
unit = 'cm';
break;
case SVGUnitTypeEnum.mm:
case SVGLengthTypeEnum.mm:
unit = 'mm';
break;
case SVGUnitTypeEnum.in:
case SVGLengthTypeEnum.in:
unit = 'in';
break;
case SVGUnitTypeEnum.pt:
case SVGLengthTypeEnum.pt:
unit = 'pt';
break;
case SVGUnitTypeEnum.pc:
case SVGLengthTypeEnum.pc:
unit = 'pc';
break;
case SVGUnitTypeEnum.ems:
case SVGUnitTypeEnum.exs:
case SVGUnitTypeEnum.percentage:
case SVGLengthTypeEnum.ems:
case SVGLengthTypeEnum.exs:
case SVGLengthTypeEnum.percentage:
throw new this[PropertySymbol.window].TypeError(
`Failed to executute 'newValueSpecifiedUnits' on 'SVGLength': Could not resolve relative length.`
`Failed to execute 'newValueSpecifiedUnits' on 'SVGLength': Could not resolve relative length.`
);
default:
break;
Expand All @@ -313,7 +313,7 @@ export default class SVGLength {
* Convert to specific units.
* @param unitType
*/
public convertToSpecifiedUnits(unitType: SVGUnitTypeEnum): void {
public convertToSpecifiedUnits(unitType: SVGLengthTypeEnum): void {
if (this[PropertySymbol.readOnly]) {
throw new this[PropertySymbol.window].TypeError(
`Failed to execute 'convertToSpecifiedUnits' on 'SVGLength': The object is read-only.`
Expand All @@ -330,37 +330,37 @@ export default class SVGLength {
let unit = '';

switch (unitType) {
case SVGUnitTypeEnum.number:
case SVGLengthTypeEnum.number:
unit = '';
break;
case SVGUnitTypeEnum.px:
case SVGLengthTypeEnum.px:
unit = 'px';
break;
case SVGUnitTypeEnum.cm:
case SVGLengthTypeEnum.cm:
value = (value / 96) * 2.54;
unit = 'cm';
break;
case SVGUnitTypeEnum.mm:
case SVGLengthTypeEnum.mm:
value = (value / 96) * 25.4;
unit = 'mm';
break;
case SVGUnitTypeEnum.in:
case SVGLengthTypeEnum.in:
value = value / 96;
unit = 'in';
break;
case SVGUnitTypeEnum.pt:
case SVGLengthTypeEnum.pt:
value = value / 72;
unit = 'pt';
break;
case SVGUnitTypeEnum.pc:
case SVGLengthTypeEnum.pc:
value = value / 6;
unit = 'pc';
break;
case SVGUnitTypeEnum.percentage:
case SVGUnitTypeEnum.ems:
case SVGUnitTypeEnum.exs:
case SVGLengthTypeEnum.percentage:
case SVGLengthTypeEnum.ems:
case SVGLengthTypeEnum.exs:
throw new this[PropertySymbol.window].TypeError(
`Failed to executute 'convertToSpecifiedUnits' on 'SVGLength': Could not resolve relative length.`
`Failed to execute 'convertToSpecifiedUnits' on 'SVGLength': Could not resolve relative length.`
);
default:
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum SVGUnitTypeEnum {
enum SVGLengthTypeEnum {
unknown = 0,
number = 1,
percentage = 2,
Expand All @@ -12,4 +12,4 @@ enum SVGUnitTypeEnum {
pc = 10
}

export default SVGUnitTypeEnum;
export default SVGLengthTypeEnum;
17 changes: 14 additions & 3 deletions packages/happy-dom/src/svg/SVGMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class SVGMatrix {
public [PropertySymbol.attributeValue]: string | null = null;
public [PropertySymbol.readOnly]: boolean = false;
public [PropertySymbol.domMatrix]: DOMMatrix | null = null;
private [PropertySymbol.cache]: { domMatrix: DOMMatrix | null; attributeValue: string } = {
domMatrix: null,
attributeValue: ''
};

/**
* Constructor.
Expand Down Expand Up @@ -365,11 +369,18 @@ export default class SVGMatrix {
? this[PropertySymbol.getAttribute]()
: this[PropertySymbol.attributeValue];

if (!attribute) {
return new DOMMatrix();
if (this[PropertySymbol.cache].attributeValue === attribute) {
return this[PropertySymbol.cache].domMatrix;
}

return <DOMMatrix>DOMMatrix[PropertySymbol.fromString](attribute);
const domMatrix = attribute
? <DOMMatrix>DOMMatrix[PropertySymbol.fromString](attribute)
: new DOMMatrix();

this[PropertySymbol.cache].domMatrix = domMatrix;
this[PropertySymbol.cache].attributeValue = attribute;

return domMatrix;
}

/**
Expand Down
Loading

0 comments on commit 9042dde

Please sign in to comment.