Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rich-text-body): realiza sanitização do texto colado #2098

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1170,4 +1170,36 @@ describe('PoRichTextBodyComponent:', () => {
expect(nativeElement.querySelector('.po-rich-text-body')).toBeTruthy();
});
});

it('sanitizeHtmlContent: should sanitize HTML content and extract text content', () => {
const htmlContent = '<div>Hello <a href="https://example.com">world</a>!</div>';
const sanitizedContent = component['sanitizeHtmlContent'](htmlContent);
expect(sanitizedContent).toBe('Hello world!');
});

it('extractTextContent: should extract text content from a node', () => {
const htmlContent = '<div>Hello <a href="https://example.com">world</a>!</div>';
const parser = new DOMParser();
const doc = parser.parseFromString(htmlContent, 'text/html');
const textContent = component['extractTextContent'](doc.body);
expect(textContent).toBe('Hello world!');
});

it('sanitizeHtmlContent: should handle nested HTML elements correctly', () => {
const htmlContent = '<div>Hello <div>beautiful <span>world</span></div>!</div>';
const sanitizedContent = component['sanitizeHtmlContent'](htmlContent);
expect(sanitizedContent).toBe('Hello beautiful world!');
});

it('sanitizeHtmlContent: should return empty string for empty HTML content', () => {
const htmlContent = '';
const sanitizedContent = component['sanitizeHtmlContent'](htmlContent);
expect(sanitizedContent).toBe('');
});

it('sanitizeHtmlContent: should return text content for text nodes only', () => {
const htmlContent = 'Just plain text';
const sanitizedContent = component['sanitizeHtmlContent'](htmlContent);
expect(sanitizedContent).toBe('Just plain text');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export class PoRichTextBodyComponent implements OnInit, OnDestroy {
}

private updateModel() {
this.modelValue = this.bodyElement.nativeElement.innerHTML;
this.modelValue = this.sanitizeHtmlContent(this.bodyElement.nativeElement.innerHTML);

this.value.emit(this.modelValue);
}
Expand Down Expand Up @@ -371,4 +371,22 @@ export class PoRichTextBodyComponent implements OnInit, OnDestroy {

return isLink;
}

private sanitizeHtmlContent(htmlContent: string): string {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlContent, 'text/html');
return this.extractTextContent(doc.body);
}

private extractTextContent(node: Node): string {
let textContent = '';
node.childNodes.forEach(childNode => {
if (childNode.nodeType === Node.TEXT_NODE) {
textContent += childNode.textContent;
} else if (childNode.nodeType === Node.ELEMENT_NODE) {
textContent += this.extractTextContent(childNode);
}
});
return textContent;
}
}
Loading