Proposal: Implement new method for parsing markup from Fetch
responses
#10076
Labels
addition/proposal
New features or enhancements
needs implementer interest
Moving the issue forward requires implementers to express interest
Spec proposal
collapsed by default to avoid cluttering the required fields below
Introduction
This proposal introduces a new method,
.markup()
, to be included in the Fetch API response parsing interface. This method offers a streamlined interface for parsing various markup data formats such as HTML and XML.Rationale
Consistency and Flexibility
Just as
.text()
and.json()
methods offer simplified handling of text and JSON data,.markup()
extends this ease of use to markup languages. By providing a single method with optional configurations, developers can handle XML and HTML data more flexibly within the same framework.Performance and Optimization
Integrating this method into the language standard allows for engine-level optimizations, potentially outperforming custom parsing solutions and improving overall performance.
Enhanced Readability and Maintenance
A unified method simplifies codebases, enhancing readability and ease of maintenance. This aligns with modern JavaScript's goal of concise and powerful syntax.
Technical Specification
.markup()
Methodresponse.markup(options)
.options
: An optional argument specifying parsing preferencestype
:"text/html" | "text/xml"
(enforces self-closing tags, etc. for HTML)Implementation Notes
.text()
and.json()
..json()
.Use Cases
Potential Challenges
Conclusion
Introducing the
.markup()
method in ECMAScript offers a versatile and optimized approach to handling XML and HTML data. This proposal seeks the TC39's consideration for this addition, which is in line with JavaScript's evolution towards a more powerful and developer-friendly language.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.The text was updated successfully, but these errors were encountered: