搜尋
首頁web前端js教程發佈 .f `@xmldom/xmldom`

情境

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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在JavaScript中替換字符串字符在JavaScript中替換字符串字符Mar 11, 2025 am 12:07 AM

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

jQuery檢查日期是否有效jQuery檢查日期是否有效Mar 01, 2025 am 08:51 AM

簡單JavaScript函數用於檢查日期是否有效。 function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //測試 var

jQuery獲取元素填充/保證金jQuery獲取元素填充/保證金Mar 01, 2025 am 08:53 AM

本文探討如何使用 jQuery 獲取和設置 DOM 元素的內邊距和外邊距值,特別是元素外邊距和內邊距的具體位置。雖然可以使用 CSS 設置元素的內邊距和外邊距,但獲取準確的值可能會比較棘手。 // 設定 $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); 你可能會認為這段代碼很

10個jQuery手風琴選項卡10個jQuery手風琴選項卡Mar 01, 2025 am 01:34 AM

本文探討了十個特殊的jQuery選項卡和手風琴。 選項卡和手風琴之間的關鍵區別在於其內容面板的顯示和隱藏方式。讓我們深入研究這十個示例。 相關文章:10個jQuery選項卡插件

10值得檢查jQuery插件10值得檢查jQuery插件Mar 01, 2025 am 01:29 AM

發現十個傑出的jQuery插件,以提升您的網站的活力和視覺吸引力!這個精選的收藏品提供了不同的功能,從圖像動畫到交互式畫廊。讓我們探索這些強大的工具:相關文章:1

HTTP與節點和HTTP-Console調試HTTP與節點和HTTP-Console調試Mar 01, 2025 am 01:37 AM

HTTP-Console是一個節點模塊,可為您提供用於執行HTTP命令的命令行接口。不管您是否針對Web服務器,Web Serv

自定義Google搜索API設置教程自定義Google搜索API設置教程Mar 04, 2025 am 01:06 AM

本教程向您展示瞭如何將自定義的Google搜索API集成到您的博客或網站中,提供了比標準WordPress主題搜索功能更精緻的搜索體驗。 令人驚訝的是簡單!您將能夠將搜索限制為Y

jQuery添加捲軸到DivjQuery添加捲軸到DivMar 01, 2025 am 01:30 AM

當div內容超出容器元素區域時,以下jQuery代碼片段可用於添加滾動條。 (無演示,請直接複製到Firebug中) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器