/// <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); var feature = CreateFeature(ref token); if (feature != null) { medium.AppendChild(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.EndOfFile); return medium; }
/// <summary> /// Scans the current medium for the @media or @import rule. /// </summary> public CssMedium CreateMedium(ref CssToken token) { var medium = new CssMedium(); if (token.Type == CssTokenType.Ident) { var identifier = token.Data; if (identifier.Equals(Keywords.Not, StringComparison.OrdinalIgnoreCase)) { medium.IsInverse = true; token = _tokenizer.Get(); } else if (identifier.Equals(Keywords.Only, StringComparison.OrdinalIgnoreCase)) { medium.IsExclusive = true; token = _tokenizer.Get(); } } if (token.Type == CssTokenType.Ident) { medium.Type = token.Data; token = _tokenizer.Get(); if (token.Type != CssTokenType.Ident || String.Compare(token.Data, Keywords.And, StringComparison.OrdinalIgnoreCase) != 0) return medium; token = _tokenizer.Get(); } do { if (token.Type != CssTokenType.RoundBracketOpen) return null; token = _tokenizer.Get(); var couldSetConstraint = TrySetConstraint(medium, ref token); if (token.Type != CssTokenType.RoundBracketClose) return null; token = _tokenizer.Get(); if (couldSetConstraint == false) return null; if (token.Type != CssTokenType.Ident || String.Compare(token.Data, Keywords.And, StringComparison.OrdinalIgnoreCase) != 0) break; token = _tokenizer.Get(); } while (token.Type != CssTokenType.Eof); return medium; }
/// <summary> /// Adds the given medium to the list of media. /// </summary> /// <param name="medium">The medium to add.</param> public void Add(CssMedium medium) { _media.Add(medium); }