Skip to content

Commit f855b1f

Browse files
committed
LibWeb: Implement XMLSerializer
The main thing that is missing is validating certain pieces of data against XML productions in well-formed mode, but nothing uses well-formed mode right now. Required by Closure Library for sanitising HTML. https://github.com/google/closure-library/blob/e687b3d8ab014787b9f10b08b3f597b637392480/closure/goog/html/sanitizer/safedomtreeprocessor.js#L117
1 parent 60fc0ce commit f855b1f

File tree

10 files changed

+938
-1
lines changed

10 files changed

+938
-1
lines changed

Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,7 @@ void generate_implementation(IDL::Interface const& interface)
19011901
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
19021902
using namespace Web::CSS;
19031903
using namespace Web::DOM;
1904+
using namespace Web::DOMParsing;
19041905
using namespace Web::Geometry;
19051906
using namespace Web::HTML;
19061907
using namespace Web::IntersectionObserver;
@@ -2816,6 +2817,7 @@ void generate_constructor_implementation(IDL::Interface const& interface)
28162817
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
28172818
using namespace Web::CSS;
28182819
using namespace Web::DOM;
2820+
using namespace Web::DOMParsing;
28192821
using namespace Web::Geometry;
28202822
using namespace Web::HTML;
28212823
using namespace Web::IntersectionObserver;
@@ -3093,6 +3095,7 @@ void generate_prototype_implementation(IDL::Interface const& interface)
30933095
using namespace Web::Crypto;
30943096
using namespace Web::CSS;
30953097
using namespace Web::DOM;
3098+
using namespace Web::DOMParsing;
30963099
using namespace Web::Geometry;
30973100
using namespace Web::HTML;
30983101
using namespace Web::IntersectionObserver;
@@ -3542,6 +3545,7 @@ void generate_iterator_implementation(IDL::Interface const& interface)
35423545
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
35433546
using namespace Web::CSS;
35443547
using namespace Web::DOM;
3548+
using namespace Web::DOMParsing;
35453549
using namespace Web::Geometry;
35463550
using namespace Web::HTML;
35473551
using namespace Web::IntersectionObserver;
@@ -3655,6 +3659,7 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface)
36553659
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
36563660
using namespace Web::CSS;
36573661
using namespace Web::DOM;
3662+
using namespace Web::DOMParsing;
36583663
using namespace Web::Geometry;
36593664
using namespace Web::HTML;
36603665
using namespace Web::IntersectionObserver;

Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(int argc, char** argv)
8585

8686
auto& interface = IDL::Parser(path, data, import_base_path).parse();
8787

