Skip to content

Commit d7af37c

Browse files
committed
Change regex form to replacement
1 parent bc80c84 commit d7af37c

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

parsercss.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* ParserCSS Class
33
*/
4-
class Parser {
4+
class Parser {
55

66
static get EMPTY() { return '' }
77

@@ -30,13 +30,19 @@
3030
static get REGEX_COMMENTS() { return /\/\*(\r|\n|.)*\*\//g }
3131

3232
static get REGEX_BRACKETS() { return /\(([^)]+)\)/ }
33+
34+
static REGEX_REPLACE(str, obj) {
35+
let re = new RegExp(Object.keys(obj).join('|'), 'g')
36+
return str.replace(re, i => obj[i])
37+
}
3338

3439
constructor(css) {
3540
this.css = css
3641
this.parseCSS()
3742

3843
delete this.rules
39-
return this
44+
delete this.query
45+
return this.blocks
4046
}
4147

4248
/**
@@ -45,45 +51,45 @@
4551
* @return object rule
4652
*/
4753
parseCSS(css = '') {
48-
let blocks = []
54+
this.blocks = []
4955
this.css = (css != Parser.EMPTY)? css:this.css
5056
this.blocksRule = this.clearCode().split(Parser.END_BLOCK)
5157
this.blocksRule.pop()
5258

5359
this.blocksRule.map(block => {
5460
[this.query, this.rules] = block.split(Parser.BEGIN_BLOCK)
5561

56-
blocks.push({
62+
this.blocks.push({
5763
query: this.query.trim(),
5864
selectors: this.parseCSSQuery(),
5965
rules: this.parseCSSBlock()
6066
})
6167
})
6268

63-
return blocks
69+
return this.blocks
6470
}
6571

6672
/**
67-
* get property and values of declarations
73+
* set property and values of declarations
6874
* @param string css
6975
* @return object rule
7076
*/
7177
parseCSSBlock(rules = '') {
72-
let blockRules = []
78+
this.blockRules = []
7379
this.rules = (rules != Parser.EMPTY)? rules : this.rules
7480
this.parseCSSRules()
7581
.split(Parser.SEMICOLON)
7682
.map(decl => {
7783
let [prop, value] = decl.split(Parser.DOTS).map(i => i.trim())
7884
if (prop != Parser.EMPTY && value != Parser.EMPTY)
79-
blockRules.push(new Rule(prop, this._replaceAll(value, Parser.DATA_URI_VALUES)))
85+
this.blockRules.push(new Rule(prop, Parser.REGEX_REPLACE(value, Parser.DATA_URI_VALUES)))
8086
})
8187

82-
return blockRules
88+
return this.blockRules
8389
}
8490

8591
/**
86-
* treat css selectors
92+
* parse css rules
8793
* @param string css rules
8894
* @return string
8995
*/
@@ -93,11 +99,17 @@
9399
.filter(d => d != Parser.EMPTY)
94100
.map(decl => {
95101
if (decl.includes(Parser.END_BRACKET_RULE)) {
96-
let p_key = Parser.REGEX_BRACKETS.exec(decl)[1]
97-
if (p_key.includes(Parser.DATA_URI)) {
98-
params[p_key] = this._replaceAll(p_key, Parser.DATA_URI_KEYS)
99-
decl = this._replaceAll(decl, params)
102+
let [p_keys, match, params, d] = [[], [], {}, decl]
103+
while (match = Parser.REGEX_BRACKETS.exec(d)) {
104+
p_keys.push(match[1])
105+
d = d.replace(match[1], '')
100106
}
107+
p_keys.map(p => {
108+
if (p.includes(Parser.DATA_URI)) {
109+
params[p] = Parser.REGEX_REPLACE(p, Parser.DATA_URI_KEYS)
110+
decl = Parser.REGEX_REPLACE(decl, params)
111+
}
112+
})
101113
}
102114

103115
return decl.trim()
@@ -123,16 +135,14 @@
123135
clearCode(css = '') {
124136
this.css = ((css != Parser.EMPTY)? css:this.css)
125137
.replace(Parser.REGEX_COMMENTS, Parser.EMPTY)
126-
// .replaceAll({' ':'', '\n': '', '\t': ''})
127-
return this.css
128-
}
129-
130-
_replaceAll(str, obj) {
131-
return str.replace((new RegExp(Object.keys(obj).join('|')), 'g'), i => obj[i])
138+
return Parser.REGEX_REPLACE(this.css, {' ':'', '\n': '', '\t': ''})
132139
}
133140
}
134141

135142

143+
/**
144+
* Rule Class
145+
*/
136146
class Rule {
137147

138148
/**
@@ -143,28 +153,28 @@ class Rule {
143153
*/
144154
constructor(prop, value) {
145155
this.prop = prop
146-
this.value = value
147-
this.details()
156+
this.value = this.set(value)
148157

149158
return this
150159
}
151160

152161
/**
153-
* get details of value rule
154-
*
162+
* set details of value rule
163+
* @return object
155164
*/
156-
details() {
165+
set(value) {
166+
this.value = value
157167
if (this.value.includes(Parser.END_BRACKET)) {
158168
let str = Parser.REGEX_BRACKETS.exec(this.value)[1]
159169
this.detail = {}
160170
this.detail.args = str.split(Parser.COMMA).map(p => p.trim())
161171
this.detail.func = this.value.trim().replace(`(${str})`, Parser.EMPTY)
162-
163-
return this.detail
164172
}
173+
return this.value
165174
}
166175
}
167176

177+
168178
window.onload = () => {
169179

170180
let cssText = document.styleSheets[0].ownerNode.innerText

0 commit comments

Comments
 (0)