How to use the css-tree.translate 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 alexjlockwood / ShapeShifter / src / app / scripts / svgo / plugins / inlineStyles.ts View on Github external
// filter for mediaqueries to be used or without any mediaquery
  var selectorsMq = cssTools.filterByMqs(selectors, opts.useMqs);

  // filter for pseudo elements to be used
  var selectorsPseudo = cssTools.filterByPseudos(selectorsMq, opts.usePseudos);

  // remove PseudoClass from its SimpleSelector for proper matching
  cssTools.cleanPseudos(selectorsPseudo);

  // stable sort selectors
  var sortedSelectors = cssTools.sortSelectors(selectorsPseudo).reverse();

  // apply <style> styles to matched elements
  for (var selector of sortedSelectors) {
    var selectorStr = csstree.translate(selector.item.data),
      selectedEls = null;

    try {
      selectedEls = document.querySelectorAll(selectorStr);
    } catch (selectError) {
      if (selectError.constructor === SyntaxError) {
        console.warn(
          'Warning: Syntax error when trying to select \n\n' +
            selectorStr +
            '\n\n, skipped. Error details: ' +
            selectError,
        );
        continue;
      }
      throw selectError;
    }</style>
github peterbe / minimalcss / hack.js View on Github external
Object.keys(cleaned).forEach(url => {
    // console.log('For URL', url);
    const obj = cleaned[url]
    const cleanedAst = csstree.fromPlainObject(obj)
    const cleanedCSS = csstree.translate(cleanedAst)
    console.log(cleanedCSS)
  })
github openstax / css-coverage.js / src / runCoverage.js View on Github external
rule.block.children.each(declaration => {
          if (ignoreDeclarations && ignoreDeclarations.indexOf(declaration.property.toLowerCase()) >= 0) {
            return // skip because it is ignored
          }
          // Append to a list of locations
          const key = cssTree.translate(declaration)
          let locs = cssDeclarations[key]
          locs = locs || []
          locs.push(declaration.loc)
          cssDeclarations[key] = locs
        })
        return cssTree.translate(selector)
github openstax / css-coverage.js / src / runCoverage.js View on Github external
const converted = rule.prelude.children.map((selector) => {
        rule.block.children.each(declaration => {
          if (ignoreDeclarations && ignoreDeclarations.indexOf(declaration.property.toLowerCase()) >= 0) {
            return // skip because it is ignored
          }
          // Append to a list of locations
          const key = cssTree.translate(declaration)
          let locs = cssDeclarations[key]
          locs = locs || []
          locs.push(declaration.loc)
          cssDeclarations[key] = locs
        })
        return cssTree.translate(selector)
      })
      cssRules.push(converted)
github pocketjoso / penthouse / test / util / normaliseCssAst.js View on Github external
export default function normaliseCssAst (css) {
  return parseCssAst(
    csstree.translate(
      parseCssAst(css)
    )
  )
}
github alexjlockwood / ShapeShifter / src / app / scripts / svgo / lib / css-tools.ts View on Github external
return selectors.filter(function(selector) {
    var pseudoSelectorsStr = csstree.translate({
      type: 'Selector',
      children: new List().fromArray(
        selector.pseudos.map(function(pseudo) {
          return pseudo.item.data;
        }),
      ),
    });
    return ~usePseudos.indexOf(pseudoSelectorsStr);
  });
}
github alexjlockwood / ShapeShifter / src / app / scripts / svgo / lib / css-tools.ts View on Github external
export function csstreeToStyleDeclaration(declaration) {
  var propertyName = declaration.property,
    propertyValue = csstree.translate(declaration.value),
    propertyPriority = declaration.important ? 'important' : '';
  return {
    name: propertyName,
    value: propertyValue,
    priority: propertyPriority,
  };
}
github alexjlockwood / ShapeShifter / src / app / scripts / svgo / lib / css-tools.ts View on Github external
return selectors.filter(function(selector) {
    if (selector.atrule === null) {
      return ~useMqs.indexOf('');
    }

    var mqName = selector.atrule.name;
    var mqStr = mqName;
    if (selector.atrule.expression.type === 'MediaQueryList') {
      var mqExpr = csstree.translate(selector.atrule.expression);
      mqStr = [mqName, mqExpr].join(' ');
    }

    return ~useMqs.indexOf(mqStr);
  });
}
github thysultan / stylis.js / benchmark / index.js View on Github external
function benchCSSTree () {
	return csstree.translate(csstree.parse(test));
}