Skip to content

Commit b13ea5d

Browse files
committed
flipX, flipY
1 parent 77b14f9 commit b13ea5d

2 files changed

Lines changed: 276 additions & 0 deletions

File tree

src/main/java/org/htmlunit/javascript/host/dom/DOMMatrixReadOnly.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.htmlunit.javascript.JavaScriptEngine;
2424
import org.htmlunit.javascript.configuration.JsxClass;
2525
import org.htmlunit.javascript.configuration.JsxConstructor;
26+
import org.htmlunit.javascript.configuration.JsxFunction;
2627
import org.htmlunit.javascript.configuration.JsxGetter;
2728
import org.htmlunit.javascript.host.Window;
2829

@@ -364,4 +365,76 @@ public double getM44() {
364365
public boolean getIs2D() {
365366
return is2D_;
366367
}
368+
369+
/**
370+
* @return a new matrix being the result of the original matrix flipped about the x-axis.
371+
* This is equivalent to multiplying the matrix by DOMMatrix(-1, 0, 0, 1, 0, 0).
372+
* The original matrix is not modified.
373+
*/
374+
@JsxFunction
375+
public DOMMatrixReadOnly flipX() {
376+
final DOMMatrixReadOnly matrix = new DOMMatrixReadOnly();
377+
final Window window = getWindow();
378+
matrix.setParentScope(window);
379+
matrix.setPrototype(window.getPrototype(DOMMatrixReadOnly.class));
380+
381+
matrix.m11_ = -this.m11_;
382+
matrix.m12_ = -this.m12_;
383+
matrix.m13_ = -this.m13_;
384+
matrix.m14_ = -this.m14_;
385+
386+
matrix.m21_ = this.m21_;
387+
matrix.m22_ = this.m22_;
388+
matrix.m23_ = this.m23_;
389+
matrix.m24_ = this.m24_;
390+
391+
matrix.m31_ = this.m31_;
392+
matrix.m32_ = this.m32_;
393+
matrix.m33_ = this.m33_;
394+
matrix.m34_ = this.m34_;
395+
396+
matrix.m41_ = this.m41_;
397+
matrix.m42_ = this.m42_;
398+
matrix.m43_ = this.m43_;
399+
matrix.m44_ = this.m44_;
400+
401+
matrix.is2D_ = this.is2D_;
402+
return matrix;
403+
}
404+
405+
/**
406+
* @return a new matrix being the result of the original matrix flipped about the x-axis.
407+
* This is equivalent to multiplying the matrix by DOMMatrix(1, 0, 0, -1, 0, 0).
408+
* The original matrix is not modified.
409+
*/
410+
@JsxFunction
411+
public DOMMatrixReadOnly flipY() {
412+
final DOMMatrixReadOnly matrix = new DOMMatrixReadOnly();
413+
final Window window = getWindow();
414+
matrix.setParentScope(window);
415+
matrix.setPrototype(window.getPrototype(DOMMatrixReadOnly.class));
416+
417+
matrix.m11_ = this.m11_;
418+
matrix.m12_ = this.m12_;
419+
matrix.m13_ = this.m13_;
420+
matrix.m14_ = this.m14_;
421+
422+
matrix.m21_ = -this.m21_;
423+
matrix.m22_ = -this.m22_;
424+
matrix.m23_ = -this.m23_;
425+
matrix.m24_ = -this.m24_;
426+
427+
matrix.m31_ = this.m31_;
428+
matrix.m32_ = this.m32_;
429+
matrix.m33_ = this.m33_;
430+
matrix.m34_ = this.m34_;
431+
432+
matrix.m41_ = this.m41_;
433+
matrix.m42_ = this.m42_;
434+
matrix.m43_ = this.m43_;
435+
matrix.m44_ = this.m44_;
436+
437+
matrix.is2D_ = this.is2D_;
438+
return matrix;
439+
}
367440
}

