-
Notifications
You must be signed in to change notification settings - Fork 169
fix: contentAnalyzer added to determine TinyMCE Editor type #2609
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
Open
marslanabdulrauf
wants to merge
3
commits into
openedx:master
Choose a base branch
from
mitodl:marslan/9166-save-setting-alert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/schedule-and-details/introducing-section/contentTypeUtils.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * Utility functions for detecting content type and | ||
| * determining appropriate editor type for TinyMCE editor | ||
| */ | ||
|
|
||
| /** | ||
| * Detects if content contains HTML tags | ||
| * @param {string} content - The content to analyze | ||
| * @returns {boolean} - True if content contains HTML tags | ||
| */ | ||
| export const containsHtml = (content) => { | ||
| if (!content || typeof content !== 'string') { | ||
| return false; | ||
| } | ||
|
|
||
| // Check for common HTML patterns | ||
| const htmlPatterns = [ | ||
| /<\/?[a-z][\s\S]*>/i, // HTML tags | ||
| /&[a-z]+;/i, // HTML entities | ||
| /&#\d+;/, // Numeric entities | ||
| ]; | ||
|
|
||
| return htmlPatterns.some(pattern => pattern.test(content)); | ||
| }; | ||
|
|
||
| /** | ||
| * Determines the appropriate editor type based on content analysis | ||
| * @param {string} content - The content to analyze | ||
| * @returns {string} - The recommended editor type ('text' or 'html') | ||
| */ | ||
| export const determineEditorType = (content) => { | ||
| if (!content || typeof content !== 'string') { | ||
| return 'text'; | ||
| } | ||
|
|
||
| // If content contains HTML, use html editor for better HTML editing | ||
| if (containsHtml(content)) { | ||
| return 'html'; | ||
| } | ||
|
|
||
| // For plain text content, use text editor | ||
| return 'text'; | ||
| }; | ||
167 changes: 167 additions & 0 deletions
167
src/schedule-and-details/introducing-section/contentTypeUtils.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /** | ||
| * Tests for content type detection utilities | ||
| */ | ||
| import { containsHtml, determineEditorType } from './contentTypeUtils'; | ||
|
|
||
| describe('contentTypeUtils', () => { | ||
| describe('containsHtml', () => { | ||
| describe('should return false for non-HTML content', () => { | ||
| test.each([ | ||
| ['empty string', ''], | ||
| ['null', null], | ||
| ['undefined', undefined], | ||
| ['number', 123], | ||
| ['plain text', 'This is just plain text'], | ||
| ['text with special characters', 'Text with @#$%^&*()'], | ||
| ['text with quotes', 'Text with "quotes" and \'apostrophes\''], | ||
| ['text with newlines', 'Line 1\nLine 2\nLine 3'], | ||
| ['text with angle brackets but not HTML', '5 < 10 and 10 > 5'], | ||
| ['mathematical expressions', 'x = y < z > w'], | ||
| ])('for %s', (description, input) => { | ||
| expect(containsHtml(input)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should return true for HTML content', () => { | ||
| test.each([ | ||
| // Basic HTML tags | ||
| ['simple paragraph', '<p>Hello world</p>'], | ||
| ['heading tag', '<h1>Title</h1>'], | ||
| ['div element', '<div>Content</div>'], | ||
| ['span element', '<span>Text</span>'], | ||
| ['self-closing tag', '<br />'], | ||
| ['self-closing without space', '<br/>'], | ||
|
|
||
| // HTML tags with attributes | ||
| ['tag with class', '<p class="intro">Text</p>'], | ||
| ['tag with id', '<div id="content">Text</div>'], | ||
| ['tag with multiple attributes', '<a href="link" target="_blank">Link</a>'], | ||
|
|
||
| // Mixed case HTML | ||
| ['uppercase tag', '<P>Paragraph</P>'], | ||
| ['mixed case tag', '<DiV>Content</DiV>'], | ||
|
|
||
| // HTML with content | ||
| ['nested tags', '<div><p>Nested paragraph</p></div>'], | ||
| ['multiple tags', '<h1>Title</h1><p>Paragraph</p>'], | ||
| ['formatting tags', 'Text with <strong>bold</strong> and <em>italic</em>'], | ||
|
|
||
| // Complex HTML structures | ||
| ['list structure', '<ul><li>Item 1</li><li>Item 2</li></ul>'], | ||
| ['table structure', '<table><tr><td>Cell</td></tr></table>'], | ||
| ['form elements', '<input type="text" name="field">'], | ||
| ['image tag', '<img src="image.jpg" alt="Image">'], | ||
|
|
||
| // HTML entities | ||
| ['named entities', 'Price: $100 & free shipping'], | ||
| ['more entities', 'Copyright © 2024 – All rights reserved'], | ||
| ['quotes entity', 'He said "Hello" to me'], | ||
| ['numeric entities', 'Special char: € ©'], | ||
|
|
||
| // HTML with text content | ||
| ['HTML in mixed content', 'Introduction: <p>This is the main content.</p> End.'], | ||
| ['multiple entities', 'A & B < C > D'], | ||
|
|
||
| // Edge cases | ||
| ['unclosed tag', '<p>Unclosed paragraph'], | ||
| ['tag with newlines', '<p>\nMultiline\ncontent\n</p>'], | ||
| ])('for %s', (description, input) => { | ||
| expect(containsHtml(input)).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('determineEditorType', () => { | ||
| describe('should return "text" for non-HTML content', () => { | ||
| test.each([ | ||
| ['empty string', ''], | ||
| ['null', null], | ||
| ['undefined', undefined], | ||
| ['number', 123], | ||
| ['plain text', 'This is just plain text content'], | ||
| ['long plain text', 'Lorem ipsum '.repeat(100)], | ||
| ['text with special chars', 'Email: [email protected], Phone: (555) 123-4567'], | ||
| ['mathematical content', '2 + 2 = 4, x < y, a > b'], | ||
| ['code-like content', 'function() { return value < threshold; }'], | ||
| ])('for %s', (description, input) => { | ||
| expect(determineEditorType(input)).toBe('text'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should return "html" for HTML content', () => { | ||
| test.each([ | ||
| // Simple HTML | ||
| ['basic paragraph', '<p>Simple paragraph</p>'], | ||
| ['heading', '<h2>Section Title</h2>'], | ||
| ['formatted text', 'Text with <strong>bold</strong> formatting'], | ||
|
|
||
| // Complex HTML structures | ||
| ['nested elements', '<div class="intro"><h2>Title</h2><p>Content</p></div>'], | ||
| ['lists', '<ul><li>First item</li><li>Second item</li></ul>'], | ||
| ['tables', '<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>'], | ||
| ['links and images', '<a href="/link"><img src="image.jpg" alt="Image"></a>'], | ||
|
|
||
| // Course content examples | ||
| ['course overview', '<div><h3>Course Overview</h3><p>This course covers...</p><ul><li>Topic 1</li></ul></div>'], | ||
| ['about sidebar', '<div class="sidebar"><h4>About</h4><p>Prerequisites: None</p></div>'], | ||
|
|
||
| // HTML entities | ||
| ['content with entities', 'Price: $100 & includes shipping – 50% off!'], | ||
| ['mixed content', 'Introduction <p>Main content with © symbol</p> conclusion'], | ||
| ])('for %s', (description, input) => { | ||
| expect(determineEditorType(input)).toBe('html'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('integration scenarios', () => { | ||
| test('should handle real course overview content', () => { | ||
| const courseOverview = ` | ||
| <div class="course-overview"> | ||
| <h2>Introduction to Computer Science</h2> | ||
| <p>This course provides a comprehensive introduction to computer science concepts.</p> | ||
| <h3>What You'll Learn:</h3> | ||
| <ul> | ||
| <li>Programming fundamentals</li> | ||
| <li>Data structures and algorithms</li> | ||
| <li>Software engineering practices</li> | ||
| </ul> | ||
| <p><strong>Prerequisites:</strong> Basic mathematics knowledge</p> | ||
| <p><em>Duration:</em> 12 weeks</p> | ||
| </div> | ||
| `; | ||
|
|
||
| expect(containsHtml(courseOverview)).toBe(true); | ||
| expect(determineEditorType(courseOverview)).toBe('html'); | ||
| }); | ||
|
|
||
| test('should handle sidebar HTML content', () => { | ||
| const sidebarHtml = ` | ||
| <div class="about-sidebar"> | ||
| <h4>Course Information</h4> | ||
| <p><strong>Instructor:</strong> Dr. Smith</p> | ||
| <p><strong>Credits:</strong> 3</p> | ||
| <p><strong>Format:</strong> Online & In-person</p> | ||
| <a href="/syllabus">Download Syllabus</a> | ||
| </div> | ||
| `; | ||
|
|
||
| expect(containsHtml(sidebarHtml)).toBe(true); | ||
| expect(determineEditorType(sidebarHtml)).toBe('html'); | ||
| }); | ||
|
|
||
| test('should handle plain text course descriptions', () => { | ||
| const plainDescription = 'A beginner-friendly course covering the basics of programming. No prior experience required.'; | ||
|
|
||
| expect(containsHtml(plainDescription)).toBe(false); | ||
| expect(determineEditorType(plainDescription)).toBe('text'); | ||
| }); | ||
|
|
||
| test('should handle empty or minimal content', () => { | ||
| expect(determineEditorType('')).toBe('text'); | ||
| expect(determineEditorType(' ')).toBe('text'); | ||
| expect(determineEditorType(null)).toBe('text'); | ||
| expect(determineEditorType(undefined)).toBe('text'); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New files added to this repo should be using TypeScript - please see the "Best practices checklist" in your PR description.