xmldom 是一個 javascript ponyfill,用於向其他運行時提供現代瀏覽器中存在的以下 API:
雷雷
- 將 XML 字串轉換為 DOM 樹
雷雷
- 建立、存取和修改 DOM 樹
雷雷
- 將 DOM 樹序列化回 XML 字串
來源: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。
但是我今天要講的是自 2022 年 10 月以來在 next 標籤下發布的所有內容。
我對這些變化感到非常興奮,因為它們為未來潛在的變化提供了明確的基礎。
TLDR:與規範更一致,差異盡可能明確。
使實作變得複雜的一個面向是解析 XML 與 HTML 的規則不同。
xmldom(某種程度上)從一開始就「支援」這兩種風格。甚至根本不需要傳遞 mimeType:應用什麼規則是根據當前正在解析的 XML 字串/節點的當前預設命名空間決定的。
這以 0.9.0 結束:從現在開始,DOMParser.parseFromString(xml, mimeType) 中的 mimeType 是強制性的,並且是唯一被檢查以決定是否應用 XML 或 HTML 規則的東西。巴斯塔。
這些資訊會保留在產生的文件(新類型屬性)中,因此在序列化它時,會再次套用正確的規則。
這是一個巨大的(並且可能是破壞性的)變化,但我真的很高興它已經準備好了,因為它使大量相關的錯誤修復變得可能/更容易實現,並且還降低了API 和實作的複雜性。
此外,它現在只接受指定的 mime 類型,並在任何其他情況下拋出 TypeError。
我個人對原生瀏覽器 API 的錯誤處理感到困惑的是,它總是回傳一個 Document,如果出現問題,parsererror 節點將是主體的第一個子節點:
由於錯誤處理在xmldom 中從來沒有以這種方式工作,但現有的錯誤處理非常複雜、令人困惑,而且文檔記錄很差,0.9.0 對其進行了簡化,現在對解析過程中發生的任何潛在錯誤具有(更)一致的行為:
它拋出一個 ParseError ? ,例如有下列情況之一的:
仍然有一些情況會被報告為警告(尤其是在解析 HTML 時)或錯誤,但不會阻止資料的處理,但是新的錯誤處理可以很容易地決定程式碼的嚴格程度需要使用 xmldom。
可以傳遞給 DOMParser 建構子的(不符合規範的)選項稱為 onError。
它需要一個具有以下簽名的函數:
建議應用其中一個來在出現任何意外的第一個訊號時停止處理 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(...)
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.
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.
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.
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中文網其他相關文章!