angular-editor icon indicating copy to clipboard operation
angular-editor copied to clipboard

remove tag when update html.

Open usama-ayub opened this issue 5 years ago • 4 comments

  • it's possible head and body tag not remove when update html.

usama-ayub avatar Dec 29 '20 11:12 usama-ayub

I need too keep the html, body and head tags.

miguelangellv avatar Aug 26 '22 10:08 miguelangellv

Having the same issue, Is there any solution to this issue?

turivishal avatar Jul 19 '23 06:07 turivishal

The same problem. Does anyone have a solution?

MeAkib avatar Jan 06 '25 06:01 MeAkib

Thank you for reporting this issue. Let me explain what's happening with <html>, <head>, and <body> tags.

Root Cause

The Angular Editor uses DomSanitizer by default, which removes certain tags for security reasons. The <html>, <head>, and <body> tags are considered document-level elements and are typically stripped by sanitization.

Why This Happens

// When you paste or set HTML like:
<html>
  <head><title>Document</title></head>
  <body>
    <p>Content</p>
  </body>
</html>

// DomSanitizer removes document-level tags, leaving:
<p>Content</p>

This is intentional security behavior - these tags can conflict with the parent page's structure.

Solutions

Option 1: Disable Sanitization (Use with Caution!)

config: AngularEditorConfig = {
  sanitize: false, // ⚠️ Only if you trust the HTML source!
  // ... other config
};

⚠️ WARNING: Only disable sanitization if:

  • You control all HTML content
  • Content comes from trusted sources
  • You implement your own XSS protection

Option 2: Use Custom Sanitizer

If you need <head> tags for specific reasons (email templates, etc.):

import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

constructor(private sanitizer: DomSanitizer) {}

// Custom sanitization that preserves document tags
customSanitize(html: string): SafeHtml {
  return this.sanitizer.sanitize(SecurityContext.HTML, html) || '';
}

Option 3: Store Document Parts Separately (Recommended)

Instead of storing full HTML documents in the editor:

// Store head and body separately
interface EmailTemplate {
  head: string;    // <title>, <style>, <meta> tags
  body: string;    // Actual content (edited in angular-editor)
}

// Combine when needed
buildFullDocument(template: EmailTemplate): string {
  return `
    <!DOCTYPE html>
    <html>
      <head>${template.head}</head>
      <body>${template.body}</body>
    </html>
  `;
}

Use Cases

Email Templates

If you're building email templates, you don't need <html>/<body> tags in the editor:

<!-- Edit only the content in angular-editor -->
<angular-editor [(ngModel)]="emailBody" [config]="config"></angular-editor>

<!-- Wrap it when sending -->
<html>
  <head>
    <style>/* Your email styles */</style>
  </head>
  <body>
    {{ emailBody }} <!-- Insert editor content here -->
  </body>
</html>

Need Help?

Could you share your specific use case? Understanding why you need to preserve <html>, <head>, and <body> tags will help us provide a better solution.

Common scenarios:

  1. Email templates → Use Option 3 (separate storage)
  2. Full HTML documents → Consider a different editor (this is a content editor, not an HTML IDE)
  3. Specific head content → Extract and store separately

Please reply with your use case so we can provide a tailored solution!

kolkov avatar Nov 22 '25 14:11 kolkov