Esempio n. 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);
        }
Esempio n. 2
0
        public void MediaQueryCheckTest()
        {
            media_query_expression e;
            media_features         k;

            e = new media_query_expression {
                feature = media_feature.width, val = 100
            };
            k = new media_features {
                width = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                width = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_width, val = 100
            };
            k = new media_features {
                width = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                width = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_width, val = 100
            };
            k = new media_features {
                width = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.height, val = 100
            };
            k = new media_features {
                height = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                height = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_height, val = 100
            };
            k = new media_features {
                height = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                height = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_height, val = 100
            };
            k = new media_features {
                height = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                height = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.depth, val = 100
            };
            k = new media_features {
                depth = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                depth = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                depth = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_depth, val = 100
            };
            k = new media_features {
                depth = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                depth = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                depth = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_depth, val = 100
            };
            k = new media_features {
                depth = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                depth = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                depth = 500
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.device_width, val = 100
            };
            k = new media_features {
                device_width = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_width = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_device_width, val = 100
            };
            k = new media_features {
                device_width = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_width = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_device_width, val = 100
            };
            k = new media_features {
                device_width = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.device_height, val = 100
            };
            k = new media_features {
                device_height = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_height = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_device_height, val = 100
            };
            k = new media_features {
                device_height = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_height = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_device_height, val = 100
            };
            k = new media_features {
                device_height = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_height = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.device_depth, val = 100
            };
            k = new media_features {
                device_depth = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_depth = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_depth = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_device_depth, val = 100
            };
            k = new media_features {
                device_depth = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_depth = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_depth = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_device_depth, val = 100
            };
            k = new media_features {
                device_depth = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_depth = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_depth = 500
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.orientation, val = (int)media_orientation.portrait
            };
            k = new media_features {
                width = 0, height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 100, height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500, height = 100
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.orientation, val = (int)media_orientation.landscape
            };
            k = new media_features {
                width = 0, height = 100
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                width = 100, height = 100
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                width = 500, height = 100
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.aspect_ratio, val = 100, val2 = 100
            };
            k = new media_features {
                width = 0, height = 100
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                width = 100, height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500, height = 100
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_aspect_ratio, val = 100, val2 = 100
            };
            k = new media_features {
                width = 0, height = 100
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                width = 100, height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500, height = 100
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_aspect_ratio, val = 100, val2 = 100
            };
            k = new media_features {
                width = 0, height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 100, height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                width = 500, height = 100
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.device_aspect_ratio, val = 100, val2 = 100
            };
            k = new media_features {
                device_width = 0, device_height = 100
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_width = 100, device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 500, device_height = 100
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_device_aspect_ratio, val = 100, val2 = 100
            };
            k = new media_features {
                device_width = 0, device_height = 100
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                device_width = 100, device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 500, device_height = 100
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_device_aspect_ratio, val = 100, val2 = 100
            };
            k = new media_features {
                device_width = 0, device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 100, device_height = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                device_width = 500, device_height = 100
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.color, val = 100
            };
            k = new media_features {
                color = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                color = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_color, val = 100
            };
            k = new media_features {
                color = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                color = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_color, val = 100
            };
            k = new media_features {
                color = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color = 500
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.color_index, val = 100
            };
            k = new media_features {
                color_index = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                color_index = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color_index = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_color_index, val = 100
            };
            k = new media_features {
                color_index = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                color_index = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color_index = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_color_index, val = 100
            };
            k = new media_features {
                color_index = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color_index = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                color_index = 500
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.monochrome, val = 100
            };
            k = new media_features {
                monochrome = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                monochrome = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                monochrome = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_monochrome, val = 100
            };
            k = new media_features {
                monochrome = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                monochrome = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                monochrome = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_monochrome, val = 100
            };
            k = new media_features {
                monochrome = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                monochrome = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                monochrome = 500
            }; Assert.IsFalse(e.check(k));

            e = new media_query_expression {
                feature = media_feature.resolution, val = 100
            };
            k = new media_features {
                resolution = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                resolution = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                resolution = 500
            }; Assert.IsFalse(e.check(k));
            e = new media_query_expression {
                feature = media_feature.min_resolution, val = 100
            };
            k = new media_features {
                resolution = 0
            }; Assert.IsFalse(e.check(k));
            k = new media_features {
                resolution = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                resolution = 500
            }; Assert.IsTrue(e.check(k));
            e = new media_query_expression {
                feature = media_feature.max_resolution, val = 100
            };
            k = new media_features {
                resolution = 0
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                resolution = 100
            }; Assert.IsTrue(e.check(k));
            k = new media_features {
                resolution = 500
            }; Assert.IsFalse(e.check(k));
        }