Пример #1
0
        Size FindSizeAttribute(string input)
        {
            Size size = new Size(_width, _height);

            if (string.IsNullOrEmpty(input))
                return size;

            try
            {
                RequiredAttribute[] attrWidth = new RequiredAttribute[] { new RequiredAttribute("width"), new RequiredAttribute("height") };
                IElementPredicate predicate = new OrPredicate(new BeginTagPredicate("embed", attrWidth), new BeginTagPredicate("object", attrWidth));
                HtmlExtractor ex = new HtmlExtractor(input);
                if (ex.Seek(predicate).Success)
                {
                    BeginTag tag = (BeginTag)ex.Element;
                    size = new Size(Convert.ToInt32(tag.GetAttributeValue("width"), CultureInfo.InvariantCulture), Convert.ToInt32(tag.GetAttributeValue("height"), CultureInfo.InvariantCulture));

                }
            }
            catch (Exception ex)
            {
                Trace.Fail("Exception thrown while trying to find video size: " + ex);
            }

            return size;
        }
Пример #2
0
        internal static IElementPredicate Parse(string criterion)
        {
            SimpleHtmlParser parser = new SimpleHtmlParser(criterion);
            Element el = parser.Next();
            if (el == null)
            {
                Trace.Fail("Criterion was null");
                throw new ArgumentException("Criterion was null");
            }
            if (parser.Next() != null)
            {
                Trace.Fail("Too many criteria");
                throw new ArgumentException("Too many criteria");
            }

            if (el is BeginTag)
            {
                BeginTag tag = (BeginTag)el;

                if (tag.HasResidue || tag.Unterminated)
                {
                    Trace.Fail("Malformed criterion");
                    throw new ArgumentException("Malformed criterion");
                }

                RequiredAttribute[] attributes = new RequiredAttribute[tag.Attributes.Length];
                for (int i = 0; i < attributes.Length; i++)
                    attributes[i] = new RequiredAttribute(tag.Attributes[i].Name, tag.Attributes[i].Value);
                return new BeginTagPredicate(tag.Name, attributes);
            }
            else if (el is EndTag)
            {
                return new EndTagPredicate(((EndTag)el).Name);
            }
            else if (el is Text)
            {
                return new TextPredicate(el.RawText);
            }
            else if (el is Comment)
            {
                return new CommentPredicate(el.RawText);
            }
            else
            {
                Trace.Fail("Invalid criterion \"" + criterion + "\"");
                throw new ArgumentException("Invalid criterion \"" + criterion + "\"");
            }
        }