CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
index.mjs261 linesDownload Raw Back to esm
1// http://www.w3.org/TR/CSS21/grammar.html2// https://github.com/visionmedia/css-parse/pull/49#issuecomment-300880273var COMMENT_REGEX = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;4 5var NEWLINE_REGEX = /\n/g;6var WHITESPACE_REGEX = /^\s*/;7 8// declaration9var PROPERTY_REGEX = /^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/;10var COLON_REGEX = /^:\s*/;11var VALUE_REGEX = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/;12var SEMICOLON_REGEX = /^[;\s]*/;13 14// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill15var TRIM_REGEX = /^\s+|\s+$/g;16 17// strings18var NEWLINE = '\n';19var FORWARD_SLASH = '/';20var ASTERISK = '*';21var EMPTY_STRING = '';22 23// types24var TYPE_COMMENT = 'comment';25var TYPE_DECLARATION = 'declaration';26 27/**28 * @param {String} style29 * @param {Object} [options]30 * @return {Object[]}31 * @throws {TypeError}32 * @throws {Error}33 */34function index (style, options) {35  if (typeof style !== 'string') {36    throw new TypeError('First argument must be a string');37  }38 39  if (!style) return [];40 41  options = options || {};42 43  /**44   * Positional.45   */46  var lineno = 1;47  var column = 1;48 49  /**50   * Update lineno and column based on `str`.51   *52   * @param {String} str53   */54  function updatePosition(str) {55    var lines = str.match(NEWLINE_REGEX);56    if (lines) lineno += lines.length;57    var i = str.lastIndexOf(NEWLINE);58    column = ~i ? str.length - i : column + str.length;59  }60 61  /**62   * Mark position and patch `node.position`.63   *64   * @return {Function}65   */66  function position() {67    var start = { line: lineno, column: column };68    return function (node) {69      node.position = new Position(start);70      whitespace();71      return node;72    };73  }74 75  /**76   * Store position information for a node.77   *78   * @constructor79   * @property {Object} start80   * @property {Object} end81   * @property {undefined|String} source82   */83  function Position(start) {84    this.start = start;85    this.end = { line: lineno, column: column };86    this.source = options.source;87  }88 89  /**90   * Non-enumerable source string.91   */92  Position.prototype.content = style;93 94  /**95   * Error `msg`.96   *97   * @param {String} msg98   * @throws {Error}99   */100  function error(msg) {101    var err = new Error(102      options.source + ':' + lineno + ':' + column + ': ' + msg103    );104    err.reason = msg;105    err.filename = options.source;106    err.line = lineno;107    err.column = column;108    err.source = style;109 110    if (options.silent) ; else {111      throw err;112    }113  }114 115  /**116   * Match `re` and return captures.117   *118   * @param {RegExp} re119   * @return {undefined|Array}120   */121  function match(re) {122    var m = re.exec(style);123    if (!m) return;124    var str = m[0];125    updatePosition(str);126    style = style.slice(str.length);127    return m;128  }129 130  /**131   * Parse whitespace.132   */133  function whitespace() {134    match(WHITESPACE_REGEX);135  }136 137  /**138   * Parse comments.139   *140   * @param {Object[]} [rules]141   * @return {Object[]}142   */143  function comments(rules) {144    var c;145    rules = rules || [];146    while ((c = comment())) {147      if (c !== false) {148        rules.push(c);149      }150    }151    return rules;152  }153 154  /**155   * Parse comment.156   *157   * @return {Object}158   * @throws {Error}159   */160  function comment() {161    var pos = position();162    if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;163 164    var i = 2;165    while (166      EMPTY_STRING != style.charAt(i) &&167      (ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))168    ) {169      ++i;170    }171    i += 2;172 173    if (EMPTY_STRING === style.charAt(i - 1)) {174      return error('End of comment missing');175    }176 177    var str = style.slice(2, i - 2);178    column += 2;179    updatePosition(str);180    style = style.slice(i);181    column += 2;182 183    return pos({184      type: TYPE_COMMENT,185      comment: str186    });187  }188 189  /**190   * Parse declaration.191   *192   * @return {Object}193   * @throws {Error}194   */195  function declaration() {196    var pos = position();197 198    // prop199    var prop = match(PROPERTY_REGEX);200    if (!prop) return;201    comment();202 203    // :204    if (!match(COLON_REGEX)) return error("property missing ':'");205 206    // val207    var val = match(VALUE_REGEX);208 209    var ret = pos({210      type: TYPE_DECLARATION,211      property: trim(prop[0].replace(COMMENT_REGEX, EMPTY_STRING)),212      value: val213        ? trim(val[0].replace(COMMENT_REGEX, EMPTY_STRING))214        : EMPTY_STRING215    });216 217    // ;218    match(SEMICOLON_REGEX);219 220    return ret;221  }222 223  /**224   * Parse declarations.225   *226   * @return {Object[]}227   */228  function declarations() {229    var decls = [];230 231    comments(decls);232 233    // declarations234    var decl;235    while ((decl = declaration())) {236      if (decl !== false) {237        decls.push(decl);238        comments(decls);239      }240    }241 242    return decls;243  }244 245  whitespace();246  return declarations();247}248 249/**250 * Trim `str`.251 *252 * @param {String} str253 * @return {String}254 */255function trim(str) {256  return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;257}258 259export { index as default };260//# sourceMappingURL=index.mjs.map261