Jsdom: SVGGElement ist nicht definiert

Erstellt am 3. Dez. 2019  ·  3Kommentare  ·  Quelle: jsdom/jsdom

Ich denke, SVGGElement ( MDN-Dokumentation ) wird in jsdom nicht unterstützt?

Basisinformation:

  • Node.js-Version: 10.150.
  • jsdom-Version: 11.5.1

Minimaler Reproduktionsfall

const { JSDOM } = require("jsdom");

const options = {
  ... your options here ...
};
const dom = new JSDOM(`
  <svg>
    <g id="g"> 
  </svg>
`, options);

const g = dom.window.document.querySelector("#g");

if (g instanceof SVGGElement) { // throws "SVGGElement is not defined"
  console.log("ok");
}

Wie verhält sich ähnlicher Code in Browsern?

https://jsfiddle.net/p3ag4v85/

(überprüfen Sie die Konsole und suchen Sie nach ok )

Anmerkungen

Ich muss sagen, dass ich den Fehler bemerkt habe, als ich meinen Code mit Jest getestet habe, also intern jsdom verwendet habe. Hier ist der Fehler, den ich bekommen habe:

● should throw an Error if g element is not found

    expect(received).toThrowError(expected)

    Expected message: "group element not found"
    Received message: "SVGGElement is not defined"

          46 |         if (!(svgElement instanceof SVGSVGElement))
          47 |             throw new Error("svg element not found");
        > 48 |         if (!(groupElement instanceof SVGGElement))
             |                                       ^
          49 |             throw new Error("g element not found");
          50 |         this.groupElement = groupElement;
          51 |         this.groupBBoxWidth = 0;

          at new Lib (lib/js/index.js:48:39)
          at test/MyLib/constructor.test.js:86:15
          at Object.<anonymous> (node_modules/expect/build/toThrowMatchers.js:81:11)
          at Object.toThrowError (node_modules/expect/build/index.js:342:33)
          at Object.<anonymous> (test/MyLib/constructor.test.js:87:7)

      85 |     expect(
      86 |         () => new Lib({ svgIdentifier: "svg", groupIdentifier: "g" })
    > 87 |     ).toThrowError(new TypeError("group element not found"));
         |       ^
      88 | });
      89 | 

      at Object.<anonymous> (test/MyLib/constructor.test.js:87:7)
svg

Hilfreichster Kommentar

Tatsächlich scheint dies nur daran zu liegen, dass wir das SVG-Element g nicht unterstützen. Unsere SVG-Unterstützung ist im Moment nicht allzu groß.

Alle 3 Kommentare

SVGElement wird unterstützt. Ihr Code sucht jedoch in Node.js global nach SVGElement . Sie müssen dom.window.SVGElement verwenden.

Ja. Dies ist in der Tat ein Fehler. Die Spezifikation scheint für SVG-Gruppen nicht korrekt implementiert zu sein.

<!DOCTYPE html>
<html>
    <body>
        <svg id="svg">
            <g id="group"></g>
        </svg>
        <script>
            "use strict";

            window.addEventListener("load", function() {
                const svg = document.getElementById("svg");
                const group = document.getElementById("group");

                console.dir(svg.constructor);   // ƒ SVGSVGElement()
                console.dir(group.constructor); // ƒ SVGGElement()
            });
        </script>
    </body>
</html>
"use strict";

const { JSDOM } = require("jsdom");

const dom = new JSDOM(`
    <svg id="svg">
        <g id="group"></g>
    </svg>
`);

const svg = dom.window.document.getElementById("svg");
const group = dom.window.document.getElementById("group");

console.log(svg.constructor);   // [Function: SVGSVGElement]
console.log(group.constructor); // [Function: SVGElement]

GitHub-Reproduktions-Repository .

Tatsächlich scheint dies nur daran zu liegen, dass wir das SVG-Element g nicht unterstützen. Unsere SVG-Unterstützung ist im Moment nicht allzu groß.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

lehni picture lehni  ·  4Kommentare

mitar picture mitar  ·  4Kommentare

vsemozhetbyt picture vsemozhetbyt  ·  4Kommentare

domenic picture domenic  ·  3Kommentare

machineghost picture machineghost  ·  4Kommentare