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

fix(jss): Wrong sheet order when the insertion point is a DOM element. #816

Closed
wants to merge 1 commit into from
Closed

Conversation

jeffhall4
Copy link

When a new sheet is added with an index higher than all of those that already exist and the insertion point is an element the style sheets are actually added to the top of the list of sheets instead of the bottom.

The logic for inserting into the head vs inserting into a different element should be the same but currently isn't.

The modified test fails without the change and passes after.

This bug is particularly impactful when using the ShadowDOM as styles need to be added to an element within the shadow tree instead of the head and currently prevents using material-ui components inside a shadow root.

Fixes #752

@@ -263,7 +263,7 @@ function insertStyle(style: HTMLElement, options: PriorityOptions) {
// https://stackoverflow.com/questions/41328728/force-casting-in-flow
const insertionPointElement: HTMLElement = (insertionPoint: any)
const {parentNode} = insertionPointElement
if (parentNode) parentNode.insertBefore(style, insertionPointElement.nextSibling)
if (parentNode) parentNode.insertBefore(style, prevNode)
Copy link
Member

Choose a reason for hiding this comment

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

prevNode will always be null here.

Copy link
Author

Choose a reason for hiding this comment

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

Indeed it will, as will it in the "getHead().insertBefore" line below. Perhaps using appendChild for both is more descriptive?

@HenriBeck
Copy link
Member

I did some digging, and I don't think this is the place where the bug is.

The problem is that prevNode returns null when the element should be inserted as the last element, and it has the highest index. nextElementSibling (which I think should be nextSibling) returns null here.

That's the problem which is causing the displacement of the style tag.

@HenriBeck
Copy link
Member

HenriBeck commented Aug 6, 2018

Your change is going to cause issues when the first style tag is attached, and there is an element after the insertion point. This would cause it to be inserted last at the parent!

@jeffhall4
Copy link
Author

ok i'll look at adding a few more test cases and a different fix over the next week.

@HenriBeck
Copy link
Member

Your current fix should be reverted. Also nextElementSibling is actually fine in line 232

@HenriBeck HenriBeck mentioned this pull request Aug 9, 2018
2 tasks
@HenriBeck
Copy link
Member

Solved by #819

@HenriBeck HenriBeck closed this Aug 12, 2018
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.

2 participants