Skip to content

Commit

Permalink
merge rules only if the first does not contain nested rules
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed May 29, 2023
1 parent 3cefd1c commit 17bb551
Show file tree
Hide file tree
Showing 22 changed files with 525 additions and 101 deletions.
19 changes: 19 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/test/ export-ignore
/docs/ export-ignore
/tools/ export-ignore
/package-lock.json export-ignore
/.gitignore export-ignore
/.gitattributes export-ignore
/rollup.config.mjs export-ignore
/tsconfig.json export-ignore
# exclude all files in test/ from stats
/test/** linguist-vendored
/docs/** linguist-vendored
/tools/** linguist-vendored
/dist/** linguist-vendored
#
# do not replace lf by crlf
*.css text
*.json text
text eol=lf

6 changes: 3 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,15 +854,15 @@
if (value[1] == value[2] &&
value[3] == value[4] &&
value[5] == value[6]) {
return `#${value[1]}${value[3]}${value[5]}`;
value = `#${value[1]}${value[3]}${value[5]}`;
}
}
if (value.length == 9) {
else if (value.length == 9) {
if (value[1] == value[2] &&
value[3] == value[4] &&
value[5] == value[6] &&
value[7] == value[8]) {
return `#${value[1]}${value[3]}${value[5]}${value[7]}`;
value = `#${value[1]}${value[3]}${value[5]}${value[7]}`;
}
}
return named_color != null && named_color.length <= value.length ? named_color : value;
Expand Down
6 changes: 3 additions & 3 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -848,15 +848,15 @@ function renderToken(token, options = {}) {
if (value[1] == value[2] &&
value[3] == value[4] &&
value[5] == value[6]) {
return `#${value[1]}${value[3]}${value[5]}`;
value = `#${value[1]}${value[3]}${value[5]}`;
}
}
if (value.length == 9) {
else if (value.length == 9) {
if (value[1] == value[2] &&
value[3] == value[4] &&
value[5] == value[6] &&
value[7] == value[8]) {
return `#${value[1]}${value[3]}${value[5]}${value[7]}`;
value = `#${value[1]}${value[3]}${value[5]}${value[7]}`;
}
}
return named_color != null && named_color.length <= value.length ? named_color : value;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.1",
"version": "0.0.1-alpha1",
"main": "dist/index.js",
"browser": "dist/index.js",
"module": "dist/index.mjs",
Expand All @@ -14,6 +14,7 @@
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.0.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.15.10",
Expand Down
16 changes: 16 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import dts from 'rollup-plugin-dts';
import typescript from "@rollup/plugin-typescript";
import nodeResolve from "@rollup/plugin-node-resolve";
import glob from "glob";
import terser from "@rollup/plugin-terser";

export default [...await new Promise((resolve, reject) => {

Expand Down Expand Up @@ -44,6 +45,21 @@ export default [...await new Promise((resolve, reject) => {
}
]
},
{
input: 'src/index.ts',
plugins: [nodeResolve(), typescript(), terser()],
output: [
{
file: './dist/index.min.mjs',
format: 'es',
},
{
file: './dist/index.min.js',
format: 'umd',
name: 'CSSParser'
}
]
},
{
input: 'src/index.ts',
plugins: [dts()],
Expand Down
11 changes: 2 additions & 9 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import exp from "constants";
import {Token} from "./tokenize";

export * from './validation';
export * from './tokenize';
export * from './stringiterator';
export * from './properties';

export declare type NodeTraverseCallback = (node: AstNode, location: Location, parent: AstRuleList, root: AstRuleStyleSheet) => void;

Expand Down Expand Up @@ -71,16 +71,9 @@ export interface AstComment extends Node {
typ: 'Comment',
val: string;
}

export interface AstInvalidComment extends Node {

typ: 'InvalidComment',
val: string;
}

export interface AstDeclaration extends Node {

nam: Token[],
nam: string,
val: Token[];
typ: 'Declaration'
}
Expand Down
16 changes: 16 additions & 0 deletions src/@types/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface PropertyType {

shorthand: string;
}

export class ShorthandPropertyType {

shorthand: string;
properties: string[];
types: string[];
}

export interface PropertySetType {

[key: string]: PropertyType | ShorthandPropertyType
}
2 changes: 2 additions & 0 deletions src/@types/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export interface ColorToken {
chi?: Token[];
}

export declare type TokenType = 'Dimension' | 'Number' | 'Perc';

export declare type Token = LiteralToken | IdentToken | CommaToken | ColonToken | SemiColonToken |
NumberToken | AtRuleToken | PercentageToken | FunctionToken | DimensionToken | StringToken |
UnclosedStringToken | HashToken | BadStringToken | BlockStartToken | BlockEndToken |
Expand Down
38 changes: 38 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"properties": {
"margin": {
"shorthand": "margin",
"properties": [ "margin-top", "margin-left", "margin-bottom", "margin-right" ],
"types": [ "Dimension", "Number", "Perc" ]
},
"margin-top": {
"shorthand": "margin"
},
"margin-left": {
"shorthand": "margin"
},
"margin-bottom": {
"shorthand": "margin"
},
"margin-right": {
"shorthand": "margin"
},
"padding": {
"shorthand": "padding",
"properties": [ "padding-top", "padding-left", "padding-bottom", "padding-right" ],
"types": [ "Dimension", "Number", "Perc" ]
},
"padding-top": {
"shorthand": "padding"
},
"padding-left": {
"shorthand": "padding"
},
"padding-bottom": {
"shorthand": "padding"
},
"padding-right": {
"shorthand": "padding"
}
}
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@


export * from './parser';
export * from './renderer';
Empty file added src/parser/declaration/index.ts
Empty file.
41 changes: 41 additions & 0 deletions src/parser/declaration/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {AstDeclaration, AstNode} from "../../@types";

import config from '../../config.json' assert {type: 'json'};
import {PropertySet} from "./set";

export class PropertyList {

private declarations: Map<any, any>;

constructor() {

this.declarations = new Map<string, AstDeclaration>;
}

add(declaration: AstNode) {

if (declaration.typ != 'Declaration') {

this.declarations.set(this.declarations.size, declaration);
return this;
}

const propertyName: string = <string> (<AstDeclaration>declaration).nam;

if (propertyName in config) {

const shorthand = config.properties[propertyName].shorthand;

if (!this.declarations.has(shorthand)) {

this.declarations.set(shorthand, new PropertySet(config[propertyName]));
}

this.declarations.get(shorthand)
}

this.declarations.set(propertyName, declaration);

return this;
}
}
55 changes: 55 additions & 0 deletions src/parser/declaration/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {AstDeclaration, ShorthandPropertyType, Token} from "../../@types";

export class PropertySet {

private config: ShorthandPropertyType;
private declarations: Map<string, AstDeclaration>;

constructor(config: ShorthandPropertyType) {

this.config = config;
this.declarations = new Map<string, AstDeclaration>;
}

add(declaration: AstDeclaration) {

if (<string>declaration.nam == this.config.shorthand) {

this.declarations.clear();

let isValid = true;
const tokens: Token[] = [];

for (let token of declaration.val) {

if (this.config.types.includes(token.typ)) {

tokens.push(token);
continue;
}

if (token.typ != 'Whitespace' && token.typ != 'Comment') {

isValid = false;
break;
}
}

if (!isValid) {

this.declarations.set(<string> declaration.nam, declaration);
}

else {

}
}

else {

this.declarations.set(<string> declaration.nam, declaration);
}

return this;
}
}
49 changes: 45 additions & 4 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AstNode, AstRule,
AstRuleStyleSheet,
ErrorDescription,
ParserOptions
ParserOptions, Token
} from "../@types";
import {tokenize} from "./tokenize";

Expand Down Expand Up @@ -58,6 +58,27 @@ export function deduplicate(ast: AstNode) {

if (node.typ == 'AtRule' && (<AstAtRule>node).nam == 'media' && (<AstAtRule>node).val == 'all') {

// merge only if the previous rule contains only declarations
let shouldMerge = true;
// @ts-ignore
let i = node.chi.length;

while (i--) {

if (node.chi[i].typ == 'Comment') {

continue;
}

shouldMerge = node.chi[i].typ == 'Declaration';
break;
}

if (!shouldMerge) {

continue;
}

// @ts-ignore
ast.chi.splice(i, 1, ...node.chi);
// @ts-ignore
Expand All @@ -76,6 +97,26 @@ export function deduplicate(ast: AstNode) {

if ('chi' in node) {

let shouldMerge = true;
// @ts-ignore
let i = node.chi.length;

while (i--) {

if (node.chi[i].typ == 'Comment') {

continue;
}

shouldMerge = node.chi[i].typ == 'Declaration';
break;
}

if (!shouldMerge) {

continue;
}

// @ts-ignore
previous.chi = node.chi.concat(...(previous.chi || []));
}
Expand Down Expand Up @@ -134,7 +175,7 @@ export function deduplicateRule(ast: AstNode): AstNode {
return ast;
}

const set: Set<string> = new Set;
const map: Map<string, Token> = new Map;

// @ts-ignore
let i: number = <number>ast.chi.length;
Expand All @@ -151,15 +192,15 @@ export function deduplicateRule(ast: AstNode): AstNode {
}

// @ts-ignore
if (set.has(node.nam)) {
if (map.has(node.nam)) {

// @ts-ignore
ast.chi.splice(i, 1);
continue;
}

// @ts-ignore
set.add(node.nam);
map.set(node.nam, node);
}

return ast;
Expand Down
Loading

0 comments on commit 17bb551

Please sign in to comment.