2016-06-23 12:47:54 -04: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-06-16 14:39:29 -04:00
|
|
|
import {afterEach, beforeEach, ddescribe, describe, expect, iit, it, xit} from '../../core/testing/testing_internal';
|
2016-06-16 16:02:18 -04:00
|
|
|
import {CssAst, CssAstVisitor, CssAtRulePredicateAst, CssBlockAst, CssBlockDefinitionRuleAst, CssBlockRuleAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssPseudoSelectorAst, CssRuleAst, CssSelectorAst, CssSelectorRuleAst, CssSimpleSelectorAst, CssStyleSheetAst, CssStyleValueAst, CssStylesBlockAst, CssUnknownRuleAst, CssUnknownTokenListAst} from '../src/css_ast';
|
|
|
|
import {BlockType, CssParseError, CssParser, CssToken} from '../src/css_parser';
|
2016-06-16 14:39:29 -04:00
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
2016-06-16 16:02:18 -04:00
|
|
|
import {isPresent} from '../src/facade/lang';
|
2016-02-02 03:37:08 -05:00
|
|
|
|
2016-06-14 19:26:57 -04:00
|
|
|
function _assertTokens(tokens: CssToken[], valuesArr: string[]): void {
|
|
|
|
expect(tokens.length).toEqual(valuesArr.length);
|
2016-02-02 03:37:08 -05:00
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
expect(tokens[i].strValue == valuesArr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
class MyVisitor implements CssAstVisitor {
|
2016-02-02 03:37:08 -05:00
|
|
|
captures: {[key: string]: any[]} = {};
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
_capture(method: string, ast: CssAst, context: any) {
|
2016-02-02 03:37:08 -05:00
|
|
|
this.captures[method] = isPresent(this.captures[method]) ? this.captures[method] : [];
|
|
|
|
this.captures[method].push([ast, context]);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
constructor(ast: CssStyleSheetAst, context: any) { ast.visit(this, context); }
|
2016-02-02 03:37:08 -05:00
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssValue(ast: CssStyleValueAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssValue', ast, context);
|
|
|
|
}
|
2016-02-02 03:37:08 -05:00
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssInlineRule(ast: CssInlineRuleAst, context: any): void {
|
|
|
|
this._capture('visitCssInlineRule', ast, context);
|
2016-02-02 03:37:08 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssAtRulePredicate(ast: CssAtRulePredicateAst, context: any): void {
|
2016-06-14 19:26:57 -04:00
|
|
|
this._capture('visitCssAtRulePredicate', ast, context);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssKeyframeRule(ast: CssKeyframeRuleAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssKeyframeRule', ast, context);
|
2016-02-02 03:37:08 -05:00
|
|
|
ast.block.visit(this, context);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssKeyframeDefinition(ast: CssKeyframeDefinitionAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssKeyframeDefinition', ast, context);
|
2016-02-02 03:37:08 -05:00
|
|
|
ast.block.visit(this, context);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssMediaQueryRule(ast: CssMediaQueryRuleAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssMediaQueryRule', ast, context);
|
2016-06-14 19:26:57 -04:00
|
|
|
ast.query.visit(this, context);
|
2016-02-02 03:37:08 -05:00
|
|
|
ast.block.visit(this, context);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssSelectorRule(ast: CssSelectorRuleAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssSelectorRule', ast, context);
|
2016-06-16 14:20:27 -04:00
|
|
|
ast.selectors.forEach((selAst: CssSelectorAst) => { selAst.visit(this, context); });
|
2016-02-02 03:37:08 -05:00
|
|
|
ast.block.visit(this, context);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssSelector(ast: CssSelectorAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssSelector', ast, context);
|
2016-06-14 19:26:57 -04:00
|
|
|
ast.selectorParts.forEach(
|
2016-06-16 14:20:27 -04:00
|
|
|
(simpleAst: CssSimpleSelectorAst) => { simpleAst.visit(this, context); });
|
2016-06-14 19:26:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssSimpleSelector(ast: CssSimpleSelectorAst, context: any): void {
|
2016-06-14 19:26:57 -04:00
|
|
|
this._capture('visitCssSimpleSelector', ast, context);
|
|
|
|
ast.pseudoSelectors.forEach(
|
2016-06-16 14:20:27 -04:00
|
|
|
(pseudoAst: CssPseudoSelectorAst) => { pseudoAst.visit(this, context); });
|
2016-02-02 03:37:08 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssDefinition(ast: CssDefinitionAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssDefinition', ast, context);
|
2016-02-02 03:37:08 -05:00
|
|
|
ast.value.visit(this, context);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssBlock(ast: CssBlockAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssBlock', ast, context);
|
2016-06-16 14:20:27 -04:00
|
|
|
ast.entries.forEach((entryAst: CssAst) => { entryAst.visit(this, context); });
|
2016-02-02 03:37:08 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssStylesBlock(ast: CssStylesBlockAst, context: any): void {
|
2016-06-14 19:26:57 -04:00
|
|
|
this._capture('visitCssStylesBlock', ast, context);
|
|
|
|
ast.definitions.forEach(
|
2016-06-16 14:20:27 -04:00
|
|
|
(definitionAst: CssDefinitionAst) => { definitionAst.visit(this, context); });
|
2016-06-14 19:26:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssStyleSheet(ast: CssStyleSheetAst, context: any): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
this._capture('visitCssStyleSheet', ast, context);
|
2016-06-16 14:20:27 -04:00
|
|
|
ast.rules.forEach((ruleAst: CssRuleAst) => { ruleAst.visit(this, context); });
|
2016-02-02 03:37:08 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssUnknownRule(ast: CssUnknownRuleAst, context: any): void {
|
|
|
|
this._capture('visitCssUnknownRule', ast, context);
|
2016-02-02 03:37:08 -05:00
|
|
|
}
|
2016-06-14 19:26:57 -04:00
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssUnknownTokenList(ast: CssUnknownTokenListAst, context: any): void {
|
|
|
|
this._capture('visitCssUnknownTokenList', ast, context);
|
2016-06-14 19:26:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
visitCssPseudoSelector(ast: CssPseudoSelectorAst, context: any): void {
|
2016-06-14 19:26:57 -04:00
|
|
|
this._capture('visitCssPseudoSelector', ast, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
function _getCaptureAst(capture: any[], index = 0): CssAst {
|
|
|
|
return <CssAst>capture[index][0];
|
2016-02-02 03:37:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function main() {
|
2016-06-14 19:26:57 -04:00
|
|
|
function parse(cssCode: string, ignoreErrors: boolean = false) {
|
2016-06-16 16:02:18 -04:00
|
|
|
var output = new CssParser().parse(cssCode, 'some-fake-css-file.css');
|
2016-02-02 03:37:08 -05:00
|
|
|
var errors = output.errors;
|
2016-06-14 19:26:57 -04:00
|
|
|
if (errors.length > 0 && !ignoreErrors) {
|
2016-02-02 03:37:08 -05:00
|
|
|
throw new BaseException(errors.map((error: CssParseError) => error.msg).join(', '));
|
|
|
|
}
|
|
|
|
return output.ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('CSS parsing and visiting', () => {
|
2016-06-16 16:02:18 -04:00
|
|
|
var ast: CssStyleSheetAst;
|
2016-02-02 03:37:08 -05:00
|
|
|
var context = {};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
var cssCode = `
|
|
|
|
.rule1 { prop1: value1 }
|
|
|
|
.rule2 { prop2: value2 }
|
|
|
|
|
|
|
|
@media all (max-width: 100px) {
|
|
|
|
#id { prop3 :value3; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@import url(file.css);
|
|
|
|
|
|
|
|
@keyframes rotate {
|
|
|
|
from {
|
|
|
|
prop4: value4;
|
|
|
|
}
|
|
|
|
50%, 100% {
|
|
|
|
prop5: value5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
ast = parse(cssCode);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse and visit a stylesheet', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssStyleSheet'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
|
|
|
var capture = captures[0];
|
|
|
|
expect(capture[0]).toEqual(ast);
|
|
|
|
expect(capture[1]).toEqual(context);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse and visit each of the stylesheet selectors', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssSelectorRule'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(3);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var rule1 = <CssSelectorRuleAst>_getCaptureAst(captures, 0);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(rule1).toEqual(ast.rules[0]);
|
|
|
|
|
2016-06-14 19:26:57 -04:00
|
|
|
var firstSelector = rule1.selectors[0];
|
|
|
|
var firstSimpleSelector = firstSelector.selectorParts[0];
|
|
|
|
_assertTokens(firstSimpleSelector.tokens, ['.', 'rule1']);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var rule2 = <CssSelectorRuleAst>_getCaptureAst(captures, 1);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(rule2).toEqual(ast.rules[1]);
|
|
|
|
|
2016-06-14 19:26:57 -04:00
|
|
|
var secondSelector = rule2.selectors[0];
|
|
|
|
var secondSimpleSelector = secondSelector.selectorParts[0];
|
|
|
|
_assertTokens(secondSimpleSelector.tokens, ['.', 'rule2']);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var rule3 = <CssSelectorRuleAst>_getCaptureAst(captures, 2);
|
|
|
|
expect(rule3).toEqual((<CssMediaQueryRuleAst>ast.rules[2]).block.entries[0]);
|
2016-06-14 19:26:57 -04:00
|
|
|
|
|
|
|
var thirdSelector = rule3.selectors[0];
|
|
|
|
var thirdSimpleSelector = thirdSelector.selectorParts[0];
|
|
|
|
_assertTokens(thirdSimpleSelector.tokens, ['#', 'rule3']);
|
2016-02-02 03:37:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse and visit each of the stylesheet style key/value definitions', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssDefinition'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(5);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def1 = <CssDefinitionAst>_getCaptureAst(captures, 0);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(def1.property.strValue).toEqual('prop1');
|
|
|
|
expect(def1.value.tokens[0].strValue).toEqual('value1');
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def2 = <CssDefinitionAst>_getCaptureAst(captures, 1);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(def2.property.strValue).toEqual('prop2');
|
|
|
|
expect(def2.value.tokens[0].strValue).toEqual('value2');
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def3 = <CssDefinitionAst>_getCaptureAst(captures, 2);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(def3.property.strValue).toEqual('prop3');
|
|
|
|
expect(def3.value.tokens[0].strValue).toEqual('value3');
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def4 = <CssDefinitionAst>_getCaptureAst(captures, 3);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(def4.property.strValue).toEqual('prop4');
|
|
|
|
expect(def4.value.tokens[0].strValue).toEqual('value4');
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def5 = <CssDefinitionAst>_getCaptureAst(captures, 4);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(def5.property.strValue).toEqual('prop5');
|
|
|
|
expect(def5.value.tokens[0].strValue).toEqual('value5');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse and visit the associated media query values', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssMediaQueryRule'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var query1 = <CssMediaQueryRuleAst>_getCaptureAst(captures, 0);
|
2016-06-14 19:26:57 -04:00
|
|
|
_assertTokens(query1.query.tokens, ['all', 'and', '(', 'max-width', '100', 'px', ')']);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(query1.block.entries.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
2016-06-14 19:26:57 -04:00
|
|
|
it('should capture the media query predicate', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssAtRulePredicate'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var predicate = <CssAtRulePredicateAst>_getCaptureAst(captures, 0);
|
2016-06-14 19:26:57 -04:00
|
|
|
expect(predicate.strValue).toEqual('@media all (max-width: 100px)');
|
|
|
|
});
|
|
|
|
|
2016-02-02 03:37:08 -05:00
|
|
|
it('should parse and visit the associated "@inline" rule values', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
2016-06-14 19:26:57 -04:00
|
|
|
var captures = visitor.captures['visitCssInlineRule'];
|
2016-02-02 03:37:08 -05:00
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var inline1 = <CssInlineRuleAst>_getCaptureAst(captures, 0);
|
2016-06-14 19:26:57 -04:00
|
|
|
expect(inline1.type).toEqual(BlockType.Import);
|
|
|
|
_assertTokens(inline1.value.tokens, ['url', '(', 'file.css', ')']);
|
2016-02-02 03:37:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse and visit the keyframe blocks', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssKeyframeRule'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var keyframe1 = <CssKeyframeRuleAst>_getCaptureAst(captures, 0);
|
2016-02-02 03:37:08 -05:00
|
|
|
expect(keyframe1.name.strValue).toEqual('rotate');
|
|
|
|
expect(keyframe1.block.entries.length).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse and visit the associated keyframe rules', () => {
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssKeyframeDefinition'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(2);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def1 = <CssKeyframeDefinitionAst>_getCaptureAst(captures, 0);
|
2016-02-02 03:37:08 -05:00
|
|
|
_assertTokens(def1.steps, ['from']);
|
|
|
|
expect(def1.block.entries.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var def2 = <CssKeyframeDefinitionAst>_getCaptureAst(captures, 1);
|
2016-02-02 03:37:08 -05:00
|
|
|
_assertTokens(def2.steps, ['50%', '100%']);
|
|
|
|
expect(def2.block.entries.length).toEqual(1);
|
|
|
|
});
|
2016-06-14 19:26:57 -04:00
|
|
|
|
|
|
|
it('should visit an unknown `@` rule', () => {
|
|
|
|
var cssCode = `
|
|
|
|
@someUnknownRule param {
|
|
|
|
one two three
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
ast = parse(cssCode, true);
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssUnknownRule'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var rule = <CssUnknownRuleAst>_getCaptureAst(captures, 0);
|
2016-06-14 19:26:57 -04:00
|
|
|
expect(rule.ruleName).toEqual('@someUnknownRule');
|
|
|
|
|
|
|
|
_assertTokens(rule.tokens, ['param', '{', 'one', 'two', 'three', '}']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should collect an invalid list of tokens before a valid selector', () => {
|
|
|
|
var cssCode = 'one two three four five; selector { }';
|
|
|
|
ast = parse(cssCode, true);
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssUnknownTokenList'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var rule = <CssUnknownTokenListAst>_getCaptureAst(captures, 0);
|
2016-06-14 19:26:57 -04:00
|
|
|
_assertTokens(rule.tokens, ['one', 'two', 'three', 'four', 'five']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should collect an invalid list of tokens after a valid selector', () => {
|
|
|
|
var cssCode = 'selector { } six seven eight';
|
|
|
|
ast = parse(cssCode, true);
|
|
|
|
var visitor = new MyVisitor(ast, context);
|
|
|
|
var captures = visitor.captures['visitCssUnknownTokenList'];
|
|
|
|
|
|
|
|
expect(captures.length).toEqual(1);
|
|
|
|
|
2016-06-16 14:20:27 -04:00
|
|
|
var rule = <CssUnknownTokenListAst>_getCaptureAst(captures, 0);
|
2016-06-14 19:26:57 -04:00
|
|
|
_assertTokens(rule.tokens, ['six', 'seven', 'eight']);
|
|
|
|
});
|
2016-02-02 03:37:08 -05:00
|
|
|
});
|
|
|
|
}
|