| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 15:55:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {describe, expect, it} from '../../../core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-07-21 11:41:25 -07:00
										 |  |  | import {CssBlockAst, CssBlockDefinitionRuleAst, CssBlockRuleAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssSelectorRuleAst, CssStyleSheetAst, CssStyleValueAst} from '../../src/css_parser/css_ast'; | 
					
						
							|  |  |  | import {BlockType, CssParseError, CssParser, CssToken, ParsedCssResult} from '../../src/css_parser/css_parser'; | 
					
						
							|  |  |  | import {ParseLocation} from '../../src/parse_util'; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  | export function assertTokens(tokens: CssToken[], valuesArr: string[]) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   for (let i = 0; i < tokens.length; i++) { | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     expect(tokens[i].strValue == valuesArr[i]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |   describe('CssParser', () => { | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |     function parse(css: string): ParsedCssResult { | 
					
						
							|  |  |  |       return new CssParser().parse(css, 'some-fake-css-file.css'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |     function makeAst(css: string): CssStyleSheetAst { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(css); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       if (errors.length > 0) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |         throw new Error(errors.map((error: CssParseError) => error.msg).join(', ')); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |       return output.ast; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |     it('should parse CSS into a stylesheet Ast', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = '.selector { prop: value123; }'; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(ast.rules.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							|  |  |  |       const selector = rule.selectors[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(selector.strValue).toEqual('.selector'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const block: CssBlockAst = rule.block; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(block.entries.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const definition = <CssDefinitionAst>block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(definition.property.strValue).toEqual('prop'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const value = <CssStyleValueAst>definition.value; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(value.tokens[0].strValue).toEqual('value123'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |     it('should parse multiple CSS selectors sharing the same set of styles', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         .class, #id, tag, [attr], key + value, * value, :-moz-any-link { | 
					
						
							|  |  |  |           prop: value123; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(ast.rules.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule.selectors.length).toBe(7); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const classRule = rule.selectors[0]; | 
					
						
							|  |  |  |       const idRule = rule.selectors[1]; | 
					
						
							|  |  |  |       const tagRule = rule.selectors[2]; | 
					
						
							|  |  |  |       const attrRule = rule.selectors[3]; | 
					
						
							|  |  |  |       const plusOpRule = rule.selectors[4]; | 
					
						
							|  |  |  |       const starOpRule = rule.selectors[5]; | 
					
						
							|  |  |  |       const mozRule = rule.selectors[6]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       assertTokens(classRule.selectorParts[0].tokens, ['.', 'class']); | 
					
						
							|  |  |  |       assertTokens(idRule.selectorParts[0].tokens, ['.', 'class']); | 
					
						
							|  |  |  |       assertTokens(attrRule.selectorParts[0].tokens, ['[', 'attr', ']']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       assertTokens(plusOpRule.selectorParts[0].tokens, ['key']); | 
					
						
							|  |  |  |       expect(plusOpRule.selectorParts[0].operator.strValue).toEqual('+'); | 
					
						
							|  |  |  |       assertTokens(plusOpRule.selectorParts[1].tokens, ['value']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       assertTokens(starOpRule.selectorParts[0].tokens, ['*']); | 
					
						
							|  |  |  |       assertTokens(starOpRule.selectorParts[1].tokens, ['value']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       assertTokens(mozRule.selectorParts[0].pseudoSelectors[0].tokens, [':', '-moz-any-link']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const style1 = <CssDefinitionAst>rule.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(style1.property.strValue).toEqual('prop'); | 
					
						
							|  |  |  |       assertTokens(style1.value.tokens, ['value123']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse keyframe rules', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @keyframes rotateMe { | 
					
						
							|  |  |  |           from { | 
					
						
							|  |  |  |             transform: rotate(-360deg); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           50% { | 
					
						
							|  |  |  |             transform: rotate(0deg); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           to { | 
					
						
							|  |  |  |             transform: rotate(360deg); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(ast.rules.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule = <CssKeyframeRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |       expect(rule.name !.strValue).toEqual('rotateMe'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const block = <CssBlockAst>rule.block; | 
					
						
							|  |  |  |       const fromRule = <CssKeyframeDefinitionAst>block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |       expect(fromRule.name !.strValue).toEqual('from'); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const fromStyle = <CssDefinitionAst>(<CssBlockAst>fromRule.block).entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(fromStyle.property.strValue).toEqual('transform'); | 
					
						
							|  |  |  |       assertTokens(fromStyle.value.tokens, ['rotate', '(', '-360', 'deg', ')']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const midRule = <CssKeyframeDefinitionAst>block.entries[1]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |       expect(midRule.name !.strValue).toEqual('50%'); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const midStyle = <CssDefinitionAst>(<CssBlockAst>midRule.block).entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(midStyle.property.strValue).toEqual('transform'); | 
					
						
							|  |  |  |       assertTokens(midStyle.value.tokens, ['rotate', '(', '0', 'deg', ')']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const toRule = <CssKeyframeDefinitionAst>block.entries[2]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |       expect(toRule.name !.strValue).toEqual('to'); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const toStyle = <CssDefinitionAst>(<CssBlockAst>toRule.block).entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(toStyle.property.strValue).toEqual('transform'); | 
					
						
							|  |  |  |       assertTokens(toStyle.value.tokens, ['rotate', '(', '360', 'deg', ')']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |     it('should parse media queries into a stylesheet Ast', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @media all and (max-width:100px) { | 
					
						
							|  |  |  |           .selector { | 
					
						
							|  |  |  |             prop: value123; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(ast.rules.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule = <CssMediaQueryRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |       assertTokens(rule.query.tokens, ['all', 'and', '(', 'max-width', ':', '100', 'px', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const block = <CssBlockAst>rule.block; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(block.entries.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule2 = <CssSelectorRuleAst>block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule2.selectors[0].strValue).toEqual('.selector'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const block2 = <CssBlockAst>rule2.block; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(block2.entries.length).toEqual(1); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse inline CSS values', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @import url('remote.css'); | 
					
						
							|  |  |  |         @charset "UTF-8"; | 
					
						
							|  |  |  |         @namespace ng url(http://angular.io/namespace/ng);
 | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const importRule = <CssInlineRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(importRule.type).toEqual(BlockType.Import); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       assertTokens(importRule.value.tokens, ['url', '(', 'remote', '.', 'css', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const charsetRule = <CssInlineRuleAst>ast.rules[1]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(charsetRule.type).toEqual(BlockType.Charset); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       assertTokens(charsetRule.value.tokens, ['UTF-8']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const namespaceRule = <CssInlineRuleAst>ast.rules[2]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(namespaceRule.type).toEqual(BlockType.Namespace); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       assertTokens( | 
					
						
							|  |  |  |           namespaceRule.value.tokens, ['ng', 'url', '(', 'http://angular.io/namespace/ng', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse CSS values that contain functions and leave the inner function data untokenized', | 
					
						
							|  |  |  |        () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         .class { | 
					
						
							|  |  |  |           background: url(matias.css); | 
					
						
							|  |  |  |           animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060); | 
					
						
							|  |  |  |           height: calc(100% - 50px); | 
					
						
							| 
									
										
										
										
											2016-03-13 10:56:20 +02:00
										 |  |  |           background-image: linear-gradient( 45deg, rgba(100, 0, 0, 0.5), black ); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |          expect(ast.rules.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const defs = (<CssSelectorRuleAst>ast.rules[0]).block.entries; | 
					
						
							| 
									
										
										
										
											2016-03-13 10:56:20 +02:00
										 |  |  |          expect(defs.length).toEqual(4); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |          assertTokens((<CssDefinitionAst>defs[0]).value.tokens, ['url', '(', 'matias.css', ')']); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          assertTokens( | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |              (<CssDefinitionAst>defs[1]).value.tokens, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              ['cubic-bezier', '(', '0.755, 0.050, 0.855, 0.060', ')']); | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |          assertTokens((<CssDefinitionAst>defs[2]).value.tokens, ['calc', '(', '100% - 50px', ')']); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          assertTokens( | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |              (<CssDefinitionAst>defs[3]).value.tokens, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              ['linear-gradient', '(', '45deg, rgba(100, 0, 0, 0.5), black', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse un-named block-level CSS values', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @font-face { | 
					
						
							|  |  |  |           font-family: "Matias"; | 
					
						
							|  |  |  |           font-weight: bold; | 
					
						
							|  |  |  |           src: url(font-face.ttf); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         @viewport { | 
					
						
							|  |  |  |           max-width: 100px; | 
					
						
							|  |  |  |           min-height: 1000px; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const fontFaceRule = <CssBlockRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(fontFaceRule.type).toEqual(BlockType.FontFace); | 
					
						
							|  |  |  |       expect(fontFaceRule.block.entries.length).toEqual(3); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const viewportRule = <CssBlockRuleAst>ast.rules[1]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(viewportRule.type).toEqual(BlockType.Viewport); | 
					
						
							|  |  |  |       expect(viewportRule.block.entries.length).toEqual(2); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse multiple levels of semicolons', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         ;;; | 
					
						
							|  |  |  |         @import url('something something') | 
					
						
							|  |  |  |         ;;;;;;;; | 
					
						
							|  |  |  |         ;;;;;;;; | 
					
						
							|  |  |  |         ;@font-face { | 
					
						
							|  |  |  |           ;src   :   url(font-face.ttf);;;;;;;; | 
					
						
							|  |  |  |           ;;;-webkit-animation:my-animation | 
					
						
							|  |  |  |         };;; | 
					
						
							|  |  |  |         @media all and (max-width:100px) | 
					
						
							|  |  |  |         {; | 
					
						
							|  |  |  |           .selector {prop: value123;}; | 
					
						
							|  |  |  |           ;.selector2{prop:1}} | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const importRule = <CssInlineRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(importRule.type).toEqual(BlockType.Import); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       assertTokens(importRule.value.tokens, ['url', '(', 'something something', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const fontFaceRule = <CssBlockRuleAst>ast.rules[1]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(fontFaceRule.type).toEqual(BlockType.FontFace); | 
					
						
							|  |  |  |       expect(fontFaceRule.block.entries.length).toEqual(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const mediaQueryRule = <CssMediaQueryRuleAst>ast.rules[2]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |       assertTokens( | 
					
						
							|  |  |  |           mediaQueryRule.query.tokens, ['all', 'and', '(', 'max-width', ':', '100', 'px', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(mediaQueryRule.block.entries.length).toEqual(2); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw an error if an unknown @value block rule is parsed', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @matias { hello: there; } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(() => { | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |         makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       }).toThrowError(/^CSS Parse Error: The CSS "at" rule "@matias" is not allowed to used here/g); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse empty rules', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         .empty-rule { } | 
					
						
							|  |  |  |         .somewhat-empty-rule { /* property: value; */ } | 
					
						
							|  |  |  |         .non-empty-rule { property: value; } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rules = ast.rules; | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:27 -07:00
										 |  |  |       expect((<CssSelectorRuleAst>rules[0]).block.entries.length).toEqual(0); | 
					
						
							|  |  |  |       expect((<CssSelectorRuleAst>rules[1]).block.entries.length).toEqual(0); | 
					
						
							|  |  |  |       expect((<CssSelectorRuleAst>rules[2]).block.entries.length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse the @document rule', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @document url(http://www.w3.org/),
 | 
					
						
							|  |  |  |                        url-prefix(http://www.w3.org/Style/),
 | 
					
						
							|  |  |  |                        domain(mozilla.org), | 
					
						
							|  |  |  |                        regexp("https:.*") | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           /* CSS rules here apply to: | 
					
						
							|  |  |  |              - The page "http://www.w3.org/". | 
					
						
							|  |  |  |              - Any page whose URL begins with "http://www.w3.org/Style/" | 
					
						
							|  |  |  |              - Any page whose URL's host is "mozilla.org" or ends with | 
					
						
							|  |  |  |                ".mozilla.org" | 
					
						
							|  |  |  |              - Any page whose URL starts with "https:" */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           /* make the above-mentioned pages really ugly */ | 
					
						
							|  |  |  |           body { | 
					
						
							|  |  |  |             color: purple; | 
					
						
							|  |  |  |             background: yellow; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rules = ast.rules; | 
					
						
							|  |  |  |       const documentRule = <CssBlockDefinitionRuleAst>rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(documentRule.type).toEqual(BlockType.Document); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule = <CssSelectorRuleAst>documentRule.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(rule.strValue).toEqual('body'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse the @page rule', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @page one { | 
					
						
							|  |  |  |           .selector { prop: value; } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         @page two { | 
					
						
							|  |  |  |           .selector2 { prop: value2; } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rules = ast.rules; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const pageRule1 = <CssBlockDefinitionRuleAst>rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |       expect(pageRule1.query.strValue).toEqual('@page one'); | 
					
						
							|  |  |  |       expect(pageRule1.query.tokens[0].strValue).toEqual('one'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(pageRule1.type).toEqual(BlockType.Page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const pageRule2 = <CssBlockDefinitionRuleAst>rules[1]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |       expect(pageRule2.query.strValue).toEqual('@page two'); | 
					
						
							|  |  |  |       expect(pageRule2.query.tokens[0].strValue).toEqual('two'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(pageRule2.type).toEqual(BlockType.Page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const selectorOne = <CssSelectorRuleAst>pageRule1.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(selectorOne.strValue).toEqual('.selector'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const selectorTwo = <CssSelectorRuleAst>pageRule2.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(selectorTwo.strValue).toEqual('.selector2'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse the @supports rule', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         @supports (animation-name: "rotate") { | 
					
						
							|  |  |  |           a:hover { animation: rotate 1s; } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = makeAst(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rules = ast.rules; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const supportsRule = <CssBlockDefinitionRuleAst>rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |       assertTokens(supportsRule.query.tokens, ['(', 'animation-name', ':', 'rotate', ')']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(supportsRule.type).toEqual(BlockType.Supports); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const selectorOne = <CssSelectorRuleAst>supportsRule.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(selectorOne.strValue).toEqual('a:hover'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should collect multiple errors during parsing', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         .class$value { something: something } | 
					
						
							|  |  |  |         @custom { something: something } | 
					
						
							|  |  |  |         #id { cool^: value } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(output.errors.length).toEqual(3); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should recover from selector errors and continue parsing', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         tag& { key: value; } | 
					
						
							|  |  |  |         .%tag { key: value; } | 
					
						
							|  |  |  |         #tag$ { key: value; } | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							|  |  |  |       const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors.length).toEqual(3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(ast.rules.length).toEqual(3); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule1 = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(rule1.selectors[0].strValue).toEqual('tag&'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule1.block.entries.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule2 = <CssSelectorRuleAst>ast.rules[1]; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(rule2.selectors[0].strValue).toEqual('.%tag'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule2.block.entries.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule3 = <CssSelectorRuleAst>ast.rules[2]; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(rule3.selectors[0].strValue).toEqual('#tag$'); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule3.block.entries.length).toEqual(1); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw an error when parsing invalid CSS Selectors', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = '.class[[prop%=value}] { style: val; }'; | 
					
						
							|  |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors.length).toEqual(3); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |       expect(errors[0].msg).toMatch(/Unexpected character \[\[\] at column 0:7/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |       expect(errors[1].msg).toMatch(/Unexpected character \[%\] at column 0:12/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |       expect(errors[2].msg).toMatch(/Unexpected character \[}\] at column 0:19/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw an error if an attribute selector is not closed properly', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = '.class[prop=value { style: val; }'; | 
					
						
							|  |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |       expect(errors[0].msg).toMatch(/Unbalanced CSS attribute selector at column 0:12/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw an error if a pseudo function selector is not closed properly', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = 'body:lang(en { key:value; }'; | 
					
						
							|  |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors[0].msg) | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |           .toMatch(/Character does not match expected Character value \("{" should match "\)"\)/); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should raise an error when a semi colon is missing from a CSS style/pair that isn\'t the last entry', | 
					
						
							|  |  |  |        () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const styles = `.class {
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         color: red | 
					
						
							|  |  |  |         background: blue | 
					
						
							|  |  |  |       }`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const output = parse(styles); | 
					
						
							|  |  |  |          const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |          expect(errors.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          expect(errors[0].msg) | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |              .toMatch(/The CSS key\/value definition did not end with a semicolon at column 1:15/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse the inner value of a :not() pseudo-selector as a CSS selector', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `div:not(.ignore-this-div) {
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         prop: value; | 
					
						
							|  |  |  |       }`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							|  |  |  |       const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors.length).toEqual(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule1 = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule1.selectors.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const simpleSelector = rule1.selectors[0].selectorParts[0]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |       assertTokens(simpleSelector.tokens, ['div']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const pseudoSelector = simpleSelector.pseudoSelectors[0]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |       expect(pseudoSelector.name).toEqual('not'); | 
					
						
							|  |  |  |       assertTokens(pseudoSelector.tokens, ['.', 'ignore-this-div']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse the inner selectors of a :host-context selector', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `body > :host-context(.a, .b, .c:hover) {
 | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |         prop: value; | 
					
						
							|  |  |  |       }`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							|  |  |  |       const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors.length).toEqual(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule1 = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |       expect(rule1.selectors.length).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const simpleSelector = rule1.selectors[0].selectorParts[1]; | 
					
						
							|  |  |  |       const innerSelectors = simpleSelector.pseudoSelectors[0].innerSelectors; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       assertTokens(innerSelectors[0].selectorParts[0].tokens, ['.', 'a']); | 
					
						
							|  |  |  |       assertTokens(innerSelectors[1].selectorParts[0].tokens, ['.', 'b']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const finalSelector = innerSelectors[2].selectorParts[0]; | 
					
						
							| 
									
										
										
										
											2016-06-14 16:26:57 -07:00
										 |  |  |       assertTokens(finalSelector.tokens, ['.', 'c', ':', 'hover']); | 
					
						
							|  |  |  |       assertTokens(finalSelector.pseudoSelectors[0].tokens, [':', 'hover']); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should raise parse errors when CSS key/value pairs are invalid', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `.class {
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         background color: value; | 
					
						
							|  |  |  |         color: value | 
					
						
							|  |  |  |         font-size; | 
					
						
							|  |  |  |         font-weight | 
					
						
							|  |  |  |       }`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors.length).toEqual(4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(errors[0].msg) | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |           .toMatch( | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |               /Identifier does not match expected Character value \("color" should match ":"\) at column 1:19/g); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(errors[1].msg) | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |           .toMatch(/The CSS key\/value definition did not end with a semicolon at column 2:15/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors[2].msg) | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |           .toMatch(/The CSS property was not paired with a style value at column 3:8/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(errors[3].msg) | 
					
						
							| 
									
										
										
										
											2016-06-22 14:53:02 -07:00
										 |  |  |           .toMatch(/The CSS property was not paired with a style value at column 4:8/g); | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should recover from CSS key/value parse errors', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |         .problem-class { background color: red; color: white; } | 
					
						
							|  |  |  |         .good-boy-class { background-color: red; color: white; } | 
					
						
							|  |  |  |        `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(ast.rules.length).toEqual(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const rule1 = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(rule1.block.entries.length).toEqual(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const style1 = <CssDefinitionAst>rule1.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(style1.property.strValue).toEqual('background color'); | 
					
						
							|  |  |  |       assertTokens(style1.value.tokens, ['red']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const style2 = <CssDefinitionAst>rule1.block.entries[1]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(style2.property.strValue).toEqual('color'); | 
					
						
							|  |  |  |       assertTokens(style2.value.tokens, ['white']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |     describe('location offsets', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       let styles: string; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       function assertMatchesOffsetAndChar( | 
					
						
							|  |  |  |           location: ParseLocation, expectedOffset: number, expectedChar: string): void { | 
					
						
							|  |  |  |         expect(location.offset).toEqual(expectedOffset); | 
					
						
							|  |  |  |         expect(styles[expectedOffset]).toEqual(expectedChar); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should collect the source span location of each AST node with regular selectors', () => { | 
					
						
							|  |  |  |         styles = '.problem-class { border-top-right: 1px; color: white; }\n'; | 
					
						
							|  |  |  |         styles += '#good-boy-rule_ { background-color: #fe4; color: teal; }'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const output = parse(styles); | 
					
						
							|  |  |  |         const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(ast.location.start, 0, '.'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(ast.location.end, 111, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const rule1 = <CssSelectorRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(rule1.location.start, 0, '.'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(rule1.location.end, 54, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const rule2 = <CssSelectorRuleAst>ast.rules[1]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(rule2.location.start, 56, '#'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(rule2.location.end, 111, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const selector1 = rule1.selectors[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(selector1.location.start, 0, '.'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(selector1.location.end, 1, 'p');  // problem-class
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const selector2 = rule2.selectors[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(selector2.location.start, 56, '#'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(selector2.location.end, 57, 'g');  // good-boy-rule_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block1 = rule1.block; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block1.location.start, 15, '{'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block1.location.end, 54, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block2 = rule2.block; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block2.location.start, 72, '{'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block2.location.end, 111, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block1def1 = <CssDefinitionAst>block1.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block1def1.location.start, 17, 'b');  // border-top-right
 | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block1def1.location.end, 36, 'p');    // px
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block1def2 = <CssDefinitionAst>block1.entries[1]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block1def2.location.start, 40, 'c');  // color
 | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block1def2.location.end, 47, 'w');    // white
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block2def1 = <CssDefinitionAst>block2.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block2def1.location.start, 74, 'b');  // background-color
 | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block2def1.location.end, 93, 'f');    // fe4
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block2def2 = <CssDefinitionAst>block2.entries[1]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block2def2.location.start, 98, 'c');  // color
 | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block2def2.location.end, 105, 't');   // teal
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block1value1 = block1def1.value; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block1value1.location.start, 35, '1'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block1value1.location.end, 36, 'p'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block1value2 = block1def2.value; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block1value2.location.start, 47, 'w'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block1value2.location.end, 47, 'w'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block2value1 = block2def1.value; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block2value1.location.start, 92, '#'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block2value1.location.end, 93, 'f'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const block2value2 = block2def2.value; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(block2value2.location.start, 105, 't'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(block2value2.location.end, 105, 't'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should collect the source span location of each AST node with media query data', () => { | 
					
						
							|  |  |  |         styles = '@media (all and max-width: 100px) { a { display:none; } }'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const output = parse(styles); | 
					
						
							|  |  |  |         const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const mediaQuery = <CssMediaQueryRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(mediaQuery.location.start, 0, '@'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(mediaQuery.location.end, 56, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const predicate = mediaQuery.query; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(predicate.location.start, 0, '@'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(predicate.location.end, 32, ')'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const rule = <CssSelectorRuleAst>mediaQuery.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(rule.location.start, 36, 'a'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(rule.location.end, 54, '}'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should collect the source span location of each AST node with keyframe data', () => { | 
					
						
							|  |  |  |         styles = '@keyframes rotateAndZoomOut { '; | 
					
						
							|  |  |  |         styles += 'from { transform: rotate(0deg); } '; | 
					
						
							|  |  |  |         styles += '100% { transform: rotate(360deg) scale(2); }'; | 
					
						
							|  |  |  |         styles += '}'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const output = parse(styles); | 
					
						
							|  |  |  |         const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const keyframes = <CssKeyframeRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(keyframes.location.start, 0, '@'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(keyframes.location.end, 108, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const step1 = <CssKeyframeDefinitionAst>keyframes.block.entries[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(step1.location.start, 30, 'f'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(step1.location.end, 62, '}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const step2 = <CssKeyframeDefinitionAst>keyframes.block.entries[1]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(step2.location.start, 64, '1'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(step2.location.end, 107, '}'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should collect the source span location of each AST node with an inline rule', () => { | 
					
						
							|  |  |  |         styles = '@import url(something.css)'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const output = parse(styles); | 
					
						
							|  |  |  |         const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const rule = <CssInlineRuleAst>ast.rules[0]; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(rule.location.start, 0, '@'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(rule.location.end, 25, ')'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const value = rule.value; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |         assertMatchesOffsetAndChar(value.location.start, 8, 'u'); | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(value.location.end, 25, ')'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should property collect the start/end locations with an invalid stylesheet', () => { | 
					
						
							|  |  |  |         styles = '#id { something: value'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const output = parse(styles); | 
					
						
							|  |  |  |         const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assertMatchesOffsetAndChar(ast.location.start, 0, '#'); | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |         assertMatchesOffsetAndChar(ast.location.end, 22, undefined !); | 
					
						
							| 
									
										
										
										
											2016-06-16 13:02:18 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |     it('should parse minified CSS content properly', () => { | 
					
						
							|  |  |  |       // this code was taken from the angular.io webpage's CSS code
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | .is-hidden{display:none!important} | 
					
						
							|  |  |  | .is-visible{display:block!important} | 
					
						
							|  |  |  | .is-visually-hidden{height:1px;width:1px;overflow:hidden;opacity:0.01;position:absolute;bottom:0;right:0;z-index:1} | 
					
						
							|  |  |  | .grid-fluid,.grid-fixed{margin:0 auto} | 
					
						
							|  |  |  | .grid-fluid .c1,.grid-fixed .c1,.grid-fluid .c2,.grid-fixed .c2,.grid-fluid .c3,.grid-fixed .c3,.grid-fluid .c4,.grid-fixed .c4,.grid-fluid .c5,.grid-fixed .c5,.grid-fluid .c6,.grid-fixed .c6,.grid-fluid .c7,.grid-fixed .c7,.grid-fluid .c8,.grid-fixed .c8,.grid-fluid .c9,.grid-fixed .c9,.grid-fluid .c10,.grid-fixed .c10,.grid-fluid .c11,.grid-fixed .c11,.grid-fluid .c12,.grid-fixed .c12{display:inline;float:left} | 
					
						
							|  |  |  | .grid-fluid .c1.grid-right,.grid-fixed .c1.grid-right,.grid-fluid .c2.grid-right,.grid-fixed .c2.grid-right,.grid-fluid .c3.grid-right,.grid-fixed .c3.grid-right,.grid-fluid .c4.grid-right,.grid-fixed .c4.grid-right,.grid-fluid .c5.grid-right,.grid-fixed .c5.grid-right,.grid-fluid .c6.grid-right,.grid-fixed .c6.grid-right,.grid-fluid .c7.grid-right,.grid-fixed .c7.grid-right,.grid-fluid .c8.grid-right,.grid-fixed .c8.grid-right,.grid-fluid .c9.grid-right,.grid-fixed .c9.grid-right,.grid-fluid .c10.grid-right,.grid-fixed .c10.grid-right,.grid-fluid .c11.grid-right,.grid-fixed .c11.grid-right,.grid-fluid .c12.grid-right,.grid-fixed .c12.grid-right{float:right} | 
					
						
							|  |  |  | .grid-fluid .c1.nb,.grid-fixed .c1.nb,.grid-fluid .c2.nb,.grid-fixed .c2.nb,.grid-fluid .c3.nb,.grid-fixed .c3.nb,.grid-fluid .c4.nb,.grid-fixed .c4.nb,.grid-fluid .c5.nb,.grid-fixed .c5.nb,.grid-fluid .c6.nb,.grid-fixed .c6.nb,.grid-fluid .c7.nb,.grid-fixed .c7.nb,.grid-fluid .c8.nb,.grid-fixed .c8.nb,.grid-fluid .c9.nb,.grid-fixed .c9.nb,.grid-fluid .c10.nb,.grid-fixed .c10.nb,.grid-fluid .c11.nb,.grid-fixed .c11.nb,.grid-fluid .c12.nb,.grid-fixed .c12.nb{margin-left:0} | 
					
						
							|  |  |  | .grid-fluid .c1.na,.grid-fixed .c1.na,.grid-fluid .c2.na,.grid-fixed .c2.na,.grid-fluid .c3.na,.grid-fixed .c3.na,.grid-fluid .c4.na,.grid-fixed .c4.na,.grid-fluid .c5.na,.grid-fixed .c5.na,.grid-fluid .c6.na,.grid-fixed .c6.na,.grid-fluid .c7.na,.grid-fixed .c7.na,.grid-fluid .c8.na,.grid-fixed .c8.na,.grid-fluid .c9.na,.grid-fixed .c9.na,.grid-fluid .c10.na,.grid-fixed .c10.na,.grid-fluid .c11.na,.grid-fixed .c11.na,.grid-fluid .c12.na,.grid-fixed .c12.na{margin-right:0} | 
					
						
							|  |  |  |        `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(errors.length).toEqual(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(ast.rules.length).toEqual(8); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse a snippet of keyframe code from animate.css properly', () => { | 
					
						
							|  |  |  |       // this code was taken from the angular.io webpage's CSS code
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styles = `
 | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  | @charset "UTF-8"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! | 
					
						
							|  |  |  |  * animate.css -http://daneden.me/animate
 | 
					
						
							|  |  |  |  * Version - 3.5.1 | 
					
						
							|  |  |  |  * Licensed under the MIT license - http://opensource.org/licenses/MIT
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2016 Daniel Eden | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .animated { | 
					
						
							|  |  |  |   -webkit-animation-duration: 1s; | 
					
						
							|  |  |  |   animation-duration: 1s; | 
					
						
							|  |  |  |   -webkit-animation-fill-mode: both; | 
					
						
							|  |  |  |   animation-fill-mode: both; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .animated.infinite { | 
					
						
							|  |  |  |   -webkit-animation-iteration-count: infinite; | 
					
						
							|  |  |  |   animation-iteration-count: infinite; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .animated.hinge { | 
					
						
							|  |  |  |   -webkit-animation-duration: 2s; | 
					
						
							|  |  |  |   animation-duration: 2s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .animated.flipOutX, | 
					
						
							|  |  |  | .animated.flipOutY, | 
					
						
							|  |  |  | .animated.bounceIn, | 
					
						
							|  |  |  | .animated.bounceOut { | 
					
						
							|  |  |  |   -webkit-animation-duration: .75s; | 
					
						
							|  |  |  |   animation-duration: .75s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @-webkit-keyframes bounce { | 
					
						
							|  |  |  |   from, 20%, 53%, 80%, to { | 
					
						
							|  |  |  |     -webkit-animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000); | 
					
						
							|  |  |  |     animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000); | 
					
						
							|  |  |  |     -webkit-transform: translate3d(0,0,0); | 
					
						
							|  |  |  |     transform: translate3d(0,0,0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   40%, 43% { | 
					
						
							|  |  |  |     -webkit-animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060); | 
					
						
							|  |  |  |     animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060); | 
					
						
							|  |  |  |     -webkit-transform: translate3d(0, -30px, 0); | 
					
						
							|  |  |  |     transform: translate3d(0, -30px, 0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   70% { | 
					
						
							|  |  |  |     -webkit-animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060); | 
					
						
							|  |  |  |     animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060); | 
					
						
							|  |  |  |     -webkit-transform: translate3d(0, -15px, 0); | 
					
						
							|  |  |  |     transform: translate3d(0, -15px, 0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   90% { | 
					
						
							|  |  |  |     -webkit-transform: translate3d(0,-4px,0); | 
					
						
							|  |  |  |     transform: translate3d(0,-4px,0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |        `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const output = parse(styles); | 
					
						
							|  |  |  |       const errors = output.errors; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(errors.length).toEqual(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const ast = output.ast; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(ast.rules.length).toEqual(6); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const finalRule = <CssBlockRuleAst>ast.rules[ast.rules.length - 1]; | 
					
						
							| 
									
										
										
										
											2016-02-02 10:37:08 +02:00
										 |  |  |       expect(finalRule.type).toEqual(BlockType.Keyframes); | 
					
						
							|  |  |  |       expect(finalRule.block.entries.length).toEqual(4); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |