parserlib: simplify/flatten _keyframe

This commit is contained in:
tophf 2022-07-01 10:29:43 +03:00
parent b742ed2c65
commit bd2b435781

View File

@ -4374,63 +4374,37 @@ self.parserlib = (() => {
_keyframes(start) { _keyframes(start) {
const stream = this._tokenStream; const stream = this._tokenStream;
const prefix = rxVendorPrefix.test(start.value) ? RegExp.$1 : ''; const prefix = rxVendorPrefix.test(start.value) ? RegExp.$1 : '';
this._ws(); const name = SyntaxUnit.fromToken(stream.mustMatch(TT.identString));
const name = this._keyframeName();
stream.mustMatch(Tokens.LBRACE); stream.mustMatch(Tokens.LBRACE);
this.fire({type: 'startkeyframes', name, prefix}, start); this.fire({type: 'startkeyframes', name, prefix}, start);
// check for key // check for key
while (true) { while (true) {
this._ws(); this._ws();
const tt = stream.peek(); const keys = [this._key(true)];
if (tt !== Tokens.IDENT && tt !== Tokens.PERCENTAGE) break; if (!keys[0]) break;
this._keyframeRule(); while (stream.match(Tokens.COMMA)) {
this._ws();
keys.push(this._key());
}
this.fire({type: 'startkeyframerule', keys}, keys[0]);
this._readDeclarations();
this.fire({type: 'endkeyframerule', keys});
} }
stream.mustMatch(Tokens.RBRACE); stream.mustMatch(Tokens.RBRACE);
this.fire({type: 'endkeyframes', name, prefix}); this.fire({type: 'endkeyframes', name, prefix});
this._ws(); this._ws();
} }
_keyframeName() { _key(optional) {
const stream = this._tokenStream; const stream = this._tokenStream;
stream.mustMatch(TT.identString); const token = stream.match(Tokens.PERCENTAGE) || stream.match(Tokens.IDENT, ['from', 'to']);
return SyntaxUnit.fromToken(stream._token); if (token) {
}
_keyframeRule() {
const keys = this._keyList();
this.fire({type: 'startkeyframerule', keys}, keys[0]);
this._readDeclarations();
this.fire({type: 'endkeyframerule', keys});
}
_keyList() {
const stream = this._tokenStream;
const keyList = [];
// must be least one key
keyList.push(this._key());
this._ws(); this._ws();
while (stream.match(Tokens.COMMA)) { return SyntaxUnit.fromToken(token);
this._ws(); } else if (!optional) {
keyList.push(this._key());
this._ws();
}
return keyList;
}
_key() {
const stream = this._tokenStream;
if (stream.match(Tokens.PERCENTAGE)) {
return SyntaxUnit.fromToken(stream._token);
}
if (stream.match(Tokens.IDENT)) {
if (/^(from|to)$/i.test(stream._token.value)) {
return SyntaxUnit.fromToken(stream._token);
}
stream.unget();
}
// if it gets here, there wasn't a valid token, so time to explode
stream.throwUnexpected(stream.LT(1), ['%', "'from'", "'to'"]); stream.throwUnexpected(stream.LT(1), ['%', "'from'", "'to'"]);
} }
}
_skipCruft() { _skipCruft() {
while (this._tokenStream.match(TT.cruft)) { /*NOP*/ } while (this._tokenStream.match(TT.cruft)) { /*NOP*/ }