Skip to content

Add CharacterLimit text node overflow logic - #98

Merged
trueadm merged 8 commits into
mainfrom
Add-overflowed-node
Feb 16, 2021
Merged

trueadm merged 8 commits into
mainfrom
Add-overflowed-node

Conversation

@trueadm

@trueadm trueadm commented Feb 16, 2021 •

Copy link
Copy Markdown
Collaborator

This PR adds text node overflow logic into the core and shows how we can wire it up. This means that overflow text that goes passed the max character limit will show in red. The character limit is hard coded in the app code at 30 chars.

I also removed editor.update() sync flag, and update tests to make them sync. The sync flag was a hack for tests only really, and we don't want to encourage its usage in actual real world cases.

Screenshot 2021-02-16 at 18 13 27

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 16, 2021
@trueadm trueadm changed the title Provide editor maxLength + overflow options Add CharacterLimit text node overflow logic Feb 16, 2021
@trueadm
trueadm marked this pull request as ready for review February 16, 2021 18:19
@trueadm
trueadm merged commit 0eb902d into main Feb 16, 2021
@trueadm
trueadm deleted the Add-overflowed-node branch February 16, 2021 19:24
acywatson pushed a commit that referenced this pull request Apr 9, 2022
* Add support for overflowed text nodes

* Undo changes, change direction

* Fix up CSS

* Fix up CSS

* Bring back the previous logic, but clean up code + fix bugs

* Fix tests

* Clean up tests
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…arkers, and use CommonMark's Unicode whitespace

## Description

The previous fix guessed where markers fall from neighbouring text nodes' formats, so it missed a tag closed and reopened around an outer tag's end (`***b****“y”*` lost the italic on “y”), text inside an inline element without a transformer, and inline decorators, and it fired where an unclosable tag wrote nothing.

`$exportTextFormat` now writes each marker between sentinel noncharacters (U+FDD0 to U+FDD2, escaped as references when they appear in text), and the export resolves them once on the joined output: for each delimiter run, it looks at the characters a reader will see on either side and writes the word character beside a run that could not otherwise open or close as a numeric reference. Markers of different characters count as separate runs, as in CommonMark.

`WHITESPACE` and `PUNCTUATION_OR_SPACE` now use CommonMark's Unicode whitespace (Zs plus tab, line feed, form feed and carriage return) instead of JavaScript's `\s`, which also counts U+FEFF and others.

## Test plan

`./node_modules/.bin/vitest run --project unit packages/lexical-markdown`

### Before

`***b****“y”*` export case: `expected '***b****“y”*' to be '***&meta#98;****“y”*'` (markdown-it renders the first with literal asterisks around “y”).

### After

```
 Test Files  9 passed (9)
      Tests  865 passed (865)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…iding flanking

## Description

The previous commit marked each format marker with sentinel noncharacters in the exported string. Those leaked into the strings that `exportChildren` and `exportFormat` hand to custom transformers, collided with the same characters in code spans, URLs and decorator text, and let the guard rewrite characters that a transformer wrote.

`$exportTextFormat` and the child exporters now return pieces (escaped text, raw output, openers and closers), and `joinPieces` turns them into Markdown wherever a string leaves the exporter: the transformer callbacks and the element exports. Only text this export escaped is written as a reference. When newlines are preserved, a raw backslash before the referenced character goes out as `&meta#92;` so that it does not escape the reference. Unpaired surrogates are left alone, since their references read back as U+FFFD. `escapeLinkPart` shares `toCharacterReference`.

## Test plan

`./node_modules/.bin/vitest run --project unit packages/lexical-markdown`

### Before

The new preserve-newlines case exported `x\&meta#98;**“y”**`, which re-imports as `x&meta#98;`.

### After

