/// <summary> /// Tries to read and set media constraints for the provided medium. /// </summary> Boolean TrySetConstraint(CssMedium medium, ref CssToken token) { if (token.Type != CssTokenType.Ident) { _tokenizer.JumpToClosedArguments(); token = _tokenizer.Get(); return(false); } var value = Pool.NewValueBuilder(); var featureName = token.Data; var val = CssValue.Empty; var feature = _parser.Options.IsToleratingInvalidConstraints ? new UnknownMediaFeature(featureName) : Factory.MediaFeatures.Create(featureName); token = _tokenizer.Get(); if (token.Type == CssTokenType.Colon) { _tokenizer.State = CssParseMode.Value; token = _tokenizer.Get(); while (token.Type != CssTokenType.RoundBracketClose || value.IsReady == false) { if (token.Type == CssTokenType.Eof) { break; } value.Apply(token); token = _tokenizer.Get(); } _tokenizer.State = CssParseMode.Data; val = value.ToPool(); } else if (token.Type == CssTokenType.Eof) { return(false); } if (feature != null && feature.TrySetValue(val)) { medium.AddConstraint(feature); return(true); } return(false); }
/// <summary> /// Scans the current medium for the @media or @import rule. /// </summary> public CssMedium CreateMedium(ref CssToken token) { var medium = new CssMedium(); CollectTrivia(ref token); if (token.Type == CssTokenType.Ident) { var identifier = token.Data; if (identifier.Isi(Keywords.Not)) { medium.IsInverse = true; token = NextToken(); CollectTrivia(ref token); } else if (identifier.Isi(Keywords.Only)) { medium.IsExclusive = true; token = NextToken(); CollectTrivia(ref token); } } if (token.Type == CssTokenType.Ident) { medium.Type = token.Data; token = NextToken(); CollectTrivia(ref token); if (token.Type != CssTokenType.Ident || !token.Data.Isi(Keywords.And)) { return(medium); } token = NextToken(); CollectTrivia(ref token); } do { if (token.Type != CssTokenType.RoundBracketOpen) { return(null); } token = NextToken(); CollectTrivia(ref token); CreateNewNode(); var feature = CloseNode(CreateFeature(ref token)); if (feature != null) { medium.AddConstraint(feature); } if (token.Type != CssTokenType.RoundBracketClose) { return(null); } token = NextToken(); CollectTrivia(ref token); if (feature == null) { return(null); } if (token.Type != CssTokenType.Ident || !token.Data.Isi(Keywords.And)) { break; } token = NextToken(); CollectTrivia(ref token); }while (token.Type != CssTokenType.Eof); return(medium); }