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

HTML serialization fixes #1633

Merged
merged 13 commits into from
Jul 1, 2022
Merged

HTML serialization fixes #1633

merged 13 commits into from
Jul 1, 2022

Conversation

tjramage
Copy link
Collaborator

@tjramage tjramage commented Jun 29, 2022

Description

This PR fixes a handful of bugs with the HTML serialization logic, and also closes the following:

Remove URI encoding in favour of HTML encoding

The HTML serializer used encodeURIComponent / decodeURIComponent which causes issues when users enter links with URI encoded query strings or code blocks. The content would not be preserved exactly as the user intended as it was mistakenly decoded. Rich text should be considered "what you see is what you get", so if a user chooses to enter "%20" in the editor, this should be serialized exactly as the user entered it.

High-level changes:

  • I have adapted the HTML serializer to use HTML encode/decode (html-entities npm library) so that all user input can be respected properly and safely serialized/deserialized without any loss of content.

  • I've added the option to convertNewLinesToHtmlBr in the HTML serializer so that \n chars within paragraphs can be converted to <br /> tags (this is optional, so retains backwards compatibility for existing users).

  • Removed the isEncoded util as it's no longer used


Broken class names with stripClassNames util

The stripClassNames util also had a bug where class names would be broken. Before, when serializing a paragraph, you would get a nested "class" string:

Before (note broken class attribute):

<p class="class= c-2e063k-0 slate-p">I am a paragraph</p>

After:

<p class="c-2e063k-0 slate-p">I am a paragraph</p>

@vercel
Copy link

vercel bot commented Jun 29, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
plate ✅ Ready (Inspect) Visit Preview Jul 1, 2022 at 1:59AM (UTC)

@changeset-bot
Copy link

changeset-bot bot commented Jun 29, 2022

🦋 Changeset detected

Latest commit: 84c3285

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 61 packages
Name Type
@udecode/plate-core Major
@udecode/plate-find-replace Major
@udecode/plate-autoformat Major
@udecode/plate-break Major
@udecode/plate-combobox Major
@udecode/plate-node-id Major
@udecode/plate-normalizers Major
@udecode/plate-reset-node Major
@udecode/plate-select Major
@udecode/plate-trailing-block Major
@udecode/plate-headless Major
@udecode/plate-alignment Major
@udecode/plate-basic-elements Major
@udecode/plate-basic-marks Major
@udecode/plate-block-quote Major
@udecode/plate-code-block Major
@udecode/plate-font Major
@udecode/plate-heading Major
@udecode/plate-highlight Major
@udecode/plate-horizontal-rule Major
@udecode/plate-image Major
@udecode/plate-indent-list Major
@udecode/plate-indent Major
@udecode/plate-kbd Major
@udecode/plate-line-height Major
@udecode/plate-link Major
@udecode/plate-list Major
@udecode/plate-media-embed Major
@udecode/plate-mention Major
@udecode/plate-paragraph Major
@udecode/plate-table Major
@udecode/plate-serializer-csv Major
@udecode/plate-serializer-docx Major
@udecode/plate-serializer-html Major
@udecode/plate-juice Major
@udecode/plate-serializer-md Major
@udecode/plate-ui-button Major
@udecode/plate-ui-combobox Major
@udecode/plate-ui-cursor Major
@udecode/plate-ui-dnd Major
@udecode/plate-ui-find-replace Major
@udecode/plate-ui-alignment Major
@udecode/plate-ui-block-quote Major
@udecode/plate-ui-code-block Major
@udecode/plate-ui-excalidraw Major
@udecode/plate-ui-font Major
@udecode/plate-ui-horizontal-rule Major
@udecode/plate-ui-image Major
@udecode/plate-ui-line-height Major
@udecode/plate-ui-link Major
@udecode/plate-ui-list Major
@udecode/plate-ui-media-embed Major
@udecode/plate-ui-mention Major
@udecode/plate-ui-table Major
@udecode/plate-ui-placeholder Major
@udecode/plate-ui-popover Major
@udecode/plate-ui-popper Major
@udecode/plate-styled-components Major
@udecode/plate-ui-toolbar Major
@udecode/plate Major
@udecode/plate-ui Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@zbeyens
Copy link
Member

zbeyens commented Jun 29, 2022

Awesome, thanks! I'll move that stuff into a new package as html-entities is quite big.

Would you mind tagging the fixed issues?

@tjramage
Copy link
Collaborator Author

@zbeyens – I think it closes #808 and #1474, so have added them to the description above. This also possibly fixes #1610 but I'm not 100% sure...

@zbeyens zbeyens merged commit 689f5a8 into main Jul 1, 2022
@zbeyens zbeyens deleted the bug/html-serialization branch July 1, 2022 14:08
@github-actions github-actions bot mentioned this pull request Jul 1, 2022
@igorsheg-wix
Copy link

I was unable to get proper line breaks with the new convertNewLinesToHtmlBr prop.
I managed to fix this by passing it down the the function here:

https://github.com/udecode/plate/pull/1633/files#diff-fa637b8f2af10c217a2a6b9b90e9653d03d3cf1bddd8fe2fb976988826ef339cL78

@tjramage
Copy link
Collaborator Author

tjramage commented Jul 6, 2022

@igorsheg-wix – could you give an example of line breaks that are missing? The logic is only concerned about leaf nodes / actual content where line breaks should be inserted, not all whitespace or \n chars between parent elements, etc.

@igorsheg-wix
Copy link

Considering this node stucture:

[
  {
    "type": "h1",
    "children": [
      {
        "text": "This is my title"
      }
    ]
  },
  {
    "type": "p",
    "children": [
      {
        "text": "this is a paragprah\nand this line is a soft break "
      }
    ],
    "id": 1657096475851
  }
]

I would expect that the serialization will return:

<div><h1>This is my title</h1></div>
<div><p>this is a paragprah<br />and this line is a soft break </p></div>

Which happens with my patch.

@tjramage
Copy link
Collaborator Author

tjramage commented Jul 6, 2022

@igorsheg-wix I'm sorry, I re-read your earlier comment and I understand exactly what you mean now. Yes you're right – this is an oversight in my PR. The option should indeed be passed down to the lower-level call to serializeHtml also... thanks for spotting this! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants