Ejemplo n.º 1
0
        /// <summary>
        ///  desrialized rule list. ImportRule is at the beginning of file, and should be ignored.
        /// </summary>
        /// <param name="cssText"></param>
        /// <param name="basehref">the base href for import rule. </param>
        /// <returns></returns>
        public static CSSRuleList deserializeRuleList(string cssText, string basehref)
        {
            CSSRuleList rules = new CSSRuleList();

            CSSFileScanner filescanner = new CSSFileScanner(cssText);

            CSSFileScannerResult result = filescanner.ReadNext();

            while (result != null)
            {
                if (result.type == enumCSSRuleType.STYLE_RULE)
                {
                    CSSStyleRule stylerule = deserializeCSSStyleRule(result.cssText);
                    if (stylerule != null)
                    {
                        rules.appendRule(stylerule);
                    }
                }
                else if (result.type == enumCSSRuleType.IMPORT_RULE)
                {
                    CSSImportRule importrule = deserializeCSSImportRule(result.cssText, basehref);
                    if (importrule != null)
                    {
                        rules.appendRule(importrule);
                    }
                }
                else if (result.type == enumCSSRuleType.MEDIA_RULE)
                {
                    CSSMediaRule mediarule = deserializeCSSMediaRule(result.cssText);
                    if (mediarule != null)
                    {
                        rules.appendRule(mediarule);
                    }
                }
                else
                {
                    //TODO: other rules are not implemented now.
                }

                result = filescanner.ReadNext();
            }

            return(rules);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// read the next rule declaration
        /// </summary>
        /// <returns></returns>
        public CSSFileScannerResult ReadNext()
        {
            int lastread = _currentRead;

            for (int i = lastread; i < _length; i++)
            {
                // check comment, skip till the */
                if (_cssText[i] == 47 && _cssText[i + 1] == 42)
                {
                    int endComment = _cssText.IndexOf("*/", i);
                    i = endComment + 1;    // to be checked.
                    continue;
                }
                else
                {
                    //if null, space or another char that should be ignored, ignore them
                    if (isEscapeChar(_cssText[i]))
                    {
                        continue;
                    }

                    if (isAtRule(_cssText[i]))
                    {
                        int    endposition = 0;
                        string ruletext    = getAtRuleText(i, ref endposition);


                        if (!string.IsNullOrEmpty(ruletext) && endposition > 0)
                        {
                            i            = endposition;
                            _currentRead = endposition + 1; // to be checked + 1 or not.

                            enumCSSRuleType type = getAtRuleType(ruletext);

                            if (type == enumCSSRuleType.reserved)
                            {
                                continue;
                            }
                            else
                            {
                                CSSFileScannerResult result = new CSSFileScannerResult();
                                result.type    = type;
                                result.cssText = ruletext;

                                return(result);
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (isValidIndent(_cssText[i]))
                        {
                            int    endMark  = 0;
                            string ruletext = getNextBlock(i, ref endMark);

                            if (!string.IsNullOrEmpty(ruletext) && endMark > 0)
                            {
                                CSSFileScannerResult result = new CSSFileScannerResult();
                                result.type    = enumCSSRuleType.STYLE_RULE;
                                result.cssText = ruletext;

                                _currentRead = endMark + 1;
                                return(result);
                            }
                        }
                        else
                        {
                            /// nothing match, can not escape, can not read. should throw exception

                            continue;
                        }
                    }
                }
            }

            return(null);
        }