/* (non-Javadoc) * @see com.itextpdf.styledxmlparser.css.CssNestedAtRule#addStatementToBody(com.itextpdf.styledxmlparser.css.CssStatement) */ public override void AddStatementToBody(CssStatement statement) { if (statement is CssMarginRule) { ((CssMarginRule)statement).SetPageSelectors(pageSelectors); } this.body.Add(statement); }
/// <summary> /// Collects fonts from a /// <see cref="iText.StyledXmlParser.Css.CssStatement"/>. /// </summary> /// <param name="cssStatement">the CSS statement</param> private void CollectFonts(CssStatement cssStatement) { if (cssStatement is CssFontFaceRule) { fonts.Add((CssFontFaceRule)cssStatement); } else { if (cssStatement is CssMediaRule && ((CssMediaRule)cssStatement).MatchMediaDevice(deviceDescription)) { foreach (CssStatement cssSubStatement in ((CssMediaRule)cssStatement).GetStatements()) { CollectFonts(cssSubStatement); } } } }
/// <summary> /// (BNF) at-rule : ATKEYWORD S* any* [ block | ';' S* ]; /// </summary> /// <returns></returns> /// <remarks> /// NOTE: each at-rule might parse differently according to CSS3 /// The @media block for example contains a block of statements /// while other at-rules with a block contain a block of declarations /// </remarks> private CssAtRule ParseAtRule() { CssAtRule atRule = new CssAtRule(); int start = this.Position + 1; // start with first char of ident char ch; while (this.Read(out ch) && !Char.IsWhiteSpace(ch)) { // continue consuming } atRule.Ident = this.Copy(start); while (this.Read(out ch) && Char.IsWhiteSpace(ch)) { // consuming whitespace } start = this.Position; // start with current char do { switch (ch) { case '{': //Block Begin { atRule.Value = this.Copy(start); bool containsRuleSets = String.Equals(atRule.Ident, CssAtRule.MediaIdent, StringComparison.Ordinal); while (true) { while (this.Read(out ch) && Char.IsWhiteSpace(ch)) { // consume whitespace } if (ch == '}') { break; } try { if (containsRuleSets) { // includes @media CssStatement statement = this.ParseStatement(); atRule.Block.Values.Add(statement); } else { // includes @font-face, @page this.PutBack(); CssDeclaration declaration = this.ParseDeclaration(); atRule.Block.Values.Add(declaration); } } catch (ParseException ex) { this.errors.Add(ex); while (this.Read(out ch) && ch != '}') { // restabilize on block end } break; } } return(atRule); } case ';': //At-Rule End { atRule.Value = this.Copy(start); return(atRule); } } } while (this.Read(out ch)); throw new UnexpectedEndOfFile("Unclosed At-Rule", this.reader.FilePath, this.reader.Line, this.reader.Column); }
/// <summary> /// (BNF) stylesheet : [ CDO | CDC | S | statement ]*; /// </summary> /// <returns>CSS StyleSheet parse tree</returns> private CssStyleSheet ParseStyleSheet() { CssStyleSheet styleSheet = new CssStyleSheet(); using (this.reader = new LineReader(this.filePath, this.source, CssParser.ReadFilters)) { this.reader.NormalizeWhiteSpace = true; #if DEBUG System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); #endif char ch; while (this.Read(out ch)) { switch (ch) { case '\uFEFF': // BOM (UTF byte order mark) case '\t': //TAB case '\n': //LF case '\r': //CR case ' ': //Space { // skip whitespace continue; } case '<': { // CDO (Char Data Open?) if (!this.Read(out ch) || ch != '-' || !this.Read(out ch) || ch != '-') { throw new SyntaxError("Expected \"<!--\"", this.reader.FilePath, this.reader.Line, this.reader.Column); } continue; } case '-': { // CDC (Char Data Close?) if (!this.Read(out ch) || ch != '-' || !this.Read(out ch) || ch != '>') { throw new SyntaxError("Expected \"-->\"", this.reader.FilePath, this.reader.Line, this.reader.Column); } continue; } default: { try { CssStatement statement = this.ParseStatement(); styleSheet.Statements.Add(statement); } catch (ParseException ex) { this.errors.Add(ex); while (this.Read(out ch) && ch != '}') { // restabilize on next statement } } continue; } } } #if DEBUG watch.Stop(); Console.WriteLine("CSS parse duration: {0} ms for {1} chars", watch.ElapsedMilliseconds, this.reader.Length); #endif } this.reader = null; this.source = null; return(styleSheet); }