How to use the css-tree.parse function in css-tree

To help you get started, we’ve selected a few css-tree examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SphinxKnight / compat-tester / src / css / compat-analyzer.js View on Github external
exports.analyzeString = function analyzeString (text, browserScope, lineShift = 0, fileName, callback, options = {"contrib":null}){
    const report = [];
    const ast = cssTree.parse(text,{positions:true});
    cssTree.walk(ast,(node) => {
        if(node.type === "Declaration"){
            if(node.property in bcd.css.properties){
                Object.keys(browserScope).map((browser)=>{
                    const supportBrowser = bcd.css.properties[node.property].__compat.support[browser];
                    let versionAddedProp;
                    if(Array.isArray(supportBrowser)){
                        // E.g. CSS property with prefixes
                        versionAddedProp = supportBrowser[0].version_added;
                    } else if(supportBrowser) {
                        versionAddedProp = supportBrowser.version_added;
                    }
                    if((versionAddedProp !== null) && ((!versionAddedProp) || (versionAddedProp !== true && semver.lt(semver.coerce(browserScope[browser]), semver.coerce(versionAddedProp)) ))){
                        report.push({
                            "featureName": "Property: " + node.property,
                            "browser":browser,
github pocketjoso / penthouse / test / basic-tests.js View on Github external
.then(result => {
        const resultRules = csstree.toPlainObject(csstree.parse(result)).children
        const originalRules = csstree.toPlainObject(csstree.parse(originalCss)).children
        resultRules.should.have.length.lessThan(originalRules.length)
        // not be empty
      })
  })
github pocketjoso / penthouse / test / post-formatting-tests.js View on Github external
}
      @media all {
        body {
          pointer-events: null;
        }
      }
    `
    const propertiesToRemove = [
      '(.*)transition(.*)',
      'cursor',
      'pointer-events',
      '(-webkit-)?tap-highlight-color',
      '(.*)user-select'
    ]

    const ast = csstree.parse(originalCss)
    const beforeRemoval = countDeclarations(ast)

    unwantedPropertiesRemover(ast, propertiesToRemove)

    beforeRemoval.should.eql(8)
    countDeclarations(ast).should.eql(0)
  })
github acupofspirt / unbemify / lib / parsers / styleParser.js View on Github external
function styleParser (styleFile) {
  const ast = csstree.parse(styleFile, {
          parseValue: false,
          onParseError ({formattedMessage}) {
            logger.error(formattedMessage)
          }
        }),
        selectorsArr = []
  let selectorsSet = new Set()

  csstree.walk(ast, {
    visit: 'ClassSelector',
    enter (node) {
      selectorsSet.add(node.name)
    }
  })

  for (const item of selectorsSet) {
github yarnaimo / vanilla-clipper / src / utils / css.ts View on Github external
(async () => {
                    const { body } = await got.get(url).catch(() => ({ body: '' }))
                    const parsed = csstree.parse(body)

                    if (parsed.type !== 'StyleSheet') {
                        return
                    }

                    await replaceImports(parsed, url)

                    list.replace(item, parsed.children)
                })()
            )
github GNS3 / gns3-web-ui / src / app / cartography / converters / styles-to-font-converter.ts View on Github external
convert(styles: string) {
    const font: Font = {
      font_family: undefined,
      font_size: undefined,
      font_weight: undefined
    };

    const ast = csstree.parse(styles, {
      context: 'declarationList'
    });

    ast.children.forEach(child => {
      if (child.property === 'font-size') {
        child.value.children.forEach(value => {
          if (value.type === 'Dimension') {
            font.font_size = parseInt(value.value);
          }
        });
      }
      if (child.property === 'font-family') {
        child.value.children.forEach(value => {
          if (value.type === 'Identifier') {
            font.font_family = value.name;
          }
github peterbe / minimalcss / src / utils.js View on Github external
function getParentSelectors(selector) {
  if (!selector) return [];
  const parentSelectors = [];
  const selectorAst = csstree.parse(selector, { context: 'selector' });

  let generatedCSS;
  while (selectorAst.children.tail) {
    selectorAst.children.prevUntil(
      selectorAst.children.tail,
      (node, item, list) => {
        list.remove(item);
        return node.type === 'Combinator' || node.type === 'WhiteSpace';
      }
    );
    generatedCSS = csstree.generate(selectorAst);
    if (generatedCSS) {
      parentSelectors.push(generatedCSS);
    }
  }
  return parentSelectors.reverse();
github wookaoer / page-skeleton-core / src / skeletonCore.js View on Github external
const stylesheetAstArray = styles.map((style) => {
            const ast = parse(style, {
                parseValue: false,
                parseRulePrelude: false
            });
            return toPlainObject(ast)
        });
github Cherrison / CrackMinApp / nodejs / nodejs / wuWxss.js View on Github external
function transformCss(style){
		let ast=csstree.parse(style);
		csstree.walk(ast,function(node){
			if(node.type=="Comment"){//Change the comment because the limit of css-tree
				node.type="Raw";
				node.value="\n/*"+node.value+"*/\n";
			}
			if(node.type=="TypeSelector"){
				if(node.name.startsWith("wx-"))node.name=node.name.slice(3);
				else if(node.name=="body")node.name="page";
			}
			if(node.children){
				const removeType=["webkit","moz","ms","o"];
				let list={};
				node.children.each((son,item)=>{
					if(son.type=="Declaration"){
						if(list[son.property]){
							let a=item,b=list[son.property],x=son,y=b.data,ans=null;
github csstree / validator / lib / validate.js View on Github external
function validate(css, filename) {
    var errors = [];
    var ast = csstree.parse(css, {
        filename: filename,
        positions: true,
        onParseError: function(error) {
            errors.push(error);
        }
    });

    csstree.walk(ast, {
        visit: 'Declaration',
        enter: function(node) {
            var match = syntax.matchDeclaration(node);
            var error = match.error;

            if (error) {
                var message = error.rawMessage || error.message || error;