Repository navigation
fix/css-processor-inline-css-validation #61
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
base: main
Are you sure you want to change the base?
Changes from all commits
ffafb2f
c4787f1
33a2f9f
49723d0
9bfbb49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Comment on lines
+12
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment should document logic, not local declarations. Move it to where the stripping logic is applied, and use |
||
|
|
||
| export class CSSInlineParseRun extends CSSParseRun { | ||
| private rules: CSSRawPropertiesList; | ||
|
|
||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Also, you're introducing a mutable variable, when that can be avoided. Consider this: const rawValueOrKeyword = validator.isKeyword(rawValue) ? validator.normalizeKeyword(rawValue) : rawValue;
// CSS keywords are case-insensitive. Values valid as written are kept
// unchanged, others get a second attempt with lowercased keywords.
const normalizedValue = validator.normalizeInlineCSSValue(rawValue) ?? validator.normalizeInlineCSSValue(rawValueOrKeyword) ; |
||
| if (lowercasedValue !== rawValue) { | ||
| normalizedValue = validator.normalizeInlineCSSValue(lowercasedValue); | ||
| } | ||
| } | ||
| if (normalizedValue === null) { | ||
| if (strict) { | ||
| throw new TypeError(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not introduce a 10 yo unmaintained package for basically a list of strings...