Skip to content

Commit

Permalink
LibWeb: Implement XMLSerializer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Lubrsi committed Jul 5, 2022
1 parent 60fc0ce commit f855b1f
Show file tree
Hide file tree
Showing 10 changed files with 938 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,7 @@ void generate_implementation(IDL::Interface const& interface)
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Geometry;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
Expand Down Expand Up @@ -2816,6 +2817,7 @@ void generate_constructor_implementation(IDL::Interface const& interface)
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Geometry;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
Expand Down Expand Up @@ -3093,6 +3095,7 @@ void generate_prototype_implementation(IDL::Interface const& interface)
using namespace Web::Crypto;
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Geometry;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
Expand Down Expand Up @@ -3542,6 +3545,7 @@ void generate_iterator_implementation(IDL::Interface const& interface)
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Geometry;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
Expand Down Expand Up @@ -3655,6 +3659,7 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface)
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Geometry;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int main(int argc, char** argv)

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

if (namespace_.is_one_of("Crypto", "CSS", "DOM", "Encoding", "HTML", "UIEvents", "Geometry", "HighResolutionTime", "IntersectionObserver", "NavigationTiming", "RequestIdleCallback", "ResizeObserver", "SVG", "Selection", "URL", "WebGL", "WebSockets", "XHR")) {
if (namespace_.is_one_of("Crypto", "CSS", "DOM", "DOMParsing", "Encoding", "HTML", "UIEvents", "Geometry", "HighResolutionTime", "IntersectionObserver", "NavigationTiming", "RequestIdleCallback", "ResizeObserver", "SVG", "Selection", "URL", "WebGL", "WebSockets", "XHR")) {
StringBuilder builder;
builder.append(namespace_);
builder.append("::");
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@
#include <LibWeb/Bindings/XMLHttpRequestEventTargetConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestEventTargetPrototype.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
#include <LibWeb/Bindings/XMLSerializerConstructor.h>
#include <LibWeb/Bindings/XMLSerializerPrototype.h>

#define ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(interface_name, constructor_name, prototype_name) \
{ \
Expand Down Expand Up @@ -539,6 +541,7 @@
ADD_WINDOW_OBJECT_INTERFACE(Worker) \
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequest) \
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequestEventTarget) \
ADD_WINDOW_OBJECT_INTERFACE(XMLSerializer) \
ADD_WINDOW_OBJECT_INTERFACE(Window) \
ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(Audio, AudioConstructor, HTMLAudioElementPrototype) \
ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(Image, ImageConstructor, HTMLImageElementPrototype) \
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ set(SOURCES
DOM/Text.idl
DOM/TreeWalker.cpp
DOMParsing/InnerHTML.cpp
DOMParsing/XMLSerializer.cpp
Dump.cpp
Encoding/TextDecoder.cpp
Encoding/TextEncoder.cpp
Expand Down
873 changes: 873 additions & 0 deletions Userland/Libraries/LibWeb/DOMParsing/XMLSerializer.cpp

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions Userland/Libraries/LibWeb/DOMParsing/XMLSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2022, Luke Wilde <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/RefCounted.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Forward.h>

namespace Web::DOMParsing {

class XMLSerializer final
: public RefCounted<XMLSerializer>
, public Bindings::Wrappable {
public:
using WrapperType = Bindings::XMLSerializerWrapper;

static NonnullRefPtr<XMLSerializer> create_with_global_object(Bindings::WindowObject&)
{
return adopt_ref(*new XMLSerializer());
}

virtual ~XMLSerializer() override;

DOM::ExceptionOr<String> serialize_to_string(NonnullRefPtr<DOM::Node> root);

private:
XMLSerializer();
};

enum class RequireWellFormed {
No,
Yes,
};

DOM::ExceptionOr<String> serialize_node_to_xml_string(NonnullRefPtr<DOM::Node> root, RequireWellFormed require_well_formed);

}
7 changes: 7 additions & 0 deletions Userland/Libraries/LibWeb/DOMParsing/XMLSerializer.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <DOM/Node.idl>

[Exposed=Window]
interface XMLSerializer {
constructor();
DOMString serializeToString(Node root);
};
5 changes: 5 additions & 0 deletions Userland/Libraries/LibWeb/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ template<typename ValueType>
class ExceptionOr;
}

namespace Web::DOMParsing {
class XMLSerializer;
}

namespace Web::Encoding {
class TextEncoder;
}
Expand Down Expand Up @@ -600,6 +604,7 @@ class XMLHttpRequestConstructor;
class XMLHttpRequestEventTargetWrapper;
class XMLHttpRequestPrototype;
class XMLHttpRequestWrapper;
class XMLSerializerWrapper;
enum class CanPlayTypeResult;
enum class DOMParserSupportedType;
enum class ResizeObserverBoxOptions;
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/TagNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace Web::HTML::TagNames {
__ENUMERATE_HTML_TAG(marquee) \
__ENUMERATE_HTML_TAG(math) \
__ENUMERATE_HTML_TAG(menu) \
__ENUMERATE_HTML_TAG(menuitem) \
__ENUMERATE_HTML_TAG(meta) \
__ENUMERATE_HTML_TAG(meter) \
__ENUMERATE_HTML_TAG(nav) \
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/idl_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ libweb_js_wrapper(DOM/ShadowRoot)
libweb_js_wrapper(DOM/StaticRange)
libweb_js_wrapper(DOM/Text)
libweb_js_wrapper(DOM/TreeWalker)
libweb_js_wrapper(DOMParsing/XMLSerializer)
libweb_js_wrapper(Encoding/TextDecoder)
libweb_js_wrapper(Encoding/TextEncoder)
libweb_js_wrapper(Geometry/DOMRect)
Expand Down

0 comments on commit f855b1f

Please sign in to comment.