src/test/java/org/htmlunit/javascript/host/dom/DOMMatrixReadOnlyTest.java

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,207 @@ public void contructorEmptyArray() throws Exception {
320320

321321
loadPageVerifyTitle2(html);
322322
}
323+
/**
324+
* @throws Exception on test failure
325+
*/
326+
@Test
327+
@Alerts({"[-1, 0, 0, 1, 0, 0]",
328+
"true"})
329+
public void flipX_identity() throws Exception {
330+
final String html = DOCTYPE_HTML
331+
+ "<html>\n"
332+
+ "<body>\n"
333+
+ "<script>\n"
334+
+ LOG_TITLE_FUNCTION
335+
+ DUMP_2D_FUNCTION
336+
+ "let m = new DOMMatrixReadOnly();\n"
337+
+ "let flipped = m.flipX();\n"
338+
+ "dump(flipped);\n"
339+
+ "</script>\n"
340+
+ "</body></html>";
341+
342+
loadPageVerifyTitle2(html);
343+
}
344+
345+
/**
346+
* @throws Exception on test failure
347+
*/
348+
@Test
349+
@Alerts({"[1, 0, 0, -1, 0, 0]",
350+
"true"})
351+
public void flipY_identity() throws Exception {
352+
final String html = DOCTYPE_HTML
353+
+ "<html>\n"
354+
+ "<body>\n"
355+
+ "<script>\n"
356+
+ LOG_TITLE_FUNCTION
357+
+ DUMP_2D_FUNCTION
358+
+ "let m = new DOMMatrixReadOnly();\n"
359+
+ "let flipped = m.flipY();\n"
360+
+ "dump(flipped);\n"
361+
+ "</script>\n"
362+
+ "</body></html>";
363+
364+
loadPageVerifyTitle2(html);
365+
}
366+
367+
/**
368+
* @throws Exception on test failure
369+
*/
370+
@Test
371+
@Alerts(DEFAULT = {"[NaN, 0, 0, 1, 0, 0]",
372+
"true"},
373+
FF = {"[NaN, 0, NaN, 1, NaN, 0]",
374+
"true"},
375+
FF_ESR = {"[NaN, 0, NaN, 1, NaN, 0]",
376+
"true"})
377+
@HtmlUnitNYI(FF = {"[NaN, 0, 0, 1, 0, 0]",
378+
"true"},
379+
FF_ESR = {"[NaN, 0, 0, 1, 0, 0]",
380+
"true"})
381+
public void flipX_NaN() throws Exception {
382+
final String html = DOCTYPE_HTML
383+
+ "<html>\n"
384+
+ "<body>\n"
385+
+ "<script>\n"
386+
+ LOG_TITLE_FUNCTION
387+
+ DUMP_2D_FUNCTION
388+
+ "let m = new DOMMatrixReadOnly([NaN, 0, 0, 1, 0, 0]);\n"
389+
+ "let flipped = m.flipX();\n"
390+
+ "dump(flipped);\n"
391+
+ "</script>\n"
392+
+ "</body></html>";
393+
394+
loadPageVerifyTitle2(html);
395+
}
396+
397+
/**
398+
* @throws Exception on test failure
399+
*/
400+
@Test
401+
@Alerts(DEFAULT = {"[1, 0, 0, -Infinity, 0, 0]",
402+
"true"},
403+
FF = {"[1, NaN, 0, -Infinity, 0, NaN]",
404+
"true"},
405+
FF_ESR = {"[1, NaN, 0, -Infinity, 0, NaN]",
406+
"true"})
407+
@HtmlUnitNYI(FF = {"[1, 0, 0, -Infinity, 0, 0]",
408+
"true"},
409+
FF_ESR = {"[1, 0, 0, -Infinity, 0, 0]",
410+
"true"})
411+
public void flipY_Infinity() throws Exception {
412+
final String html = DOCTYPE_HTML
413+
+ "<html>\n"
414+
+ "<body>\n"
415+
+ "<script>\n"
416+
+ LOG_TITLE_FUNCTION
417+
+ DUMP_2D_FUNCTION
418+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, Infinity, 0, 0]);\n"
419+
+ "let flipped = m.flipY();\n"
420+
+ "dump(flipped);\n"
421+
+ "</script>\n"
422+
+ "</body></html>";
423+
424+
loadPageVerifyTitle2(html);
425+
}
426+
/**
427+
* @throws Exception on test failure
428+
*/
429+
@Test
430+
@Alerts({"[-2, -3, 4, 5, 6, 7]",
431+
"true",
432+
"[2, 3, 4, 5, 6, 7]",
433+
"true"})
434+
public void flipX_general() throws Exception {
435+
final String html = DOCTYPE_HTML
436+
+ "<html>\n"
437+
+ "<body>\n"
438+
+ "<script>\n"
439+
+ LOG_TITLE_FUNCTION
440+
+ DUMP_2D_FUNCTION
441+
+ "let m = new DOMMatrixReadOnly([2, 3, 4, 5, 6, 7]);\n"
442+
+ "let flipped = m.flipX();\n"
443+
+ "dump(flipped);\n"
444+
+ "dump(m);\n"
445+
+ "</script>\n"
446+
+ "</body></html>";
447+
448+
loadPageVerifyTitle2(html);
449+
}
450+
451+
/**
452+
* @throws Exception on test failure
453+
*/
454+
@Test
455+
@Alerts({"[2, 3, -4, -5, 6, 7]",
456+
"true",
457+
"[2, 3, 4, 5, 6, 7]",
458+
"true"})
459+
public void flipY_general() throws Exception {
460+
final String html = DOCTYPE_HTML
461+
+ "<html>\n"
462+
+ "<body>\n"
463+
+ "<script>\n"
464+
+ LOG_TITLE_FUNCTION
465+
+ DUMP_2D_FUNCTION
466+
+ "let m = new DOMMatrixReadOnly([2, 3, 4, 5, 6, 7]);\n"
467+
+ "let flipped = m.flipY();\n"
468+
+ "dump(flipped);\n"
469+
+ "dump(m);\n"
470+
+ "</script>\n"
471+
+ "</body></html>";
472+
473+
loadPageVerifyTitle2(html);
474+
}
475+
/**
476+
* @throws Exception on test failure
477+
*/
478+
@Test
479+
@Alerts({"[-16, -15, 12, 11, 4, 3]",
480+
"1[-16, -15, -14, -13]",
481+
"2[12, 11, 10, 9]",
482+
"3[8, 7, 6, 5]",
483+
"4[4, 3, 2, 1]",
484+
"false"})
485+
public void flipX_3D() throws Exception {
486+
final String html = DOCTYPE_HTML
487+
+ "<html>\n"
488+
+ "<body>\n"
489+
+ "<script>\n"
490+
+ LOG_TITLE_FUNCTION
491+
+ DUMP_FUNCTION
492+
+ "let m = new DOMMatrixReadOnly([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);\n"
493+
+ "let flipped = m.flipX();\n"
494+
+ "dump(flipped);\n"
495+
+ "</script>\n"
496+
+ "</body></html>";
497+
498+
loadPageVerifyTitle2(html);
499+
}
500+
501+
/**
502+
* @throws Exception on test failure
503+
*/
504+
@Test
505+
@Alerts({"[16, 15, -12, -11, 4, 3]",
506+
"1[16, 15, 14, 13]",
507+
"2[-12, -11, -10, -9]",
508+
"3[8, 7, 6, 5]",
509+
"4[4, 3, 2, 1]",
510+
"false"})
511+
public void flipY_3D() throws Exception {
512+
final String html = DOCTYPE_HTML
513+
+ "<html>\n"
514+
+ "<body>\n"
515+
+ "<script>\n"
516+
+ LOG_TITLE_FUNCTION
517+
+ DUMP_FUNCTION
518+
+ "let m = new DOMMatrixReadOnly([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);\n"
519+
+ "let flipped = m.flipY();\n"
520+
+ "dump(flipped);\n"
521+
+ "</script>\n"
522+
+ "</body></html>";
523+
524+
loadPageVerifyTitle2(html);
525+
}
323526
}

0 commit comments

Comments
 (0)