首页 > web前端 > js教程 > 正文

发布 .f `@xmldom/xmldom`

WBOY
发布: 2024-08-30 18:39:35
原创
580 人浏览过

语境

xmldom 是一个 javascript ponyfill,用于向其他运行时提供现代浏览器中存在的以下 API:

  • 将 XML 字符串转换为 DOM 树
  new DOMParser().parseFromString(xml, mimeType) =>  Document
登录后复制
  • 创建、访问和修改 DOM 树
  new DOMImplementation().createDocument(...) => Document
登录后复制
  • 将 DOM 树序列化回 XML 字符串
  new XMLSerializer().serializeToString(node) => string
登录后复制

来源:xmldom 自述文件

历史

自从我在 2020 年 6 月开始为分叉的 xmldom 库做出贡献以来,已经发布了 40 个版本。

这是一个非常有趣且具有挑战性的项目,并且很可能会在相当长的一段时间内保持这种状态。

据 GitHub 称,自分叉以来已有 50 多人为其做出了贡献。

再次感谢所有贡献者。

这并不包括所有设法从原始无作用域 xmldom 包迁移到有作用域 @xmldom/xmldom 包版本 0.7.0 以获得所有安全修复程序的人。
作为 lts 标签发布的最新版本是 0.7.13。

最后一个具有重大更改的版本是 0.8.0,发布于 2021 年 12 月 22 日,大约 3 年前。
最新发布的版本是0.8.10。

0.9.0 (2024-08-29)

但是我今天要讲的是自 2022 年 10 月以来在 next 标签下发布的所有内容。

我对这些变化感到非常兴奋,因为它们为未来潜在的变化提供了明确的基础。

TLDR:与规范更加一致,差异尽可能明确。

1. 强制mimeType交还控制权

使实现变得复杂的一个方面是解析 XML 与 HTML 的规则不同。
xmldom(在某种程度上)从一开始就“支持”这两种风格。甚至根本不需要传递 mimeType:应用什么规则是根据当前正在解析的 XML 字符串/节点的当前默认命名空间决定的。

这以 0.9.0 结束:从现在开始,DOMParser.parseFromString(xml, mimeType) 中的 mimeType 是强制性的,并且是唯一被检查以决定是否应用 XML 或 HTML 规则的东西。巴斯塔。

该信息会保留在生成的文档(新类型属性)中,因此在序列化它时,会再次应用正确的规则。

这是一个巨大的(并且可能是破坏性的)变化,但我真的很高兴它已经准备好了,因为它使大量相关的错误修复变得可能/更容易实现,并且还降低了 API 和实现的复杂性。

此外,它现在只接受指定的 mime 类型,并在任何其他情况下抛出 TypeError。

严格性和错误处理

我个人对原生浏览器 API 的错误处理感到困惑的是,它总是返回一个 Document,如果出现问题,parsererror 节点将是主体的第一个子节点:

Release .f `@xmldom/xmldom`

由于错误处理在 xmldom 中从来没有以这种方式工作,但现有的错误处理非常复杂、混乱且文档记录很差,0.9.0 对其进行了简化,现在对解析过程中发生的任何潜在错误具有(更加)一致的行为:
它抛出一个 ParseError ?,例如有下列情况之一的:

  • 在以前的版本中,对于某些格式不正确的 XML 字符串,返回的 Document 可能没有 documentElement,这很可能会导致后面的代码出现 TypeErrors。
  • 几个格式不正确的 XML 字符串现在将正确报告为 fatalError,现在总是阻止任何进一步的处理。
  • 一些以前未报告为错误或仅报告为警告的事情现在也报告为 fatalError

仍然有一些情况会被报告为警告(尤其是在解析 HTML 时)或错误,但不会阻止数据的处理,但是新的错误处理可以很容易地决定代码的严格程度需要使用 xmldom。

可以传递给 DOMParser 构造函数的(不符合规范的)选项称为 onError。
它需要一个具有以下签名的函数:

function onError(level:ErrorLevel, message:string, context: DOMHandler):void;
登录后复制
  • ErrorLevel 是警告、错误或 fatalError
  • xmldom 已经为两个最常见的用例提供了实现:
    • onErrorStopParsing 也会针对所有错误级别问题抛出 ParseError
    • onWarningStopParsing 也会针对所有错误级别问题抛出 ParseError

建议应用其中一个来在出现任何意外的第一个信号时停止处理 XML:

// prevent parsing of XML that has `error`s
new DOMParser({onError: onErrorStopParsing}).parseFromString(...)
// prevent parsing of XML that has `warning`s
new DOMParser({onError: onWarningStopParsing}).parseFromString(...)
登录后复制

compareDocumentPosition, extended HTML entities , null instead of undefined, ...

Another fork of the original xmldom repository made it's way back into our repo by extending the HTML entities to the complete set (also available in 0.8.x) and porting over the implementation of the compareDocumentPosition API. Thank you, and welcome @zorkow

Along the way several places where xmldom so far returned undefined instead of null, have been fixed to adhere to the spec.

And I discovered that the former author seems to have preferred iterating from the end of a list in so many places, that attributes were processed in the reverse order in multiple places, which is now fixed.

The implementation of the removeChild API changed quite a bit, to comply to the spec and throws a DOMException when it should.

And 3 related bugs were fixed in a way that clearly states what the future direction of xmldom is:
Support for lax HTML parsing rules will only be provided if proper strict XML parsing doesn't suffer from it.
The former (broken) "support" for automatic self closing tags in HTML is gone.

coctype internalSubset

More recently @shunkica invested a huge amount of time end effort to fix tons of issues in the former handling of the internalSubset part of the !DOCTYPE.

It is now preserved as part of the internalSubset property of the doctype of a Document and many wrong doctype declarations are now correctly detected as such and reported as a fatalError.

Also thanks to @kboshold for the latest bug fix in this area.

Along the way we created a new module containing regular expressions for the relevant grammar, and correctness checks are based on those and they are properly covered by tests.

It is not the goal of xmldom to become a validating parser, but this a great step to support those documents that come with more complex DTDs.

And there is even more

Up to now development was done using Node v10, since this is also the lowest version xmldom currently supports. As part of the work on the upcoming version, I decided to switch to v18 for development, since more and more devDependencies also made this a minimum requirement. This will be the new minimum runtime version for the time being starting with this release.

I initiated a public poll / dicussion to ask people which version of Node or other runtimes they need support for.
The next breaking release will most likely drop support for some older Node versions, if there is no feedback indicating something different.

Along the way plenty of APIs have received jsdoc comments with proper types.

Thank you

for taking the time to read through all of this.

Those are quite some changes, and I'm very excited to be able to ship those.

I hope you are as excited as I am :)

If you need more details you can go through the very detailed changelog, or head over to the repository and join or start a discussion or file an issue.

以上是发布 .f `@xmldom/xmldom`的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!