Beispiel #1
0
        public static media_query create_from_string(string str, document doc)
        {
            var tokens = new List <string>();

            html.split_string(str, tokens, " \t\r\n", "", "(");

            var query = new media_query();

            for (var i = 0; i < tokens.Count; i++)
            {
                var tok = tokens[i];
                if (tok == "not")
                {
                    query._not = true;
                }
                else if (tok[0] == '(')
                {
                    tok = tok.Substring(1);
                    if (tok[tok.Length - 1] == ')')
                    {
                        tok = tok.Remove(tok.Length - 1, 1);
                    }
                    var expr        = new media_query_expression();
                    var expr_tokens = new List <string>();
                    html.split_string(tok, expr_tokens, ":");
                    if (expr_tokens.Count != 0)
                    {
                        expr_tokens[0] = expr_tokens[0].Trim();
                        expr.feature   = (media_feature)html.value_index(expr_tokens[0], types.media_feature_strings, (int)media_feature.none);
                        if (expr.feature != media_feature.none)
                        {
                            if (expr_tokens.Count == 1)
                            {
                                expr.check_as_bool = true;
                            }
                            else
                            {
                                expr_tokens[1]     = expr_tokens[1].Trim();
                                expr.check_as_bool = false;
                                if (expr.feature == media_feature.orientation)
                                {
                                    expr.val = html.value_index(expr_tokens[1], types.media_orientation_strings, (int)media_orientation.landscape);
                                }
                                else
                                {
                                    var slash_pos = expr_tokens[1].IndexOf('/');
                                    if (slash_pos != -1)
                                    {
                                        var val1 = expr_tokens[1].Substring(0, slash_pos).Trim();
                                        var val2 = expr_tokens[1].Substring(slash_pos + 1).Trim();
                                        expr.val  = int.TryParse(val1, out var v) ? v : 0;
                                        expr.val2 = int.TryParse(val2, out v) ? v : 0;
                                    }
                                    else
                                    {
                                        var length = new css_length();
                                        length.fromString(expr_tokens[1]);
                                        if (length.units == css_units.dpcm)
                                        {
                                            expr.val = (int)(length.val * 2.54);
                                        }
                                        else if (length.units == css_units.dpi)
                                        {
                                            expr.val = (int)(length.val * 2.54);
                                        }
                                        else
                                        {
                                            if (doc != null)
                                            {
                                                doc.cvt_units(length, doc.container.get_default_font_size());
                                            }
                                            expr.val = (int)length.val;
                                        }
                                    }
                                }
                            }
                            query._expressions.Add(expr);
                        }
                    }
                }
                else
                {
                    query._media_type = (media_type)html.value_index(tok, types.media_type_strings, (int)media_type.all);
                }
            }
            return(query);
        }
Beispiel #2
0
 public media_query(media_query val)
 {
     _not         = val._not;
     _expressions = val._expressions;
     _media_type  = val._media_type;
 }