chromium/third_party/blink/renderer/core/xml/parser/xml_document_parser.cc

/*
 * Copyright (C) 2000 Peter Kelly ([email protected])
 * Copyright (C) 2005, 2006, 2008, 2014 Apple Inc. All rights reserved.
 * Copyright (C) 2006 Alexey Proskuryakov ([email protected])
 * Copyright (C) 2007 Samuel Weinig ([email protected])
 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
 * Copyright (C) 2008 Holger Hans Peter Freyther
 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved.
 * (http://www.torchmobile.com/)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/core/xml/parser/xml_document_parser.h"

#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlversion.h>
#include <libxslt/xslt.h>

#include <algorithm>
#include <memory>

#include "base/auto_reset.h"
#include "base/numerics/safe_conversions.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/cdata_section.h"
#include "third_party/blink/renderer/core/dom/comment.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/document_fragment.h"
#include "third_party/blink/renderer/core/dom/document_parser_timing.h"
#include "third_party/blink/renderer/core/dom/document_type.h"
#include "third_party/blink/renderer/core/dom/processing_instruction.h"
#include "third_party/blink/renderer/core/dom/throw_on_dynamic_markup_insertion_count_incrementer.h"
#include "third_party/blink/renderer/core/dom/transform_source.h"
#include "third_party/blink/renderer/core/dom/xml_document.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/html/custom/ce_reactions_scope.h"
#include "third_party/blink/renderer/core/html/html_html_element.h"
#include "third_party/blink/renderer/core/html/html_template_element.h"
#include "third_party/blink/renderer/core/html/parser/html_construction_site.h"
#include "third_party/blink/renderer/core/html/parser/html_entity_parser.h"
#include "third_party/blink/renderer/core/html/parser/text_resource_decoder.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/loader/frame_loader.h"
#include "third_party/blink/renderer/core/loader/image_loader.h"
#include "third_party/blink/renderer/core/svg/graphics/svg_image.h"
#include "third_party/blink/renderer/core/xml/document_xml_tree_viewer.h"
#include "third_party/blink/renderer/core/xml/document_xslt.h"
#include "third_party/blink/renderer/core/xml/parser/shared_buffer_reader.h"
#include "third_party/blink/renderer/core/xml/parser/xml_document_parser_scope.h"
#include "third_party/blink/renderer/core/xml/parser/xml_parser_input.h"
#include "third_party/blink/renderer/core/xmlns_names.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_type_names.h"
#include "third_party/blink/renderer/platform/loader/fetch/raw_resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_error.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/text/utf8.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"

namespace blink {

// FIXME: HTMLConstructionSite has a limit of 512, should these match?
static const unsigned kMaxXMLTreeDepth =;

static inline String ToString(const xmlChar* string, size_t length) {}

static inline String ToString(const xmlChar* string) {}

static inline AtomicString ToAtomicString(const xmlChar* string,
                                          size_t length) {}

static inline AtomicString ToAtomicString(const xmlChar* string) {}

static inline bool HasNoStyleInformation(Document* document) {}

class PendingStartElementNSCallback final
    : public XMLDocumentParser::PendingCallback {};

class PendingEndElementNSCallback final
    : public XMLDocumentParser::PendingCallback {};

class PendingCharactersCallback final
    : public XMLDocumentParser::PendingCallback {};

class PendingProcessingInstructionCallback final
    : public XMLDocumentParser::PendingCallback {};

class PendingCDATABlockCallback final
    : public XMLDocumentParser::PendingCallback {};

class PendingCommentCallback final : public XMLDocumentParser::PendingCallback {};

class PendingInternalSubsetCallback final
    : public XMLDocumentParser::PendingCallback {};

class PendingErrorCallback final : public XMLDocumentParser::PendingCallback {};

void XMLDocumentParser::PushCurrentNode(ContainerNode* n) {}

void XMLDocumentParser::PopCurrentNode() {}

void XMLDocumentParser::ClearCurrentNodeStack() {}

void XMLDocumentParser::Append(const String& input_source) {}

void XMLDocumentParser::HandleError(XMLErrors::ErrorType type,
                                    const char* formatted_message,
                                    TextPosition position) {}

void XMLDocumentParser::CreateLeafTextNodeIfNeeded() {}

bool XMLDocumentParser::UpdateLeafTextNode() {}

void XMLDocumentParser::Detach() {}

void XMLDocumentParser::end() {}

void XMLDocumentParser::Finish() {}

void XMLDocumentParser::InsertErrorMessageBlock() {}

bool XMLDocumentParser::IsWaitingForScripts() const {}

void XMLDocumentParser::PauseParsing() {}

bool XMLDocumentParser::ParseDocumentFragment(
    const String& chunk,
    DocumentFragment* fragment,
    Element* context_element,
    ParserContentPolicy parser_content_policy,
    ExceptionState* exception_state) {}

static int g_global_descriptor =;

static int MatchFunc(const char*) {}

static inline void SetAttributes(
    Element* element,
    Vector<Attribute, kAttributePrealloc>& attribute_vector,
    ParserContentPolicy parser_content_policy) {}

static void SwitchEncoding(xmlParserCtxtPtr ctxt, bool is_8bit) {}

static void ParseChunk(xmlParserCtxtPtr ctxt, const String& chunk) {}

static void FinishParsing(xmlParserCtxtPtr ctxt) {}

#define xmlParseChunk

static bool IsLibxmlDefaultCatalogFile(const String& url_string) {}

static bool ShouldAllowExternalLoad(const KURL& url) {}

static void* OpenFunc(const char* uri) {}

static int ReadFunc(void* context, char* buffer, int len) {}

static int WriteFunc(void*, const char*, int) {}

static int CloseFunc(void* context) {}

static void ErrorFunc(void*, const char*, ...) {}

static void InitializeLibXMLIfNecessary() {}

scoped_refptr<XMLParserContext> XMLParserContext::CreateStringParser(
    xmlSAXHandlerPtr handlers,
    void* user_data) {}

// Chunk should be encoded in UTF-8
scoped_refptr<XMLParserContext> XMLParserContext::CreateMemoryParser(
    xmlSAXHandlerPtr handlers,
    void* user_data,
    const std::string& chunk) {}

// --------------------------------

bool XMLDocumentParser::SupportsXMLVersion(const String& version) {}

XMLDocumentParser::XMLDocumentParser(Document& document,
                                     LocalFrameView* frame_view)
    :{}

XMLDocumentParser::XMLDocumentParser(DocumentFragment* fragment,
                                     Element* parent_element,
                                     ParserContentPolicy parser_content_policy)
    :{}

XMLParserContext::~XMLParserContext() {}

XMLDocumentParser::~XMLDocumentParser() = default;

void XMLDocumentParser::Trace(Visitor* visitor) const {}

void XMLDocumentParser::DoWrite(const String& parse_string) {}

struct xmlSAX2Namespace {};

static inline void HandleNamespaceAttributes(
    Vector<Attribute, kAttributePrealloc>& prefixed_attributes,
    const xmlChar** libxml_namespaces,
    int nb_namespaces,
    ExceptionState& exception_state) {}

struct xmlSAX2Attributes {};

static inline void HandleElementAttributes(
    Vector<Attribute, kAttributePrealloc>& prefixed_attributes,
    const xmlChar** libxml_attributes,
    int nb_attributes,
    const HashMap<AtomicString, AtomicString>& initial_prefix_to_namespace_map,
    ExceptionState& exception_state) {}

void XMLDocumentParser::StartElementNs(const AtomicString& local_name,
                                       const AtomicString& prefix,
                                       const AtomicString& uri,
                                       int nb_namespaces,
                                       const xmlChar** libxml_namespaces,
                                       int nb_attributes,
                                       int nb_defaulted,
                                       const xmlChar** libxml_attributes) {}

void XMLDocumentParser::EndElementNs() {}

void XMLDocumentParser::NotifyScriptExecuted() {}

void XMLDocumentParser::SetScriptStartPosition(TextPosition text_position) {}

void XMLDocumentParser::Characters(const xmlChar* chars, int length) {}

void XMLDocumentParser::GetError(XMLErrors::ErrorType type,
                                 const char* message,
                                 va_list args) {}

void XMLDocumentParser::GetProcessingInstruction(const String& target,
                                                 const String& data) {}

void XMLDocumentParser::CdataBlock(const String& text) {}

void XMLDocumentParser::Comment(const String& text) {}

enum StandaloneInfo {};

void XMLDocumentParser::StartDocument(const String& version,
                                      const String& encoding,
                                      int standalone) {}

void XMLDocumentParser::EndDocument() {}

void XMLDocumentParser::InternalSubset(const String& name,
                                       const String& external_id,
                                       const String& system_id) {}

static inline XMLDocumentParser* GetParser(void* closure) {}

static void StartElementNsHandler(void* closure,
                                  const xmlChar* local_name,
                                  const xmlChar* prefix,
                                  const xmlChar* uri,
                                  int nb_namespaces,
                                  const xmlChar** namespaces,
                                  int nb_attributes,
                                  int nb_defaulted,
                                  const xmlChar** libxml_attributes) {}

static void EndElementNsHandler(void* closure,
                                const xmlChar*,
                                const xmlChar*,
                                const xmlChar*) {}

static void CharactersHandler(void* closure, const xmlChar* chars, int length) {}

static void ProcessingInstructionHandler(void* closure,
                                         const xmlChar* target,
                                         const xmlChar* data) {}

static void CdataBlockHandler(void* closure, const xmlChar* text, int length) {}

static void CommentHandler(void* closure, const xmlChar* text) {}

PRINTF_FORMAT(2, 3)
static void WarningHandler(void* closure, const char* message, ...) {}

PRINTF_FORMAT(2, 3)
static void NormalErrorHandler(void* closure, const char* message, ...) {}

// Using a static entity and marking it XML_INTERNAL_PREDEFINED_ENTITY is a hack
// to avoid malloc/free. Using a global variable like this could cause trouble
// if libxml implementation details were to change
// TODO(https://crbug.com/344484975): The XML_INTERNAL_PREDEFINED_ENTITY is in
// fact overridden in GetXHTMLEntity() below for all uses, so it's not
// behaving as documented.
static xmlChar g_shared_xhtml_entity_result[9] =;

static xmlEntityPtr SharedXHTMLEntity() {}

static size_t ConvertUTF16EntityToUTF8(const UChar* utf16_entity,
                                       size_t number_of_code_units,
                                       char* target,
                                       size_t target_size) {}

static xmlEntityPtr GetXHTMLEntity(const xmlChar* name) {}

static xmlEntityPtr GetEntityHandler(void* closure, const xmlChar* name) {}

static void StartDocumentHandler(void* closure) {}

static void EndDocumentHandler(void* closure) {}

static void InternalSubsetHandler(void* closure,
                                  const xmlChar* name,
                                  const xmlChar* external_id,
                                  const xmlChar* system_id) {}

static void ExternalSubsetHandler(void* closure,
                                  const xmlChar*,
                                  const xmlChar* external_id,
                                  const xmlChar*) {}

static void IgnorableWhitespaceHandler(void*, const xmlChar*, int) {}

void XMLDocumentParser::InitializeParserContext(const std::string& chunk) {}

void XMLDocumentParser::DoEnd() {}

xmlDocPtr XmlDocPtrForString(Document* document,
                             const String& source,
                             const String& url) {}

OrdinalNumber XMLDocumentParser::LineNumber() const {}

OrdinalNumber XMLDocumentParser::ColumnNumber() const {}

TextPosition XMLDocumentParser::GetTextPosition() const {}

void XMLDocumentParser::StopParsing() {}

void XMLDocumentParser::ResumeParsing() {}

bool XMLDocumentParser::AppendFragmentSource(const String& chunk) {}

void XMLDocumentParser::DidAddPendingParserBlockingStylesheet() {}

void XMLDocumentParser::DidLoadAllPendingParserBlockingStylesheets() {}

void XMLDocumentParser::CheckIfBlockingStyleSheetAdded() {}

void XMLDocumentParser::ExecuteScriptsWaitingForResources() {}

// --------------------------------

struct AttributeParseState {};

static void AttributesStartElementNsHandler(void* closure,
                                            const xmlChar* xml_local_name,
                                            const xmlChar* /*xmlPrefix*/,
                                            const xmlChar* /*xmlURI*/,
                                            int /*nbNamespaces*/,
                                            const xmlChar** /*namespaces*/,
                                            int nb_attributes,
                                            int /*nbDefaulted*/,
                                            const xmlChar** libxml_attributes) {}

HashMap<String, String> ParseAttributes(const String& string, bool& attrs_ok) {}

#undef xmlParseChunk

}  // namespace blink