@@ -52,51 +52,75 @@ public DomParser(CssParser cssParser)
5252 }
5353
5454 /// <summary>
55- /// Generate css tree by parsing the given html and applying the given css style data on it.
55+ /// Parses the html and loads everything the box tree is built from: the style sheets of the
56+ /// document and its <c>@font-face</c> fonts. The only step that does I/O.
5657 /// </summary>
5758 /// <param name="html">the html to parse</param>
5859 /// <param name="htmlContainer">the html container to use for reference resolve</param>
59- /// <param name="cssData">the css data to use </param>
60- /// <returns>the root of the generated tree </returns>
61- public async Task < ( CssBox root , CssData cssData ) > GenerateCssTree ( string html , HtmlContainerInt htmlContainer , CssData cssData )
60+ /// <param name="cssData">the base css data, the document's style sheets are added to a copy </param>
61+ /// <returns>the parsed document, to pass to <see cref="BuildCssTree"/>, and the css data with the document's style sheets </returns>
62+ public async Task < ( CssBox root , CssData cssData ) > LoadAsync ( string html , HtmlContainerInt htmlContainer , CssData cssData )
6263 {
63- var root = HtmlParser . ParseDocument ( html ) ;
64+ var root = ParseDocument ( html , htmlContainer ) ;
6465 if ( root != null )
6566 {
66- root . HtmlContainer = htmlContainer ;
67-
6867 ( cssData , _ ) = await CascadeParseStyles ( root , htmlContainer , cssData , false ) . ConfigureAwait ( false ) ;
6968
7069 await RegisterFontFaces ( cssData , htmlContainer ) . ConfigureAwait ( false ) ;
70+ }
71+ return ( root , cssData ) ;
72+ }
73+
74+ /// <summary>
75+ /// Parses the html again, for building a new box tree from already loaded css data.
76+ /// </summary>
77+ public CssBox ParseDocument ( string html , HtmlContainerInt htmlContainer )
78+ {
79+ var root = HtmlParser . ParseDocument ( html ) ;
80+ if ( root != null )
81+ root . HtmlContainer = htmlContainer ;
82+ return root ;
83+ }
7184
72- // The device @media queries are evaluated against: media type and colour scheme come
73- // from the platform adapter, the viewport from the container's max size (which is the
74- // available width/height at this point; zero means "not known yet", and geometry
75- // conditions then match permissively).
76- var media = MediaQueryContext . FromAdapter (
77- htmlContainer . Adapter , htmlContainer . MaxSize . Width , htmlContainer . MaxSize . Height ) ;
85+ /// <summary>
86+ /// Builds the box tree of a parsed document by applying the css data to it. Synchronous, all
87+ /// resources it needs were loaded by <see cref="LoadAsync"/>. Changes the tree, so each build
88+ /// needs a freshly parsed document.
89+ /// </summary>
90+ /// <param name="root">the parsed document</param>
91+ /// <param name="htmlContainer">the html container to use for reference resolve</param>
92+ /// <param name="cssData">the css data from <see cref="LoadAsync"/></param>
93+ public void BuildCssTree ( CssBox root , HtmlContainerInt htmlContainer , CssData cssData )
94+ {
95+ if ( root == null )
96+ return ;
7897
79- CascadeApplyStyles ( root , cssData , media ) ;
98+ // The device @media queries are evaluated against: media type and colour scheme come
99+ // from the platform adapter, the viewport from the container's max size (which is the
100+ // available width/height at this point; zero means "not known yet", and geometry
101+ // conditions then match permissively).
102+ var media = MediaQueryContext . FromAdapter (
103+ htmlContainer . Adapter , htmlContainer . MaxSize . Width , htmlContainer . MaxSize . Height ) ;
80104
81- SetTextSelectionStyle ( htmlContainer , cssData ) ;
105+ CascadeApplyStyles ( root , cssData , media ) ;
82106
83- CssCounterEngine . ResolveCounters ( root ) ;
107+ SetTextSelectionStyle ( htmlContainer , cssData ) ;
84108
85- CorrectTextBoxes ( root ) ;
109+ CssCounterEngine . ResolveCounters ( root ) ;
86110
87- CorrectImgBoxes ( root ) ;
111+ CorrectTextBoxes ( root ) ;
88112
89- CorrectLineBreaksBlocks ( root ) ;
113+ CorrectImgBoxes ( root ) ;
90114
91- CorrectInlineBoxesParent ( root ) ;
115+ CorrectLineBreaksBlocks ( root ) ;
92116
93- CorrectBlockInsideInline ( root ) ;
117+ CorrectInlineBoxesParent ( root ) ;
94118
95- CorrectInlineBoxesParent ( root ) ;
119+ CorrectBlockInsideInline ( root ) ;
96120
97- CorrectAnonymousTables ( root ) ;
98- }
99- return ( root , cssData ) ;
121+ CorrectInlineBoxesParent ( root ) ;
122+
123+ CorrectAnonymousTables ( root ) ;
100124 }
101125
102126
0 commit comments