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-06-21 11:10:25 -07:00
|
|
|
import * as chars from './chars';
|
2016-06-16 13:02:18 -07:00
|
|
|
import {BlockType, CssAst, CssAtRulePredicateAst, CssBlockAst, CssBlockDefinitionRuleAst, CssBlockRuleAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssPseudoSelectorAst, CssRuleAst, CssSelectorAst, CssSelectorRuleAst, CssSimpleSelectorAst, CssStyleSheetAst, CssStyleValueAst, CssStylesBlockAst, CssUnknownRuleAst, CssUnknownTokenListAst, mergeTokens} from './css_ast';
|
|
|
|
import {CssLexer, CssLexerMode, CssScanner, CssToken, CssTokenType, generateErrorMessage, isNewline} from './css_lexer';
|
2016-06-21 11:10:25 -07:00
|
|
|
import {isPresent} from './facade/lang';
|
2016-06-16 11:39:29 -07:00
|
|
|
import {ParseError, ParseLocation, ParseSourceFile, ParseSourceSpan} from './parse_util';
|
2016-06-14 16:26:57 -07:00
|
|
|
|
|
|
|
const SPACE_OPERATOR = ' ';
|
2016-04-12 09:40:37 -07:00
|
|
|
|
2016-06-16 11:39:29 -07:00
|
|
|
export {CssToken} from './css_lexer';
|
2016-06-16 13:02:18 -07:00
|
|
|
export {BlockType} from './css_ast';
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
const SLASH_CHARACTER = '/';
|
|
|
|
const GT_CHARACTER = '>';
|
|
|
|
const TRIPLE_GT_OPERATOR_STR = '>>>';
|
|
|
|
const DEEP_OPERATOR_STR = '/deep/';
|
|
|
|
|
|
|
|
const EOF_DELIM_FLAG = 1;
|
|
|
|
const RBRACE_DELIM_FLAG = 2;
|
|
|
|
const LBRACE_DELIM_FLAG = 4;
|
|
|
|
const COMMA_DELIM_FLAG = 8;
|
|
|
|
const COLON_DELIM_FLAG = 16;
|
|
|
|
const SEMICOLON_DELIM_FLAG = 32;
|
|
|
|
const NEWLINE_DELIM_FLAG = 64;
|
|
|
|
const RPAREN_DELIM_FLAG = 128;
|
|
|
|
const LPAREN_DELIM_FLAG = 256;
|
|
|
|
const SPACE_DELIM_FLAG = 512;
|
|
|
|
|
|
|
|
function _pseudoSelectorSupportsInnerSelectors(name: string): boolean {
|
|
|
|
return ['not', 'host', 'host-context'].indexOf(name) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSelectorOperatorCharacter(code: number): boolean {
|
|
|
|
switch (code) {
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$SLASH:
|
|
|
|
case chars.$TILDA:
|
|
|
|
case chars.$PLUS:
|
|
|
|
case chars.$GT:
|
2016-06-14 16:26:57 -07:00
|
|
|
return true;
|
|
|
|
default:
|
2016-06-21 11:10:25 -07:00
|
|
|
return chars.isWhitespace(code);
|
2016-06-14 16:26:57 -07:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
function getDelimFromToken(token: CssToken): number {
|
|
|
|
return getDelimFromCharacter(token.numValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDelimFromCharacter(code: number): number {
|
|
|
|
switch (code) {
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$EOF:
|
2016-06-14 16:26:57 -07:00
|
|
|
return EOF_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$COMMA:
|
2016-06-14 16:26:57 -07:00
|
|
|
return COMMA_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$COLON:
|
2016-06-14 16:26:57 -07:00
|
|
|
return COLON_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$SEMICOLON:
|
2016-06-14 16:26:57 -07:00
|
|
|
return SEMICOLON_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$RBRACE:
|
2016-06-14 16:26:57 -07:00
|
|
|
return RBRACE_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$LBRACE:
|
2016-06-14 16:26:57 -07:00
|
|
|
return LBRACE_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$RPAREN:
|
2016-06-14 16:26:57 -07:00
|
|
|
return RPAREN_DELIM_FLAG;
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$SPACE:
|
|
|
|
case chars.$TAB:
|
2016-06-14 16:26:57 -07:00
|
|
|
return SPACE_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
default:
|
2016-06-14 16:26:57 -07:00
|
|
|
return isNewline(code) ? NEWLINE_DELIM_FLAG : 0;
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
function characterContainsDelimiter(code: number, delimiters: number): boolean {
|
2016-06-16 11:54:20 -07:00
|
|
|
return (getDelimFromCharacter(code) & delimiters) > 0;
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ParsedCssResult {
|
2016-06-16 11:20:27 -07:00
|
|
|
constructor(public errors: CssParseError[], public ast: CssStyleSheetAst) {}
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CssParser {
|
|
|
|
private _errors: CssParseError[] = [];
|
|
|
|
private _file: ParseSourceFile;
|
2016-06-16 13:02:18 -07:00
|
|
|
private _scanner: CssScanner;
|
|
|
|
private _lastToken: CssToken;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param css the CSS code that will be parsed
|
|
|
|
* @param url the name of the CSS file containing the CSS source code
|
|
|
|
*/
|
|
|
|
parse(css: string, url: string): ParsedCssResult {
|
|
|
|
var lexer = new CssLexer();
|
|
|
|
this._file = new ParseSourceFile(css, url);
|
|
|
|
this._scanner = lexer.scan(css, false);
|
|
|
|
|
|
|
|
var ast = this._parseStyleSheet(EOF_DELIM_FLAG);
|
|
|
|
|
|
|
|
var errors = this._errors;
|
|
|
|
this._errors = [];
|
|
|
|
|
|
|
|
var result = new ParsedCssResult(errors, ast);
|
|
|
|
this._file = null;
|
|
|
|
this._scanner = null;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_parseStyleSheet(delimiters: number): CssStyleSheetAst {
|
|
|
|
var results: CssRuleAst[] = [];
|
|
|
|
this._scanner.consumeEmptyStatements();
|
|
|
|
while (this._scanner.peek != chars.$EOF) {
|
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
|
|
|
results.push(this._parseRule(delimiters));
|
|
|
|
}
|
|
|
|
var span: ParseSourceSpan = null;
|
|
|
|
if (results.length > 0) {
|
|
|
|
var firstRule = results[0];
|
|
|
|
// we collect the last token like so incase there was an
|
|
|
|
// EOF token that was emitted sometime during the lexing
|
|
|
|
span = this._generateSourceSpan(firstRule, this._lastToken);
|
|
|
|
}
|
|
|
|
return new CssStyleSheetAst(span, results);
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
/** @internal */
|
|
|
|
_getSourceContent(): string { return isPresent(this._scanner) ? this._scanner.input : ''; }
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_extractSourceContent(start: number, end: number): string {
|
|
|
|
return this._getSourceContent().substring(start, end + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_generateSourceSpan(start: CssToken|CssAst, end: CssToken|CssAst = null): ParseSourceSpan {
|
|
|
|
var startLoc: ParseLocation;
|
|
|
|
if (start instanceof CssAst) {
|
|
|
|
startLoc = start.location.start;
|
|
|
|
} else {
|
|
|
|
var token = start;
|
|
|
|
if (!isPresent(token)) {
|
|
|
|
// the data here is invalid, however, if and when this does
|
|
|
|
// occur, any other errors associated with this will be collected
|
|
|
|
token = this._lastToken;
|
|
|
|
}
|
|
|
|
startLoc = new ParseLocation(this._file, token.index, token.line, token.column);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPresent(end)) {
|
|
|
|
end = this._lastToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
var endLine: number;
|
|
|
|
var endColumn: number;
|
|
|
|
var endIndex: number;
|
|
|
|
if (end instanceof CssAst) {
|
|
|
|
endLine = end.location.end.line;
|
|
|
|
endColumn = end.location.end.col;
|
|
|
|
endIndex = end.location.end.offset;
|
|
|
|
} else if (end instanceof CssToken) {
|
|
|
|
endLine = end.line;
|
|
|
|
endColumn = end.column;
|
|
|
|
endIndex = end.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
var endLoc = new ParseLocation(this._file, endIndex, endLine, endColumn);
|
|
|
|
return new ParseSourceSpan(startLoc, endLoc);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_resolveBlockType(token: CssToken): BlockType {
|
|
|
|
switch (token.strValue) {
|
|
|
|
case '@-o-keyframes':
|
|
|
|
case '@-moz-keyframes':
|
|
|
|
case '@-webkit-keyframes':
|
|
|
|
case '@keyframes':
|
|
|
|
return BlockType.Keyframes;
|
|
|
|
|
|
|
|
case '@charset':
|
|
|
|
return BlockType.Charset;
|
|
|
|
|
|
|
|
case '@import':
|
|
|
|
return BlockType.Import;
|
|
|
|
|
|
|
|
case '@namespace':
|
|
|
|
return BlockType.Namespace;
|
|
|
|
|
|
|
|
case '@page':
|
|
|
|
return BlockType.Page;
|
|
|
|
|
|
|
|
case '@document':
|
|
|
|
return BlockType.Document;
|
|
|
|
|
|
|
|
case '@media':
|
|
|
|
return BlockType.MediaQuery;
|
|
|
|
|
|
|
|
case '@font-face':
|
|
|
|
return BlockType.FontFace;
|
|
|
|
|
|
|
|
case '@viewport':
|
|
|
|
return BlockType.Viewport;
|
|
|
|
|
|
|
|
case '@supports':
|
|
|
|
return BlockType.Supports;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return BlockType.Unsupported;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseRule(delimiters: number): CssRuleAst {
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek == chars.$AT) {
|
2016-02-02 10:37:08 +02:00
|
|
|
return this._parseAtRule(delimiters);
|
|
|
|
}
|
|
|
|
return this._parseSelectorRule(delimiters);
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseAtRule(delimiters: number): CssRuleAst {
|
2016-06-14 16:26:57 -07:00
|
|
|
const start = this._getScannerIndex();
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
2016-02-02 10:37:08 +02:00
|
|
|
var token = this._scan();
|
2016-06-16 13:02:18 -07:00
|
|
|
var startToken = token;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
this._assertCondition(
|
|
|
|
token.type == CssTokenType.AtKeyword,
|
|
|
|
`The CSS Rule ${token.strValue} is not a valid [@] rule.`, token);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-16 11:20:27 -07:00
|
|
|
var block: CssBlockAst;
|
2016-06-14 16:26:57 -07:00
|
|
|
var type = this._resolveBlockType(token);
|
2016-07-13 10:28:11 -07:00
|
|
|
var span: ParseSourceSpan;
|
|
|
|
var tokens: CssToken[];
|
|
|
|
var endToken: CssToken;
|
|
|
|
var end: number;
|
|
|
|
var strValue: string;
|
|
|
|
var query: CssAtRulePredicateAst;
|
2016-02-02 10:37:08 +02:00
|
|
|
switch (type) {
|
|
|
|
case BlockType.Charset:
|
|
|
|
case BlockType.Namespace:
|
|
|
|
case BlockType.Import:
|
2016-07-13 10:28:11 -07:00
|
|
|
let value = this._parseValue(delimiters);
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
|
|
|
this._scanner.consumeEmptyStatements();
|
2016-07-13 10:28:11 -07:00
|
|
|
span = this._generateSourceSpan(startToken, value);
|
2016-06-16 13:02:18 -07:00
|
|
|
return new CssInlineRuleAst(span, type, value);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
case BlockType.Viewport:
|
|
|
|
case BlockType.FontFace:
|
|
|
|
block = this._parseStyleBlock(delimiters);
|
2016-07-13 10:28:11 -07:00
|
|
|
span = this._generateSourceSpan(startToken, block);
|
2016-06-16 13:02:18 -07:00
|
|
|
return new CssBlockRuleAst(span, type, block);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
case BlockType.Keyframes:
|
2016-07-13 10:28:11 -07:00
|
|
|
tokens = this._collectUntilDelim(delimiters | RBRACE_DELIM_FLAG | LBRACE_DELIM_FLAG);
|
2016-02-02 10:37:08 +02:00
|
|
|
// keyframes only have one identifier name
|
2016-07-13 10:28:11 -07:00
|
|
|
let name = tokens[0];
|
|
|
|
block = this._parseKeyframeBlock(delimiters);
|
|
|
|
span = this._generateSourceSpan(startToken, block);
|
2016-06-16 13:02:18 -07:00
|
|
|
return new CssKeyframeRuleAst(span, name, block);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
case BlockType.MediaQuery:
|
|
|
|
this._scanner.setMode(CssLexerMode.MEDIA_QUERY);
|
2016-07-13 10:28:11 -07:00
|
|
|
tokens = this._collectUntilDelim(delimiters | RBRACE_DELIM_FLAG | LBRACE_DELIM_FLAG);
|
|
|
|
endToken = tokens[tokens.length - 1];
|
2016-06-16 13:02:18 -07:00
|
|
|
// we do not track the whitespace after the mediaQuery predicate ends
|
|
|
|
// so we have to calculate the end string value on our own
|
2016-07-13 10:28:11 -07:00
|
|
|
end = endToken.index + endToken.strValue.length - 1;
|
|
|
|
strValue = this._extractSourceContent(start, end);
|
|
|
|
span = this._generateSourceSpan(startToken, endToken);
|
|
|
|
query = new CssAtRulePredicateAst(span, strValue, tokens);
|
2016-06-14 16:26:57 -07:00
|
|
|
block = this._parseBlock(delimiters);
|
2016-06-16 13:02:18 -07:00
|
|
|
strValue = this._extractSourceContent(start, this._getScannerIndex() - 1);
|
|
|
|
span = this._generateSourceSpan(startToken, block);
|
|
|
|
return new CssMediaQueryRuleAst(span, strValue, query, block);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
case BlockType.Document:
|
|
|
|
case BlockType.Supports:
|
|
|
|
case BlockType.Page:
|
|
|
|
this._scanner.setMode(CssLexerMode.AT_RULE_QUERY);
|
2016-07-13 10:28:11 -07:00
|
|
|
tokens = this._collectUntilDelim(delimiters | RBRACE_DELIM_FLAG | LBRACE_DELIM_FLAG);
|
|
|
|
endToken = tokens[tokens.length - 1];
|
2016-06-16 13:02:18 -07:00
|
|
|
// we do not track the whitespace after this block rule predicate ends
|
|
|
|
// so we have to calculate the end string value on our own
|
2016-07-13 10:28:11 -07:00
|
|
|
end = endToken.index + endToken.strValue.length - 1;
|
|
|
|
strValue = this._extractSourceContent(start, end);
|
|
|
|
span = this._generateSourceSpan(startToken, tokens[tokens.length - 1]);
|
|
|
|
query = new CssAtRulePredicateAst(span, strValue, tokens);
|
2016-06-14 16:26:57 -07:00
|
|
|
block = this._parseBlock(delimiters);
|
2016-06-16 13:02:18 -07:00
|
|
|
strValue = this._extractSourceContent(start, block.end.offset);
|
|
|
|
span = this._generateSourceSpan(startToken, block);
|
|
|
|
return new CssBlockDefinitionRuleAst(span, strValue, type, query, block);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
// if a custom @rule { ... } is used it should still tokenize the insides
|
|
|
|
default:
|
2016-07-13 10:28:11 -07:00
|
|
|
let listOfTokens: CssToken[] = [];
|
|
|
|
let tokenName = token.strValue;
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.ALL);
|
2016-06-08 16:38:52 -07:00
|
|
|
this._error(
|
|
|
|
generateErrorMessage(
|
2016-06-16 13:02:18 -07:00
|
|
|
this._getSourceContent(),
|
|
|
|
`The CSS "at" rule "${tokenName}" is not allowed to used here`, token.strValue,
|
|
|
|
token.index, token.line, token.column),
|
2016-06-08 16:38:52 -07:00
|
|
|
token);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-16 11:54:20 -07:00
|
|
|
this._collectUntilDelim(delimiters | LBRACE_DELIM_FLAG | SEMICOLON_DELIM_FLAG)
|
2016-02-02 10:37:08 +02:00
|
|
|
.forEach((token) => { listOfTokens.push(token); });
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek == chars.$LBRACE) {
|
2016-06-14 16:26:57 -07:00
|
|
|
listOfTokens.push(this._consume(CssTokenType.Character, '{'));
|
2016-06-16 11:54:20 -07:00
|
|
|
this._collectUntilDelim(delimiters | RBRACE_DELIM_FLAG | LBRACE_DELIM_FLAG)
|
2016-02-02 10:37:08 +02:00
|
|
|
.forEach((token) => { listOfTokens.push(token); });
|
2016-06-14 16:26:57 -07:00
|
|
|
listOfTokens.push(this._consume(CssTokenType.Character, '}'));
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
2016-07-13 10:28:11 -07:00
|
|
|
endToken = listOfTokens[listOfTokens.length - 1];
|
|
|
|
span = this._generateSourceSpan(startToken, endToken);
|
2016-06-16 13:02:18 -07:00
|
|
|
return new CssUnknownRuleAst(span, tokenName, listOfTokens);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseSelectorRule(delimiters: number): CssRuleAst {
|
2016-06-14 16:26:57 -07:00
|
|
|
const start = this._getScannerIndex();
|
2016-02-02 10:37:08 +02:00
|
|
|
var selectors = this._parseSelectors(delimiters);
|
|
|
|
var block = this._parseStyleBlock(delimiters);
|
2016-06-16 13:02:18 -07:00
|
|
|
var ruleAst: CssRuleAst;
|
|
|
|
var span: ParseSourceSpan;
|
|
|
|
var startSelector = selectors[0];
|
2016-06-14 16:26:57 -07:00
|
|
|
if (isPresent(block)) {
|
2016-06-16 13:02:18 -07:00
|
|
|
var span = this._generateSourceSpan(startSelector, block);
|
|
|
|
ruleAst = new CssSelectorRuleAst(span, selectors, block);
|
2016-06-14 16:26:57 -07:00
|
|
|
} else {
|
2016-06-16 13:02:18 -07:00
|
|
|
var name = this._extractSourceContent(start, this._getScannerIndex() - 1);
|
2016-06-14 16:26:57 -07:00
|
|
|
var innerTokens: CssToken[] = [];
|
2016-06-16 11:20:27 -07:00
|
|
|
selectors.forEach((selector: CssSelectorAst) => {
|
|
|
|
selector.selectorParts.forEach((part: CssSimpleSelectorAst) => {
|
2016-06-14 16:26:57 -07:00
|
|
|
part.tokens.forEach((token: CssToken) => { innerTokens.push(token); });
|
|
|
|
});
|
|
|
|
});
|
2016-06-16 13:02:18 -07:00
|
|
|
var endToken = innerTokens[innerTokens.length - 1];
|
|
|
|
span = this._generateSourceSpan(startSelector, endToken);
|
|
|
|
ruleAst = new CssUnknownTokenListAst(span, name, innerTokens);
|
2016-06-14 16:26:57 -07:00
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
|
|
|
this._scanner.consumeEmptyStatements();
|
2016-06-16 13:02:18 -07:00
|
|
|
return ruleAst;
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseSelectors(delimiters: number): CssSelectorAst[] {
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= LBRACE_DELIM_FLAG | SEMICOLON_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-28 11:35:59 -07:00
|
|
|
var selectors: CssSelectorAst[] = [];
|
2016-02-02 10:37:08 +02:00
|
|
|
var isParsingSelectors = true;
|
|
|
|
while (isParsingSelectors) {
|
|
|
|
selectors.push(this._parseSelector(delimiters));
|
|
|
|
|
|
|
|
isParsingSelectors = !characterContainsDelimiter(this._scanner.peek, delimiters);
|
|
|
|
|
|
|
|
if (isParsingSelectors) {
|
|
|
|
this._consume(CssTokenType.Character, ',');
|
|
|
|
isParsingSelectors = !characterContainsDelimiter(this._scanner.peek, delimiters);
|
2016-06-14 16:26:57 -07:00
|
|
|
if (isParsingSelectors) {
|
|
|
|
this._scanner.consumeWhitespace();
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectors;
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_scan(): CssToken {
|
|
|
|
var output = this._scanner.scan();
|
|
|
|
var token = output.token;
|
|
|
|
var error = output.error;
|
|
|
|
if (isPresent(error)) {
|
|
|
|
this._error(error.rawMessage, token);
|
|
|
|
}
|
2016-06-16 13:02:18 -07:00
|
|
|
this._lastToken = token;
|
2016-02-02 10:37:08 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
/** @internal */
|
|
|
|
_getScannerIndex(): number { return this._scanner.index; }
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_consume(type: CssTokenType, value: string = null): CssToken {
|
|
|
|
var output = this._scanner.consume(type, value);
|
|
|
|
var token = output.token;
|
|
|
|
var error = output.error;
|
|
|
|
if (isPresent(error)) {
|
|
|
|
this._error(error.rawMessage, token);
|
|
|
|
}
|
2016-06-16 13:02:18 -07:00
|
|
|
this._lastToken = token;
|
2016-02-02 10:37:08 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseKeyframeBlock(delimiters: number): CssBlockAst {
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= RBRACE_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.KEYFRAME_BLOCK);
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var startToken = this._consume(CssTokenType.Character, '{');
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-28 11:35:59 -07:00
|
|
|
var definitions: CssKeyframeDefinitionAst[] = [];
|
2016-02-02 10:37:08 +02:00
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
|
|
|
definitions.push(this._parseKeyframeDefinition(delimiters));
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var endToken = this._consume(CssTokenType.Character, '}');
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var span = this._generateSourceSpan(startToken, endToken);
|
|
|
|
return new CssBlockAst(span, definitions);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseKeyframeDefinition(delimiters: number): CssKeyframeDefinitionAst {
|
2016-06-14 16:26:57 -07:00
|
|
|
const start = this._getScannerIndex();
|
2016-06-28 11:35:59 -07:00
|
|
|
var stepTokens: CssToken[] = [];
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= LBRACE_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
2016-06-16 11:54:20 -07:00
|
|
|
stepTokens.push(this._parseKeyframeLabel(delimiters | COMMA_DELIM_FLAG));
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek != chars.$LBRACE) {
|
2016-02-02 10:37:08 +02:00
|
|
|
this._consume(CssTokenType.Character, ',');
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 13:02:18 -07:00
|
|
|
var stylesBlock = this._parseStyleBlock(delimiters | RBRACE_DELIM_FLAG);
|
|
|
|
var span = this._generateSourceSpan(stepTokens[0], stylesBlock);
|
|
|
|
var ast = new CssKeyframeDefinitionAst(span, stepTokens, stylesBlock);
|
|
|
|
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
2016-06-16 13:02:18 -07:00
|
|
|
return ast;
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_parseKeyframeLabel(delimiters: number): CssToken {
|
|
|
|
this._scanner.setMode(CssLexerMode.KEYFRAME_BLOCK);
|
|
|
|
return mergeTokens(this._collectUntilDelim(delimiters));
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parsePseudoSelector(delimiters: number): CssPseudoSelectorAst {
|
2016-06-14 16:26:57 -07:00
|
|
|
const start = this._getScannerIndex();
|
|
|
|
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters &= ~COMMA_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
|
|
|
|
// we keep the original value since we may use it to recurse when :not, :host are used
|
|
|
|
var startingDelims = delimiters;
|
|
|
|
|
|
|
|
var startToken = this._consume(CssTokenType.Character, ':');
|
|
|
|
var tokens = [startToken];
|
|
|
|
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek == chars.$COLON) { // ::something
|
2016-06-16 13:02:18 -07:00
|
|
|
tokens.push(this._consume(CssTokenType.Character, ':'));
|
2016-06-14 16:26:57 -07:00
|
|
|
}
|
|
|
|
|
2016-06-16 11:20:27 -07:00
|
|
|
var innerSelectors: CssSelectorAst[] = [];
|
2016-06-14 16:26:57 -07:00
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.PSEUDO_SELECTOR);
|
|
|
|
|
|
|
|
// host, host-context, lang, not, nth-child are all identifiers
|
|
|
|
var pseudoSelectorToken = this._consume(CssTokenType.Identifier);
|
|
|
|
var pseudoSelectorName = pseudoSelectorToken.strValue;
|
|
|
|
tokens.push(pseudoSelectorToken);
|
|
|
|
|
|
|
|
// host(), lang(), nth-child(), etc...
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek == chars.$LPAREN) {
|
2016-06-14 16:26:57 -07:00
|
|
|
this._scanner.setMode(CssLexerMode.PSEUDO_SELECTOR_WITH_ARGUMENTS);
|
|
|
|
|
|
|
|
var openParenToken = this._consume(CssTokenType.Character, '(');
|
|
|
|
tokens.push(openParenToken);
|
|
|
|
|
|
|
|
// :host(innerSelector(s)), :not(selector), etc...
|
|
|
|
if (_pseudoSelectorSupportsInnerSelectors(pseudoSelectorName)) {
|
2016-06-16 11:54:20 -07:00
|
|
|
var innerDelims = startingDelims | LPAREN_DELIM_FLAG | RPAREN_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
if (pseudoSelectorName == 'not') {
|
|
|
|
// the inner selector inside of :not(...) can only be one
|
|
|
|
// CSS selector (no commas allowed) ... This is according
|
|
|
|
// to the CSS specification
|
2016-06-16 11:54:20 -07:00
|
|
|
innerDelims |= COMMA_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// :host(a, b, c) {
|
|
|
|
this._parseSelectors(innerDelims).forEach((selector, index) => {
|
|
|
|
innerSelectors.push(selector);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// this branch is for things like "en-us, 2k + 1, etc..."
|
|
|
|
// which all end up in pseudoSelectors like :lang, :nth-child, etc..
|
2016-06-16 11:54:20 -07:00
|
|
|
var innerValueDelims = delimiters | LBRACE_DELIM_FLAG | COLON_DELIM_FLAG |
|
|
|
|
RPAREN_DELIM_FLAG | LPAREN_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, innerValueDelims)) {
|
|
|
|
var token = this._scan();
|
|
|
|
tokens.push(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeParenToken = this._consume(CssTokenType.Character, ')');
|
|
|
|
tokens.push(closeParenToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
const end = this._getScannerIndex() - 1;
|
2016-06-16 13:02:18 -07:00
|
|
|
var strValue = this._extractSourceContent(start, end);
|
|
|
|
|
|
|
|
var endToken = tokens[tokens.length - 1];
|
|
|
|
var span = this._generateSourceSpan(startToken, endToken);
|
|
|
|
return new CssPseudoSelectorAst(span, strValue, pseudoSelectorName, tokens, innerSelectors);
|
2016-06-14 16:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseSimpleSelector(delimiters: number): CssSimpleSelectorAst {
|
2016-06-14 16:26:57 -07:00
|
|
|
const start = this._getScannerIndex();
|
|
|
|
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= COMMA_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.SELECTOR);
|
2016-06-14 16:26:57 -07:00
|
|
|
var selectorCssTokens: CssToken[] = [];
|
2016-06-16 11:20:27 -07:00
|
|
|
var pseudoSelectors: CssPseudoSelectorAst[] = [];
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
var previousToken: CssToken;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-16 11:54:20 -07:00
|
|
|
var selectorPartDelimiters = delimiters | SPACE_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
var loopOverSelector = !characterContainsDelimiter(this._scanner.peek, selectorPartDelimiters);
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
var hasAttributeError = false;
|
|
|
|
while (loopOverSelector) {
|
|
|
|
var peek = this._scanner.peek;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
switch (peek) {
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$COLON:
|
2016-06-14 16:26:57 -07:00
|
|
|
var innerPseudo = this._parsePseudoSelector(delimiters);
|
|
|
|
pseudoSelectors.push(innerPseudo);
|
|
|
|
this._scanner.setMode(CssLexerMode.SELECTOR);
|
|
|
|
break;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$LBRACKET:
|
2016-06-14 16:26:57 -07:00
|
|
|
// we set the mode after the scan because attribute mode does not
|
|
|
|
// allow attribute [] values. And this also will catch any errors
|
|
|
|
// if an extra "[" is used inside.
|
|
|
|
selectorCssTokens.push(this._scan());
|
|
|
|
this._scanner.setMode(CssLexerMode.ATTRIBUTE_SELECTOR);
|
2016-02-02 10:37:08 +02:00
|
|
|
break;
|
|
|
|
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$RBRACKET:
|
2016-06-14 16:26:57 -07:00
|
|
|
if (this._scanner.getMode() != CssLexerMode.ATTRIBUTE_SELECTOR) {
|
|
|
|
hasAttributeError = true;
|
|
|
|
}
|
|
|
|
// we set the mode early because attribute mode does not
|
|
|
|
// allow attribute [] values
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.SELECTOR);
|
2016-06-14 16:26:57 -07:00
|
|
|
selectorCssTokens.push(this._scan());
|
|
|
|
break;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
default:
|
|
|
|
if (isSelectorOperatorCharacter(peek)) {
|
|
|
|
loopOverSelector = false;
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
var token = this._scan();
|
|
|
|
previousToken = token;
|
|
|
|
selectorCssTokens.push(token);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
loopOverSelector = !characterContainsDelimiter(this._scanner.peek, selectorPartDelimiters);
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
hasAttributeError =
|
|
|
|
hasAttributeError || this._scanner.getMode() == CssLexerMode.ATTRIBUTE_SELECTOR;
|
|
|
|
if (hasAttributeError) {
|
|
|
|
this._error(
|
|
|
|
`Unbalanced CSS attribute selector at column ${previousToken.line}:${previousToken.column}`,
|
|
|
|
previousToken);
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var end = this._getScannerIndex() - 1;
|
|
|
|
|
|
|
|
// this happens if the selector is not directly followed by
|
|
|
|
// a comma or curly brace without a space in between
|
|
|
|
if (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
2016-06-14 16:26:57 -07:00
|
|
|
var operator: CssToken = null;
|
|
|
|
var operatorScanCount = 0;
|
|
|
|
var lastOperatorToken: CssToken = null;
|
|
|
|
while (operator == null && !characterContainsDelimiter(this._scanner.peek, delimiters) &&
|
|
|
|
isSelectorOperatorCharacter(this._scanner.peek)) {
|
|
|
|
var token = this._scan();
|
|
|
|
var tokenOperator = token.strValue;
|
|
|
|
operatorScanCount++;
|
|
|
|
lastOperatorToken = token;
|
|
|
|
if (tokenOperator != SPACE_OPERATOR) {
|
|
|
|
switch (tokenOperator) {
|
|
|
|
case SLASH_CHARACTER:
|
|
|
|
// /deep/ operator
|
|
|
|
let deepToken = this._consume(CssTokenType.Identifier);
|
|
|
|
let deepSlash = this._consume(CssTokenType.Character);
|
|
|
|
let index = lastOperatorToken.index;
|
|
|
|
let line = lastOperatorToken.line;
|
|
|
|
let column = lastOperatorToken.column;
|
|
|
|
if (isPresent(deepToken) && deepToken.strValue.toLowerCase() == 'deep' &&
|
|
|
|
deepSlash.strValue == SLASH_CHARACTER) {
|
|
|
|
token = new CssToken(
|
|
|
|
lastOperatorToken.index, lastOperatorToken.column, lastOperatorToken.line,
|
|
|
|
CssTokenType.Identifier, DEEP_OPERATOR_STR);
|
|
|
|
} else {
|
|
|
|
let text = SLASH_CHARACTER + deepToken.strValue + deepSlash.strValue;
|
|
|
|
this._error(
|
|
|
|
generateErrorMessage(
|
2016-06-16 13:02:18 -07:00
|
|
|
this._getSourceContent(), `${text} is an invalid CSS operator`, text, index,
|
2016-06-14 16:26:57 -07:00
|
|
|
line, column),
|
|
|
|
lastOperatorToken);
|
|
|
|
token = new CssToken(index, column, line, CssTokenType.Invalid, text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GT_CHARACTER:
|
|
|
|
// >>> operator
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek == chars.$GT && this._scanner.peekPeek == chars.$GT) {
|
2016-06-14 16:26:57 -07:00
|
|
|
this._consume(CssTokenType.Character, GT_CHARACTER);
|
|
|
|
this._consume(CssTokenType.Character, GT_CHARACTER);
|
|
|
|
token = new CssToken(
|
|
|
|
lastOperatorToken.index, lastOperatorToken.column, lastOperatorToken.line,
|
|
|
|
CssTokenType.Identifier, TRIPLE_GT_OPERATOR_STR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
operator = token;
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 13:02:18 -07:00
|
|
|
|
|
|
|
// so long as there is an operator then we can have an
|
|
|
|
// ending value that is beyond the selector value ...
|
|
|
|
// otherwise it's just a bunch of trailing whitespace
|
|
|
|
if (isPresent(operator)) {
|
|
|
|
end = operator.index;
|
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
this._scanner.consumeWhitespace();
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var strValue = this._extractSourceContent(start, end);
|
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
// if we do come across one or more spaces inside of
|
|
|
|
// the operators loop then an empty space is still a
|
|
|
|
// valid operator to use if something else was not found
|
2016-06-21 11:10:25 -07:00
|
|
|
if (operator == null && operatorScanCount > 0 && this._scanner.peek != chars.$LBRACE) {
|
2016-06-14 16:26:57 -07:00
|
|
|
operator = lastOperatorToken;
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
// please note that `endToken` is reassigned multiple times below
|
|
|
|
// so please do not optimize the if statements into if/elseif
|
|
|
|
var startTokenOrAst: CssToken|CssAst = null;
|
|
|
|
var endTokenOrAst: CssToken|CssAst = null;
|
|
|
|
if (selectorCssTokens.length > 0) {
|
|
|
|
startTokenOrAst = startTokenOrAst || selectorCssTokens[0];
|
|
|
|
endTokenOrAst = selectorCssTokens[selectorCssTokens.length - 1];
|
|
|
|
}
|
|
|
|
if (pseudoSelectors.length > 0) {
|
|
|
|
startTokenOrAst = startTokenOrAst || pseudoSelectors[0];
|
|
|
|
endTokenOrAst = pseudoSelectors[pseudoSelectors.length - 1];
|
|
|
|
}
|
|
|
|
if (isPresent(operator)) {
|
|
|
|
startTokenOrAst = startTokenOrAst || operator;
|
|
|
|
endTokenOrAst = operator;
|
|
|
|
}
|
|
|
|
|
|
|
|
var span = this._generateSourceSpan(startTokenOrAst, endTokenOrAst);
|
|
|
|
return new CssSimpleSelectorAst(span, selectorCssTokens, strValue, pseudoSelectors, operator);
|
2016-06-14 16:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseSelector(delimiters: number): CssSelectorAst {
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= COMMA_DELIM_FLAG;
|
2016-06-14 16:26:57 -07:00
|
|
|
this._scanner.setMode(CssLexerMode.SELECTOR);
|
|
|
|
|
2016-06-16 11:20:27 -07:00
|
|
|
var simpleSelectors: CssSimpleSelectorAst[] = [];
|
2016-06-14 16:26:57 -07:00
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
|
|
|
simpleSelectors.push(this._parseSimpleSelector(delimiters));
|
|
|
|
this._scanner.consumeWhitespace();
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var firstSelector = simpleSelectors[0];
|
|
|
|
var lastSelector = simpleSelectors[simpleSelectors.length - 1];
|
|
|
|
var span = this._generateSourceSpan(firstSelector, lastSelector);
|
|
|
|
return new CssSelectorAst(span, simpleSelectors);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseValue(delimiters: number): CssStyleValueAst {
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= RBRACE_DELIM_FLAG | SEMICOLON_DELIM_FLAG | NEWLINE_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.STYLE_VALUE);
|
2016-06-14 16:26:57 -07:00
|
|
|
const start = this._getScannerIndex();
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
var tokens: CssToken[] = [];
|
|
|
|
var wsStr = '';
|
2016-02-02 10:37:08 +02:00
|
|
|
var previous: CssToken;
|
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
2016-06-14 16:26:57 -07:00
|
|
|
var token: CssToken;
|
2016-02-02 10:37:08 +02:00
|
|
|
if (isPresent(previous) && previous.type == CssTokenType.Identifier &&
|
2016-06-21 11:10:25 -07:00
|
|
|
this._scanner.peek == chars.$LPAREN) {
|
2016-02-02 10:37:08 +02:00
|
|
|
token = this._consume(CssTokenType.Character, '(');
|
|
|
|
tokens.push(token);
|
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.STYLE_VALUE_FUNCTION);
|
|
|
|
|
|
|
|
token = this._scan();
|
|
|
|
tokens.push(token);
|
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.STYLE_VALUE);
|
|
|
|
|
|
|
|
token = this._consume(CssTokenType.Character, ')');
|
|
|
|
tokens.push(token);
|
|
|
|
} else {
|
|
|
|
token = this._scan();
|
2016-06-14 16:26:57 -07:00
|
|
|
if (token.type == CssTokenType.Whitespace) {
|
|
|
|
wsStr += token.strValue;
|
|
|
|
} else {
|
|
|
|
wsStr = '';
|
2016-02-02 10:37:08 +02:00
|
|
|
tokens.push(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previous = token;
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:26:57 -07:00
|
|
|
const end = this._getScannerIndex() - 1;
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.consumeWhitespace();
|
|
|
|
|
|
|
|
var code = this._scanner.peek;
|
2016-06-21 11:10:25 -07:00
|
|
|
if (code == chars.$SEMICOLON) {
|
2016-02-02 10:37:08 +02:00
|
|
|
this._consume(CssTokenType.Character, ';');
|
2016-06-21 11:10:25 -07:00
|
|
|
} else if (code != chars.$RBRACE) {
|
2016-02-02 10:37:08 +02:00
|
|
|
this._error(
|
2016-06-08 16:38:52 -07:00
|
|
|
generateErrorMessage(
|
2016-06-16 13:02:18 -07:00
|
|
|
this._getSourceContent(), `The CSS key/value definition did not end with a semicolon`,
|
2016-06-08 16:38:52 -07:00
|
|
|
previous.strValue, previous.index, previous.line, previous.column),
|
2016-02-02 10:37:08 +02:00
|
|
|
previous);
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var strValue = this._extractSourceContent(start, end);
|
|
|
|
var startToken = tokens[0];
|
|
|
|
var endToken = tokens[tokens.length - 1];
|
|
|
|
var span = this._generateSourceSpan(startToken, endToken);
|
|
|
|
return new CssStyleValueAst(span, tokens, strValue);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_collectUntilDelim(delimiters: number, assertType: CssTokenType = null): CssToken[] {
|
2016-06-28 11:35:59 -07:00
|
|
|
var tokens: CssToken[] = [];
|
2016-02-02 10:37:08 +02:00
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
|
|
|
var val = isPresent(assertType) ? this._consume(assertType) : this._scan();
|
|
|
|
tokens.push(val);
|
|
|
|
}
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseBlock(delimiters: number): CssBlockAst {
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= RBRACE_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var startToken = this._consume(CssTokenType.Character, '{');
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.consumeEmptyStatements();
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var results: CssRuleAst[] = [];
|
2016-02-02 10:37:08 +02:00
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
|
|
|
results.push(this._parseRule(delimiters));
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var endToken = this._consume(CssTokenType.Character, '}');
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.BLOCK);
|
|
|
|
this._scanner.consumeEmptyStatements();
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var span = this._generateSourceSpan(startToken, endToken);
|
|
|
|
return new CssBlockAst(span, results);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseStyleBlock(delimiters: number): CssStylesBlockAst {
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters |= RBRACE_DELIM_FLAG | LBRACE_DELIM_FLAG;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.STYLE_BLOCK);
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var startToken = this._consume(CssTokenType.Character, '{');
|
|
|
|
if (startToken.numValue != chars.$LBRACE) {
|
2016-06-14 16:26:57 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-16 11:20:27 -07:00
|
|
|
var definitions: CssDefinitionAst[] = [];
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.consumeEmptyStatements();
|
|
|
|
|
|
|
|
while (!characterContainsDelimiter(this._scanner.peek, delimiters)) {
|
|
|
|
definitions.push(this._parseDefinition(delimiters));
|
|
|
|
this._scanner.consumeEmptyStatements();
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var endToken = this._consume(CssTokenType.Character, '}');
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
this._scanner.setMode(CssLexerMode.STYLE_BLOCK);
|
|
|
|
this._scanner.consumeEmptyStatements();
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var span = this._generateSourceSpan(startToken, endToken);
|
|
|
|
return new CssStylesBlockAst(span, definitions);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-06-16 11:20:27 -07:00
|
|
|
_parseDefinition(delimiters: number): CssDefinitionAst {
|
2016-02-02 10:37:08 +02:00
|
|
|
this._scanner.setMode(CssLexerMode.STYLE_BLOCK);
|
|
|
|
|
|
|
|
var prop = this._consume(CssTokenType.Identifier);
|
2016-06-16 13:02:18 -07:00
|
|
|
var parseValue: boolean = false;
|
|
|
|
var value: CssStyleValueAst = null;
|
|
|
|
var endToken: CssToken|CssStyleValueAst = prop;
|
2016-02-02 10:37:08 +02:00
|
|
|
|
|
|
|
// the colon value separates the prop from the style.
|
|
|
|
// there are a few cases as to what could happen if it
|
|
|
|
// is missing
|
|
|
|
switch (this._scanner.peek) {
|
2016-06-21 11:10:25 -07:00
|
|
|
case chars.$SEMICOLON:
|
|
|
|
case chars.$RBRACE:
|
|
|
|
case chars.$EOF:
|
2016-02-02 10:37:08 +02:00
|
|
|
parseValue = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
var propStr = [prop.strValue];
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek != chars.$COLON) {
|
2016-02-02 10:37:08 +02:00
|
|
|
// this will throw the error
|
|
|
|
var nextValue = this._consume(CssTokenType.Character, ':');
|
|
|
|
propStr.push(nextValue.strValue);
|
|
|
|
|
|
|
|
var remainingTokens = this._collectUntilDelim(
|
2016-06-16 11:54:20 -07:00
|
|
|
delimiters | COLON_DELIM_FLAG | SEMICOLON_DELIM_FLAG, CssTokenType.Identifier);
|
2016-02-02 10:37:08 +02:00
|
|
|
if (remainingTokens.length > 0) {
|
|
|
|
remainingTokens.forEach((token) => { propStr.push(token.strValue); });
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
endToken = prop =
|
|
|
|
new CssToken(prop.index, prop.column, prop.line, prop.type, propStr.join(' '));
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// this means we've reached the end of the definition and/or block
|
2016-06-21 11:10:25 -07:00
|
|
|
if (this._scanner.peek == chars.$COLON) {
|
2016-02-02 10:37:08 +02:00
|
|
|
this._consume(CssTokenType.Character, ':');
|
|
|
|
parseValue = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parseValue) {
|
|
|
|
value = this._parseValue(delimiters);
|
2016-06-16 13:02:18 -07:00
|
|
|
endToken = value;
|
2016-02-02 10:37:08 +02:00
|
|
|
} else {
|
2016-06-08 16:38:52 -07:00
|
|
|
this._error(
|
|
|
|
generateErrorMessage(
|
2016-06-16 13:02:18 -07:00
|
|
|
this._getSourceContent(), `The CSS property was not paired with a style value`,
|
2016-06-08 16:38:52 -07:00
|
|
|
prop.strValue, prop.index, prop.line, prop.column),
|
|
|
|
prop);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 13:02:18 -07:00
|
|
|
var span = this._generateSourceSpan(prop, endToken);
|
|
|
|
return new CssDefinitionAst(span, prop, value);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_assertCondition(status: boolean, errorMessage: string, problemToken: CssToken): boolean {
|
|
|
|
if (!status) {
|
|
|
|
this._error(errorMessage, problemToken);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:25:22 -07:00
|
|
|
/** @internal */
|
2016-02-02 10:37:08 +02:00
|
|
|
_error(message: string, problemToken: CssToken) {
|
|
|
|
var length = problemToken.strValue.length;
|
2016-06-08 16:38:52 -07:00
|
|
|
var error = CssParseError.create(
|
|
|
|
this._file, 0, problemToken.line, problemToken.column, length, message);
|
2016-02-02 10:37:08 +02:00
|
|
|
this._errors.push(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CssParseError extends ParseError {
|
2016-06-08 16:38:52 -07:00
|
|
|
static create(
|
|
|
|
file: ParseSourceFile, offset: number, line: number, col: number, length: number,
|
|
|
|
errMsg: string): CssParseError {
|
2016-02-02 10:37:08 +02:00
|
|
|
var start = new ParseLocation(file, offset, line, col);
|
2016-06-16 13:02:18 -07:00
|
|
|
var end = new ParseLocation(file, offset, line, col + length);
|
2016-02-02 10:37:08 +02:00
|
|
|
var span = new ParseSourceSpan(start, end);
|
2016-06-08 16:38:52 -07:00
|
|
|
return new CssParseError(span, 'CSS Parse Error: ' + errMsg);
|
2016-02-02 10:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(span: ParseSourceSpan, message: string) { super(span, message); }
|
|
|
|
}
|