Jsdom: SVGGElement 未定义

创建于 2019-12-03  ·  3评论  ·  资料来源: jsdom/jsdom

我猜 jsdom 不支持 SVGGElement( MDN 文档)?

基础信息:

  • Node.js 版本: 10.150。
  • jsdom 版本: 11.5.1

最小复制案例

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");
}

类似代码在浏览器中的行为如何?

https://jsfiddle.net/p3ag4v85/

(检查控制台并查找ok

笔记

我不得不说,当我使用 Jest 测试我的代码时,我注意到了这个错误,即在内部使用 jsdom。 这是我得到的错误:

● 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 g元素。 我们的 SVG 支持目前还不是很好。

所有3条评论

支持SVGElement 。 但是,您的代码正在 Node.js 全局中查找SVGElement 。 您需要使用dom.window.SVGElement

是的。 这确实是一个错误。 该规范似乎没有为 SVG 组正确实施。

<!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 复制存储库

事实上,看起来这只是因为我们不支持 SVG g元素。 我们的 SVG 支持目前还不是很好。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

machineghost picture machineghost  ·  4评论

tolmasky picture tolmasky  ·  4评论

eszthoff picture eszthoff  ·  3评论

domenic picture domenic  ·  3评论

drewish picture drewish  ·  4评论