UNPKG

css-kits

Version:

Parse css to javascript object. Support change class and id

69 lines (68 loc) 2.4 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./helpful"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.detectAllClassAndId = void 0; const helpful_1 = require("./helpful"); const latterOfNameRegEx = /^[a-zA-Z0-9\_\-]$/; function detectAllClassAndId(subject) { const results = []; const currentState = { startIndex: -1, type: null, }; const pushRange = (newSelectorType, index) => { if (currentState.type !== null) { if (currentState.startIndex < 0) { throw new Error('An unknown error !' + index); } results.push({ start: currentState.startIndex, end: index, }); } if (newSelectorType === null) { currentState.startIndex = -1; } else { currentState.startIndex = index; } currentState.type = newSelectorType; }; (0, helpful_1.styleForEach)({ subject, callbackFn: (e) => { switch (e.value) { case '.': { pushRange('class', e.index); return; } case '#': { pushRange('id', e.index); return; } default: { if (currentState.type === null) return; if (latterOfNameRegEx.test(e.value)) return; pushRange(null, e.index); return; } } }, }); if (currentState.type !== null) { pushRange(null, subject.length); } return results; } exports.detectAllClassAndId = detectAllClassAndId; });