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

Introduce HTML string document import #5895

Merged
merged 18 commits into from
May 21, 2024
Merged

Introduce HTML string document import #5895

merged 18 commits into from
May 21, 2024

Conversation

artf
Copy link
Member

@artf artf commented May 21, 2024

This PR introduces a new feature that allows importing the HTML string as documents.
More details about the current problem and the implemented solution.

Problem

Currently, the editor imports only the content inside the body, so if you try to import this content inside the main wrapper...

editor.getWrapper().components(`
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Doc title</title>
  </head>
  <body>
    <h1>Body title</h1>
  </body>
</html>
`);

... and try to get its HTML content (eg. via editor.getHtml()), this is what you get

<body>
  <meta charset="utf-8"/>
  <title>Doc title</title>
  <h1>Body title</h1>
</body>

All the HTML document information is lost (eg. doctype, head, etc.), and some elements (eg. inside the head) are moved inside the body.
This was intentional as the editor's main purpose was always to manage only the content of the pages but this is not always suitable for all use cases.

Solution

To support the document import, the HTML parser and the wrapper component were extended to handle those data.
In order to avoid possible breaking changes, a new option asDocument is required.

editor.getWrapper().components(`
<!DOCTYPE html>
 ...
</html>
`, { asDocument: true });

Now the document information will be preserved if you try to get its HTML.

Additional API

The HTML document data are stored inside the wrapper component.

const wrapper = editor.getWrapper();
const { 
  head, // ComponentHead
  docEl,  // Component
  doctype, // string
} = wrapper;

head.toHTML(); // `<head ...</head>`
docEl.toHTML(); // '<html></html>' // no children here
doctype; //  '<!DOCTYPE html>'

Caveats

  • The asDocument option is only valid if used on the wrapper component.
  • In case you're making use of the config.canvas.styles/config.canvas.scripts options, those will be replaced with the content of the imported document.

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

Successfully merging this pull request may close these issues.

1 participant