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: support insert style to head when has nest element #1106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

css-render use `rush` as monorepo manager.

`npm i -g @mircosoft/rush`
`npm i -g @microsoft/rush`

Before you start to make some change, run `rush update` then `rush install`.
10 changes: 10 additions & 0 deletions packages/css-render/__tests__/mount/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ describe('head', () => {
'gogogo'
)
})
it('mount before link element with nested', () => {
document.head.insertAdjacentHTML(
'afterbegin',
'<xxx><link></xxx>'
)
style.mount({ id: 'gogogo with nested', head: true })
expect(document.head.firstElementChild?.firstElementChild?.getAttribute('cssr-id')).to.equal(
'gogogo with nested'
)
})
})

describe('force', () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/css-render/src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ function mount (
}

if (head) {
document.head.insertBefore(
target,
document.head.querySelector('style, link')
)
const firstStyleOrLinkEl = document.head.querySelector('style, link')
// ensure Target and firstStyleOrLinkEl are under the same parent node
if (firstStyleOrLinkEl?.parentNode) {
firstStyleOrLinkEl.parentNode.insertBefore(target, firstStyleOrLinkEl)
} else {
document.head.appendChild(target)
}
} else {
document.head.appendChild(target)
}
Expand Down