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 for #908: toString in hydrated group sheet #930

Merged
merged 1 commit into from
Feb 14, 2022
Merged
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
56 changes: 29 additions & 27 deletions packages/core/src/sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ export const createSheet = (/** @type {DocumentOrShadowRoot} */ root) => {
/** @type {SheetGroup} Object hosting the hydrated stylesheet. */
let groupSheet

const toString = () => {
const { cssRules } = groupSheet.sheet
return [].map
.call(cssRules, (cssRule, cssRuleIndex) => {
const { cssText } = cssRule

let lastRuleCssText = ''

if (cssText.startsWith('--sxs')) return ''

if (cssRules[cssRuleIndex - 1] && (lastRuleCssText = cssRules[cssRuleIndex - 1].cssText).startsWith('--sxs')) {
if (!cssRule.cssRules.length) return ''

for (const name in groupSheet.rules) {
if (groupSheet.rules[name].group === cssRule) {
return `--sxs{--sxs:${[...groupSheet.rules[name].cache].join(' ')}}${cssText}`
}
}

return cssRule.cssRules.length ? `${lastRuleCssText}${cssText}` : ''
}

return cssText
})
.join('')
}

const reset = () => {
if (groupSheet) {
const { rules, sheet } = groupSheet
Expand Down Expand Up @@ -83,7 +110,7 @@ export const createSheet = (/** @type {DocumentOrShadowRoot} */ root) => {
if (!groupName) continue

// create a group sheet if one does not already exist
if (!groupSheet) groupSheet = { sheet, reset, rules: {} }
if (!groupSheet) groupSheet = { sheet, reset, rules: {}, toString }

// add the group to the group sheet
groupSheet.rules[groupName] = { group, index, cache: new Set(cache) }
Expand Down Expand Up @@ -115,32 +142,7 @@ export const createSheet = (/** @type {DocumentOrShadowRoot} */ root) => {
sheet: root ? (root.head || root).appendChild(document.createElement('style')).sheet : createCSSMediaRule('', 'text/css'),
rules: {},
reset,
toString() {
const { cssRules } = groupSheet.sheet
return [].map
.call(cssRules, (cssRule, cssRuleIndex) => {
const { cssText } = cssRule

let lastRuleCssText = ''

if (cssText.startsWith('--sxs')) return ''

if (cssRules[cssRuleIndex - 1] && (lastRuleCssText = cssRules[cssRuleIndex - 1].cssText).startsWith('--sxs')) {
if (!cssRule.cssRules.length) return ''

for (const name in groupSheet.rules) {
if (groupSheet.rules[name].group === cssRule) {
return `--sxs{--sxs:${[...groupSheet.rules[name].cache].join(' ')}}${cssText}`
}
}

return cssRule.cssRules.length ? `${lastRuleCssText}${cssText}` : ''
}

return cssText
})
.join('')
},
toString,
}
}

Expand Down
33 changes: 33 additions & 0 deletions packages/core/tests/issue-908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createStitches } from '../src/index.js'

const styleRule = `--sxs { --sxs:1 lTyTw fJmROo; }`;
const mediaRule = `@media { body { margin: auto; }`;

const createStylesheet = (...preloadedStyles) => {
let rules = [];
const insertRule = (rule, index = rules.length) => {
if (rule.startsWith('--sxs')) {
rules.splice(index, 0, {type: 1, cssText: rule});
}
if (rule.startsWith('@media')) {
rules.splice(index, 0, {type: 4, cssText: rule, cssRules: []});
}
};
preloadedStyles.forEach(insertRule);
return {
insertRule,
cssRules: rules
};
}

describe('Issue #908', () => {
test('Getting hydratable stylesheet', () => {
const { getCssText } = createStitches({
root: {
styleSheets: [createStylesheet(styleRule, mediaRule)]
}
});

expect(getCssText()).toBe(mediaRule)
})
})