dom_sanitizer

moon add moonbit-community/dom_sanitizer@0.1.0
Download zip
Version
0.1.0
License
Apache-2.0
Last updated
4 months ago
Downloads
18
README

#moonbit-community/dom_sanitizer

A JS-target HTML sanitizer that delegates to DOMPurify.

When the JS runtime does not provide a DOM-capable window, the package falls back to jsdom.

///|
test {
let safe = @dom_sanitizer.sanitize(
"<p onclick=\"alert(1)\">hi<script>alert(1)</script></p>",
)
inspect(safe, content="<p>hi</p>")
}

///|
test {
let config = { ..@dom_sanitizer.Config::default(), safe_for_templates: true }
let safe = @dom_sanitizer.sanitize("<p>{{ user }}</p>", config~)
inspect(safe, content="<p> </p>")
}

#
Config

pub(all) struct Config {
allowed_tags : Array[String]?
allowed_attr : Array[String]?
allowed_namespaces : Array[String]?
add_tags : Array[String]?
add_tag_check : (String) -> Bool?
add_attr : Array[String]?
add_attr_check : (String, String) -> Bool?
add_data_uri_tags : Array[String]?
add_uri_safe_attr : Array[String]?
allow_aria_attr : Bool
allow_data_attr : Bool
allow_unknown_protocols : Bool
allow_self_close_in_attr : Bool
forbid_tags : Array[String]?
forbid_attr : Array[String]?
forbid_contents : Array[String]?
add_forbid_contents : Array[String]?
force_body : Bool
keep_content : Bool
namespace_uri : String
parser_media_type : String
safe_for_templates : Bool
safe_for_xml : Bool
sanitize_dom : Bool
sanitize_named_props : Bool
whole_document : Bool
use_profiles : UseProfilesConfig?
custom_element_handling : CustomElementHandling?
allowed_uri_check : (String) -> Bool?
html_integration_points : Array[String]?
mathml_text_integration_points : Array[String]?
}

Configuration for sanitize.

#
Config::default

fn Config::default() -> Config

#
CustomElementHandling

pub(all) struct CustomElementHandling {
tag_name_check : (String) -> Bool?
attribute_name_check : (String, String) -> Bool?
allow_customized_built_in_elements : Bool
}

Custom element handling options.

#
CustomElementHandling::default

#
UseProfilesConfig

pub(all) struct UseProfilesConfig {
html : Bool
svg : Bool
svg_filters : Bool
math_ml : Bool
}

Element profile switches modeled after DOMPurify USE_PROFILES.

#
UseProfilesConfig::default

#
namespace_html

let namespace_html : String

HTML namespace constant used by Config::namespace.

#
namespace_mathml

let namespace_mathml : String

MathML namespace constant used by Config::namespace.

#
namespace_svg

let namespace_svg : String

SVG namespace constant used by Config::namespace.

#
parser_media_type_html

let parser_media_type_html : String

Default HTML parser media type.

#
parser_media_type_xhtml

let parser_media_type_xhtml : String

XHTML parser media type.

#
sanitize

fn sanitize(input : String, config? : Config) -> String

Sanitize a dirty HTML string and return a safe HTML string.

This implementation delegates to DOMPurify and is limited to string input/output on the JS target.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io