Closed brandonmcconnell closed 10 months ago
Duplicate of #2142.
Seemed to me kind of like a dupe of https://github.com/whatwg/fetch/issues/16
@annevk This proposal does not relate to streaming HTML content into elements.
At least to me #2142 covers the idea of a streaming parser API generally.
And yeah, I guess Domenic is correct that exposing a method directly on Response
for this is a non-starter.
Spec proposal
collapsed by default to avoid cluttering the required fields below
What problem are you trying to solve?
Currently, developers handling XML and HTML content in ECMAScript face a lack of native, streamlined methods for parsing these markup languages. This leads to reliance on custom or third-party parsing solutions, which can vary in efficiency, security, and ease of use.
What solutions exist today?
Presently, developers typically use custom-built parsers or third-party libraries to parse XML and HTML content. For example, libraries like
xml2js
ornode-html-parser
provide these capabilities, but they require additional dependencies and may not be optimized for all use cases. These solutions often lead to inconsistent implementations and may pose security risks, especially when parsing HTML content.One workaround involves using the
.text()
method and then parsing its content using a newDOMParser
.For example:
This method is a bit cumbersome and does not provide any any of the security benefits of the Sanitizer API.
How would you solve it?
The solution is to introduce a new method,
.markup()
, into the ECMAScript standard. This method will unify and simplify the parsing of XML and HTML content. By offering an optional configuration argument, it allows developers to specify the content type (XML or HTML) and other parsing preferences. For instance,response.markup({ type: "text/html" })
would parse HTML content while appropriately handling self-closing tags (the default behavior). This approach ensures consistency, optimizes performance, and reduces the security risks associated with third-party parsers.Anything else?
In addition to providing a unified method for parsing markup languages, the
.markup()
method will include robust error handling and security features, especially vital for HTML parsing to prevent cross-site scripting (XSS) attacks. It should natively support theSanitizer
API, similar to how thesetHTML()
method will.Its design will be in line with the existing
.text()
and.json()
methods, ensuring familiarity and ease of adoption for developers. The proposal also considers the need for backward compatibility and browser support, suggesting the development of polyfills for older environments.