88-
if (namespace_.is_one_of("Crypto", "CSS", "DOM", "Encoding", "HTML", "UIEvents", "Geometry", "HighResolutionTime", "IntersectionObserver", "NavigationTiming", "RequestIdleCallback", "ResizeObserver", "SVG", "Selection", "URL", "WebGL", "WebSockets", "XHR")) {
88+
if (namespace_.is_one_of("Crypto", "CSS", "DOM", "DOMParsing", "Encoding", "HTML", "UIEvents", "Geometry", "HighResolutionTime", "IntersectionObserver", "NavigationTiming", "RequestIdleCallback", "ResizeObserver", "SVG", "Selection", "URL", "WebGL", "WebSockets", "XHR")) {
8989
StringBuilder builder;
9090
builder.append(namespace_);
9191
builder.append("::");

Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h

+3
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@
355355
#include <LibWeb/Bindings/XMLHttpRequestEventTargetConstructor.h>
356356
#include <LibWeb/Bindings/XMLHttpRequestEventTargetPrototype.h>
357357
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
358+
#include <LibWeb/Bindings/XMLSerializerConstructor.h>
359+
#include <LibWeb/Bindings/XMLSerializerPrototype.h>
358360

359361
#define ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(interface_name, constructor_name, prototype_name) \
360362
{ \
@@ -539,6 +541,7 @@
539541
ADD_WINDOW_OBJECT_INTERFACE(Worker) \
540542
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequest) \
541543
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequestEventTarget) \
544+
ADD_WINDOW_OBJECT_INTERFACE(XMLSerializer) \
542545
ADD_WINDOW_OBJECT_INTERFACE(Window) \
543546
ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(Audio, AudioConstructor, HTMLAudioElementPrototype) \
544547
ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(Image, ImageConstructor, HTMLImageElementPrototype) \

Userland/Libraries/LibWeb/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ set(SOURCES
110110
DOM/Text.idl
111111
DOM/TreeWalker.cpp
112112
DOMParsing/InnerHTML.cpp
113+
DOMParsing/XMLSerializer.cpp
113114
Dump.cpp
114115
Encoding/TextDecoder.cpp
115116
Encoding/TextEncoder.cpp

Userland/Libraries/LibWeb/DOMParsing/XMLSerializer.cpp

+873
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2022, Luke Wilde <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <AK/RefCounted.h>
10+
#include <LibWeb/Bindings/Wrappable.h>
11+
#include <LibWeb/Forward.h>
12+
13+
namespace Web::DOMParsing {
14+
15+
class XMLSerializer final
16+
: public RefCounted<XMLSerializer>
17+
, public Bindings::Wrappable {
18+
public:
19+
using WrapperType = Bindings::XMLSerializerWrapper;
20+
21+
static NonnullRefPtr<XMLSerializer> create_with_global_object(Bindings::WindowObject&)
22+
{
23+
return adopt_ref(*new XMLSerializer());
24+
}
25+
26+
virtual ~XMLSerializer() override;
27+
28+
DOM::ExceptionOr<String> serialize_to_string(NonnullRefPtr<DOM::Node> root);
29+
30+
private:
31+
XMLSerializer();
32+
};
33+
34+
enum class RequireWellFormed {
35+
No,
36+
Yes,
37+
};
38+
39+
DOM::ExceptionOr<String> serialize_node_to_xml_string(NonnullRefPtr<DOM::Node> root, RequireWellFormed require_well_formed);
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import <DOM/Node.idl>
2+
3+
[Exposed=Window]
4+
interface XMLSerializer {
5+
constructor();
6+
DOMString serializeToString(Node root);
7+
};

Userland/Libraries/LibWeb/Forward.h

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ template<typename ValueType>
159159
class ExceptionOr;
160160
}
161161

162+
namespace Web::DOMParsing {
163+
class XMLSerializer;
164+
}
165+
162166
namespace Web::Encoding {
163167
class TextEncoder;
164168
}
@@ -600,6 +604,7 @@ class XMLHttpRequestConstructor;
600604
class XMLHttpRequestEventTargetWrapper;
601605
class XMLHttpRequestPrototype;
602606
class XMLHttpRequestWrapper;
607+
class XMLSerializerWrapper;
603608
enum class CanPlayTypeResult;
604609
enum class DOMParserSupportedType;
605610
enum class ResizeObserverBoxOptions;

Userland/Libraries/LibWeb/HTML/TagNames.h

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ namespace Web::HTML::TagNames {
9090
__ENUMERATE_HTML_TAG(marquee) \
9191
__ENUMERATE_HTML_TAG(math) \
9292
__ENUMERATE_HTML_TAG(menu) \
93+
__ENUMERATE_HTML_TAG(menuitem) \
9394
__ENUMERATE_HTML_TAG(meta) \
9495
__ENUMERATE_HTML_TAG(meter) \
9596
__ENUMERATE_HTML_TAG(nav) \

Userland/Libraries/LibWeb/idl_files.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ libweb_js_wrapper(DOM/ShadowRoot)
4848
libweb_js_wrapper(DOM/StaticRange)
4949
libweb_js_wrapper(DOM/Text)
5050
libweb_js_wrapper(DOM/TreeWalker)
51+
libweb_js_wrapper(DOMParsing/XMLSerializer)
5152
libweb_js_wrapper(Encoding/TextDecoder)
5253
libweb_js_wrapper(Encoding/TextEncoder)
5354
libweb_js_wrapper(Geometry/DOMRect)

0 commit comments

Comments
 (0)