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

Better default styles computation. #108

Merged
merged 9 commits into from
Jan 21, 2023
Merged

Conversation

IDisposable
Copy link
Member

@IDisposable IDisposable commented Jan 19, 2023

Uses the correct tag hierarchy when computing the default style to account for (implicit) browser stylesheets that use tags within a hierarchy to specify default styles.

We create the elements in the sandbox with all the required parent nodes to match up to the first block-level element, compute the default styling and then tear down that hierarchy. This work is still only done once per tag-and-parents combination.

Resolves #106 and thus also Resolves #95

This ensures that browser-stylesheets that have parent tag based rules
work correctly. This is evident when doing (for example) a bare `TD` before, the default style was defaulted to be `padding: opx`, but when the `TD` is actually created as a child of the normal hierarchy (e.g. `TABLE`, `TBODY`, `TR`, `TD`) then the defaulted padding is `padding: 1px`.
Refactored a bit for clarity
Resolves #106 and thus also Resolves #95
@IDisposable
Copy link
Member Author

Hey @meche-gh, this should do the trick... I also refactored that part because it was getting a bit long-winded.

src/dom-to-image-more.js Outdated Show resolved Hide resolved
@IDisposable
Copy link
Member Author

IDisposable commented Jan 19, 2023 via email

@zm-cttae
Copy link

zm-cttae commented Jan 19, 2023

For td - if we cache as tagName or acsentStopper tagName when there is a stopper - the fix should always kick in when TD is wrapped by TABLE, and we get new cache entries for tag abuse e.g. div>td. Plus tags like SPAN and I will probably only produce the permutations div span, p span

@IDisposable
Copy link
Member Author

IDisposable commented Jan 19, 2023 via email

@zm-cttae
Copy link

Also wondering if we would be better served for now in pulling in your post
filter stuff and maybe putting the optimizations behind a feature flag?

I was thinking the filter could be used for the other dom-to-image fork packages. At the same time, I'd imagine a lot of opt-ins. I'll see if I can refactor my secret WIP branch into a neat minimal PR.

@zm-cttae
Copy link

zm-cttae commented Jan 19, 2023

Just to be clear, we do want to be creating default elements against the full hierarchy tree, and my concern about the perf can be handled by a change to the logic for creating the cache key. I.e. TABLE.TBODY.TR.TD gets inserted in the sandbox iframe as such but cached as TABLE TD. The keying logic on the cache would also change

Comment on lines 1048 to 1054
'SVG',
'TABLE',
'UL',
// these are ultimate stoppers in case something drastic changes in how the DOM works
'BODY',
'HEAD',
'HTML',
Copy link

@zm-cttae zm-cttae Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The svg addition is ultimately custom - it's a performance boost!
SVG tags inside an <svg> are not going to have their default CSS change because of the parent elements outside of their <svg> tag

Copy link

@zm-cttae zm-cttae Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also add <math> as the MathML sectioning root. It is not part of the HTML spec unfortunately - it has the same relation to HTML that JS does!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little gotcha about math-the tagName property is lowercase. I personally think we benefit from lowercasing the key and the list of tags (to handle case-sensitive tag names).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

math added, in lower case... I suspect when browsers actually do adopt the tag it'll morph to uppercase...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. We need to put svg (another lowercase exception) after the "these are ultimate stoppers" comment.
Just to be sure it doesn't get deleted later when the list gets updated

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We need to lowercase svg also!
  • The svg tag won't appear in the MDN list so we should move it under the // these are ultimate stoppers comment

Copy link
Member Author

@IDisposable IDisposable Jan 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, doh... Fixed that real quick... completely missed what you were going for...

src/dom-to-image-more.js Outdated Show resolved Hide resolved
return defaultStyle;

function computeTagHierarchy(sourceNode) {
const ELEMENT_NODE = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const ELEMENT_NODE = 1;
const ELEMENT_NODE = Node.ELEMENT_NODE || 1;

Need to be "playing it safe" with this global also!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled those changes in and now have the ability to key the defaultStyleCache with either the fill tag hierachy (strict mode) or just ascentStopper + tagName ('relaxed` mode). This should make it possible revert if we broke something.

Also cleaned up the Promise handling to return the right things along the chain.
This necessitates passing the entire `options` block down the `cloneNode` path, that will be handy later anyway.
Moved `SVG` tag to where it makes sense.
@IDisposable IDisposable merged commit ff81f36 into master Jan 21, 2023
@IDisposable IDisposable deleted the better-default-styles branch January 25, 2023 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants