Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [5.6.0] - 2024-12-13

- Add new directives to freeze rules and declarations `/*rtl:freeze*/`, `/*rtl:begin:freeze*/` and `/*rtl:end:freeze*/`
- Remove references to the legacy package

## [5.5.1] - 2024-11-24

- Small refactor to avoid a circular dependency between two parsers
Expand Down
539 changes: 218 additions & 321 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@ export type RuleParser = (
parsers: Parsers,
container: Container,
parentSourceDirective?: string,
parentFreezeDirectiveActive?: boolean,
hasParentRule?: boolean
) => void;

export type AtRuleParser = (
parsers: Parsers,
container: Container,
parentSourceDirective?: string,
parentFreezeDirectiveActive?: boolean,
hasParentRule?: boolean
) => void;

Expand All @@ -170,6 +172,7 @@ export type DeclarationParser = (
container: DeclarationContainer,
hasParentRule: boolean,
ruleSourceDirectiveValue: string,
parentFreezeDirectiveActive: boolean,
processRule: boolean,
rename: boolean
) => void;
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum TYPEOF {

export enum CONTROL_DIRECTIVE {
IGNORE = 'ignore',
FREEZE = 'freeze',
URLS = 'urls',
RULES = 'rules',
SOURCE = 'source',
Expand Down
11 changes: 10 additions & 1 deletion src/parsers/atrules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const parseAtRules = (
parsers: Parsers,
container: Container,
parentSourceDirective: string = undefined,
parentFreezeDirectiveActive: boolean = false,
hasParentRule = false
): void => {

Expand Down Expand Up @@ -54,6 +55,11 @@ export const parseAtRules = (
parentSourceDirective
);

const freezeDirectiveActive = (
checkDirective(controlDirectives, CONTROL_DIRECTIVE.FREEZE) ||
parentFreezeDirectiveActive
);

if (
hasParentRule &&
node.nodes
Expand All @@ -62,6 +68,7 @@ export const parseAtRules = (
node,
hasParentRule,
sourceDirectiveValue,
freezeDirectiveActive,
checkDirective(controlDirectives, CONTROL_DIRECTIVE.RULES),
checkDirective(controlDirectives, CONTROL_DIRECTIVE.URLS)
);
Expand All @@ -70,14 +77,16 @@ export const parseAtRules = (
parsers.parseAtRules(
parsers,
node,
parentSourceDirective,
sourceDirectiveValue,
freezeDirectiveActive,
hasParentRule
);

parsers.parseRules(
parsers,
node,
sourceDirectiveValue,
freezeDirectiveActive,
hasParentRule
);

Expand Down
17 changes: 15 additions & 2 deletions src/parsers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const parseDeclarations = (
container: DeclarationContainer,
hasParentRule: boolean,
ruleSourceDirectiveValue: string,
ruleFreezeDirectiveActive: boolean,
processRule: boolean,
rename: boolean
): void => {
Expand Down Expand Up @@ -151,11 +152,11 @@ export const parseDeclarations = (
return;
}

const processUrlDirective = checkDirective(controlDirectives, CONTROL_DIRECTIVE.URLS);
const isProcessUrlDirectiveActive = checkDirective(controlDirectives, CONTROL_DIRECTIVE.URLS);

const declString = `${decl.toString()};`;
const declFlippedString = rtlcss.process(declString, {
processUrls: processUrls || processUrlDirective || rename,
processUrls: processUrls || isProcessUrlDirectiveActive || rename,
processEnv,
useCalc,
stringMap,
Expand Down Expand Up @@ -215,6 +216,18 @@ export const parseDeclarations = (
!sourceDirectiveValue ||
sourceDirectiveValue === source ||
mode === Mode.diff;

if (
checkDirective(controlDirectives, CONTROL_DIRECTIVE.FREEZE) ||
ruleFreezeDirectiveActive
) {
if (mode === Mode.combined) {
appendDeclarationToRule(decl, containerFlipped);
declarationsProps.push(declPropUnprefixed);
deleteDeclarations.push(decl);
}
return;
}

if (
declProp === declFlippedProp &&
Expand Down
37 changes: 34 additions & 3 deletions src/parsers/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const parseRules = (
parsers: Parsers,
container: Container,
parentSourceDirective: string = undefined,
parentFreezeDirectiveActive: boolean = false,
hasParentRule = false
): void => {

Expand Down Expand Up @@ -92,19 +93,47 @@ export const parseRules = (
addToIgnoreRulesInDiffMode(node);
return;
}

const sourceDirectiveValue = getSourceDirectiveValue(
controlDirectives,
parentSourceDirective
);

const freezeDirectiveActive = (
checkDirective(controlDirectives, CONTROL_DIRECTIVE.FREEZE) ||
parentFreezeDirectiveActive
);

if (freezeDirectiveActive) {
if (mode === Mode.combined) {
addSelectorPrefixes(
node,
(
(
!sourceDirectiveValue &&
source === Source.ltr
) ||
(
sourceDirectiveValue &&
sourceDirectiveValue === Source.ltr
)
)
? ltrPrefix
: rtlPrefix
);
}
addToIgnoreRulesInDiffMode(node);
return;
}

if (hasSelectorsPrefixed(node)) {
addToIgnoreRulesInDiffMode(node);
} else {
parsers.parseDeclarations(
node,
hasParentRule,
sourceDirectiveValue,
freezeDirectiveActive,
checkDirective(controlDirectives, CONTROL_DIRECTIVE.RULES),
checkDirective(controlDirectives, CONTROL_DIRECTIVE.URLS)
);
Expand All @@ -113,14 +142,16 @@ export const parseRules = (
parsers.parseAtRules(
parsers,
node,
parentSourceDirective,
sourceDirectiveValue,
freezeDirectiveActive,
true
);

parsers.parseRules(
parsers,
node,
parentSourceDirective,
sourceDirectiveValue,
freezeDirectiveActive,
true
);

Expand Down
11 changes: 9 additions & 2 deletions src/utilities/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,14 @@ export const removeEmptyRules = (rule: Container): void => {

export const appendRules = (): void => {
const { rules } = store;
rules.forEach(({rule, ruleLTR, ruleRTL, ruleBoth, ruleSafe}): void => {
rules.forEach((ruleObject): void => {
const {
rule,
ruleLTR,
ruleRTL,
ruleBoth,
ruleSafe
} = ruleObject;
if (ruleBoth.nodes.length) {
rule.after(ruleBoth);
}
Expand Down Expand Up @@ -309,7 +316,7 @@ export const parseRuleNames = (): void => {
let ruleFlippedSecond: Rule | undefined = undefined;

for (const selector of rule.selectors) {
const flip = selector.replace(replaceRegExp, (match: string, group: string): string => replaceHash[group]);
const flip = selector.replace(replaceRegExp, (__match: string, group: string): string => replaceHash[group]);
if (rulesHash[flip]) {
ruleFlippedSecond = rulesHash[flip].clone();
break;
Expand Down
29 changes: 24 additions & 5 deletions src/utilities/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,32 @@ export const addProperSelectorPrefixes = (
source
} = store.options;
if (mode === Mode.combined) {
addSelectorPrefixes(ruleFlipped, source === Source.ltr ? ltrPrefix : rtlPrefix);
addSelectorPrefixes(ruleFlippedSecond, source === Source.rtl ? ltrPrefix : rtlPrefix);
addSelectorPrefixes(
ruleFlipped,
source === Source.ltr
? ltrPrefix
: rtlPrefix
);
addSelectorPrefixes(
ruleFlippedSecond,
source === Source.ltr
? rtlPrefix
: ltrPrefix
);
} else {
addSelectorPrefixes(ruleFlipped, source === Source.ltr ? rtlPrefix : ltrPrefix);
addSelectorPrefixes(ruleFlippedSecond, source === Source.rtl ? rtlPrefix : ltrPrefix);
addSelectorPrefixes(
ruleFlipped,
source === Source.ltr
? rtlPrefix
: ltrPrefix
);
addSelectorPrefixes(
ruleFlippedSecond,
source === Source.ltr
? ltrPrefix
: rtlPrefix
);
}

addSelectorPrefixes(ruleBoth, bothPrefix);
addSelectorPrefixes(ruleSafe, bothPrefix);
};
54 changes: 54 additions & 0 deletions tests/__snapshots__/basic-options/combined/basic.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -666,5 +666,59 @@ html[dir="rtl"] .test50 {
.test53 {
margin-block-start: 10px;
margin-block-end: 5px;
}

[dir="ltr"] .test54 {
background: gray;
color: red;
margin: 0px 2px 10px 5px;
padding-left: 10px;
text-align: right;
}

.test55, .test56 {
z-index: 100;
}

[dir="ltr"] .test55, [dir="ltr"] .test56 {
float: left;
background: linear-gradient(180deg, #fff27e 0%, #ffffff 100%);
padding: 10px 5px 2px 20px;
margin: 10px 20px 30px 40px;
text-align: left;
}

[dir="rtl"] .test55, [dir="rtl"] .test56 {
float: right;
margin: 10px 40px 30px 20px;
text-align: right;
}

[dir] .test55, [dir] .test56 {
background-clip: text;
-webkit-background-clip: text;
}

@media only screen and (min-width: 630px) {
[dir="rtl"] .test57 {
text-align: right;
color: rgba(255, 242, 126, 1);
}

[dir="rtl"] .test58 {
padding: 20px;
margin: auto;
}
}

html[dir="ltr"] .test59 {
border-radius: 5px;
text-align: left;
}

[dir="ltr"]:root .test60, [dir="ltr"] .test61 {
bottom: 20x;
margin: 10px 20px 30px;
z-index: 10;
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -734,5 +734,59 @@ html[dir="rtl"] .test50 {
.test53 {
margin-block-start: 10px;
margin-block-end: 5px;
}

[dir="ltr"] .test54 {
background: gray;
color: red;
margin: 0px 2px 10px 5px;
padding-left: 10px;
text-align: right;
}

.test55, .test56 {
z-index: 100;
}

[dir="ltr"] .test55, [dir="ltr"] .test56 {
float: left;
background: linear-gradient(180deg, #fff27e 0%, #ffffff 100%);
padding: 10px 5px 2px 20px;
margin: 10px 20px 30px 40px;
text-align: left;
}

[dir="rtl"] .test55, [dir="rtl"] .test56 {
float: right;
margin: 10px 40px 30px 20px;
text-align: right;
}

[dir] .test55, [dir] .test56 {
background-clip: text;
-webkit-background-clip: text;
}

@media only screen and (min-width: 630px) {
[dir="rtl"] .test57 {
text-align: right;
color: rgba(255, 242, 126, 1);
}

[dir="rtl"] .test58 {
padding: 20px;
margin: auto;
}
}

html[dir="ltr"] .test59 {
border-radius: 5px;
text-align: left;
}

[dir="ltr"]:root .test60, [dir="ltr"] .test61 {
bottom: 20x;
margin: 10px 20px 30px;
z-index: 10;
}"
`;
Loading
Loading