use crate::block::BlockFieldMeta;
use crate::widget::WidgetKind;
use crate::{register_block, Block, BlockField};
#[derive(Default)]
pub struct HeadingBlock;
impl Block for HeadingBlock {
fn type_name(&self) -> &'static str {
"heading"
}
fn verbose_name(&self) -> &'static str {
"Heading"
}
fn icon(&self) -> Option<&'static str> {
Some("title")
}
fn label_format(&self) -> Option<&'static str> {
Some("{text}")
}
fn preview_value(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({ "text": "Example heading", "level": "2" }))
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::char("text", "Text").required(),
BlockField::choice("level", "Level", [("2", "H2"), ("3", "H3"), ("4", "H4")]),
]
}
}
#[derive(Default)]
pub struct ParagraphBlock;
impl Block for ParagraphBlock {
fn type_name(&self) -> &'static str {
"paragraph"
}
fn verbose_name(&self) -> &'static str {
"Paragraph"
}
fn icon(&self) -> Option<&'static str> {
Some("notes")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "body".to_owned(),
label: "Body".to_owned(),
widget: WidgetKind::Markdown,
options: Vec::new(),
help: Some("GFM markdown.".to_owned()),
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct ImageBlock;
impl Block for ImageBlock {
fn type_name(&self) -> &'static str {
"image"
}
fn verbose_name(&self) -> &'static str {
"Image"
}
fn icon(&self) -> Option<&'static str> {
Some("image")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "media_id".to_owned(),
label: "Image".to_owned(),
widget: WidgetKind::MediaPicker,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "alt".to_owned(),
label: "Alt text".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: Some("Empty if decorative.".to_owned()),
required: false,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "caption".to_owned(),
label: "Caption".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
#[derive(Default)]
pub struct QuoteBlock;
impl Block for QuoteBlock {
fn type_name(&self) -> &'static str {
"quote"
}
fn verbose_name(&self) -> &'static str {
"Quote"
}
fn icon(&self) -> Option<&'static str> {
Some("format_quote")
}
fn label_format(&self) -> Option<&'static str> {
Some("“{text}”")
}
fn preview_value(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({
"text": "The best way to predict the future is to invent it.",
"attribution": "Alan Kay"
}))
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "text".to_owned(),
label: "Quote".to_owned(),
widget: WidgetKind::Textarea,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "attribution".to_owned(),
label: "Attribution".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
#[derive(Default)]
pub struct EmbedBlock;
impl Block for EmbedBlock {
fn type_name(&self) -> &'static str {
"embed"
}
fn verbose_name(&self) -> &'static str {
"Embed"
}
fn icon(&self) -> Option<&'static str> {
Some("smart_display")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "url".to_owned(),
label: "URL".to_owned(),
widget: WidgetKind::Url,
options: Vec::new(),
help: Some("YouTube or Vimeo URL (others link out).".to_owned()),
required: true,
meta: BlockFieldMeta::default(),
}]
}
fn extra_context(
&self,
value: &serde_json::Value,
_ctx: &crate::block::BlockRenderCtx<'_>,
) -> serde_json::Map<String, serde_json::Value> {
let url = value
.get("url")
.and_then(serde_json::Value::as_str)
.unwrap_or("")
.trim();
let (src, provider, aspect) = match crate::embed::resolve(url) {
Some(t) => (
t.src,
t.provider.name().to_owned(),
t.aspect_ratio.to_owned(),
),
None => (String::new(), String::new(), String::new()),
};
let mut m = serde_json::Map::new();
m.insert("embed_src".to_owned(), serde_json::Value::String(src));
m.insert("provider".to_owned(), serde_json::Value::String(provider));
m.insert("aspect_ratio".to_owned(), serde_json::Value::String(aspect));
m.insert("safe_href".to_owned(), safe_href_value(url));
m
}
}
#[must_use]
pub fn safe_href(raw: &str) -> Option<&str> {
let raw = raw.trim();
let squashed: String = raw.chars().filter(|c| !c.is_ascii_control() && *c != ' ').collect();
let scheme = match squashed.find([':', '/', '?', '#']) {
Some(i) if squashed[i..].starts_with(':') => squashed[..i].to_ascii_lowercase(),
_ => return Some(raw),
};
matches!(scheme.as_str(), "http" | "https" | "mailto" | "tel").then_some(raw)
}
fn safe_href_value(raw: &str) -> serde_json::Value {
serde_json::Value::String(safe_href(raw).unwrap_or_default().to_owned())
}
const CODE_LANGUAGES: &[(&str, &str)] = &[
("bash", "Bash / shell"),
("c", "C"),
("cpp", "C++"),
("csharp", "C#"),
("css", "CSS"),
("diff", "Diff"),
("dockerfile", "Dockerfile"),
("go", "Go"),
("html", "HTML"),
("java", "Java"),
("javascript", "JavaScript"),
("json", "JSON"),
("kotlin", "Kotlin"),
("markdown", "Markdown"),
("php", "PHP"),
("python", "Python"),
("ruby", "Ruby"),
("rust", "Rust"),
("scss", "SCSS"),
("sql", "SQL"),
("swift", "Swift"),
("toml", "TOML"),
("typescript", "TypeScript"),
("xml", "XML"),
("yaml", "YAML"),
];
#[derive(Default)]
pub struct CodeBlock;
impl Block for CodeBlock {
fn type_name(&self) -> &'static str {
"code"
}
fn verbose_name(&self) -> &'static str {
"Code"
}
fn icon(&self) -> Option<&'static str> {
Some("code")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::choice("language", "Language", CODE_LANGUAGES.iter().copied()),
BlockField::text("code", "Code").required(),
]
}
fn preview_value(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({
"language": "rust",
"code": "fn main() {\n println!(\"hello\");\n}",
}))
}
}
#[derive(Default)]
pub struct TextBlock;
impl Block for TextBlock {
fn type_name(&self) -> &'static str {
"text"
}
fn verbose_name(&self) -> &'static str {
"Text"
}
fn icon(&self) -> Option<&'static str> {
Some("text_fields")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "body".to_owned(),
label: "Body".to_owned(),
widget: WidgetKind::Textarea,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct BooleanBlock;
impl Block for BooleanBlock {
fn type_name(&self) -> &'static str {
"boolean"
}
fn verbose_name(&self) -> &'static str {
"Boolean (checkbox)"
}
fn icon(&self) -> Option<&'static str> {
Some("check_box")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Checked".to_owned(),
widget: WidgetKind::Boolean,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct DateBlock;
impl Block for DateBlock {
fn type_name(&self) -> &'static str {
"date"
}
fn verbose_name(&self) -> &'static str {
"Date"
}
fn icon(&self) -> Option<&'static str> {
Some("calendar_today")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Date".to_owned(),
widget: WidgetKind::Date,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct DateTimeBlock;
impl Block for DateTimeBlock {
fn type_name(&self) -> &'static str {
"datetime"
}
fn verbose_name(&self) -> &'static str {
"Date & time"
}
fn icon(&self) -> Option<&'static str> {
Some("event")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "When".to_owned(),
widget: WidgetKind::Datetime,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct EmailBlock;
impl Block for EmailBlock {
fn type_name(&self) -> &'static str {
"email"
}
fn verbose_name(&self) -> &'static str {
"Email"
}
fn icon(&self) -> Option<&'static str> {
Some("mail")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "value".to_owned(),
label: "Email address".to_owned(),
widget: WidgetKind::Email,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "label".to_owned(),
label: "Display label".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: Some("Defaults to the email address itself.".to_owned()),
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
#[derive(Default)]
pub struct UrlBlock;
impl Block for UrlBlock {
fn type_name(&self) -> &'static str {
"url"
}
fn verbose_name(&self) -> &'static str {
"URL"
}
fn icon(&self) -> Option<&'static str> {
Some("link")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "value".to_owned(),
label: "URL".to_owned(),
widget: WidgetKind::Url,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "label".to_owned(),
label: "Display label".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: Some("Defaults to the URL itself.".to_owned()),
required: false,
meta: BlockFieldMeta::default(),
},
]
}
fn extra_context(
&self,
value: &serde_json::Value,
_ctx: &crate::block::BlockRenderCtx<'_>,
) -> serde_json::Map<String, serde_json::Value> {
let raw = value.get("value").and_then(serde_json::Value::as_str).unwrap_or("");
let mut m = serde_json::Map::new();
m.insert("safe_href".to_owned(), safe_href_value(raw));
m
}
}
#[derive(Default)]
pub struct IntegerBlock;
impl Block for IntegerBlock {
fn type_name(&self) -> &'static str {
"integer"
}
fn verbose_name(&self) -> &'static str {
"Integer"
}
fn icon(&self) -> Option<&'static str> {
Some("123")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Value".to_owned(),
widget: WidgetKind::Number,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct ChoiceBlock;
impl Block for ChoiceBlock {
fn type_name(&self) -> &'static str {
"choice"
}
fn verbose_name(&self) -> &'static str {
"Choice"
}
fn icon(&self) -> Option<&'static str> {
Some("list_alt")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Choice".to_owned(),
widget: WidgetKind::Select,
options: Vec::new(),
help: Some(
"Pick a value from the host-configured list. Edit the block to extend choices."
.to_owned(),
),
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct TableBlock;
impl Block for TableBlock {
fn type_name(&self) -> &'static str {
"table"
}
fn verbose_name(&self) -> &'static str {
"Table"
}
fn icon(&self) -> Option<&'static str> {
Some("table")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "tsv".to_owned(),
label: "Rows".to_owned(),
widget: WidgetKind::Textarea,
options: Vec::new(),
help: Some(
"One row per line, columns separated by tabs. The first row renders as <thead>. Paste from a spreadsheet to fill — Excel / Sheets / Numbers all copy as TSV."
.to_owned(),
),
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "caption".to_owned(),
label: "Caption".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
#[derive(Default)]
pub struct FloatBlock;
impl Block for FloatBlock {
fn type_name(&self) -> &'static str {
"float"
}
fn verbose_name(&self) -> &'static str {
"Float"
}
fn icon(&self) -> Option<&'static str> {
Some("decimal_increase")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Value".to_owned(),
widget: WidgetKind::Float,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct TimeBlock;
impl Block for TimeBlock {
fn type_name(&self) -> &'static str {
"time"
}
fn verbose_name(&self) -> &'static str {
"Time"
}
fn icon(&self) -> Option<&'static str> {
Some("schedule")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Time".to_owned(),
widget: WidgetKind::Time,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct MultipleChoiceBlock;
impl Block for MultipleChoiceBlock {
fn type_name(&self) -> &'static str {
"multiple_choice"
}
fn verbose_name(&self) -> &'static str {
"Multiple choice"
}
fn icon(&self) -> Option<&'static str> {
Some("checklist")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "values".to_owned(),
label: "Choices".to_owned(),
widget: WidgetKind::Checkboxes,
options: Vec::new(),
help: Some(
"Pick zero or more values from the host-configured list. Stored as a JSON array."
.to_owned(),
),
required: false,
meta: BlockFieldMeta::default(),
}]
}
fn render(
&self,
value: &serde_json::Value,
ctx: &crate::block::BlockRenderCtx<'_>,
) -> Result<String, crate::block::BlockError> {
let mut prepared = value.as_object().cloned().unwrap_or_default();
let parsed: Vec<serde_json::Value> = prepared
.get("values")
.and_then(serde_json::Value::as_str)
.and_then(|s| serde_json::from_str(s).ok())
.unwrap_or_default();
prepared.insert("values".to_owned(), serde_json::Value::Array(parsed));
crate::block::render::default_render(self, &serde_json::Value::Object(prepared), ctx)
}
}
#[derive(Default)]
pub struct StaticBlock;
impl Block for StaticBlock {
fn type_name(&self) -> &'static str {
"static"
}
fn verbose_name(&self) -> &'static str {
"Static block"
}
fn icon(&self) -> Option<&'static str> {
Some("horizontal_rule")
}
fn fields(&self) -> Vec<BlockField> {
Vec::new()
}
}
#[derive(Default)]
pub struct RawHtmlBlock;
impl Block for RawHtmlBlock {
fn type_name(&self) -> &'static str {
"raw_html"
}
fn verbose_name(&self) -> &'static str {
"Raw HTML"
}
fn icon(&self) -> Option<&'static str> {
Some("code")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "html".to_owned(),
label: "HTML".to_owned(),
widget: WidgetKind::Textarea,
options: Vec::new(),
help: Some(
"Raw HTML. The public template sanitises through ammonia before render — script / iframe / on* attrs are stripped."
.to_owned(),
),
required: true,
meta: BlockFieldMeta::default(),
}]
}
fn render(
&self,
value: &serde_json::Value,
ctx: &crate::block::BlockRenderCtx<'_>,
) -> Result<String, crate::block::BlockError> {
let mut prepared = value.as_object().cloned().unwrap_or_default();
let cleaned = prepared
.get("html")
.and_then(serde_json::Value::as_str)
.map(ammonia::clean)
.unwrap_or_default();
prepared.insert("html".to_owned(), serde_json::Value::String(cleaned));
crate::block::render::default_render(self, &serde_json::Value::Object(prepared), ctx)
}
}
#[derive(Default)]
pub struct PageChooserBlock;
impl Block for PageChooserBlock {
fn type_name(&self) -> &'static str {
"page_chooser"
}
fn verbose_name(&self) -> &'static str {
"Page link"
}
fn icon(&self) -> Option<&'static str> {
Some("article")
}
fn description(&self) -> Option<&'static str> {
Some("Link to another page in this site.")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "page_id".to_owned(),
label: "Page".to_owned(),
widget: WidgetKind::PageChooser,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "label".to_owned(),
label: "Display label".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: Some("Optional override — defaults to the chosen page's title.".to_owned()),
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
#[derive(Default)]
pub struct SnippetChooserBlock;
impl Block for SnippetChooserBlock {
fn type_name(&self) -> &'static str {
"snippet_chooser"
}
fn verbose_name(&self) -> &'static str {
"Snippet"
}
fn icon(&self) -> Option<&'static str> {
Some("library_books")
}
fn description(&self) -> Option<&'static str> {
Some("Pick a reusable snippet from the library.")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "snippet_id".to_owned(),
label: "Snippet".to_owned(),
widget: WidgetKind::SnippetChooser,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct DocumentChooserBlock;
impl Block for DocumentChooserBlock {
fn type_name(&self) -> &'static str {
"document_chooser"
}
fn verbose_name(&self) -> &'static str {
"Document link"
}
fn icon(&self) -> Option<&'static str> {
Some("description")
}
fn description(&self) -> Option<&'static str> {
Some("Link to a PDF or other non-image document in the media library.")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "document_id".to_owned(),
label: "Document".to_owned(),
widget: WidgetKind::DocumentChooser,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "label".to_owned(),
label: "Display label".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: Some("Optional — defaults to the document's title.".to_owned()),
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
register_block!(HeadingBlock);
register_block!(ParagraphBlock);
register_block!(ImageBlock);
register_block!(QuoteBlock);
register_block!(EmbedBlock);
register_block!(CodeBlock);
register_block!(TextBlock);
register_block!(BooleanBlock);
register_block!(DateBlock);
register_block!(DateTimeBlock);
register_block!(EmailBlock);
register_block!(UrlBlock);
register_block!(IntegerBlock);
register_block!(ChoiceBlock);
register_block!(TableBlock);
#[derive(Default)]
pub struct TypedTableRowBlock;
impl Block for TypedTableRowBlock {
fn type_name(&self) -> &'static str {
"typed_table_row"
}
fn verbose_name(&self) -> &'static str {
"Table row"
}
fn icon(&self) -> Option<&'static str> {
Some("table_rows")
}
fn description(&self) -> Option<&'static str> {
Some("One row in a typed table. Subclass to define your own columns.")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "label".to_owned(),
label: "Label".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: None,
required: true,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "value".to_owned(),
label: "Value".to_owned(),
widget: WidgetKind::Number,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
},
BlockField::Widget {
name: "notes".to_owned(),
label: "Notes".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
},
]
}
}
#[derive(Default)]
pub struct TypedTableBlock;
impl Block for TypedTableBlock {
fn type_name(&self) -> &'static str {
"typed_table"
}
fn verbose_name(&self) -> &'static str {
"Typed table"
}
fn icon(&self) -> Option<&'static str> {
Some("grid_on")
}
fn description(&self) -> Option<&'static str> {
Some("Typed grid — each column is a properly-typed widget. Subclass to define your own columns.")
}
fn fields(&self) -> Vec<BlockField> {
vec![
BlockField::Widget {
name: "caption".to_owned(),
label: "Caption".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: None,
required: false,
meta: BlockFieldMeta::default(),
},
BlockField::Repeat {
name: "rows".to_owned(),
label: "Rows".to_owned(),
item_type: "typed_table_row".to_owned(),
min: None,
max: None,
},
]
}
}
#[derive(Default)]
pub struct RichTextBlock;
impl Block for RichTextBlock {
fn type_name(&self) -> &'static str {
"rich_text"
}
fn verbose_name(&self) -> &'static str {
"Rich text"
}
fn icon(&self) -> Option<&'static str> {
Some("article")
}
fn description(&self) -> Option<&'static str> {
Some("Formatted text — hosts wire a WYSIWYG via the richtext widget hook.")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "body".to_owned(),
label: "Body".to_owned(),
widget: WidgetKind::RichText,
options: Vec::new(),
help: Some(
"Hosts swap in EasyMDE / TipTap / Draftail via the richtext widget hook. Without one, the editor falls back to the markdown toolbar."
.to_owned(),
),
required: true,
meta: BlockFieldMeta::default(),
}]
}
fn render(
&self,
value: &serde_json::Value,
ctx: &crate::block::BlockRenderCtx<'_>,
) -> Result<String, crate::block::BlockError> {
let mut prepared = value.as_object().cloned().unwrap_or_default();
let cleaned = prepared
.get("body")
.and_then(serde_json::Value::as_str)
.map(crate::markdown::sanitize_html)
.unwrap_or_default();
prepared.insert("body".to_owned(), serde_json::Value::String(cleaned));
crate::block::render::default_render(self, &serde_json::Value::Object(prepared), ctx)
}
}
#[derive(Default)]
pub struct DecimalBlock;
impl Block for DecimalBlock {
fn type_name(&self) -> &'static str {
"decimal"
}
fn verbose_name(&self) -> &'static str {
"Decimal"
}
fn icon(&self) -> Option<&'static str> {
Some("attach_money")
}
fn description(&self) -> Option<&'static str> {
Some("Decimal number (stored as text for precision).")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Value".to_owned(),
widget: WidgetKind::Float,
options: Vec::new(),
help: Some(
"Stored as a string for exact precision; the editor renders a number input."
.to_owned(),
),
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
#[derive(Default)]
pub struct RegexBlock;
impl Block for RegexBlock {
fn type_name(&self) -> &'static str {
"regex"
}
fn verbose_name(&self) -> &'static str {
"Regex-validated text"
}
fn icon(&self) -> Option<&'static str> {
Some("pattern")
}
fn description(&self) -> Option<&'static str> {
Some("Plain text validated against a regex pattern (host-configured).")
}
fn fields(&self) -> Vec<BlockField> {
vec![BlockField::Widget {
name: "value".to_owned(),
label: "Value".to_owned(),
widget: WidgetKind::Text,
options: Vec::new(),
help: Some(
"Hosts wire a pattern via BlockField::widget(...).with_meta(\
BlockFieldMeta::default().pattern(\"...\")). The browser \
enforces it on form submit."
.to_owned(),
),
required: true,
meta: BlockFieldMeta::default(),
}]
}
}
register_block!(FloatBlock);
register_block!(DecimalBlock);
register_block!(RegexBlock);
register_block!(RichTextBlock);
register_block!(TypedTableRowBlock);
register_block!(TypedTableBlock);
register_block!(TimeBlock);
register_block!(MultipleChoiceBlock);
register_block!(StaticBlock);
register_block!(RawHtmlBlock);
register_block!(PageChooserBlock);
register_block!(SnippetChooserBlock);
register_block!(DocumentChooserBlock);
#[cfg(test)]
mod safe_href_tests {
use super::safe_href;
#[test]
fn script_schemes_are_dropped_however_spelled() {
for bad in [
"javascript:alert(1)",
" JAVASCRIPT:alert(1)",
"java\tscript:alert(1)",
"java\nscript:alert(1)",
"\u{1}javascript:x",
"vbscript:x",
"data:text/html;base64,PHNjcmlwdD4=",
] {
assert_eq!(safe_href(bad), None, "{bad:?}");
}
}
#[test]
fn web_mail_phone_and_relative_targets_pass() {
for ok in [
"https://example.com",
"HTTP://example.com",
"mailto:a@example.com",
"tel:+1555",
"/about",
"about/team",
"#top",
"?q=1",
"/path:with:colons",
] {
assert_eq!(safe_href(ok), Some(ok), "{ok:?}");
}
}
}
#[cfg(test)]
mod raw_html_tests {
use super::RawHtmlBlock;
use crate::block::{Block, BlockRenderCtx};
#[test]
fn executable_markup_is_stripped_on_render() {
let mut tera = tera::Tera::default();
tera.add_raw_template("blocks/raw_html.html", include_str!("../admin/templates/blocks/raw_html.html"))
.expect("template");
let ctx = BlockRenderCtx::new(&tera);
let html = r#"<p>Hello <b>there</b></p><script>alert(1)</script><img src="x.png" onerror="alert(2)"><a href="javascript:alert(3)">x</a><iframe src="https://evil.example"></iframe><svg onload="alert(4)"></svg>"#;
let out = RawHtmlBlock.render(&serde_json::json!({ "html": html }), &ctx).expect("render");
assert!(out.contains("<b>there</b>"), "safe markup survives: {out}");
for bad in ["<script", "alert(1)", "onerror", "javascript:", "<iframe", "onload"] {
assert!(!out.contains(bad), "{bad} stripped: {out}");
}
}
}