mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: format
This commit is contained in:
@@ -1,144 +1,158 @@
|
||||
import {
|
||||
parse
|
||||
} from "css-tree";
|
||||
import { parse } from "css-tree";
|
||||
|
||||
function mapSelectors(selector: string): string[] {
|
||||
if (!selector) {
|
||||
return [];
|
||||
}
|
||||
if (!selector) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return selector.split(/\s*(?![^(]*\)),\s*/).map(s => s.replace(/\u200C/g, ","));
|
||||
return selector
|
||||
.split(/\s*(?![^(]*\)),\s*/)
|
||||
.map((s) => s.replace(/\u200C/g, ","));
|
||||
}
|
||||
|
||||
function mapPosition(node, css) {
|
||||
let res: any = {
|
||||
start: {
|
||||
line: node.loc.start.line,
|
||||
column: node.loc.start.column
|
||||
},
|
||||
end: {
|
||||
line: node.loc.end.line,
|
||||
column: node.loc.end.column
|
||||
},
|
||||
content: css
|
||||
};
|
||||
let res: any = {
|
||||
start: {
|
||||
line: node.loc.start.line,
|
||||
column: node.loc.start.column,
|
||||
},
|
||||
end: {
|
||||
line: node.loc.end.line,
|
||||
column: node.loc.end.column,
|
||||
},
|
||||
content: css,
|
||||
};
|
||||
|
||||
if (node.loc.source && node.loc.source !== "<unknown>") {
|
||||
res.source = node.loc.source;
|
||||
}
|
||||
if (node.loc.source && node.loc.source !== "<unknown>") {
|
||||
res.source = node.loc.source;
|
||||
}
|
||||
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
function transformAst(node, css, type = null) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.type === "StyleSheet") {
|
||||
return {
|
||||
type: "stylesheet",
|
||||
stylesheet: {
|
||||
rules: node.children.map(child => transformAst(child, css)).filter(child => child !== null).toArray(),
|
||||
parsingErrors: []
|
||||
}
|
||||
};
|
||||
}
|
||||
if (node.type === "StyleSheet") {
|
||||
return {
|
||||
type: "stylesheet",
|
||||
stylesheet: {
|
||||
rules: node.children
|
||||
.map((child) => transformAst(child, css))
|
||||
.filter((child) => child !== null)
|
||||
.toArray(),
|
||||
parsingErrors: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (node.type === "Atrule") {
|
||||
let atrule: any = {
|
||||
type: node.name,
|
||||
};
|
||||
if (node.type === "Atrule") {
|
||||
let atrule: any = {
|
||||
type: node.name,
|
||||
};
|
||||
|
||||
if (node.name === "supports" || node.name === "media") {
|
||||
atrule[node.name] = node.prelude.value;
|
||||
atrule.rules = transformAst(node.block, css);
|
||||
} else if (node.name === "page") {
|
||||
atrule.selectors = node.prelude ? mapSelectors(node.prelude.value) : [];
|
||||
atrule.declarations = transformAst(node.block, css);
|
||||
} else if (node.name === "document") {
|
||||
atrule.document = node.prelude ? node.prelude.value : "";
|
||||
atrule.vendor = "";
|
||||
atrule.rules = transformAst(node.block, css);
|
||||
} else if (node.name === "font-face") {
|
||||
atrule.declarations = transformAst(node.block, css);
|
||||
} else if (node.name === "import" || node.name === "charset" || node.name === "namespace") {
|
||||
atrule[node.name] = node.prelude ? node.prelude.value : "";
|
||||
} else if (node.name === "keyframes") {
|
||||
atrule.name = node.prelude ? node.prelude.value : "";
|
||||
atrule.keyframes = transformAst(node.block, css, "keyframe");
|
||||
atrule.vendor = undefined;
|
||||
} else {
|
||||
atrule.rules = transformAst(node.block, css);
|
||||
}
|
||||
if (node.name === "supports" || node.name === "media") {
|
||||
atrule[node.name] = node.prelude.value;
|
||||
atrule.rules = transformAst(node.block, css);
|
||||
} else if (node.name === "page") {
|
||||
atrule.selectors = node.prelude
|
||||
? mapSelectors(node.prelude.value)
|
||||
: [];
|
||||
atrule.declarations = transformAst(node.block, css);
|
||||
} else if (node.name === "document") {
|
||||
atrule.document = node.prelude ? node.prelude.value : "";
|
||||
atrule.vendor = "";
|
||||
atrule.rules = transformAst(node.block, css);
|
||||
} else if (node.name === "font-face") {
|
||||
atrule.declarations = transformAst(node.block, css);
|
||||
} else if (
|
||||
node.name === "import" ||
|
||||
node.name === "charset" ||
|
||||
node.name === "namespace"
|
||||
) {
|
||||
atrule[node.name] = node.prelude ? node.prelude.value : "";
|
||||
} else if (node.name === "keyframes") {
|
||||
atrule.name = node.prelude ? node.prelude.value : "";
|
||||
atrule.keyframes = transformAst(node.block, css, "keyframe");
|
||||
atrule.vendor = undefined;
|
||||
} else {
|
||||
atrule.rules = transformAst(node.block, css);
|
||||
}
|
||||
|
||||
atrule.position = mapPosition(node, css);
|
||||
atrule.position = mapPosition(node, css);
|
||||
|
||||
return atrule;
|
||||
}
|
||||
return atrule;
|
||||
}
|
||||
|
||||
if (node.type === "Block") {
|
||||
return node.children.map(child => transformAst(child, css, type)).filter(child => child !== null).toArray();
|
||||
}
|
||||
if (node.type === "Block") {
|
||||
return node.children
|
||||
.map((child) => transformAst(child, css, type))
|
||||
.filter((child) => child !== null)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
if (node.type === "Rule") {
|
||||
let value = node.prelude.value;
|
||||
if (node.type === "Rule") {
|
||||
let value = node.prelude.value;
|
||||
|
||||
let res: any = {
|
||||
type: type != null ? type : "rule",
|
||||
declarations: transformAst(node.block, css),
|
||||
position: mapPosition(node, css)
|
||||
};
|
||||
let res: any = {
|
||||
type: type != null ? type : "rule",
|
||||
declarations: transformAst(node.block, css),
|
||||
position: mapPosition(node, css),
|
||||
};
|
||||
|
||||
if (type === "keyframe") {
|
||||
res.values = mapSelectors(value);
|
||||
} else {
|
||||
res.selectors = mapSelectors(value);
|
||||
}
|
||||
if (type === "keyframe") {
|
||||
res.values = mapSelectors(value);
|
||||
} else {
|
||||
res.selectors = mapSelectors(value);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
if (node.type === "Comment") {
|
||||
return {
|
||||
type: "comment",
|
||||
comment: node.value,
|
||||
position: mapPosition(node, css)
|
||||
};
|
||||
}
|
||||
if (node.type === "Comment") {
|
||||
return {
|
||||
type: "comment",
|
||||
comment: node.value,
|
||||
position: mapPosition(node, css),
|
||||
};
|
||||
}
|
||||
|
||||
if (node.type === "Declaration") {
|
||||
return {
|
||||
type: "declaration",
|
||||
property: node.property,
|
||||
value: node.value.value ? node.value.value.trim() : "",
|
||||
position: mapPosition(node, css)
|
||||
};
|
||||
}
|
||||
if (node.type === "Declaration") {
|
||||
return {
|
||||
type: "declaration",
|
||||
property: node.property,
|
||||
value: node.value.value ? node.value.value.trim() : "",
|
||||
position: mapPosition(node, css),
|
||||
};
|
||||
}
|
||||
|
||||
if (node.type === "Raw") {
|
||||
return null;
|
||||
}
|
||||
if (node.type === "Raw") {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw Error(`Unknown node type ${node.type}`);
|
||||
throw Error(`Unknown node type ${node.type}`);
|
||||
}
|
||||
|
||||
export function cssTreeParse(css, source): any {
|
||||
let errors = [];
|
||||
let ast = parse(css, {
|
||||
parseValue: false,
|
||||
parseAtrulePrelude: false,
|
||||
parseRulePrelude: false,
|
||||
positions: true,
|
||||
filename: source,
|
||||
onParseError: error => {
|
||||
errors.push(`${source}:${error.line}:${error.column}: ${error.formattedMessage}`);
|
||||
}
|
||||
});
|
||||
let errors = [];
|
||||
let ast = parse(css, {
|
||||
parseValue: false,
|
||||
parseAtrulePrelude: false,
|
||||
parseRulePrelude: false,
|
||||
positions: true,
|
||||
filename: source,
|
||||
onParseError: (error) => {
|
||||
errors.push(
|
||||
`${source}:${error.line}:${error.column}: ${error.formattedMessage}`
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw new Error(errors[0]);
|
||||
}
|
||||
if (errors.length > 0) {
|
||||
throw new Error(errors[0]);
|
||||
}
|
||||
|
||||
return transformAst(ast, css);
|
||||
}
|
||||
return transformAst(ast, css);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
22
nativescript-core/css/reworkcss.d.ts
vendored
22
nativescript-core/css/reworkcss.d.ts
vendored
@@ -1,33 +1,33 @@
|
||||
export interface Position {
|
||||
start: { line: number; column: number };
|
||||
end: { line: number; column: number };
|
||||
start: { line: number; column: number };
|
||||
end: { line: number; column: number };
|
||||
}
|
||||
|
||||
export interface Node {
|
||||
type: "rule" | "keyframes" | "declaration" | "import";
|
||||
position: Position;
|
||||
type: "rule" | "keyframes" | "declaration" | "import";
|
||||
position: Position;
|
||||
}
|
||||
|
||||
export interface Declaration extends Node {
|
||||
property: string;
|
||||
value: string;
|
||||
property: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface Rule extends Node {
|
||||
selectors: string[];
|
||||
declarations: Node[];
|
||||
selectors: string[];
|
||||
declarations: Node[];
|
||||
}
|
||||
|
||||
export interface Keyframes extends Rule {
|
||||
name: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface StyleSheet {
|
||||
rules: Node[];
|
||||
rules: Node[];
|
||||
}
|
||||
|
||||
export interface SyntaxTree {
|
||||
stylesheet: StyleSheet;
|
||||
stylesheet: StyleSheet;
|
||||
}
|
||||
|
||||
export function parse(css: string, options: any): SyntaxTree;
|
||||
|
||||
8
nativescript-core/css/system-classes.d.ts
vendored
8
nativescript-core/css/system-classes.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* String value "ns-" used for CSS system class prefix.
|
||||
*/
|
||||
* String value "ns-" used for CSS system class prefix.
|
||||
*/
|
||||
export const CLASS_PREFIX: string;
|
||||
|
||||
/**
|
||||
@@ -28,7 +28,7 @@ export function pushToSystemCssClasses(value: string): number;
|
||||
/**
|
||||
* Removes value from the list of current system classes
|
||||
* Intended for internal use only
|
||||
* @param value
|
||||
* @param value
|
||||
*/
|
||||
export function removeSystemCssClass(value: string): string;
|
||||
|
||||
@@ -58,4 +58,4 @@ export function pushToRootViewCssClasses(value: string): number;
|
||||
* @deprecated Use removeCssClass() instead
|
||||
* @param value
|
||||
*/
|
||||
export function removeFromRootViewCssClasses(value: string): string;
|
||||
export function removeFromRootViewCssClasses(value: string): string;
|
||||
|
||||
@@ -7,38 +7,38 @@ export const MODAL_ROOT_VIEW_CSS_CLASS = `${CLASS_PREFIX}${MODAL}`;
|
||||
export const ROOT_VIEW_CSS_CLASS = `${CLASS_PREFIX}${ROOT}`;
|
||||
|
||||
export function getSystemCssClasses(): string[] {
|
||||
return cssClasses;
|
||||
return cssClasses;
|
||||
}
|
||||
|
||||
export function pushToSystemCssClasses(value: string): number {
|
||||
cssClasses.push(value);
|
||||
cssClasses.push(value);
|
||||
|
||||
return cssClasses.length;
|
||||
return cssClasses.length;
|
||||
}
|
||||
|
||||
export function removeSystemCssClass(value: string): string {
|
||||
const index = cssClasses.indexOf(value);
|
||||
let removedElement;
|
||||
const index = cssClasses.indexOf(value);
|
||||
let removedElement;
|
||||
|
||||
if (index > -1) {
|
||||
removedElement = cssClasses.splice(index, 1);
|
||||
}
|
||||
if (index > -1) {
|
||||
removedElement = cssClasses.splice(index, 1);
|
||||
}
|
||||
|
||||
return removedElement;
|
||||
return removedElement;
|
||||
}
|
||||
|
||||
export function getModalRootViewCssClass(): string {
|
||||
return MODAL_ROOT_VIEW_CSS_CLASS;
|
||||
return MODAL_ROOT_VIEW_CSS_CLASS;
|
||||
}
|
||||
|
||||
export function getRootViewCssClasses(): string[] {
|
||||
return [ROOT_VIEW_CSS_CLASS, ...cssClasses];
|
||||
return [ROOT_VIEW_CSS_CLASS, ...cssClasses];
|
||||
}
|
||||
|
||||
export function pushToRootViewCssClasses(value: string): number {
|
||||
return pushToSystemCssClasses(value) + 1; // because of ROOT_VIEW_CSS_CLASS
|
||||
return pushToSystemCssClasses(value) + 1; // because of ROOT_VIEW_CSS_CLASS
|
||||
}
|
||||
|
||||
export function removeFromRootViewCssClasses(value: string): string {
|
||||
return removeSystemCssClass(value);
|
||||
}
|
||||
return removeSystemCssClass(value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user