```
 Test Files  9 passed (9)
      Tests  866 passed (866)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 31, 2026
… node, keep link matches, and narrow the scheme guard

## Description

Fixes from the sixth review round of this branch against main:

- The scan of a line is only reused when the text after a match is still what it was before the text-match transformer ran. 52c4375 read it after `replace`, so a transformer that edits the next node, such as a mention that trims the space after it, had the old offsets applied to the changed text: `@bob *a* *b*` kept its asterisks.
- A link match found earlier in a line is kept and moved to the rest of the text, with its `index`, `input`, `groups` and `indices`, rather than found again. Searching again cost the whole label at each step, so emphasis before a long link imported slower than on main.
- No reference is held back before `www.` or `mailto:` any more, only before `http://`, `https://` and `ftp://`. A `;` before `www.` or `mailto:` starts no autolink, so bold `.` then `bwww.x.co` exports as `**.**&meta#98;www.x.co` again, which both importers read back.
- Whether a text match cut a run of a delimiter or backtick is decided in `advanceFormatScan`, from the delimiter characters the scan records once, rather than at its caller from the transformer tags at each step.
- The deferred imports have named fields.
- The changeset says that the letter right before a scheme gets no reference.

Formatting right beside a bare URL still cannot round-trip: no reference is written inside the URL, since GFM readers would link a different URL, and import now follows CommonMark there.

## Test plan

### Before

`pnpm vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts --project unit -t "writes a reference before|changes it"` with the sources of 52c4375:

```
     × writes a reference before "bwww.x.co", which readers do not autolink 28ms
     × writes a reference before "bmailto:[email protected]", which readers do not autolink 3ms
     × scans the text after a match again when its transformer changes it 9ms
      Tests  3 failed | 763 skipped (766)
```

Importing `'*a* '.repeat(n) + '[a'.repeat(20000) + '](u)'`: main 999 ms at n=1000 and 7241 ms at n=4000.

### After

```
      Tests  3 passed | 763 skipped (766)
```

The same import: 409 ms and 1494 ms.

Importing 90,000 random lines over emphasis-heavy, Unicode and bracket-heavy alphabets gives the same editor state as 52c4375. `@lexical/mdast` reads `**.**&meta#98;www.x.co` as bold `.` then plain `bwww.x.co`.

`pnpm vitest run scripts/__tests__/unit/treeShakingSource.test.ts packages/lexical-markdown`:

```
 Test Files  11 passed (11)
      Tests  1339 passed (1339)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Nov 3, 2026
…efore markers as CJK-friendly readers do

## Description

The CJK-friendly amendment reads a variation selector (U+FE00 to U+FE0E) before a marker as the character it varies. Strict readers read it as a letter. Neither package's export reconciled the two readings in every case:

- `@lexical/mdast` export did not account for variation selectors. Bold `©︎` before `2024` exported as `**©︎**2024`, which the default import reads as literal asterisks.
- `@lexical/markdown` export missed the case where a piece consists only of a selector, whose base ends the previous piece (`x*︎*b`). Import missed it where a selector starts a text that was cut from the line.

Now `@lexical/mdast` serializes variation selectors with stand-ins as it does emoji, and writes one as a reference where the readers' flanking differs (`**©&#xFE0E;**&#x32;024`). It always writes one before `~`, because micromark-extension-cjk-friendly-gfm-strikethrough 2.0.1 never lets `~~` close after a selector. A backslash before such a reference is escaped. `flankingBefore` in `@lexical/markdown` takes what lies before the text, so a leading selector reads its base there.

Also:

- The CJK-friendly mdast export finds references after a leading byte order mark at the right offsets.
- Stand-ins are found with one walk over text and node values.
- Units are classified with `micromark-util-classify-character`, as `mdast-util-to-markdown` does.
- `get-east-asian-width` is required at ^1.7.0, the data the `@lexical/markdown` table comes from.

## Test plan

### Before

```
$ npx vitest run packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts packages/lexical-markdown/src/__tests__/unit/MarkdownPunctuation.test.ts
AssertionError: expected [ '**©︎**2024', [ [ '**©︎**2024', 0 ] ] ] to deeply equal [ '**©&#xFE0E;**&#x32;024', … ]
AssertionError: expected 'x*︎*b' to be 'x*︎*&meta#98;'
```

### After

```
$ npx vitest run packages/lexical-mdast packages/lexical-markdown packages/lexical-playground/__tests__/unit scripts/__tests__/unit/treeShakingSource.test.ts
 Test Files  50 passed (50)
      Tests  2045 passed (2045)
```

A throwaway round-trip fuzz with variation selectors, emoji, CJK, backslashes and markers in the text found no failures: 3×5,000 cases for `@lexical/mdast` in three configurations, and for `@lexical/markdown` in its three mode pairs. `tsc -p ./tsconfig.test.json` and Flow pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants