text: // Copyright 2020 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SANITIZER_API_SANITIZER_H_
  #define THIRD_PARTY_BLINK_RENDERER_MODULES_SANITIZER_API_SANITIZER_H_
  
  #include "third_party/blink/renderer/bindings/modules/v8/v8_typedefs.h"
  #include "third_party/blink/renderer/core/dom/node.h"
  #include "third_party/blink/renderer/modules/modules_export.h"
  #include "third_party/blink/renderer/modules/sanitizer_api/sanitizer_config_impl.h"
  #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  
  namespace blink {
  
  class ContainerNode;
  class Document;
  class DocumentFragment;
  class ExceptionState;
  class ExecutionContext;
  class SanitizerConfig;
  class ScriptState;
  
  class MODULES_EXPORT Sanitizer final : public ScriptWrappable {
    DEFINE_WRAPPERTYPEINFO();
  
   public:
    static Sanitizer* Create(ExecutionContext*,
                             const SanitizerConfig*,
                             ExceptionState&);
    Sanitizer(ExecutionContext*, const SanitizerConfig*);
    ~Sanitizer() override;
  
    DocumentFragment* sanitize(ScriptState* script_state,
                               const V8SanitizerInput* input,
                               ExceptionState& exception_state);
    Element* sanitizeFor(ScriptState* script_state,
                         const String& element,
                         const String& markup,
                         ExceptionState& exception_state);
  
    SanitizerConfig* getConfiguration() const;
    static SanitizerConfig* getDefaultConfiguration();
  
    // Get a (shared) Sanitizer instance with the default configuration.
    static Sanitizer* getDefaultInstance();
  
    // Implementation of ElementSanitizer::SetHTML, so that we have
    // all the sanitizer logic in one place.
    void ElementSetHTML(ScriptState* script_state,
                        Element& element,
                        const String& markup,
                        ExceptionState& exception_state);
  
    void Trace(Visitor*) const override;
  
   private:
    bool AttrListMatches(const HashMap<String, Vector<String>>&,
                         const String& attr,
                         const String& element);
    Node* DropElement(Node*, ContainerNode*);
    Node* BlockElement(Node*, ContainerNode*, ExceptionState&);
    Node* KeepElement(Node*, ContainerNode*, String&, LocalDOMWindow*);
  
    void ElementFormatter(HashSet<String>&, const Vector<String>&);
    void AttrFormatter(HashMap<String, Vector<String>>&,
                       const Vector<std::pair<String, Vector<String>>>&);
  
    DocumentFragment* PrepareFragment(LocalDOMWindow* window,
                                      ScriptState* script_state,
                                      const V8SanitizerInput* input,
                                      ExceptionState& exception_state);
    void DoSanitizing(ContainerNode*, LocalDOMWindow*, ExceptionState&);
  
    SanitizerConfigImpl config_;
  };
  }  // namespace blink
  
  #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_SANITIZER_API_SANITIZER_H_
  