UNPKG

sass-formatter

Version:
186 lines (185 loc) 7.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.escapeRegExp = escapeRegExp; exports.isVar = isVar; exports.isAtImport = isAtImport; exports.isStar = isStar; exports.isCssSelector = isCssSelector; exports.isClassOrId = isClassOrId; exports.isSelectorOperator = isSelectorOperator; exports.isAdjacentSelector = isAdjacentSelector; exports.isEmptyOrWhitespace = isEmptyOrWhitespace; exports.isProperty = isProperty; exports.isAnd = isAnd; exports.isAtExtend = isAtExtend; exports.isInclude = isInclude; exports.isKeyframes = isKeyframes; exports.isPseudo = isPseudo; exports.isBracketSelector = isBracketSelector; exports.isHtmlTag = isHtmlTag; exports.isVoidHtmlTag = isVoidHtmlTag; exports.isReset = isReset; exports.isIgnore = isIgnore; exports.isSassSpace = isSassSpace; exports.isScssOrCss = isScssOrCss; exports.isCssPseudo = isCssPseudo; exports.isCssOneLiner = isCssOneLiner; exports.isComment = isComment; exports.isBlockCommentStart = isBlockCommentStart; exports.isBlockCommentEnd = isBlockCommentEnd; exports.isMoreThanOneClassOrId = isMoreThanOneClassOrId; exports.isBracketOrWhitespace = isBracketOrWhitespace; exports.isAtForwardOrAtUse = isAtForwardOrAtUse; exports.isInterpolatedProperty = isInterpolatedProperty; exports.hasPropertyValueSpace = hasPropertyValueSpace; exports.getDistance = getDistance; function escapeRegExp(text) { return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); } /** Check whether text is a variable: `/^[\t ]*(\$|--)\S+[\t ]*:.*/ function isVar(text) { return /^[\t ]*(\$|--)\S+[\t ]*:.*/.test(text); } /** Check whether text @import: `/^[\t ]*@import/` */ function isAtImport(text) { return /^[\t ]*@import/.test(text); } /** Check whether text is a \*: `/^[\t ]*?\*\/` */ function isStar(text) { return /^[\t ]*?\*/.test(text); } /** Check whether text is a css selector: `/^[\t ]*[{}]?[\t ]*[#\.%@=]/` */ function isCssSelector(text) { return /^[\t ]*[{}]?[\t ]*[#\.%@=]/.test(text); } /**Check whether text is class, id or placeholder: `/^[\t ]*[#\.%]/` */ function isClassOrId(text) { return /^[\t ]*[#\.%]/.test(text); } /**Check whether text starts with one of [>\~]: `/^[\t ]*[>~]/` */ function isSelectorOperator(text) { return /^[\t ]*[>~]/.test(text); } /**`/^[\t ]*\+[\t ]+/` */ function isAdjacentSelector(text) { return /^[\t ]*\+[\t ]+/.test(text); } /**Check whether text is class, id or placeholder: `/^[\t ]*\r?\n?$/` */ function isEmptyOrWhitespace(text) { return /^[\t ]*\r?\n?$/.test(text); } /** Check whether text is a property: `^[\t ]*[\w\-]+[\t ]*:` */ function isProperty(text) { // if (empty) { // return !/^[\t ]*[\w\-]+ *: *\S+/.test(text); // } return /^[\t ]*[\w\-]+[\t ]*:/.test(text); } /** Check whether text starts with &: `/^[\t ]*&/` */ function isAnd(text) { return /^[\t ]*&/.test(text); } /** Check whether text is a extend: `/^[\t ]*@extend/` */ function isAtExtend(text) { return /^[\t ]*@extend/.test(text); } /** Check whether text is include mixin statement */ function isInclude(text) { return /^[\t ]*(@include|\+\w)/.test(text); } /** Check whether text is a @keyframes: `/^[\t ]*@keyframes/` */ function isKeyframes(text) { return /^[\t ]*@keyframes/.test(text); } /** Check whether text is a Pseudo selector: `/^[\t ]*\\?::?/`. */ function isPseudo(text) { return /^[\t ]*\\?::?/.test(text); } /** Check whether text is bracket selector: `/^[\t ]*\[[\w=\-*'' ]*\]/`*/ function isBracketSelector(text) { return /^[\t ]*\[[\w=\-*'' ]*\]/.test(text); } /** Check whether text starts with an html tag. */ function isHtmlTag(text) { return /^[\t ]*(a|abbr|address|area|article|aside|audio|b|base|bdi|bdo|blockquote|body|br|button|canvas|caption|cite|code|col|colgroup|data|datalist|dd|del|details|dfn|dialog|div|dl|dt|em|embed|fieldset|figcaption|figure|footer|form|h1|h2|h3|h4|h5|h6|head|header|hgroup|hr|html|i|iframe|img|picture|input|ins|kbd|keygen|label|legend|li|link|main|map|mark|menu|menuitem|meta|meter|nav|noscript|object|ol|optgroup|option|output|p|param|pre|progress|q|rb|rp|rt|rtc|ruby|s|samp|script|section|select|small|source|span|strong|style|sub|summary|sup|svg|table|tbody|td|template|textarea|tfoot|th|thead|time|title|tr|track|u|ul|var|video|wbr|path|circle|ellipse|line|polygon|polyline|rect|text|slot|h[1-6]?|([a-z]+\-[a-z]+[\t ]*(?!:)))((:|::|,|\.|#|\[)[\^:$#{}()\w\-\[\]='",\.# \t+\/>]*)?[ \t]*[&]?[ \t]*{?$/.test(text); } /** Check whether text starts with a self closing html tag. */ function isVoidHtmlTag(text) { return /^[\t ]*(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr|command|keygen|menuitem|path)((:|::|,|\.|#|\[)[:$#{}()\w\-\[\]='",\.# ]*)?$/.test(text); } /** Check whether text starts with //R: `/^[\t ]*\/?\/\/ *R *$/` */ function isReset(text) { return /^[\t ]*\/?\/\/ *R *$/.test(text); } /** Check whether text starts with //I: `/^[\t ]*\/?\/\/ *I *$/` */ function isIgnore(text) { return /^[\t ]*\/?\/\/ *I *$/.test(text); } /** Check whether text starts with //S: `/^[\t ]*\/?\/\/ *S *$/` */ function isSassSpace(text) { return /^[\t ]*\/?\/\/ *S *$/.test(text); } /** Returns true if the string has brackets or semicolons at the end, comments get ignored. */ function isScssOrCss(text) { // Check if has brackets at the end and ignore comments. return /[;\{\}][\t ]*(\/\/.*)?$/.test(text); } /** `/^[\t ]*[&.#%].*:/` */ function isCssPseudo(text) { return /^[\t ]*[&.#%].*:/.test(text); } /** `/^[\t ]*[&.#%][\w-]*(?!#)[\t ]*\{.*[;\}][\t ]*$/` */ function isCssOneLiner(text) { return /^[\t ]*[&.#%][\w-]*(?!#)[\t ]*\{.*[;\}][\t ]*$/.test(text); } /** `/^[\t ]*::?[\w\-]+\(.*\)/` */ // export function isPseudoWithParenthesis(text: string) { // return /^[\t ]*::?[\w\-]+\(.*\)/.test(text); // } /** `/^[\t ]*(\/\/|\/\*)/` */ function isComment(text) { return /^[\t ]*(\/\/|\/\*)/.test(text); } /** `/^[\t ]*(\/\*)/` */ function isBlockCommentStart(text) { return /^[\t ]*(\/\*)/.test(text); } /** `/[\t ]*(\*\/)/` */ function isBlockCommentEnd(text) { return /[\t ]*(\*\/)/.test(text); } /** `/^[\t ]*[\.#%].* ?, *[\.#%].*\/` */ function isMoreThanOneClassOrId(text) { return /^[\t ]*[\.#%].* ?, *[\.#%].*/.test(text); } /** `/^[\t ]*[}{]+[\t }{]*$/` */ function isBracketOrWhitespace(text) { return /^[\t ]*[}{]+[\t }{]*$/.test(text); } /** `/[\t ]*@forward|[\t ]*@use/` */ function isAtForwardOrAtUse(text) { return /[\t ]*@forward|[\t ]*@use/.test(text); } function isInterpolatedProperty(text) { return /^[\t ]*[\w-]*#\{.*?\}[\w-]*:(?!:)/.test(text); } function hasPropertyValueSpace(text) { return /^[\t ]*([\w ]+|[\w ]*#\{.*?\}[\w ]*): [^ ]/.test(text); } /** returns the distance between the beginning and the first char. */ function getDistance(text, tabSize) { let count = 0; for (let i = 0; i < text.length; i++) { const char = text[i]; if (char !== ' ' && char !== '\t') { break; } if (char === '\t') { count += tabSize; } else { count++; } } return count; }