Skip to content

feat(placeholders): add {%mail_typed_html%} placeholder and HTML sanitization - #846

Draft
tonicofonico wants to merge 4 commits into
micz:mainfrom
tonicofonico:feature/mail-typed-html
Draft

feat(placeholders): add {%mail_typed_html%} placeholder and HTML sanitization#846
tonicofonico wants to merge 4 commits into
micz:mainfrom
tonicofonico:feature/mail-typed-html

Conversation

@tonicofonico

Copy link
Copy Markdown

Summary of Changes

This PR introduces the new {%mail_typed_html%} placeholder and improves the HTML rendering pipeline when substituting AI responses into Thunderbird's compose editor.

1. New Placeholder {%mail_typed_html%}

  • Extracts user-typed text in the compose window as rich-text HTML (preserving <b>, <i>, <u>, <ul>, <li>, <p>, font sizes, etc.) before any quoted reply or forward blocks (.moz-cite-prefix, .moz-forward-container).
  • Registered in js/mzta-placeholders.js as a built-in placeholder with type: 2 (composing mode).
  • Added localization key placeholder_mail_typed_html in _locales/en/messages.json with a tip on using recommended system instructions.
  • Documented in claude-spec/03-placeholders.md.

2. Automatic HTML Rendering & Sanitization

  • DOM Range Insertion: Updated replaceSelectedText in js/mzta-compose-script.js to parse HTML strings and insert actual DOM child nodes (DocumentFragment) instead of inserting raw <body> or text nodes.
  • UI & Button Stripping: Sanitizes incoming HTML by removing injected UI elements (<button>, <svg>, <form>, <script>, <style>) and extracts clean content directly from .markdown or assistant response wrappers.
  • Recursive Entity Decoding: Automatically decodes escaped HTML entities (e.g. &lt;p&gt;) before DOM parsing to prevent raw tags from appearing as literal text in the email.
  • Markdown-it HTML Support: Enabled { html: true } in api_webchat/streamingMessage.js so raw HTML tags are preserved across WebChat stream responses.

Files Changed:

  • _locales/en/messages.json
  • api_webchat/streamingMessage.js
  • claude-spec/03-placeholders.md
  • js/mzta-compose-script.js
  • js/mzta-menus.js
  • js/mzta-placeholders.js
  • js/mzta-utils-prompt.js

@tonicofonico

Copy link
Copy Markdown
Author

Summary of Implementation & Technical Evaluation: Embedded Image Tokenization & Restoration

This PR adds support for Embedded Image Tokenization and Automatic Restoration to resolve payload size limits and "message too long" errors when processing emails containing embedded base64 images.

Key Additions & Features

  1. Embedded Image Tokenization (js/mzta-utils.js):

    • tokenizeHtmlImages(html) parses HTML payloads using native DOMParser() (with regex fallback) to replace large data:image/ URIs and <img ...> tags with compact token placeholders ([[THUNDERAI_IMG_1]], [[THUNDERAI_IMG_2]], etc.).
    • Prevents prompt payload explosion and context window overflow while preserving image positions.
  2. Automatic Image Restoration in Response (mzta-background.js):

    • restoreHtmlImages(text, imageMap) intercepts AI responses before insertion into the Thunderbird compose editor (_replaceSelectedText and _replyMessage).
    • Automatically replaces [[THUNDERAI_IMG_N]] tokens back with their original full <img src="data:image/..."> tags so no images are lost in final email drafts.
  3. Per-Prompt Property Flag (clean_embedded_images):

    • Introduced clean_embedded_images attribute ("1" = enabled by default) across prompt definitions in js/mzta-prompts.js.
    • Added a UI checkbox in the Custom Prompts management page (pages/customprompts/mzta-custom-prompts.html & .js).
    • Serialized in _default_prompts_properties and included in allowed export keys.
    • Added i18n label "customPrompts_form_label_clean_embedded_images" in _locales/en/messages.json.
  4. Code Quality & Verification:

    • Passed independent code audit for Thunderbird MailExtension API compliance.
    • Unified image counter scope across multiple HTML placeholders in getPlaceholdersValues.
    • Updated buildTranslationPrompt in js/mzta-utils-prompt.js.
    • All modified JavaScript files pass syntax check (node --check).

@tonicofonico
tonicofonico marked this pull request as ready for review August 5, 2026 10:41
@micz

micz commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Thank you for your PR, I'll check it after releasing version 5.0.0.

@micz

micz commented Aug 13, 2026

Copy link
Copy Markdown
Owner

I read the summary of your PR.
Could you split it in two PRs, one for the placeholder and the other one for the HTML Rendering & Sanitization?

You should also rebase the PRs to the v5.0.0 branch and check what I've done for the HTML sanitization, because I did many modifications on that part.

Thank you!

@micz
micz marked this pull request as draft August 13, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants