Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active December 16, 2015 20:29
Show Gist options
  • Select an option

  • Save foo9/5493064 to your computer and use it in GitHub Desktop.

Select an option

Save foo9/5493064 to your computer and use it in GitHub Desktop.
class StyleSheet
constructor: () ->
@cssRule = null
parseCSS: (CSSText) ->
parser = new CSSParser()
@cssRule = parser.parse CSSText
return
# TODO:
styleNames: () ->
return
clear: () ->
@cssRule = null
return
# TODO:
getStyle: (styleName) ->
return
# TODO:
setStyle: (styleName, styleObject) ->
return
# TODO:
transform: (formatObject) ->
return
class CSSParser
parse: (CSSText) ->
rulePattern = /([^\}\s]+)\s*?\{\s*([^\}]*;)\s*\}/mg
declPattern = /\s*([^:]+)\s*\:\s*([^;]+);\s*$/mg
commentPattern = /\/\*[^\*\/]+\*\//mg
text = CSSText.replace(commentPattern, '')
rules = []
match = []
while (match = rulePattern.exec(text))
#console.log match[1]
#console.log match[2]
selector = new Selector(match[1])
declarations = []
m = []
while (m = declPattern.exec(match[2]))
#console.log m[1]
#console.log m[2]
declarations.push(new Declaration(m[1], m[2]))
rules.push(new CSSRule(selector, declarations))
# DEBUG:
for v in rules
v.toString()
return rules
class CSSRule
constructor: (@selector, @declarations) ->
toString: () ->
console.log "#{@selector.pattern} {"
for v in @declarations
console.log "\t#{v.property}: #{v.value};"
console.log "}"
class Selector
constructor: (@pattern) ->
class Declaration
constructor: (@property, @value) ->
text = '''
#fuga {
color: #fff;
letter-spacing: 1;
font-size: 10px;
}
/* piyo */
.hoge {
color: #abc;
font-weight: bold;
/* comment */
background: rgb(255, 127, 0);
}
'''
style = new StyleSheet()
style.parseCSS(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment