Skip to main content

fallow_extract/
html.rs

1//! HTML file parsing for script, stylesheet, and Angular template references.
2//!
3//! Extracts `<script src="...">` and `<link rel="stylesheet" href="...">` references
4//! from HTML files, creating graph edges so that referenced JS/CSS assets (and their
5//! transitive imports) are reachable from the HTML entry point.
6//!
7//! Also scans for Angular template syntax (`{{ }}`, `[prop]`, `(event)`, `@if`, etc.)
8//! and stores referenced identifiers as `MemberAccess` entries with a sentinel object,
9//! enabling the analysis phase to credit component class members used in external templates.
10
11use std::path::Path;
12use std::sync::LazyLock;
13
14use oxc_span::Span;
15
16use crate::asset_url::normalize_asset_url;
17use crate::sfc_template::angular::{self, ANGULAR_TPL_SENTINEL};
18use crate::{ImportInfo, ImportedName, MemberAccess, ModuleInfo};
19use fallow_types::discover::FileId;
20
21/// Regex to match HTML comments (`<!-- ... -->`) for stripping before extraction.
22static HTML_COMMENT_RE: LazyLock<regex::Regex> =
23    LazyLock::new(|| crate::static_regex(r"(?s)<!--.*?-->"));
24
25/// Regex to extract `src` attribute from `<script>` tags.
26/// Matches both `<script src="...">` and `<script type="module" src="...">`.
27/// Uses `(?s)` so `.` matches newlines (multi-line attributes).
28static SCRIPT_SRC_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
29    crate::static_regex(r#"(?si)<script\b(?:[^>"']|"[^"]*"|'[^']*')*?\bsrc\s*=\s*["']([^"']+)["']"#)
30});
31
32/// Regex to extract `href` attribute from `<link>` tags with `rel="stylesheet"` or
33/// `rel="modulepreload"`.
34/// Handles attributes in any order (rel before or after href).
35static LINK_HREF_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
36    crate::static_regex(
37        r#"(?si)<link\b(?:[^>"']|"[^"]*"|'[^']*')*?\brel\s*=\s*["'](stylesheet|modulepreload)["'](?:[^>"']|"[^"]*"|'[^']*')*?\bhref\s*=\s*["']([^"']+)["']"#,
38    )
39});
40
41/// Regex for the reverse attribute order: href before rel.
42static LINK_HREF_REVERSE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
43    crate::static_regex(
44        r#"(?si)<link\b(?:[^>"']|"[^"]*"|'[^']*')*?\bhref\s*=\s*["']([^"']+)["'](?:[^>"']|"[^"]*"|'[^']*')*?\brel\s*=\s*["'](stylesheet|modulepreload)["']"#,
45    )
46});
47
48/// Check if a path is an HTML file.
49pub(crate) fn is_html_file(path: &Path) -> bool {
50    path.extension()
51        .and_then(|e| e.to_str())
52        .is_some_and(|ext| ext == "html")
53}
54
55/// Returns true if an HTML asset reference is a remote URL that should be skipped.
56pub(crate) fn is_remote_url(src: &str) -> bool {
57    src.starts_with("http://")
58        || src.starts_with("https://")
59        || src.starts_with("//")
60        || src.starts_with("data:")
61}
62
63/// Build-time template placeholders that aren't valid import specifiers and
64/// never resolve to a real file. Skip them at extraction time so they don't
65/// enter the import graph as unresolvable specifiers.
66///
67/// - `{{ ... }}` covers Handlebars (Ember `index.html`'s `{{rootURL}}`,
68///   `{{config.assetsPath}}`), Mustache (Jekyll, Hugo), Jinja2 (Pelican /
69///   11ty plugins), and pre-compiled Vue / Angular templates whose
70///   interpolation has leaked into a checked-in HTML scaffold.
71/// - `###...###` covers ember-cli blueprint scaffold placeholders
72///   (`###APPNAME###`, `###DUMMY###`) checked in as addon-fixture templates.
73///
74/// Neither shape is a legal URL or path character outside template engines,
75/// so the skip is generic across frameworks rather than gated on a plugin.
76/// Returns `true` for any `src` / `href` value that contains either marker.
77pub(crate) fn is_template_placeholder(value: &str) -> bool {
78    value.contains("{{") || value.contains("###")
79}
80
81/// Extract local (non-remote) asset references from HTML-like markup.
82///
83/// Returns the raw `src`/`href` strings (trimmed, remote URLs filtered). Shared
84/// between the HTML file parser and the JS/TS visitor's tagged template
85/// literal override so `` html`<script src="...">` `` in Hono/lit-html/htm
86/// layouts emits the same asset edges as a real `.html` file.
87pub(crate) fn collect_asset_refs(source: &str) -> Vec<String> {
88    let stripped = HTML_COMMENT_RE.replace_all(source, "");
89    let mut refs: Vec<String> = Vec::new();
90
91    for cap in SCRIPT_SRC_RE.captures_iter(&stripped) {
92        if let Some(m) = cap.get(1) {
93            let src = m.as_str().trim();
94            if !src.is_empty() && !is_remote_url(src) && !is_template_placeholder(src) {
95                refs.push(src.to_string());
96            }
97        }
98    }
99
100    for cap in LINK_HREF_RE.captures_iter(&stripped) {
101        if let Some(m) = cap.get(2) {
102            let href = m.as_str().trim();
103            if !href.is_empty() && !is_remote_url(href) && !is_template_placeholder(href) {
104                refs.push(href.to_string());
105            }
106        }
107    }
108    for cap in LINK_HREF_REVERSE_RE.captures_iter(&stripped) {
109        if let Some(m) = cap.get(1) {
110            let href = m.as_str().trim();
111            if !href.is_empty() && !is_remote_url(href) && !is_template_placeholder(href) {
112                refs.push(href.to_string());
113            }
114        }
115    }
116
117    refs
118}
119
120/// Parse an HTML file, extracting script and stylesheet references as imports.
121#[cfg(test)]
122pub(crate) fn parse_html_to_module(file_id: FileId, source: &str, content_hash: u64) -> ModuleInfo {
123    parse_html_to_module_with_complexity(file_id, source, content_hash, false)
124}
125
126/// Parse an HTML file and optionally compute Angular template complexity.
127pub(crate) fn parse_html_to_module_with_complexity(
128    file_id: FileId,
129    source: &str,
130    content_hash: u64,
131    need_complexity: bool,
132) -> ModuleInfo {
133    let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
134
135    let mut imports: Vec<ImportInfo> = collect_asset_refs(source)
136        .into_iter()
137        .map(|raw| ImportInfo {
138            source: normalize_asset_url(&raw),
139            imported_name: ImportedName::SideEffect,
140            local_name: String::new(),
141            is_type_only: false,
142            from_style: false,
143            span: Span::default(),
144            source_span: Span::default(),
145        })
146        .collect();
147
148    imports.sort_unstable_by(|a, b| a.source.cmp(&b.source));
149    imports.dedup_by(|a, b| a.source == b.source);
150
151    let angular::AngularTemplateRefs {
152        identifiers,
153        member_accesses: template_member_accesses,
154        security_sinks,
155    } = angular::collect_angular_template_refs(source);
156    let mut member_accesses: Vec<MemberAccess> = identifiers
157        .into_iter()
158        .map(|name| MemberAccess {
159            object: ANGULAR_TPL_SENTINEL.to_string(),
160            member: name,
161        })
162        .collect();
163    member_accesses.extend(template_member_accesses);
164
165    let complexity = if need_complexity {
166        crate::template_complexity::compute_angular_template_complexity(source)
167            .into_iter()
168            .collect()
169    } else {
170        Vec::new()
171    };
172
173    ModuleInfo {
174        file_id,
175        exports: Vec::new(),
176        imports,
177        re_exports: Vec::new(),
178        dynamic_imports: Vec::new(),
179        dynamic_import_patterns: Vec::new(),
180        require_calls: Vec::new(),
181        package_path_references: Vec::new(),
182        member_accesses,
183        whole_object_uses: Vec::new(),
184        has_cjs_exports: false,
185        has_angular_component_template_url: false,
186        content_hash,
187        suppressions: parsed_suppressions.suppressions,
188        unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
189        unused_import_bindings: Vec::new(),
190        type_referenced_import_bindings: Vec::new(),
191        value_referenced_import_bindings: Vec::new(),
192        line_offsets: fallow_types::extract::compute_line_offsets(source),
193        complexity,
194        flag_uses: Vec::new(),
195        class_heritage: vec![],
196        injection_tokens: vec![],
197        local_type_declarations: Vec::new(),
198        public_signature_type_references: Vec::new(),
199        namespace_object_aliases: Vec::new(),
200        iconify_prefixes: Vec::new(),
201        iconify_icon_names: Vec::new(),
202        auto_import_candidates: Vec::new(),
203        directives: Vec::new(),
204        security_sinks,
205        security_sinks_skipped: 0,
206        security_unresolved_callee_sites: Vec::new(),
207        tainted_bindings: Vec::new(),
208        sanitized_sink_args: Vec::new(),
209        security_control_sites: Vec::new(),
210        callee_uses: Vec::new(),
211    }
212}
213
214#[cfg(test)]
215mod tests {
216    use super::*;
217
218    #[test]
219    fn is_html_file_html() {
220        assert!(is_html_file(Path::new("index.html")));
221    }
222
223    #[test]
224    fn is_html_file_nested() {
225        assert!(is_html_file(Path::new("pages/about.html")));
226    }
227
228    #[test]
229    fn is_html_file_rejects_htm() {
230        assert!(!is_html_file(Path::new("index.htm")));
231    }
232
233    #[test]
234    fn is_html_file_rejects_js() {
235        assert!(!is_html_file(Path::new("app.js")));
236    }
237
238    #[test]
239    fn is_html_file_rejects_ts() {
240        assert!(!is_html_file(Path::new("app.ts")));
241    }
242
243    #[test]
244    fn is_html_file_rejects_vue() {
245        assert!(!is_html_file(Path::new("App.vue")));
246    }
247
248    #[test]
249    fn remote_url_http() {
250        assert!(is_remote_url("http://example.com/script.js"));
251    }
252
253    #[test]
254    fn remote_url_https() {
255        assert!(is_remote_url("https://cdn.example.com/style.css"));
256    }
257
258    #[test]
259    fn remote_url_protocol_relative() {
260        assert!(is_remote_url("//cdn.example.com/lib.js"));
261    }
262
263    #[test]
264    fn remote_url_data() {
265        assert!(is_remote_url("data:text/javascript;base64,abc"));
266    }
267
268    #[test]
269    fn local_relative_not_remote() {
270        assert!(!is_remote_url("./src/entry.js"));
271    }
272
273    #[test]
274    fn local_root_relative_not_remote() {
275        assert!(!is_remote_url("/src/entry.js"));
276    }
277
278    #[test]
279    fn extracts_module_script_src() {
280        let info = parse_html_to_module(
281            FileId(0),
282            r#"<script type="module" src="./src/entry.js"></script>"#,
283            0,
284        );
285        assert_eq!(info.imports.len(), 1);
286        assert_eq!(info.imports[0].source, "./src/entry.js");
287    }
288
289    #[test]
290    fn extracts_plain_script_src() {
291        let info = parse_html_to_module(
292            FileId(0),
293            r#"<script src="./src/polyfills.js"></script>"#,
294            0,
295        );
296        assert_eq!(info.imports.len(), 1);
297        assert_eq!(info.imports[0].source, "./src/polyfills.js");
298    }
299
300    #[test]
301    fn extracts_multiple_scripts() {
302        let info = parse_html_to_module(
303            FileId(0),
304            r#"
305            <script type="module" src="./src/entry.js"></script>
306            <script src="./src/polyfills.js"></script>
307            "#,
308            0,
309        );
310        assert_eq!(info.imports.len(), 2);
311    }
312
313    #[test]
314    fn skips_inline_script() {
315        let info = parse_html_to_module(FileId(0), r#"<script>console.log("hello");</script>"#, 0);
316        assert!(info.imports.is_empty());
317    }
318
319    #[test]
320    fn skips_handlebars_placeholder_in_script_src() {
321        let info = parse_html_to_module(
322            FileId(0),
323            r#"<script src="{{rootURL}}assets/app.js"></script>
324               <script src="{{config.assetsPath}}vendor.js"></script>"#,
325            0,
326        );
327        assert!(
328            info.imports.is_empty(),
329            "Handlebars-placeholder script srcs should not enter the import graph; got {:?}",
330            info.imports
331        );
332    }
333
334    #[test]
335    fn skips_handlebars_placeholder_in_link_href() {
336        let info = parse_html_to_module(
337            FileId(0),
338            r#"<link rel="stylesheet" href="{{rootURL}}assets/app.css">"#,
339            0,
340        );
341        assert!(info.imports.is_empty());
342    }
343
344    #[test]
345    fn skips_ember_cli_blueprint_placeholder() {
346        let info = parse_html_to_module(
347            FileId(0),
348            r####"<script src="###APPNAME###/app.js"></script>"####,
349            0,
350        );
351        assert!(info.imports.is_empty());
352    }
353
354    #[test]
355    fn extracts_normal_specifier_alongside_placeholders() {
356        let info = parse_html_to_module(
357            FileId(0),
358            r#"<script src="{{rootURL}}assets/app.js"></script>
359               <script src="./src/main.ts"></script>"#,
360            0,
361        );
362        assert_eq!(info.imports.len(), 1);
363        assert_eq!(info.imports[0].source, "./src/main.ts");
364    }
365
366    #[test]
367    fn skips_remote_script() {
368        let info = parse_html_to_module(
369            FileId(0),
370            r#"<script src="https://cdn.example.com/lib.js"></script>"#,
371            0,
372        );
373        assert!(info.imports.is_empty());
374    }
375
376    #[test]
377    fn skips_protocol_relative_script() {
378        let info = parse_html_to_module(
379            FileId(0),
380            r#"<script src="//cdn.example.com/lib.js"></script>"#,
381            0,
382        );
383        assert!(info.imports.is_empty());
384    }
385
386    #[test]
387    fn extracts_stylesheet_link() {
388        let info = parse_html_to_module(
389            FileId(0),
390            r#"<link rel="stylesheet" href="./src/global.css" />"#,
391            0,
392        );
393        assert_eq!(info.imports.len(), 1);
394        assert_eq!(info.imports[0].source, "./src/global.css");
395    }
396
397    #[test]
398    fn extracts_modulepreload_link() {
399        let info = parse_html_to_module(
400            FileId(0),
401            r#"<link rel="modulepreload" href="./src/vendor.js" />"#,
402            0,
403        );
404        assert_eq!(info.imports.len(), 1);
405        assert_eq!(info.imports[0].source, "./src/vendor.js");
406    }
407
408    #[test]
409    fn extracts_link_with_reversed_attrs() {
410        let info = parse_html_to_module(
411            FileId(0),
412            r#"<link href="./src/global.css" rel="stylesheet" />"#,
413            0,
414        );
415        assert_eq!(info.imports.len(), 1);
416        assert_eq!(info.imports[0].source, "./src/global.css");
417    }
418
419    #[test]
420    fn bare_script_src_normalized_to_relative() {
421        let info = parse_html_to_module(FileId(0), r#"<script src="app.js"></script>"#, 0);
422        assert_eq!(info.imports.len(), 1);
423        assert_eq!(info.imports[0].source, "./app.js");
424    }
425
426    #[test]
427    fn bare_module_script_src_normalized_to_relative() {
428        let info = parse_html_to_module(
429            FileId(0),
430            r#"<script type="module" src="main.ts"></script>"#,
431            0,
432        );
433        assert_eq!(info.imports.len(), 1);
434        assert_eq!(info.imports[0].source, "./main.ts");
435    }
436
437    #[test]
438    fn bare_stylesheet_link_href_normalized_to_relative() {
439        let info = parse_html_to_module(
440            FileId(0),
441            r#"<link rel="stylesheet" href="styles.css" />"#,
442            0,
443        );
444        assert_eq!(info.imports.len(), 1);
445        assert_eq!(info.imports[0].source, "./styles.css");
446    }
447
448    #[test]
449    fn bare_link_href_reversed_attrs_normalized_to_relative() {
450        let info = parse_html_to_module(
451            FileId(0),
452            r#"<link href="styles.css" rel="stylesheet" />"#,
453            0,
454        );
455        assert_eq!(info.imports.len(), 1);
456        assert_eq!(info.imports[0].source, "./styles.css");
457    }
458
459    #[test]
460    fn bare_modulepreload_link_href_normalized_to_relative() {
461        let info = parse_html_to_module(
462            FileId(0),
463            r#"<link rel="modulepreload" href="vendor.js" />"#,
464            0,
465        );
466        assert_eq!(info.imports.len(), 1);
467        assert_eq!(info.imports[0].source, "./vendor.js");
468    }
469
470    #[test]
471    fn bare_asset_with_subdir_normalized_to_relative() {
472        let info = parse_html_to_module(FileId(0), r#"<script src="assets/app.js"></script>"#, 0);
473        assert_eq!(info.imports.len(), 1);
474        assert_eq!(info.imports[0].source, "./assets/app.js");
475    }
476
477    #[test]
478    fn root_absolute_script_src_unchanged() {
479        let info = parse_html_to_module(FileId(0), r#"<script src="/src/main.ts"></script>"#, 0);
480        assert_eq!(info.imports.len(), 1);
481        assert_eq!(info.imports[0].source, "/src/main.ts");
482    }
483
484    #[test]
485    fn parent_relative_script_src_unchanged() {
486        let info = parse_html_to_module(
487            FileId(0),
488            r#"<script src="../shared/vendor.js"></script>"#,
489            0,
490        );
491        assert_eq!(info.imports.len(), 1);
492        assert_eq!(info.imports[0].source, "../shared/vendor.js");
493    }
494
495    #[test]
496    fn skips_preload_link() {
497        let info = parse_html_to_module(
498            FileId(0),
499            r#"<link rel="preload" href="./src/font.woff2" as="font" />"#,
500            0,
501        );
502        assert!(info.imports.is_empty());
503    }
504
505    #[test]
506    fn skips_icon_link() {
507        let info =
508            parse_html_to_module(FileId(0), r#"<link rel="icon" href="./favicon.ico" />"#, 0);
509        assert!(info.imports.is_empty());
510    }
511
512    #[test]
513    fn skips_remote_stylesheet() {
514        let info = parse_html_to_module(
515            FileId(0),
516            r#"<link rel="stylesheet" href="https://fonts.googleapis.com/css" />"#,
517            0,
518        );
519        assert!(info.imports.is_empty());
520    }
521
522    #[test]
523    fn skips_commented_out_script() {
524        let info = parse_html_to_module(
525            FileId(0),
526            r#"<!-- <script src="./old.js"></script> -->
527            <script src="./new.js"></script>"#,
528            0,
529        );
530        assert_eq!(info.imports.len(), 1);
531        assert_eq!(info.imports[0].source, "./new.js");
532    }
533
534    #[test]
535    fn skips_commented_out_link() {
536        let info = parse_html_to_module(
537            FileId(0),
538            r#"<!-- <link rel="stylesheet" href="./old.css" /> -->
539            <link rel="stylesheet" href="./new.css" />"#,
540            0,
541        );
542        assert_eq!(info.imports.len(), 1);
543        assert_eq!(info.imports[0].source, "./new.css");
544    }
545
546    #[test]
547    fn handles_multiline_script_tag() {
548        let info = parse_html_to_module(
549            FileId(0),
550            "<script\n  type=\"module\"\n  src=\"./src/entry.js\"\n></script>",
551            0,
552        );
553        assert_eq!(info.imports.len(), 1);
554        assert_eq!(info.imports[0].source, "./src/entry.js");
555    }
556
557    #[test]
558    fn handles_multiline_link_tag() {
559        let info = parse_html_to_module(
560            FileId(0),
561            "<link\n  rel=\"stylesheet\"\n  href=\"./src/global.css\"\n/>",
562            0,
563        );
564        assert_eq!(info.imports.len(), 1);
565        assert_eq!(info.imports[0].source, "./src/global.css");
566    }
567
568    #[test]
569    fn full_vite_html() {
570        let info = parse_html_to_module(
571            FileId(0),
572            r#"<!doctype html>
573<html>
574  <head>
575    <link rel="stylesheet" href="./src/global.css" />
576    <link rel="icon" href="/favicon.ico" />
577  </head>
578  <body>
579    <div id="app"></div>
580    <script type="module" src="./src/entry.js"></script>
581  </body>
582</html>"#,
583            0,
584        );
585        assert_eq!(info.imports.len(), 2);
586        let sources: Vec<&str> = info.imports.iter().map(|i| i.source.as_str()).collect();
587        assert!(sources.contains(&"./src/global.css"));
588        assert!(sources.contains(&"./src/entry.js"));
589    }
590
591    #[test]
592    fn empty_html() {
593        let info = parse_html_to_module(FileId(0), "", 0);
594        assert!(info.imports.is_empty());
595    }
596
597    #[test]
598    fn html_with_no_assets() {
599        let info = parse_html_to_module(
600            FileId(0),
601            r"<!doctype html><html><body><h1>Hello</h1></body></html>",
602            0,
603        );
604        assert!(info.imports.is_empty());
605    }
606
607    #[test]
608    fn single_quoted_attributes() {
609        let info = parse_html_to_module(FileId(0), r"<script src='./src/entry.js'></script>", 0);
610        assert_eq!(info.imports.len(), 1);
611        assert_eq!(info.imports[0].source, "./src/entry.js");
612    }
613
614    #[test]
615    fn all_imports_are_side_effect() {
616        let info = parse_html_to_module(
617            FileId(0),
618            r#"<script src="./entry.js"></script>
619            <link rel="stylesheet" href="./style.css" />"#,
620            0,
621        );
622        for imp in &info.imports {
623            assert!(matches!(imp.imported_name, ImportedName::SideEffect));
624            assert!(imp.local_name.is_empty());
625            assert!(!imp.is_type_only);
626        }
627    }
628
629    #[test]
630    fn suppression_comments_extracted() {
631        let info = parse_html_to_module(
632            FileId(0),
633            "<!-- fallow-ignore-file -->\n<script src=\"./entry.js\"></script>",
634            0,
635        );
636        assert_eq!(info.imports.len(), 1);
637    }
638
639    #[test]
640    fn angular_template_extracts_member_refs() {
641        let info = parse_html_to_module(
642            FileId(0),
643            "<h1>{{ title() }}</h1>\n\
644             <p [class.highlighted]=\"isHighlighted\">{{ greeting() }}</p>\n\
645             <button (click)=\"onButtonClick()\">Toggle</button>",
646            0,
647        );
648        let names: rustc_hash::FxHashSet<&str> = info
649            .member_accesses
650            .iter()
651            .filter(|a| a.object == ANGULAR_TPL_SENTINEL)
652            .map(|a| a.member.as_str())
653            .collect();
654        assert!(names.contains("title"), "should contain 'title'");
655        assert!(
656            names.contains("isHighlighted"),
657            "should contain 'isHighlighted'"
658        );
659        assert!(names.contains("greeting"), "should contain 'greeting'");
660        assert!(
661            names.contains("onButtonClick"),
662            "should contain 'onButtonClick'"
663        );
664    }
665
666    #[test]
667    fn plain_html_no_angular_refs() {
668        let info = parse_html_to_module(
669            FileId(0),
670            "<!doctype html><html><body><h1>Hello</h1></body></html>",
671            0,
672        );
673        assert!(info.member_accesses.is_empty());
674    }
675}