Skip to content

Commit

Permalink
refactor: cleanup and completion of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax committed Aug 29, 2020
1 parent e737e58 commit 1f8a364
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 523 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# cssauron-noeval
# cssauron

[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/gofunky/cssauron-noeval/build/master?style=for-the-badge)](https://github.com/gofunky/cssauron-noeval/actions)
[![Codecov](https://img.shields.io/codecov/c/github/gofunky/cssauron-noeval?style=for-the-badge)](https://codecov.io/gh/gofunky/cssauron-noeval)
[![Renovate Status](https://img.shields.io/badge/renovate-enabled-green?style=for-the-badge&logo=renovatebot&color=1a1f6c)](https://app.renovatebot.com/dashboard#github/gofunky/cssauron-noeval)
[![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/cssauron-noeval?style=for-the-badge)](https://libraries.io/npm/cssauron-noeval)
[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/cssauron-noeval?style=for-the-badge)](https://snyk.io/test/github/gofunky/cssauron-noeval)
[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/gofunky/cssauron/build/master?style=for-the-badge)](https://github.com/gofunky/cssauron/actions)
[![Codecov](https://img.shields.io/codecov/c/github/gofunky/cssauron?style=for-the-badge)](https://codecov.io/gh/gofunky/cssauron)
[![Renovate Status](https://img.shields.io/badge/renovate-enabled-green?style=for-the-badge&logo=renovatebot&color=1a1f6c)](https://app.renovatebot.com/dashboard#github/gofunky/cssauron)
[![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/@gofunky%2Fcssauron?style=for-the-badge)](https://libraries.io/npm/@gofunky%2Fcssauron)
[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@gofunky/cssauron?style=for-the-badge)](https://snyk.io/test/github/gofunky/cssauron)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-purple.svg?style=for-the-badge)](https://standardjs.com)
[![CodeFactor](https://www.codefactor.io/repository/github/gofunky/cssauron-noeval/badge?style=for-the-badge)](https://www.codefactor.io/repository/github/gofunky/cssauron-noeval)
[![node-current](https://img.shields.io/node/v/cssauron-noeval?style=for-the-badge)](https://www.npmjs.com/package/cssauron-noeval)
[![NPM version](https://img.shields.io/npm/v/cssauron-noeval.svg?style=for-the-badge)](https://www.npmjs.com/package/cssauron-noeval)
[![NPM Downloads](https://img.shields.io/npm/dm/cssauron-noeval?style=for-the-badge&color=ff69b4)](https://www.npmjs.com/package/cssauron-noeval)
[![GitHub License](https://img.shields.io/github/license/gofunky/cssauron-noeval.svg?style=for-the-badge)](https://github.com/gofunky/cssauron-noeval/blob/master/LICENSE)
[![GitHub last commit](https://img.shields.io/github/last-commit/gofunky/cssauron-noeval.svg?style=for-the-badge&color=9cf)](https://github.com/gofunky/cssauron-noeval/commits/master)
[![CodeFactor](https://www.codefactor.io/repository/github/gofunky/cssauron/badge?style=for-the-badge)](https://www.codefactor.io/repository/github/gofunky/cssauron)
[![node-current](https://img.shields.io/node/v/@gofunky/cssauron?style=for-the-badge)](https://www.npmjs.com/package/@gofunky/cssauron)
[![NPM version](https://img.shields.io/npm/v/@gofunky/cssauron.svg?style=for-the-badge)](https://www.npmjs.com/package/@gofunky/cssauron)
[![NPM Downloads](https://img.shields.io/npm/dm/@gofunky/cssauron?style=for-the-badge&color=ff69b4)](https://www.npmjs.com/package/@gofunky/cssauron)
[![GitHub License](https://img.shields.io/github/license/gofunky/cssauron.svg?style=for-the-badge)](https://github.com/gofunky/cssauron/blob/master/LICENSE)
[![GitHub last commit](https://img.shields.io/github/last-commit/gofunky/cssauron.svg?style=for-the-badge&color=9cf)](https://github.com/gofunky/cssauron/commits/master)

build a matching function in CSS for any nested object structure without eval

Expand Down
4 changes: 2 additions & 2 deletions lib/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSTransform } from './transform'
import { CssTransform } from './transform'
import { Selector } from './selectors'
import { Optionable, SelectorOptions } from './options'

Expand All @@ -10,7 +10,7 @@ export class CSSAuron extends Optionable {
parse = (selector) => {
const selectors = new Selector(this.options)

CSSTransform.create(selector)
CssTransform.create(selector)
.on('data', selectors.group)
.end()

Expand Down
16 changes: 8 additions & 8 deletions lib/selectors.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSTransform } from './transform'
import { CssTerms } from './terms'
import { Optionable } from './options'
import { CSSAuron } from './index'
import check from './check'
Expand Down Expand Up @@ -59,12 +59,12 @@ export class Selector extends Optionable {
}

group = (token) => {
if (token.type === CSSTransform.COMMA) {
if (token.type === CssTerms.COMMA) {
this.#items.unshift(this.#bits = [])
return
}

if (token.type === CSSTransform.OPERATION || token.type === CSSTransform.ANY_CHILD) {
if (token.type === CssTerms.OPERATION || token.type === CssTerms.ANY_CHILD) {
this.#bits.unshift(new Traversal(this.options, token.data))
this.#bits.unshift(check())
return
Expand All @@ -73,16 +73,16 @@ export class Selector extends Optionable {
this.#bits[0] = this.#bits[0] || check()
const crnt = this.#bits[0]

if (token.type === CSSTransform.SUBJECT) {
if (token.type === CssTerms.SUBJECT) {
crnt.subject = this.#items[0].subject = true
return
}

crnt.push(
token.type === CSSTransform.CLASS ? this._listContains(token.type, token.data)
: token.type === CSSTransform.ATTR ? this._attr(token)
: token.type === CSSTransform.PSEUDO_CLASS || token.type === CSSTransform.PSEUDO_PSEUDO ? this._pseudo(token)
: token.type === CSSTransform.STAR ? Boolean
token.type === CssTerms.CLASS ? this._listContains(token.type, token.data)
: token.type === CssTerms.ATTR ? this._attr(token)
: token.type === CssTerms.PSEUDO_CLASS || token.type === CssTerms.PSEUDO_PSEUDO ? this._pseudo(token)
: token.type === CssTerms.STAR ? Boolean
: this._matches(token.type, token.data)
)
}
Expand Down
18 changes: 18 additions & 0 deletions lib/terms.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const CssTerms = Object.freeze({
PSEUDO_START: 'pseudo-start',
ATTR_START: 'attr-start',
ANY_CHILD: 'any-child',
ATTR_COMP: 'attr-comp',
ATTR_END: 'attr-end',
PSEUDO_PSEUDO: '::',
PSEUDO_CLASS: ':',
READY: '(ready)',
OPERATION: 'op',
CLASS: 'class',
COMMA: 'comma',
ATTR: 'attr',
SUBJECT: '!',
TAG: 'tag',
STAR: '*',
ID: 'id'
})
114 changes: 49 additions & 65 deletions lib/transform.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import { Transform } from 'stream'
import { CssTerms } from './terms'

export class CSSTransform extends Transform {
static PSEUDO_START = 'pseudo-start'
static ATTR_START = 'attr-start'
static ANY_CHILD = 'any-child'
static ATTR_COMP = 'attr-comp'
static ATTR_END = 'attr-end'
static PSEUDO_PSEUDO = '::'
static PSEUDO_CLASS = ':'
static READY = '(ready)'
static OPERATION = 'op'
static CLASS = 'class'
static COMMA = 'comma'
static ATTR = 'attr'
static SUBJECT = '!'
static TAG = 'tag'
static STAR = '*'
static ID = 'id'

export class CssTransform extends Transform {
#escaped = false
#gathered = []
#state = CSSTransform.READY
#state = CssTerms.READY
#data = []
#idx = 0
#length
Expand All @@ -33,7 +17,7 @@ export class CSSTransform extends Transform {
#selector

static create (selector) {
return new CSSTransform(selector)
return new CssTransform(selector)
}

constructor (selector) {
Expand Down Expand Up @@ -65,34 +49,34 @@ export class CSSTransform extends Transform {

while (this.#idx < this.#length && (this.#c = this.#data[this.#idx++])) {
switch (this.#state) {
case CSSTransform.READY:
case CssTerms.READY:
this.stateReady()
break
case CSSTransform.ANY_CHILD:
case CssTerms.ANY_CHILD:
this.stateAnyChild()
break
case CSSTransform.OPERATION:
case CssTerms.OPERATION:
this.stateOp()
break
case CSSTransform.ATTR_START:
case CssTerms.ATTR_START:
this.stateAttrStart()
break
case CSSTransform.ATTR_COMP:
case CssTerms.ATTR_COMP:
this.stateAttrCompare()
break
case CSSTransform.ATTR_END:
case CssTerms.ATTR_END:
this.stateAttrEnd()
break
case CSSTransform.PSEUDO_CLASS:
case CSSTransform.PSEUDO_PSEUDO:
case CssTerms.PSEUDO_CLASS:
case CssTerms.PSEUDO_PSEUDO:
this.statePseudo()
break
case CSSTransform.PSEUDO_START:
case CssTerms.PSEUDO_START:
this.statePseudoStart()
break
case CSSTransform.ID:
case CSSTransform.TAG:
case CSSTransform.CLASS:
case CssTerms.ID:
case CssTerms.TAG:
case CssTerms.CLASS:
this.stateGather()
break
}
Expand All @@ -112,7 +96,7 @@ export class CSSTransform extends Transform {
}

this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
this.#idx--
}

Expand All @@ -123,35 +107,35 @@ export class CSSTransform extends Transform {

if (/[>+~]/.test(this.#c)) {
this.#idx--
this.#state = CSSTransform.OPERATION
this.#state = CssTerms.OPERATION
return
}

this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
this.#idx--
}

statePseudo () {
this.#rhs = this.#state
this.stateGather(true)

if (this.#state !== CSSTransform.READY) {
if (this.#state !== CssTerms.READY) {
return
}

if (this.#c === '(') {
this.#lhs = this.#gathered.join('')
this.#state = CSSTransform.PSEUDO_START
this.#state = CssTerms.PSEUDO_START
this.#gathered.length = 0
this.#depth = 1
this.#idx++
return
}

this.#state = CSSTransform.PSEUDO_CLASS
this.#state = CssTerms.PSEUDO_CLASS
this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
}

statePseudoStart () {
Expand Down Expand Up @@ -194,46 +178,46 @@ export class CSSTransform extends Transform {
data: this.#lhs + '(' + this.#gathered.join('') + ')'
})

this.#state = CSSTransform.READY
this.#state = CssTerms.READY
this.#lhs = this.#rhs = this.#cmp = null
this.#gathered.length = 0
}
}

subject () {
this.#state = CSSTransform.SUBJECT
this.#gathered = [CSSTransform.SUBJECT]
this.#state = CssTerms.SUBJECT
this.#gathered = [CssTerms.SUBJECT]
this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
}

star () {
this.#state = CSSTransform.STAR
this.#gathered = [CSSTransform.STAR]
this.#state = CssTerms.STAR
this.#gathered = [CssTerms.STAR]
this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
}

comma () {
this.#state = CSSTransform.COMMA
this.#gathered = [CSSTransform.COMMA]
this.#state = CssTerms.COMMA
this.#gathered = [CssTerms.COMMA]
this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
}

stateReady () {
switch (true) {
case this.#c === '#':
this.#state = CSSTransform.ID
this.#state = CssTerms.ID
break
case this.#c === '.':
this.#state = CSSTransform.CLASS
this.#state = CssTerms.CLASS
break
case this.#c === ':':
this.#state = CSSTransform.PSEUDO_CLASS
this.#state = CssTerms.PSEUDO_CLASS
break
case this.#c === '[':
this.#state = CSSTransform.ATTR_START
this.#state = CssTerms.ATTR_START
break
case this.#c === '!':
this.subject()
Expand All @@ -245,13 +229,13 @@ export class CSSTransform extends Transform {
this.comma()
break
case /[>+~]/.test(this.#c):
this.#state = CSSTransform.OPERATION
this.#state = CssTerms.OPERATION
break
case /\s/.test(this.#c):
this.#state = CSSTransform.ANY_CHILD
this.#state = CssTerms.ANY_CHILD
break
case /[\w\d\-_]/.test(this.#c):
this.#state = CSSTransform.TAG
this.#state = CssTerms.TAG
this.#idx--
break
}
Expand All @@ -260,20 +244,20 @@ export class CSSTransform extends Transform {
stateAttrStart () {
this.stateGather(true)

if (this.#state !== CSSTransform.READY) {
if (this.#state !== CssTerms.READY) {
return
}

if (this.#c === ']') {
this.#state = CSSTransform.ATTR
this.#state = CssTerms.ATTR
this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
return
}

this.#lhs = this.#gathered.join('')
this.#gathered.length = 0
this.#state = CSSTransform.ATTR_COMP
this.#state = CssTerms.ATTR_COMP
}

stateAttrCompare () {
Expand All @@ -284,7 +268,7 @@ export class CSSTransform extends Transform {
if (this.#gathered.length === 2 || this.#c === '=') {
this.#cmp = this.#gathered.join('')
this.#gathered.length = 0
this.#state = CSSTransform.ATTR_END
this.#state = CssTerms.ATTR_END
this.#quote = null
}
}
Expand Down Expand Up @@ -319,20 +303,20 @@ export class CSSTransform extends Transform {

this.stateGather(true)

if (this.#state !== CSSTransform.READY) {
if (this.#state !== CssTerms.READY) {
return
}

this.push({
type: CSSTransform.ATTR,
type: CssTerms.ATTR,
data: {
lhs: this.#lhs,
rhs: this.#gathered.join(''),
cmp: this.#cmp
}
})

this.#state = CSSTransform.READY
this.#state = CssTerms.READY
this.#lhs = this.#rhs = this.#cmp = null
this.#gathered.length = 0
}
Expand All @@ -343,7 +327,7 @@ export class CSSTransform extends Transform {
this.#escaped = true
} else {
!quietly && this.emitToken()
this.#state = CSSTransform.READY
this.#state = CssTerms.READY
this.#idx--
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "node --experimental-modules --experimental-specifier-resolution=node test/index.mjs | tap-spec",
"coverage": "nyc --reporter=lcov node --experimental-modules --experimental-specifier-resolution=node test/index.mjs | tap-nyc",
"report": "nyc report --reporter=html",
"lint": "eslint",
"lint": "standard",
"publish": "npm-deploy-git-tag"
},
"repository": {
Expand Down
Loading

0 comments on commit 1f8a364

Please sign in to comment.