@@ -6,6 +6,8 @@ import type {
66 StyleItem ,
77} from '../types.js' ;
88import { parseDocument } from '../utils/parseDocument.js' ;
9+ import { sortDocument } from '../utils/sortDocument.js' ;
10+ import { upsertBodySctipts } from './upsertBodySctipts.js' ;
911import { upsertFavicon } from './upsertFavicon.js' ;
1012import { upsertHeadInlineScripts } from './upsertHeadInlineScripts.js' ;
1113import { upsertHeadInlineStyles } from './upsertHeadInlineStyles.js' ;
@@ -18,16 +20,18 @@ export class TemplateParser {
1820 protected readonly document : DefaultTreeAdapterTypes . Document ;
1921 protected readonly head : DefaultTreeAdapterTypes . Element ;
2022 protected readonly body : DefaultTreeAdapterTypes . Element ;
23+ protected readonly html : DefaultTreeAdapterTypes . Element ;
2124
2225 constructor ( htmlSource : string ) {
23- const { document, head, body } = parseDocument ( htmlSource ) ;
26+ const { document, head, body, html } = parseDocument ( htmlSource ) ;
2427 this . document = document ;
28+ this . html = html ;
2529 this . head = head ;
2630 this . body = body ;
2731 }
2832
2933 /**
30- * Upsert the title tag
34+ * Upsert the title tag - inserts at beginning of <head>
3135 * @param title - The title to upsert
3236 * @returns The TemplateParser instance
3337 */
@@ -37,7 +41,7 @@ export class TemplateParser {
3741 }
3842
3943 /**
40- * Upsert the favicon tag
44+ * Upsert the favicon tag - inserts at beginning of <head> after title tag
4145 * @param href - The favicon to upsert
4246 * @param rel - The rel attribute of the favicon tag
4347 * @param attributes - The attributes of the favicon tag
@@ -53,8 +57,8 @@ export class TemplateParser {
5357 }
5458
5559 /**
56- * Upsert the head before html tags
57- * @param tags - The tags to upsert
60+ * Upsert meta tags in < head> - inserts at beginning for SEO priority
61+ * @param tags - The meta tags to upsert
5862 * @returns The TemplateParser instance
5963 */
6064 public upsertHeadMetaTags ( tags : string [ ] ) : TemplateParser {
@@ -63,8 +67,8 @@ export class TemplateParser {
6367 }
6468
6569 /**
66- * Upsert the head before styles
67- * @param styles - The styles to upsert
70+ * Upsert external stylesheets in < head> - supports position-based insertion
71+ * @param styles - The external styles to upsert
6872 * @returns The TemplateParser instance
6973 */
7074 public upsertHeadStyles ( styles : StyleItem [ ] ) : TemplateParser {
@@ -73,8 +77,8 @@ export class TemplateParser {
7377 }
7478
7579 /**
76- * Upsert the head inline styles
77- * @param styles - The styles to upsert
80+ * Upsert inline styles in < head> - supports position-based insertion
81+ * @param styles - The inline styles to upsert
7882 * @returns The TemplateParser instance
7983 */
8084 public upsertHeadInlineStyles ( styles : StyleInlineItem [ ] ) : TemplateParser {
@@ -83,8 +87,8 @@ export class TemplateParser {
8387 }
8488
8589 /**
86- * Upsert the head before scripts
87- * @param scripts - The scripts to upsert
90+ * Upsert external scripts in < head> - supports position-based insertion
91+ * @param scripts - The external scripts to upsert
8892 * @returns The TemplateParser instance
8993 */
9094 public upsertHeadScripts ( scripts : ScriptItem [ ] ) : TemplateParser {
@@ -93,8 +97,8 @@ export class TemplateParser {
9397 }
9498
9599 /**
96- * Upsert the inline scripts
97- * @param scripts - The scripts to upsert
100+ * Upsert inline scripts in <head> - supports position-based insertion
101+ * @param scripts - The inline scripts to upsert
98102 * @returns The TemplateParser instance
99103 */
100104 public upsertHeadInlineScripts ( scripts : ScriptInlineItem [ ] ) : TemplateParser {
@@ -103,12 +107,12 @@ export class TemplateParser {
103107 }
104108
105109 /**
106- * Upsert the body after scripts
110+ * Upsert scripts in < body> - supports position-based insertion, typically at end for performance
107111 * @param scripts - The scripts to upsert
108112 * @returns The TemplateParser instance
109113 */
110114 public upsertBodyScripts ( scripts : ScriptItem [ ] ) : TemplateParser {
111- upsertScripts ( this . body , scripts ) ;
115+ upsertBodySctipts ( this . body , scripts ) ;
112116 return this ;
113117 }
114118
@@ -117,7 +121,8 @@ export class TemplateParser {
117121 * @returns The serialized html string
118122 */
119123 public serialize ( ) : string {
120- return serialize ( this . document ) ;
124+ const sortedDocument = sortDocument ( this . document ) ;
125+ return serialize ( sortedDocument ) ;
121126 }
122127
123128 /**
0 commit comments