rasterizeHTML.js icon indicating copy to clipboard operation
rasterizeHTML.js copied to clipboard

Don't add padding and margin to HTML fragments

Open cburgmer opened this issue 11 years ago • 4 comments

Passing HTML fragments to drawHTML() will automatically apply padding/margin to the body element (see #77). This should be suppressed.

cburgmer avatar Oct 26 '14 16:10 cburgmer

There seems to be no clear way for distinguishing a full HTML document from a fragment. The HTML5 parser is very forgiving and so even a short fragment like <style>...</style><i>...</i> ends up being a valid document in its canonical form <html><head><style>...</style></head><body><i>...</i></body></html>. Thus any combination of HTML can potentially represent a full document and so discarding the <body> element (the one triggering the margin) for just retaining its children does not seem acceptable.

The only option left seems to have the user provide the information we need. Something like drawHtmlFragment() could then work around the margin issue.

cburgmer avatar Oct 26 '14 20:10 cburgmer

  • [ ] Can we handle fragments or just a single element.
  • [ ] Return parse errors for fragments via promise?
  • [ ] Render in WebKit
  • [ ] Rename drawHTML to drawHtmlDocument
  • [ ] Proper camel casing for all methods (HTML vs Html)

cburgmer avatar Jul 07 '16 14:07 cburgmer

a workaround if anyone fall into this padding issue:

const wrapHTML = (html) =>
`
<html>
  <head>
    <style>
      html, body {
        padding: 0;
        margin: 0;
      }
    </style>
  </head>
  <body>${html}</body>
</html>
`;

gre avatar Jul 26 '16 01:07 gre

It's been a while, but the issue I think was that in Chrome + Safari the foreignObject gets height 0, although the content's height is greater than that. Not sure whether reading https://stackoverflow.com/questions/12540436/height-of-parent-div-is-zero-even-if-it-has-child-with-finite-heights can help.

cburgmer avatar Jun 23 '17 21:06 cburgmer