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

fixed bug on jss-plugin-nested #1591

Closed
wants to merge 1 commit into from
Closed
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
91 changes: 67 additions & 24 deletions packages/jss-plugin-nested/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import warning from 'tiny-warning'

const separatorRegExp = /\s*,\s*/g
const parentRegExp = /&/g
// cssfn:
import {
parseSelectors,
flatMapSelectors,
selectorsToString,
} from '@cssfn/css-selector'
Copy link
Member

Choose a reason for hiding this comment

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

I am afraid we can't do this, its a huge bundle-size increase

Copy link
Member

Choose a reason for hiding this comment

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

We need a solution that is super minimalistic

Copy link
Member

Choose a reason for hiding this comment

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

Wow did you write the selector parser to fix this bug? If so huge props in any case!
But jss is runtime-executed, it gets bundled with a client-side app, the only way forward is to reduce the size, even potentially remove features to make it small.

Choose a reason for hiding this comment

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

@heyymarco wow dude! thats some serious work!


const refRegExp = /\$([\w-]+)/g

/**
Expand All @@ -26,26 +31,64 @@ export default function jssNested() {
}
}

function replaceParentRefs(nestedProp, parentProp) {
const parentSelectors = parentProp.split(separatorRegExp)
const nestedSelectors = nestedProp.split(separatorRegExp)

let result = ''

for (let i = 0; i < parentSelectors.length; i++) {
const parent = parentSelectors[i]

for (let j = 0; j < nestedSelectors.length; j++) {
const nested = nestedSelectors[j]
if (result) result += ', '
// Replace all & by the parent or prefix & with the parent.
result +=
nested.indexOf('&') !== -1 ? nested.replace(parentRegExp, parent) : `${parent} ${nested}`
}
}

return result
}
const combineSelector = (parentSelector, nestedSelector) => {
const parentSelectors = parentSelector ? parseSelectors(parentSelector) : [[]];
if (!parentSelectors) return null; // parsing error => invalid selector

const nestedSelectors = parseSelectors(nestedSelector);
if (!nestedSelectors) return null; // parsing error => invalid selector



const combinedSelectors = (
parentSelectors
.flatMap((parentSelector) =>
flatMapSelectors(nestedSelectors, (selector) => {
const [
/*
selector types:
'&' = parent selector
'*' = universal selector
'[' = attribute selector
'' = element selector
'#' = ID selector
'.' = class selector
':' = pseudo class selector
'::' = pseudo element selector
*/
selectorType,

/*
selector name:
string = the name of [element, ID, class, pseudo class, pseudo element] selector
*/
// selectorName,

/*
selector parameter(s):
string = the parameter of pseudo class selector, eg: nth-child(2n+3) => '2n+3'
array = [name, operator, value, options] of attribute selector, eg: [data-msg*="you & me" i] => ['data-msg', '*=', 'you & me', 'i']
SelectorList = nested selector(s) of pseudo class [:is(...), :where(...), :not(...)]
*/
// selectorParams,
] = selector;



// we're only interested of selector type '&'

// replace selector type of `&` with `parentSelector`:
if (selectorType === '&') return parentSelector;

// preserve the another selector types:
return selector;
})
)
);

// convert back the parsed_object_tree to string:
return selectorsToString(combinedSelectors);
};

function getOptions(rule, container, prevOptions) {
// Options has been already created, now we only increase index.
Expand Down Expand Up @@ -74,14 +117,14 @@ export default function jssNested() {
let replaceRef
for (const prop in style) {
const isNested = prop.indexOf('&') !== -1
const isNestedConditional = prop[0] === '@'
const isNestedConditional = (prop[0] === '@') && !['@font-face', '@keyframes'].includes(prop);

if (!isNested && !isNestedConditional) continue

options = getOptions(styleRule, container, options)

if (isNested) {
let selector = replaceParentRefs(prop, styleRule.selector)
let selector = combineSelector(styleRule.selector, prop)
// Lazily create the ref replacer function just once for
// all nested rules within the sheet.
if (!replaceRef) replaceRef = getReplaceRef(container, sheet)
Expand Down