public string Serialize() { if (!IsValid) { return(string.Empty); } if (!Lookup.TryKeyword(Values[0].AsEnum <EMediaFeatureName>(), out string Name)) { throw new CssSyntaxErrorException("Could not find media-feature name"); } StringBuilder sb = new StringBuilder(); /* 1) Append a "(" (U+0028), followed by the media feature name, converted to ASCII lowercase, to s. */ sb.Append(UnicodeCommon.CHAR_LEFT_PARENTHESES); if (Context == EMediaFeatureContext.Boolean) { sb.Append(Name); } else if (Context == EMediaFeatureContext.Range) { sb.Append(Name); if (Operators.Length == 0 && Values.Length == 2) { /* If a value is given append a ":" (U+003A), followed by a single SPACE (U+0020), followed by the serialized media feature value. */ sb.Append(UnicodeCommon.CHAR_COLON); sb.Append(UnicodeCommon.CHAR_SPACE); sb.Append(Values[1].Serialize()); } else { /* Serialize all value/operator pairs */ for (int i = 1; i < Operators.Length; i++) { CssValue B = Values[i]; EMediaOperator op = Operators[i - 1]; if (!Lookup.TryKeyword(op, out string outComparator)) { throw new CssSyntaxErrorException("Unable to find the specified comparator within the CSS enum LUT"); } sb.Append(B.Serialize()); sb.Append(outComparator); } } } /* 3) Append a ")" (U+0029) to s. */ sb.Append(UnicodeCommon.CHAR_RIGHT_PARENTHESES); return(sb.ToString()); }
static bool Compare(Document document, CssValue ValueA, CssValue ValueB, EMediaOperator Op) { /* An Excerpt: * "If a media feature references a concept which does not exist on the device where the UA is running (for example, speech UAs do not have a concept of “width”), the media feature must always evaluate to false." */ double A, B; /* First resolve these values to numbers we can compare */ if (ValueA.Type == ECssValueTypes.KEYWORD) { if (!Lookup.TryEnum(ValueA.AsString(), out EMediaFeatureName _)) {/* If we cant find this keyword then we treat it as if it were an unsupported feature */ return(false); } A = (double)Resolve_Media_Name_Value(document, ValueA.AsEnum <EMediaFeatureName>()); } else { A = ValueA.AsDecimal(); } if (ValueB.Type == ECssValueTypes.KEYWORD) { if (!Lookup.TryEnum(ValueB.AsString(), out EMediaFeatureName _)) {/* If we cant find this keyword then we treat it as if it were an unsupported feature */ return(false); } B = (double)Resolve_Media_Name_Value(document, ValueB.AsEnum <EMediaFeatureName>()); } else { B = ValueB.AsDecimal(); } /* Compare them and return the result */ switch (Op) { case EMediaOperator.EqualTo: { return(A == B); } case EMediaOperator.LessThan: { return(A < B); } case EMediaOperator.GreaterThan: { return(A > B); } case EMediaOperator.LessThanEq: { return(A < B || (A == B)); } case EMediaOperator.GreaterThanEq: { return(A > B || (A == B)); } } return(false); }