88 NodeType : HtmlNodeType ,
99 SourceProcessor : HtmlSourceProcessor
1010} = require ( "../../lib/html/syntax" ) ;
11+ const GenericSourceProcessor = require ( "../../lib/util/SourceProcessor" ) ;
1112const { PrintContext } = require ( "../../lib/util/SourceProcessor" ) ;
1213
1314const CSS = "a {\n\tcolor : #ff0000 ;\n}\n" ;
@@ -22,6 +23,55 @@ const LANGUAGES = [
2223] ;
2324
2425describe ( "SourceProcessor" , ( ) => {
26+ // A language whose printer is a module of its own hands one of these over
27+ // instead of the printer, so a walk that never prints never loads it.
28+ describe ( "deferred printer" , ( ) => {
29+ /**
30+ * @returns {{ processor: EXPECTED_ANY, loads: () => number } } a processor that counts what it was asked for
31+ */
32+ const counting = ( ) => {
33+ let loads = 0 ;
34+ const processor = new GenericSourceProcessor (
35+ ( ) => { } ,
36+ undefined ,
37+ ( ) => {
38+ loads ++ ;
39+ return ( ) => "" ;
40+ }
41+ ) ;
42+ return { processor, loads : ( ) => loads } ;
43+ } ;
44+
45+ it ( "is not asked for by a walk" , ( ) => {
46+ const { processor, loads } = counting ( ) ;
47+ expect ( processor . process ( "x" ) ) . toBeUndefined ( ) ;
48+ expect ( loads ( ) ) . toBe ( 0 ) ;
49+ } ) ;
50+
51+ it ( "is asked for once, by the first print" , ( ) => {
52+ const { processor, loads } = counting ( ) ;
53+ processor . process ( "x" ) ;
54+ processor . process ( "x" , { mode : "minify" } ) ;
55+ expect ( loads ( ) ) . toBe ( 1 ) ;
56+ } ) ;
57+
58+ it ( "is reused by every print after it" , ( ) => {
59+ const { processor, loads } = counting ( ) ;
60+ processor . process ( "x" , { mode : "minify" } ) ;
61+ const first = processor . _printer ;
62+ processor . process ( "x" , { mode : "beautify" } ) ;
63+ expect ( loads ( ) ) . toBe ( 1 ) ;
64+ expect ( processor . _printer ) . toBe ( first ) ;
65+ } ) ;
66+
67+ it ( "stays out of a processor that was handed its printer" , ( ) => {
68+ const printer = ( ) => "" ;
69+ const processor = new GenericSourceProcessor ( ( ) => { } , printer ) ;
70+ processor . process ( "x" , { mode : "minify" } ) ;
71+ expect ( processor . _printer ) . toBe ( printer ) ;
72+ } ) ;
73+ } ) ;
74+
2575 // Dropping the store has to let go of the text, not just stop answering for
2676 // it: a stylesheet prints ~300k nodes, which is the output over again.
2777 describe ( "printed-text store" , ( ) => {
0 commit comments