Пример #1
0
        public static media_query create_from_string(string str, document doc)
        {
            var query  = new media_query();
            var tokens = new List <string>();

            html.split_string(str, tokens, " \t\r\n", "", "(");
            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);
        }
Пример #2
0
        public void CvtUnitsTest()
        {
            var doc = new document(new container_test(), null);

            doc.cvt_units("", 10, out var is_percent);
            var c = new css_length();

            c.fromString("10%"); doc.cvt_units(c, 10, 100);
            c.fromString("10em"); doc.cvt_units(c, 10, 100);
            c.fromString("10pt"); doc.cvt_units(c, 10, 100);
            c.fromString("10in"); doc.cvt_units(c, 10, 100);
            c.fromString("10cm"); doc.cvt_units(c, 10, 100);
            c.fromString("10mm"); doc.cvt_units(c, 10, 100);
            c.fromString("10vm"); doc.cvt_units(c, 10, 100);
            c.fromString("10vh"); doc.cvt_units(c, 10, 100);
            c.fromString("10vmin"); doc.cvt_units(c, 10, 100);
            c.fromString("10vmax"); doc.cvt_units(c, 10, 100);
            c.fromString("10"); doc.cvt_units(c, 10, 100);
        }