Skip to content
6 changes: 3 additions & 3 deletions doc-tools/doc-pages/src/pages/PageConceptCSS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ export default function PageConceptCSS() {
precedence over classes styles, ...etc.
</Paragraph>
<Admonition type="important">
<InlineCode>important!</InlineCode> directives, complex selectors such
as <InlineCode>div &gt; *</InlineCode>, pseudo-classes and
pseudo-elements are not supported.
The <InlineCode>!important</InlineCode> flag will be ignored. Declaration blocks
after complex selectors such as <InlineCode>div &gt; *</InlineCode>,
pseudo-classes and pseudo-elements will be ignored.
</Admonition>
</Chapter>
</Page>
Expand Down
69 changes: 65 additions & 4 deletions packages/css-processor/src/CSSInlineParseRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { CSSPropertiesValidationRegistry } from './CSSPropertiesValidationRegist
import { ValidatorsType } from './makepropertiesValidators';
import { CSSProperties } from './processor-types';
import { ShortMergeRequest } from './ShortMergeRequest';
import { GenericCSSPropertyValidator } from './validators/GenericPropertyValidator';
import { LongCSSPropertyValidator } from './validators/LongCSSPropertyValidator';

type CSSRawPropertiesList = [string, any][];

const IMPORTANT_FLAG_REGEX = /\s*!\s*important\s*$/i;

export class CSSInlineParseRun extends CSSParseRun {
private rules: CSSRawPropertiesList;

Expand All @@ -25,24 +28,82 @@ export class CSSInlineParseRun extends CSSParseRun {
.map((prop) => prop.split(':'))
.reduce<CSSRawPropertiesList>((acc, prop) => {
if (prop.length === 2) {
return [...acc, [prop[0].trim(), prop[1].trim()]];
// The `!important` flag is stripped and ignored. Between style
// sources, this makes no difference: inline styles are merged last,
// and tagsStyles, classesStyles and idsStyles are JS objects which
// can't carry the flag. Within one style attribute however,
// declarations are applied in order, so a later declaration
// overrides an earlier important one, unlike in browsers:
// `color: red !important; color: blue` gives blue.
return [
...acc,
[prop[0].trim(), prop[1].replace(IMPORTANT_FLAG_REGEX, '').trim()]
];
}
return acc;
}, []);
}

/**
* Find the registered property matching a raw inline CSS property name.
*
* @remarks CSS property names are ASCII case-insensitive (e.g. `COLOR`).
* Names registered as written are kept, which preserves support for
* camelCase names (e.g. `paddingBottom`), others get a second attempt
* lowercased.
*/
private resolvePropertyName(rawName: string): keyof ValidatorsType | null {
const camelCaseName = getPropertyName(rawName);
if (this.validationMap.shouldRegisterInlineProperty(camelCaseName)) {
return camelCaseName;
}
const lowercasedName = rawName.toLowerCase();
if (lowercasedName !== rawName) {
const lowercasedCamelCaseName = getPropertyName(lowercasedName);
if (
this.validationMap.shouldRegisterInlineProperty(lowercasedCamelCaseName)
) {
return lowercasedCamelCaseName;
}
}
return null;
}

/**
* Normalize the lowercased form of a raw inline CSS value, or return null
* when lowercasing leaves it unchanged, since it already failed as written.
*
* @remarks Values can be non-strings when expanded from a shorthand.
*/
private normalizeLowercasedValue(
validator: GenericCSSPropertyValidator,
rawValue: unknown
) {
if (typeof rawValue !== 'string') {
return null;
}
const lowercasedValue = validator.lowercaseInlineCSSValue(rawValue);
return lowercasedValue === rawValue
? null
: validator.normalizeInlineCSSValue(lowercasedValue);
}

Comment on lines +72 to +90

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This part of the PR i'm not following. In #59, the problem was property names, not values.

Can you explain in more details what lower-casing values addresses?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I just felt like such a request might follow.
I decided to go ahead and make an opt-in solution that lowercases values too

CSS allows for example text-decoration: UNDERLINE, and RN doesn't work with textDecorationLine: "UNDERLINE", it needs to be lowercased

normalizeProp(
rule: CSSRawPropertiesList[number],
strict = false
): null | [keyof ValidatorsType, any] {
const rawName = rule[0];
const rawValue = rule[1];
const camelCaseName = getPropertyName(rawName);
if (!this.validationMap.shouldRegisterInlineProperty(camelCaseName)) {
const camelCaseName = this.resolvePropertyName(rawName);
if (camelCaseName === null) {
return null;
}
const validator = this.validationMap.getValidatorForProperty(camelCaseName);
const normalizedValue = validator.normalizeInlineCSSValue(rawValue);
// CSS keywords and units are case-insensitive. Values valid as written
// are kept unchanged, others get a second attempt lowercased.
const normalizedValue =
validator.normalizeInlineCSSValue(rawValue) ??
this.normalizeLowercasedValue(validator, rawValue);
if (normalizedValue === null) {
if (strict) {
throw new TypeError();
Expand Down
Loading
Loading