Skip to content

Commit 1b20904

Browse files
committed
convert some simple values
1 parent 08a4150 commit 1b20904

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

src/index.ts

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { isSupportsBrowserhack, isMediaBrowserhack } from './atrules/atrules.js'
1717
import { getCombinators, getComplexity, isPrefixed, hasPseudoClass, isAccessibility } from './selectors/utils.js'
1818
import { calculateForAST as calculateSpecificity } from './selectors/specificity.js'
1919
import { colorFunctions, colorKeywords, namedColors, systemColors } from './values/colors.js'
20-
import { destructure, isSystemFont } from './values/destructure-font-shorthand.js'
20+
import { destructure, isSystemFont, SYSTEM_FONTS } from './values/destructure-font-shorthand.js'
2121
import { isValueKeyword, keywords, isValueReset } from './values/values.js'
2222
import { analyzeAnimation } from './values/animations.js'
2323
import { isValuePrefixed } from './values/vendor-prefix.js'
@@ -503,31 +503,71 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
503503
let value = node.value as CSSNode
504504

505505
let { text } = value
506-
let loc = wallaceLoc(value)
506+
let valueLoc = wallaceLoc(value)
507507
let complexity = 1
508508

509509
// auto, inherit, initial, etc.
510510
if (keywords.has(text)) {
511-
valueKeywords.p(text, loc)
511+
valueKeywords.p(text, valueLoc)
512512
valueComplexities.push(complexity)
513513
return
514514
}
515515

516+
//#region VALUE COMPLEXITY
516517
// i.e. `background-image: -webkit-linear-gradient()`
517518
if (isValuePrefixed(value)) {
518-
vendorPrefixedValues.p(value.text, loc)
519+
vendorPrefixedValues.p(value.text, valueLoc)
519520
complexity++
520521
}
521522

522523
// i.e. `property: value\9`
523524
if (isIe9Hack(value)) {
524-
valueBrowserhacks.p(text, loc)
525+
valueBrowserhacks.p(text, valueLoc)
525526
complexity++
526527
}
528+
//#endregion VALUE COMPLEXITY
527529

528530
// TODO: should shorthands be counted towards complexity?
529531
valueComplexities.push(complexity)
530532

533+
// Process properties first that don't have colors,
534+
// so we can avoid further walking them;
535+
if (
536+
isProperty('margin', property) ||
537+
isProperty('margin-block', property) ||
538+
isProperty('margin-inline', property) ||
539+
isProperty('margin-top', property) ||
540+
isProperty('margin-right', property) ||
541+
isProperty('margin-bottom', property) ||
542+
isProperty('margin-left', property) ||
543+
isProperty('padding', property) ||
544+
isProperty('padding-block', property) ||
545+
isProperty('padding-inline', property) ||
546+
isProperty('padding-top', property) ||
547+
isProperty('padding-right', property) ||
548+
isProperty('padding-bottom', property) ||
549+
isProperty('padding-left', property)
550+
) {
551+
if (isValueReset(value)) {
552+
resets.p(property, valueLoc)
553+
}
554+
} else if (isProperty('z-index', property)) {
555+
zindex.p(value.text, valueLoc)
556+
return SKIP
557+
} else if (isProperty('font', property)) {
558+
// TODO: implement
559+
} else if (isProperty('font-size', property)) {
560+
if (!SYSTEM_FONTS.has(value.text)) {
561+
fontSizes.p(value.text, valueLoc)
562+
}
563+
} else if (isProperty('font-family', property)) {
564+
if (!SYSTEM_FONTS.has(value.text)) {
565+
fontFamilies.p(value.text, valueLoc)
566+
}
567+
} else if (isProperty('line-height', property)) {
568+
lineHeights.p(value.text, valueLoc)
569+
}
570+
531571
wallaceWalk2(value, (valueNode) => {
532572
if (valueNode.type_name === 'Dimension') {
533573
let unit = valueNode.unit!
@@ -611,7 +651,7 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
611651
let loc = node.loc!
612652

613653
if (isValueKeyword(node)) {
614-
valueComplexities.push(1)
654+
// valueComplexities.push(1)
615655
break
616656
}
617657

@@ -638,12 +678,12 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
638678
isProperty('padding-bottom', property) ||
639679
isProperty('padding-left', property)
640680
) {
641-
if (isValueReset(node)) {
642-
resets.p(property, declaration.loc!)
643-
}
681+
// if (isValueReset(node)) {
682+
// resets.p(property, declaration.loc!)
683+
// }
644684
} else if (isProperty('z-index', property)) {
645-
zindex.p(stringifyNode(node), loc)
646-
return this.skip
685+
// zindex.p(stringifyNode(node), loc)
686+
// return this.skip
647687
} else if (isProperty('font', property)) {
648688
if (isSystemFont(node)) return
649689

@@ -672,17 +712,17 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
672712

673713
break
674714
} else if (isProperty('font-size', property)) {
675-
if (!isSystemFont(node)) {
676-
fontSizes.p(stringifyNode(node), loc)
677-
}
715+
// if (!isSystemFont(node)) {
716+
// fontSizes.p(stringifyNode(node), loc)
717+
// }
678718
break
679719
} else if (isProperty('font-family', property)) {
680-
if (!isSystemFont(node)) {
681-
fontFamilies.p(stringifyNode(node), loc)
682-
}
720+
// if (!isSystemFont(node)) {
721+
// fontFamilies.p(stringifyNode(node), loc)
722+
// }
683723
break
684724
} else if (isProperty('line-height', property)) {
685-
lineHeights.p(stringifyNode(node), loc)
725+
// lineHeights.p(stringifyNode(node), loc)
686726
} else if (isProperty('transition', property) || isProperty('animation', property)) {
687727
analyzeAnimation(children, function (item: { type: string; value: CssNode }) {
688728
if (item.type === 'fn') {

src/values/destructure-font-shorthand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { keywords } from './values.js'
33
import { Identifier, Nr, Operator } from '../css-tree-node-types.js'
44
import type { CssNode, Value } from 'css-tree'
55

6-
const SYSTEM_FONTS = new KeywordSet(['caption', 'icon', 'menu', 'message-box', 'small-caption', 'status-bar'])
6+
export const SYSTEM_FONTS = new KeywordSet(['caption', 'icon', 'menu', 'message-box', 'small-caption', 'status-bar'])
77

88
const SIZE_KEYWORDS = new KeywordSet([
99
/* <absolute-size> values */

src/values/values.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,17 @@ export function isValueKeyword(node: Value) {
2424
return firstChild!.type === Identifier && keywords.has(firstChild.name)
2525
}
2626

27-
export function isValueKeywordWallace(node: CSSNode) {
28-
if (!node.has_children) return false
29-
let size = node.children.length
30-
if (size > 1 || size === 0) return false
31-
return node.first_child?.type_name === 'Identifier' && keywords.has(node.first_child.name)
32-
}
33-
3427
function isZero(string: string): boolean {
3528
return parseFloat(string) === 0
3629
}
3730

3831
/**
3932
* Test whether a value is a reset (0, 0px, -0.0e0 etc.)
4033
*/
41-
export function isValueReset(node: Value): boolean {
42-
for (let child of node.children.toArray()) {
43-
if (child.type === Nr && isZero(child.value)) continue
44-
if (child.type === Dimension && isZero(child.value)) continue
34+
export function isValueReset(node: CSSNode): boolean {
35+
for (let child of node.children) {
36+
if (child.type_name === 'Number' && isZero(child.text)) continue
37+
if (child.type_name === 'Dimension' && child.value === 0) continue
4538
return false
4639
}
4740

0 commit comments

Comments
 (0)