Skip to content
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
8 changes: 5 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,11 @@ 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 is accepted and ignored
in inline styles: a later declaration in the same{' '}
<RefHtmlAttr name="style" /> attribute still overrides it. Complex
selectors such as <InlineCode>div &gt; *</InlineCode>, pseudo-classes
and pseudo-elements are not supported.
</Admonition>
</Chapter>
</Page>
Expand Down
1 change: 1 addition & 0 deletions packages/css-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"prepack": "yarn run build"
},
"dependencies": {
"css-color-keywords": "^1.0.0",
"css-to-react-native": "^3.2.0",
"csstype": "^3.1.3"
},
Expand Down
54 changes: 50 additions & 4 deletions packages/css-processor/src/CSSInlineParseRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import { LongCSSPropertyValidator } from './validators/LongCSSPropertyValidator'

type CSSRawPropertiesList = [string, any][];

/**
* 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.
*/
const IMPORTANT_FLAG_REGEX = /\s*!\s*important\s*$/i;

export class CSSInlineParseRun extends CSSParseRun {
private rules: CSSRawPropertiesList;

Expand All @@ -25,24 +35,60 @@ 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()]];
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;
}

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);
let normalizedValue = validator.normalizeInlineCSSValue(rawValue);
if (normalizedValue === null && typeof rawValue === 'string') {
// CSS keywords are case-insensitive. Values valid as written are kept
// unchanged, others get a second attempt with lowercased keywords.
const lowercasedValue = validator.lowercaseInlineCSSKeywords(rawValue);
if (lowercasedValue !== rawValue) {
normalizedValue = validator.normalizeInlineCSSValue(lowercasedValue);
}
}
if (normalizedValue === null) {
if (strict) {
throw new TypeError();
Expand Down
Loading
Loading