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 unicode Regex miscounting emoji length #2942

Merged
merged 1 commit into from Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Tokenizer.ts
Expand Up @@ -625,7 +625,8 @@ export class _Tokenizer {
const nextChar = match[1] || match[2] || '';

if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
const lLength = match[0].length - 1;
// unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
calculuschild marked this conversation as resolved.
Show resolved Hide resolved
const lLength = [...match[0]].length - 1;
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;

const endReg = match[0][0] === '*' ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
Expand All @@ -639,7 +640,7 @@ export class _Tokenizer {

if (!rDelim) continue; // skip single * in __abc*abc__

rLength = rDelim.length;
rLength = [...rDelim].length;

if (match[3] || match[4]) { // found another Left Delim
delimTotal += rLength;
Expand All @@ -658,7 +659,7 @@ export class _Tokenizer {
// Remove extra characters. *a*** -> *a*
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);

const raw = src.slice(0, lLength + match.index + rLength + 1);
const raw = [...src].slice(0, lLength + match.index + rLength + 1).join('');

// Create `em` if smallest delimiter has odd char count. *a***
if (Math.min(lLength, rLength) % 2) {
Expand Down
11 changes: 11 additions & 0 deletions test/specs/new/emoji_inline.html
@@ -0,0 +1,11 @@
<p>Situations where it fails:</p>
<p><strong>test 💁</strong></p>
<p><strong>💁 test</strong></p>
<p><strong>🤓 test</strong></p>
<p><strong>🏖️ test</strong></p>
<p><strong>🏖️🤓💁 test</strong></p>
<p>Situations where it works:</p>
<p>**💁 **</p>
<p><strong>⚠️ test</strong></p>
<p>Here, the emoji rendering works, but the text doesn't get rendered in italic.</p>
<p><em>💁 test</em></p>
21 changes: 21 additions & 0 deletions test/specs/new/emoji_inline.md
@@ -0,0 +1,21 @@
Situations where it fails:

**test 💁**

**💁 test**

**🤓 test**

**🏖️ test**

**🏖️🤓💁 test**

Situations where it works:

**💁 **

**⚠️ test**

Here, the emoji rendering works, but the text doesn't get rendered in italic.

*💁 test*