Example #1
0
		rules.forEach(function(rule, idx) {
			debug('%j', rule);

			switch(rule.type) {
				// {
				//  "type":"media"
				//  "media":"screen and (min-width: 1370px)",
				//  "rules":[{"type":"rule","selectors":["#foo"],"declarations":[]}]
				// }
				case 'media':
					this.emit('media', rule.media, rule.rules);

					// now run recursively to parse rules within the media query
					if (rule.rules) {
						this.parseRules(rule.rules);
					}

					this.emit('mediaEnd', rule.media, rule.rules);
					break;

				// {
				//  "type":"rule",
				//  "selectors":[".ui-header .ui-btn-up-a",".ui-header .ui-btn-hover-a"],
				//  "declarations":[{"type":"declaration","property":"border","value":"0"},{"type":"declaration","property":"background","value":"none"}]
				// }
				case 'rule':
					this.emit('rule', rule);

					if (!rule.selectors || !rule.declarations) {
						return;
					}

					// analyze each selector and declaration
					rule.selectors.forEach(function(selector) {
						var parsedSelector,
							expressions = [],
							i, len;

						// "#features > div:first-child" will become two expressions:
						//  {"combinator":" ","tag":"*","id":"features"}
						//  {"combinator":">","tag":"div","pseudos":[{"key":"first-child","value":null}]}
						parsedSelector = slickParse(selector)[0];

						// convert object with keys to array with numeric index
						for (i=0, len=parsedSelector.length; i<len; i++) {
							expressions.push(parsedSelector[i]);
						}

						this.emit('selector', rule, selector, expressions);

						expressions.forEach(function(expression) {
							this.emit('expression', selector, expression);
						}, this);
					}, this);

					rule.declarations.forEach(function(declaration) {
						switch(declaration.type) {
							case 'declaration':
								this.emit('declaration', rule, declaration.property, declaration.value);
								break;

							case 'comment':
								this.emit('comment', declaration.comment);
								break;
						}
					}, this);
					break;

				// {"type":"comment","comment":" Cached as static-css-r518-9b0f5ab4632defb55d67a1d672aa31bd120f4414 "}
				case 'comment':
					this.emit('comment', rule.comment);
					break;

				// {"type":"import","import":"url('/css/styles.css')"}
				case 'import':
					this.emit('import', rule.import);
					break;
			}
		}, this);
Example #2
0
		rules.forEach(function(rule, idx) {
			debug('%j', rule);

			// store the default current position
			//
			// it will be used when this.addOffender is called from within the rule
			// it can be overridden by providing a "custom" position via a call to this.setCurrentPosition
			this.setCurrentPosition(rule.position);

			switch (rule.type) {
				// {
				//  "type":"media"
				//  "media":"screen and (min-width: 1370px)",
				//  "rules":[{"type":"rule","selectors":["#foo"],"declarations":[]}]
				// }
				case 'media':
					this.emit('media', rule.media, rule.rules);

					// now run recursively to parse rules within the media query
					if (rule.rules) {
						this.parseRules(rule.rules);
					}

					this.emit('mediaEnd', rule.media, rule.rules);
					break;

					// {
					//  "type":"rule",
					//  "selectors":[".ui-header .ui-btn-up-a",".ui-header .ui-btn-hover-a"],
					//  "declarations":[{"type":"declaration","property":"border","value":"0"},{"type":"declaration","property":"background","value":"none"}]
					// }
				case 'rule':
					if (!rule.selectors || !rule.declarations) {
						return;
					}

					this.emit('rule', rule);

					// analyze each selector and declaration
					rule.selectors.forEach(function(selector) {
						var parsedSelector,
							expressions = [],
							i, len;

						// "#features > div:first-child" will become two expressions:
						//  {"combinator":" ","tag":"*","id":"features"}
						//  {"combinator":">","tag":"div","pseudos":[{"key":"first-child","value":null}]}
						parsedSelector = slickParse(selector)[0];

						if (typeof parsedSelector === 'undefined') {
							var positionDump = "Rule position start @ " + rule.position.start.line + ':' + rule.position.start.column + ", end @ " + rule.position.end.line + ':' + rule.position.end.column;
							throw this.error('Unable to parse "' + selector + '" selector. ' + positionDump, analyzer.EXIT_PARSING_FAILED);
						}

						// convert object with keys to array with numeric index
						for (i = 0, len = parsedSelector.length; i < len; i++) {
							expressions.push(parsedSelector[i]);
						}

						this.emit('selector', rule, selector, expressions);

						expressions.forEach(function(expression) {
							this.emit('expression', selector, expression);
						}, this);
					}, this);

					rule.declarations.forEach(function(declaration) {
						this.setCurrentPosition(declaration.position);

						switch (declaration.type) {
							case 'declaration':
								this.emit('declaration', rule, declaration.property, declaration.value);
								break;

							case 'comment':
								this.emit('comment', declaration.comment);
								break;
						}
					}, this);
					break;

					// {"type":"comment","comment":" Cached as static-css-r518-9b0f5ab4632defb55d67a1d672aa31bd120f4414 "}
				case 'comment':
					this.emit('comment', rule.comment);
					break;

					// {"type":"font-face","declarations":[{"type":"declaration","property":"font-family","value":"myFont"...
				case 'font-face':
					this.emit('font-face', rule);
					break;

					// {"type":"import","import":"url('/css/styles.css')"}
				case 'import':
					this.emit('import', rule.import);
					break;
			}
		}, this);