Example #1
0
        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());
        }