swc_html_codegen/
ctx.rs

1use std::ops::{Deref, DerefMut};
2
3use crate::{writer::HtmlWriter, CodeGenerator};
4
5impl<'b, W> CodeGenerator<'b, W>
6where
7    W: HtmlWriter,
8{
9    /// Original context is restored when returned guard is dropped.
10    #[inline]
11    pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'_, 'b, W> {
12        let orig_ctx = self.ctx;
13
14        self.ctx = ctx;
15
16        WithCtx {
17            orig_ctx,
18            inner: self,
19        }
20    }
21}
22
23#[derive(Debug, Default, Clone, Copy)]
24pub(crate) struct Ctx {
25    pub need_escape_text: bool,
26}
27
28pub(super) struct WithCtx<'w, 'a, I: 'w + HtmlWriter> {
29    inner: &'w mut CodeGenerator<'a, I>,
30    orig_ctx: Ctx,
31}
32
33impl<'w, I: HtmlWriter> Deref for WithCtx<'_, 'w, I> {
34    type Target = CodeGenerator<'w, I>;
35
36    fn deref(&self) -> &CodeGenerator<'w, I> {
37        self.inner
38    }
39}
40impl<'w, I: HtmlWriter> DerefMut for WithCtx<'_, 'w, I> {
41    fn deref_mut(&mut self) -> &mut CodeGenerator<'w, I> {
42        self.inner
43    }
44}
45
46impl<I: HtmlWriter> Drop for WithCtx<'_, '_, I> {
47    fn drop(&mut self) {
48        self.inner.ctx = self.orig_ctx;
49    }
50}