Skip to content

Commit b05cd51

Browse files
committed
Add parser rules for @document
1 parent 7676805 commit b05cd51

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/css/Parser.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Parser.prototype = function(){
127127
this._viewport();
128128
this._skipCruft();
129129
break;
130+
case Tokens.DOCUMENT_SYM:
131+
this._document();
132+
this._skipCruft();
133+
break;
130134
case Tokens.UNKNOWN_SYM: //unknown @ rule
131135
tokenStream.get();
132136
if (!this.options.strict){
@@ -368,6 +372,8 @@ Parser.prototype = function(){
368372
this._font_face();
369373
} else if (tokenStream.peek() == Tokens.VIEWPORT_SYM){
370374
this._viewport();
375+
} else if (tokenStream.peek() == Tokens.DOCUMENT_SYM){
376+
this._document();
371377
} else if (!this._ruleset()){
372378
break;
373379
}
@@ -749,6 +755,92 @@ Parser.prototype = function(){
749755

750756
},
751757

758+
_document: function(){
759+
/*
760+
* document
761+
* : DOCUMENT_SYM S*
762+
* _document_function [ ',' S* _document_function ]* S*
763+
* '{' S* ruleset* '}'
764+
* ;
765+
*/
766+
767+
var tokenStream = this._tokenStream,
768+
token,
769+
tt,
770+
functions = [],
771+
prefix = "";
772+
773+
tokenStream.mustMatch(Tokens.DOCUMENT_SYM);
774+
token = tokenStream.token();
775+
if (/^@\-([^\-]+)\-/.test(token.value)) {
776+
prefix = RegExp.$1;
777+
}
778+
779+
this._readWhitespace();
780+
functions.push(this._document_function());
781+
782+
while(tokenStream.match(Tokens.COMMA)) {
783+
this._readWhitespace();
784+
functions.push(this._document_function());
785+
}
786+
787+
tokenStream.mustMatch(Tokens.LBRACE);
788+
this._readWhitespace();
789+
790+
this.fire({
791+
type: "startdocument",
792+
functions: functions,
793+
prefix: prefix,
794+
line: token.startLine,
795+
col: token.startCol
796+
});
797+
798+
while(true) {
799+
if (tokenStream.peek() == Tokens.PAGE_SYM){
800+
this._page();
801+
} else if (tokenStream.peek() == Tokens.FONT_FACE_SYM){
802+
this._font_face();
803+
} else if (tokenStream.peek() == Tokens.VIEWPORT_SYM){
804+
this._viewport();
805+
} else if (tokenStream.peek() == Tokens.MEDIA_SYM){
806+
this._media();
807+
} else if (!this._ruleset()){
808+
break;
809+
}
810+
}
811+
812+
tokenStream.mustMatch(Tokens.RBRACE);
813+
this._readWhitespace();
814+
815+
this.fire({
816+
type: "enddocument",
817+
functions: functions,
818+
prefix: prefix,
819+
line: token.startLine,
820+
col: token.startCol
821+
});
822+
},
823+
824+
_document_function: function(){
825+
/*
826+
* document_function
827+
* : function | URI S*
828+
* ;
829+
*/
830+
831+
var tokenStream = this._tokenStream,
832+
value;
833+
834+
if (tokenStream.match(Tokens.URI)) {
835+
value = tokenStream.token().value;
836+
this._readWhitespace();
837+
} else {
838+
value = this._function();
839+
}
840+
841+
return value;
842+
},
843+
752844
_operator: function(inFunction){
753845

754846
/*

0 commit comments

Comments
 (0)