-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsassijs.min.js
53 lines (52 loc) · 15.7 KB
/
sassijs.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Function.prototype.method=function(name,lambda){this.prototype[name]=lambda;return this;};Function.method('inherits',function(parent){var d={};var p=(this.prototype=new parent());this.method('_super',function _super(name){if(!(name in d)){d[name]=0;}
var f,r,t=d[name];var v=parent.prototype;if(t){while(t){v=v.constructor.prototype;t-=1;}
f=v[name];}else{f=p[name];if(f==this[name]){f=v[name];}}
d[name]+=1;r=f.apply(this,Array.prototype.slice.apply(arguments,[1]));d[name]-=1;return r;});return this;});Function.method('swiss',function(parent){for(var i=1;i<arguments.length;i++){var name=arguments[i];this.prototype[name]=parent.prototype[name];}
return this;});Function.prototype.bind=function(object){var method=this;var temp=function(){return method.apply(object,arguments);};return temp;}
String.method('toCamelCase',function(){if(this.length<1){return this;}
var newString='';var parts=this.split(/[^a-zA-Z0-9]/);for(var i=0;i<parts.length;i++){var part=parts[i];if(part.length>0){newString+=(part[0].toUpperCase()+part.slice(1));}}
return newString;});DummyPepperClass=function(){this.dummyAttr=null;};DummyPepperClass.method('getDummyAttr',function(){return this.dummyAttr;});DummyPepperClass.method('setDummyAttr',function(newValue){this.dummyAttr=newValue;return this;});Array.method('hasElement',function(element){for(var i=0;i<this.length;i++){if(element==this[i]){return true;}}
return false;});Sassijs=function(template){this.template=template;};Sassijs.method('getTree',function(){if(this.tree==null){this.determineTree();}
return this.tree;});Sassijs.method('getTemplate',function(){return this.template;});Sassijs.method('determineTree',function(){this.tree=new SassijsTree(this.getTemplate());});Sassijs.method('getStyleElement',function(){return'<style type="text/css"><!--\n'+this.getTree().getRoot().getCss().join('\n')+'\n--></style>';});Sassijs.method('writeToDocument',function(){var style=document.createElement('style');var definition=this.getTree().getRoot().getCss().join('\n');style.setAttribute("type","text/css");if(style.styleSheet){style.styleSheet.cssText=definition;}else{var text=document.createTextNode(definition);style.appendChild(text);}
document.getElementsByTagName('head')[0].appendChild(style);});Helper=function(){};Helper.method('handle_interpolation',function(string){});Helper.method('human_indentation',function(indentation,was){was=(was==true)?true:false;if(indentation.indexOf('\t')==-1){noun=' space';}else if(indentation.indexOf(' ')==-1){noun=' tab';}else{return"'"+indentation+(was?"' was":"'");}
singular=((indentation.length==1)?true:false);if(was){was=(singular?' was':' were');}else{was='';}
return(indentation.length.toString()+noun+(singular?'':'s')+was);});Sassijs.prototype.helper=new Helper();StringScanner=function(string){this.original=string;this.index=0;};StringScanner.method('advanceIndex',function(advance){this.index=this.index+advance;return this;});StringScanner.method('getCurrent',function(){return this.original.slice(this.index);});StringScanner.method('scan',function(pattern){var newIndex=this.getCurrent().search(pattern);if(newIndex>=0){var result=this.getCurrent().match(pattern);this.advanceIndex(newIndex+result.toString().length);return result.toString();}
return false;});StringScanner.method('scanIndex',function(pattern){var newIndex=this.getCurrent().search(pattern);if(newIndex>=0){var result=this.getCurrent().match(pattern);this.advanceIndex(newIndex+result.toString().length);return this.index;}
return false;});SassijError=function(newComment,newLineNumber,supressRaise){this.setComment(newComment==undefined?"Unspecified Error":newComment);this.setLineNumber(newLineNumber==undefined?"None":newLineNumber);if(supressRaise!=true){throw(this.toString());}}
SassijError.method('getComment',function(){return this.comment;});SassijError.method('getLineNumber',function(){return this.lineNumber;});SassijError.method('setComment',function(newValue){this.comment=newValue;return this;});SassijError.method('setLineNumber',function(newValue){this.lineNumber=newValue;return this;});SassijError.method('shout',function(){alert(this.toString());});SassijError.method('toString',function(){return(this.getComment()+": "+this.getLineNumber());});SassijEnvironment=function(){this.parent=null;this.vars=null;this.mixins=null;}
SassijsTreeNode=function(line,newOptions){this.children=[];if(newOptions){this.options=newOptions;}else{this.options={style:'none'};}
this.style=this.options.style;this.line=line;};SassijsTreeNode.method('getChildren',function(){return this.children;});SassijsTreeNode.method('getLine',function(){return this.line;});SassijsTreeNode.method('setChildren',function(newValue){this.children=newValue;return this;});SassijsTreeNode.method('appendChild',function(child){if(error=this.isInvalidChild(child)){throw(new SassijError(error,child.line));}
this.children.push(child);return this;});SassijsTreeNode.method('getLastChild',function(){return this.getChildren()[this.getChildren().length-1];});SassijsTreeNode.method('getSpecies',function(){return this.species;});SassijsTreeNode.method('toS',function(){var result="";for(child in this.getChildren()){if(child.isType(AttrNode)){throw(new SassijError('Attributes aren\'t allowed at the root of a document.',child.line));}else{result+=child.toS(1);}}
return result;});SassijsTreeNode.method('isInvalidChild',function(child){return false;});SassijsTreeNode.method('getCss',function(){var css='';var ruleChildren=[];var attributeChildren=[];for(var i=0;i<this.getChildren().length;i++){switch(this.getChildren()[i].getSpecies()){case'rule':ruleChildren=ruleChildren.concat(this.getChildren()[i].getCss());break;case'attribute':attributeChildren.push(this.getChildren()[i].getCss());break;default:break;}}
if(attributeChildren.length>0){ruleChildren.unshift("{ "+attributeChildren.join(' ')+" }");}
if(ruleChildren.length>0){for(var i=0;i<ruleChildren.length;i++){prefixRule=this.getLine().getSyntax();if(prefixRule.length>0){ruleChildren[i]=prefixRule+" "+ruleChildren[i];}}
return ruleChildren;}else{}});SassijsTreeNodeAttribute=function(line){this.children=[];this.species='attribute';this.line=line;this.regex=/^:([^\s=:]+)\s*(=?)(?:\s+|$)(.*)/;this.regexAlternate=/^([^\s=:]+)(\s*=|:)(?:\s+|$)(.*)/;};SassijsTreeNodeAttribute.inherits(SassijsTreeNode);SassijsTreeNodeAttribute.method('getParts',function(){if(this.parts!=null){return this.parts;}
this.parts=this.getLine().getSyntax().match(this.regex);if(this.parts==null){this.parts=this.getLine().getSyntax().match(this.regexAlternate);}
return this.parts;});SassijsTreeNodeAttribute.method('getKey',function(){if(this.key==null){this.key=this.getParts()[1];}
return this.key;});SassijsTreeNodeAttribute.method('getValue',function(){var expressionChar='!';if(this.value!=null){return this.value;}
if(this.getParts()[2]==expressionChar){}else{this.value=this.getParts()[3];}
return this.value;});SassijsTreeNodeAttribute.method('getCss',function(){return this.getKey()+": "+this.getValue()+";";});SassijsTreeNodeComment=function(line){this.children=[];this.species='comment';this.line=line;};SassijsTreeNodeComment.inherits(SassijsTreeNode);SassijsTreeNodeComment.method('getCss',function(){var css=this.getLine().getSyntax()+" */";return css;});SassijsTreeNodeDirective=function(line){this.children=[];this.species='directive';this.line=line;};SassijsTreeNodeDirective.inherits(SassijsTreeNode);SassijsTreeNodeDirective.method('getCss',function(){var directive=/^@([^\s=:]+)\s*(=?)(?:\s+|$)(.*)/;parts=this.getLine().getSyntax().match(directive);if(parts==null){}
var css='@'+parts[1]+' url('+parts[3]+'.css);';return css;});SassijsTreeNodeMixinDefinition=function(line){this.species='mixinDefinition';this.line=line;};SassijsTreeNodeMixinDefinition.inherits(SassijsTreeNode);SassijsTreeNodeMixinDefinition.method('getCss',function(){var css='';var children=this.getChildren();for(i=0;i<children.length;i++){css+=children[i].getCss();}
return css;});SassijsTreeNodeMixinInclude=function(){this.species='mixinInclude';};SassijsTreeNodeMixinInclude.inherits(SassijsTreeNode);SassijsTreeNodeRule=function(line){this.children=[];this.species='rule';this.line=line;};SassijsTreeNodeRule.inherits(SassijsTreeNode);SassijsTreeNodeVariable=function(line){this.children=[];this.species='variable';this.line=line;this.regex=/^!([^\s=:]+)\s*(=?)(?:\s+|$)(.*)/;};SassijsTreeNodeVariable.inherits(SassijsTreeNode);SassijsTreeNodeVariable.method('getParts',function(){if(this.parts!=null){return this.parts;}
this.parts=this.getLine().getSyntax().match(this.regex);if(this.parts==null){this.parts=this.getLine().getSyntax().match(this.regexAlternate);}
return this.parts;});SassijsTreeNodeVariable.method('getKey',function(){if(this.key==null){this.key=this.getParts()[1];}
return this.key;});SassijsTreeNodeVariable.method('getValue',function(){var expressionChar='!';if(this.value!=null){return this.value;}
if(this.getParts()[2]==expressionChar){}else{this.value=this.getParts()[3];}
return this.value;});SassijsExpressionUnit=function(string){this.unit=string;this.acceptableChars=['\\*','\\/','\\%','\\+','\\-','\\(','\\)','\\=\\=','\\!\\=','\\>\\=','\\<\\=','\\>','\\<'];this.sizeSpecies=['in','cm','pc','mm','pt','px'];this.sizeConversion_table=[[1,2.54,6,25.4,72,72],[null,1,2.36220473,10,28.3464567,28.3464567],[null,null,1,4.23333333,12,12],[null,null,null,1,2.83464567,2.83464567],[null,null,null,null,1,1],[null,null,null,null,null,1]];this.sizeConversionToPoints={in:72,cm:28.3464567,pc:12,mm:2.83464567,px:1,pt:1};this.termRegex=/([\d\.]+)([\w]*)/;this.termsAndOperatorsRegex=/[\d\.\w]+|\+|\*|\/|%|-|\(|\)|==|!=|>=|<=|>|</g;};SassijsExpressionUnit.method('getSpecies',function(sample){terms=sample.match(/(\w|\d|\.)+/g);firstTerm=terms[0];sizeRegex=new RegExp('(\d|\.)+'+this.sizeSpecies.join('|'));if(sizeRegex.exec(firstTerm)){return'size';}});SassijsExpressionUnit.method('getSizeInPoints',function(term){var unitAndSpecies=term.match(this.termRegex);if(!unitAndSpecies){return false;}
var unit=unitAndSpecies[1];var species=unitAndSpecies[2];return(unit*this.sizeConversionToPoints[species]);});SassijsExpressionUnit.method('getNormalizedSize',function(sample){var termsAndOperators=sample.match(this.termsAndOperatorsRegex);var normalized=[];for(var i=0;i<termsAndOperators.length;i++){var unitInPoints=this.getSizeInPoints(termsAndOperators[i]);if(unitInPoints){normalized.push(unitInPoints);}else{normalized.push(termsAndOperators[i]);}}
return normalized.join(' ');});SassijsExpression=function(string,variables){this.string=string;this.variables=variables;this.parts=[];this.identifiedParts=[];this.acceptableChars=['*','/','%','+','-','(',')','==','!=','>=','<=','>','<'];this.operators=['*','/','%','+','-'];this.first_operators=['*','/','%'];this.second_operators=['+','-'];};SassijsExpression.method('getExpression',function(){return this.string.slice(1,(this.string.length));});SassijsExpression.method('getValue',function(){var sassijsExpressionUnit=new SassijsExpressionUnit();var species=sassijsExpressionUnit.getSpecies(this.getExpression());switch(species){case'size':var expressionString=sassijsExpressionUnit.getNormalizedSize(this.getExpression());var points=eval(expressionString);return points+'pt';default:return this.getExpression();}});SassijsFile=function(url,async){this.loaded=false;this.template=null;this.fetch(url,async);}
SassijsFile.inherits(Sassijs);SassijsFile.method('isLoaded',function(){return this.loaded;});SassijsFile.method('fetch',function(url,async){if(async!=false){async=true;}
if(window.XMLHttpRequest){req=new XMLHttpRequest();}else if(window.ActiveXObject){req=new ActiveXObject("Microsoft.XMLHTTP");}
if((async)&&(req!=undefined)){var that=this;req.onreadystatechange=function(){if(req.readyState==4){if((req.status==200)||(req.status==0)){this.loaded=true;this.template=req.responseText;}else{this.loaded=false;}}};req.open("GET",url);req.send("");}else{var that=this;req.open("GET",url,false);req.send("");if(req.readyState==4){if((req.status==200)||(req.status==0)){this.loaded=true;this.template=req.responseText;}else{this.loaded=false;}}}});SassijsLine=function(string,tab,lineNumber){this.lineNumber=lineNumber;this.syntax='';this.tabCount=0;findTab=new RegExp("^"+tab);var potentialSyntax=string;while(result=findTab.exec(potentialSyntax)){potentialSyntax=RegExp.rightContext;this.tabCount++;}
this.syntax=string.replace(/[\s|\t]+/g,' ').replace(/^\s+|\s+$/g,'');}
SassijsLine.method('getSyntax',function(){return this.syntax;});SassijsLine.method('getTabCount',function(){return this.tabCount;});SassijsLine.method('getLineNumber',function(){return this.lineNumber;});SassijsLine.method('determineNode',function(){var attributeChar=':';var variableChar='!';var commentChar='/';var sassCommentChar='/';var cssCommentChar='*';var directiveChar='@';var escapeChar='\\';var mixinDefinitionChar='=';var mixinIncludeChar='+';var attributeAlternateMatcher=/^[^\s:]+\s*[=:](\s|$)/;switch(this.getSyntax()[0]){case attributeChar:if(this.getSyntax()[1]!=':'){return new SassijsTreeNodeAttribute(this);}else{return new SassijsTreeNodeRule(this);}
case variableChar:return new SassijsTreeNodeVariable(this);case commentChar:if(this.getSyntax()[1]==sassCommentChar){return null;}else if(this.getSyntax()[1]==cssCommentChar){return new SassijsTreeNodeComment(this);}else{return new SassijsTreeNodeRule(this);}
case directiveChar:return new SassijsTreeNodeDirective(this);case escapeChar:case mixinDefinitionChar:return new SassijsTreeNodeMixinDefinition(this);case mixinIncludeChar:return new SassijsTreeNodeMixinInclude(this);default:if(this.getSyntax().match(attributeAlternateMatcher)){return new SassijsTreeNodeAttribute(this);}else{return new SassijsTreeNodeRule(this);}}});SassijsTree=function(template){this.template=template.replace(/\r|\n|\r\n/g,"\n");this.determineTab();var rootLine=new SassijsLine('',this.getTab(),0);this.root=rootLine.determineNode();this.determineLines();this.determineNodes();}
SassijsTree.method('getTab',function(){return this.tab;});SassijsTree.method('setTab',function(newValue){this.tab=newValue;return this;});SassijsTree.method('getRoot',function(){return this.root;});SassijsTree.method('getLines',function(){return this.lines;});SassijsTree.method('getTemplate',function(){return this.template;});SassijsTree.method('getTemplateLines',function(){return this.getTemplate().split('\n');});SassijsTree.method('determineTab',function(){var lines=this.getTemplateLines();for(var i=0;i<lines.length;i++){var firstTab=lines[i].match(/^(\s|\t)+/);if(firstTab){this.setTab(firstTab[0]);break;}}});SassijsTree.method('determineLines',function(){this.lines=new Array();var lines=this.getTemplateLines();for(var i=0;i<lines.length;i++){this.lines.push(new SassijsLine(lines[i],this.getTab(),(i+1)));}});SassijsTree.method('determineNodes',function(){var previousTabCount=0;var previousNode=this.getRoot();for(var i=0;i<this.getLines().length;i++){var line=this.getLines()[i];if(line.getSyntax().length>0){var newNode=line.determineNode();if(newNode!=null){if(line.getTabCount()==previousTabCount){parent=this.getRoot();for(var j=0;j<previousTabCount;j++){var parent=parent.getLastChild();}
parent.appendChild(newNode);}
if(line.getTabCount()>previousTabCount){previousNode.appendChild(newNode);}
if(line.getTabCount()<previousTabCount){parent=this.getRoot();for(var j=0;j<line.getTabCount();j++){var parent=parent.getLastChild();}
parent.appendChild(newNode);}
previousTabCount=newNode.getLine().getTabCount();previousNode=newNode;}}}});SassijsAutoloader={getTemplates:function(){var sassLinks=[];var potentialSass=document.getElementsByTagName('link');for(var i=0;i<potentialSass.length;i++){if(potentialSass[i].getAttribute('type')=='text/sass'){sassLinks.push(potentialSass[i].getAttribute('href'));}}
return sassLinks;},run:function(){var sassLinks=SassijsAutoloader.getTemplates();for(var i=0;i<sassLinks.length;i++){sassijs=new SassijsFile(sassLinks[i],false);sassijs.writeToDocument();}}}
window.onload=function(){SassijsAutoloader.run();};