webui_parser/plugin/mod.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4//! Parser plugin trait and built-in plugin implementations.
5//!
6//! The plugin system is framework-aware but phase-local — parser plugins
7//! classify framework-owned attributes, capture processed component templates,
8//! and emit per-element hydration metadata for the handler.
9
10pub mod fast;
11pub mod webui;
12
13use crate::component_registry::Component;
14use crate::Result;
15
16/// Parser-owned decision about how an attribute should be handled.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum AttributeAction {
19 /// Let the parser process the attribute normally.
20 Keep,
21 /// Skip the attribute without incrementing the element binding count.
22 Skip,
23 /// Skip the attribute and count it as a dynamic binding.
24 SkipAndCountBinding,
25}
26
27/// Build artifacts extracted from a parser plugin after parsing completes.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum ParserPluginArtifacts {
30 /// The plugin did not produce any post-parse artifacts.
31 None,
32 /// Client component templates keyed by tag name.
33 ComponentTemplates(Vec<(String, String)>),
34}
35
36/// A parser plugin that can customize template parsing behavior.
37///
38/// Plugins receive callbacks at key points during HTML parsing:
39/// - **Fragment lifecycle**: `start_fragment` before a fragment parse begins
40/// - **Component registration**: `register_component_template` when a component template is finalized
41/// - **Attribute classification**: `classify_attribute` for framework-specific attrs
42/// - **Element completion**: `finish_element` after attributes are processed
43///
44/// WebUI calls these hooks during parsing; plugins decide what (if anything) to do.
45pub trait ParserPlugin {
46 /// Called before parsing begins for a fragment.
47 ///
48 /// Plugins can use this to reset fragment-local counters while preserving
49 /// global build-level state such as tracked component templates.
50 fn start_fragment(&mut self, _fragment_id: &str) {}
51
52 /// Called when a component template has been fully processed for the active
53 /// CSS strategy and wrapped for shadow DOM rendering.
54 fn register_component_template(
55 &mut self,
56 tag_name: &str,
57 component: &Component,
58 processed_template: &str,
59 ) -> Result<()>;
60
61 /// Decide how a framework-owned attribute should be handled.
62 fn classify_attribute(&mut self, attr_name: &str) -> AttributeAction;
63
64 /// Called after all attributes on an element have been processed.
65 /// `binding_attribute_count` is the number of dynamic attribute bindings found.
66 /// Returns optional opaque bytes to emit as a `Plugin` protocol fragment.
67 fn finish_element(&mut self, binding_attribute_count: u32) -> Option<Vec<u8>>;
68
69 /// Consume the plugin and return any build artifacts it captured.
70 fn into_artifacts(self: Box<Self>) -> ParserPluginArtifacts {
71 ParserPluginArtifacts::None
72 }
73}