-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure that emoji content is correctly rewritten (#2852)
- Loading branch information
1 parent
9c84955
commit 63c72dc
Showing
6 changed files
with
58 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,23 @@ | ||
import { expect } from '@open-wc/testing'; | ||
import { rewriteEmojiContent } from './rewriteEmojiContent'; | ||
|
||
describe('emoji rewrite tests', () => { | ||
it('rewrites an emoji correctly', async () => { | ||
const result = rewriteEmojiContent(`<emoji id="cool" alt="😎" title="Cool"></emoji>`); | ||
await expect(result).to.be.equal('😎'); | ||
}); | ||
it('rewrites an emoji in a p tag correctly', async () => { | ||
const result = rewriteEmojiContent(`<p><emoji id="cool" alt="😎" title="Cool"></emoji></p>`); | ||
await expect(result).to.be.equal('<p>😎</p>'); | ||
}); | ||
it('rewrites multiple emoji in a p correctly', async () => { | ||
const result = rewriteEmojiContent( | ||
`<p><emoji id="cool" alt="😎" title="Cool"></emoji><emoji id="1f92a_zanyface" alt="🤪" title="Zany face"></emoji></p>` | ||
); | ||
await expect(result).to.be.equal('<p>😎🤪</p>'); | ||
}); | ||
it('returns the original value if there is no emoji', async () => { | ||
const result = rewriteEmojiContent('<p><em>Seb is cool</em></p>'); | ||
await expect(result).to.be.equal('<p><em>Seb is cool</em></p>'); | ||
}); | ||
}); |
This file contains 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,32 @@ | ||
/** | ||
* Regex to detect and extract emoji alt text | ||
* | ||
* Pattern breakdown: | ||
* (<emoji[^>]+): Captures the opening emoji tag, including any attributes. | ||
* alt=["'](\w*[^"']*)["']: Matches and captures the "alt" attribute value within single or double quotes. The value can contain word characters but not quotes. | ||
* (.[^>]): Captures any remaining text within the opening emoji tag, excluding the closing angle bracket. | ||
* ><\/emoji>: Matches the remaining part of the tag. | ||
*/ | ||
const emojiRegex = /(<emoji[^>]+)alt=["'](\w*[^"']*)["'](.[^>]+)><\/emoji>/; | ||
const emojiMatch = (messageContent: string): RegExpMatchArray | null => { | ||
return messageContent.match(emojiRegex); | ||
}; | ||
// iterative repave the emoji custom element with the content of the alt attribute | ||
// on the emoji element | ||
const processEmojiContent = (messageContent: string): string => { | ||
let result = messageContent; | ||
let match = emojiMatch(result); | ||
while (match) { | ||
result = result.replace(emojiRegex, '$2'); | ||
match = emojiMatch(result); | ||
} | ||
return result; | ||
}; | ||
|
||
/** | ||
* if the content contains an <emoji> tag with an alt attribute the content is replaced by replacing the emoji tags with the content of their alt attribute. | ||
* @param {string} content | ||
* @returns {string} the content with any emoji tags replaced by the content of their alt attribute. | ||
*/ | ||
export const rewriteEmojiContent = (content: string): string => | ||
emojiMatch(content) ? processEmojiContent(content) : content; |
File renamed without changes.
File renamed without changes.
This file contains 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