-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Fixes sheet ordering #819
Fixes sheet ordering #819
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,45 +216,66 @@ function findCommentNode(text: string): Node | null { | |
return null | ||
} | ||
|
||
type PrevNode = { | ||
parent: ?Node, | ||
node: ?Node | ||
} | ||
|
||
/** | ||
* Find a node before which we can insert the sheet. | ||
*/ | ||
function findPrevNode(options: PriorityOptions): ?Node | null { | ||
function findPrevNode(options: PriorityOptions): PrevNode | false { | ||
const {registry} = sheets | ||
|
||
if (registry.length > 0) { | ||
// Try to insert before the next higher sheet. | ||
let sheet = findHigherSheet(registry, options) | ||
if (sheet) return sheet.renderer.element | ||
if (sheet) { | ||
return { | ||
parent: sheet.renderer.element.parentNode, | ||
node: sheet.renderer.element | ||
} | ||
} | ||
|
||
// Otherwise insert after the last attached. | ||
sheet = findHighestSheet(registry, options) | ||
if (sheet) return sheet.renderer.element.nextElementSibling | ||
if (sheet) { | ||
return { | ||
parent: sheet.renderer.element.parentNode, | ||
node: sheet.renderer.element.nextSibling | ||
} | ||
} | ||
} | ||
|
||
// Try to find a comment placeholder if registry is empty. | ||
const {insertionPoint} = options | ||
if (insertionPoint && typeof insertionPoint === 'string') { | ||
const comment = findCommentNode(insertionPoint) | ||
if (comment) return comment.nextSibling | ||
if (comment) { | ||
return { | ||
parent: comment.parentNode, | ||
node: comment.nextSibling | ||
} | ||
} | ||
|
||
// If user specifies an insertion point and it can't be found in the document - | ||
// bad specificity issues may appear. | ||
warning(insertionPoint === 'jss', '[JSS] Insertion point "%s" not found.', insertionPoint) | ||
} | ||
|
||
return null | ||
return false | ||
} | ||
|
||
/** | ||
* Insert style element into the DOM. | ||
*/ | ||
function insertStyle(style: HTMLElement, options: PriorityOptions) { | ||
const {insertionPoint} = options | ||
const prevNode = findPrevNode(options) | ||
const nextNode = findPrevNode(options) | ||
|
||
if (nextNode !== false && nextNode.parent) { | ||
nextNode.parent.insertBefore(style, nextNode.node) | ||
|
||
if (prevNode) { | ||
const {parentNode} = prevNode | ||
if (parentNode) parentNode.insertBefore(style, prevNode) | ||
return | ||
} | ||
|
||
|
@@ -268,7 +289,7 @@ function insertStyle(style: HTMLElement, options: PriorityOptions) { | |
return | ||
} | ||
|
||
getHead().insertBefore(style, prevNode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it worked basically before same way, because the insertBefore api allows to pass prevNode === null and it will be inserted in the end of the parent, so the same as appendChild There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems a bit simpler to me |
||
getHead().appendChild(style) | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like calling parentNode in advance doesn't bring much value other than makes findPrevNode semantics wrong. Mb I oversee something but to me it looks like we could keep the return value as it was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is when returning nextSibling and when it returns
null
we can't get the parentNodeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arent you appending in the end of the head when nextSibling is null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily, it was supposed to work with appending it at the end of the parent of the stylesheet