1
- use std:: collections:: HashMap ;
1
+ use std:: { collections:: HashMap , fmt } ;
2
2
3
3
use itertools:: Itertools ;
4
- use serde:: { Deserialize , Serialize } ;
4
+ use serde:: {
5
+ de:: { MapAccess , Visitor } ,
6
+ ser:: SerializeMap ,
7
+ Deserialize , Deserializer , Serialize , Serializer ,
8
+ } ;
5
9
6
10
use super :: asset:: HtmlPluginAttribute ;
7
11
use crate :: config:: { HtmlInject , HtmlRspackPluginBaseOptions , HtmlScriptLoading } ;
@@ -10,14 +14,64 @@ use crate::config::{HtmlInject, HtmlRspackPluginBaseOptions, HtmlScriptLoading};
10
14
#[ serde( rename_all = "camelCase" ) ]
11
15
pub struct HTMLPluginTag {
12
16
pub tag_name : String ,
17
+ #[ serde(
18
+ serialize_with = "serialize_attributes" ,
19
+ deserialize_with = "deserialize_attributes"
20
+ ) ]
13
21
pub attributes : Vec < HtmlPluginAttribute > ,
14
22
pub void_tag : bool ,
15
- pub content : Option < String > ,
23
+ #[ serde( rename = "innerHTML" ) ]
24
+ pub inner_html : Option < String > ,
16
25
// `head`, `body`, `false`
17
26
#[ serde( skip) ]
18
27
pub append_to : HtmlInject ,
19
28
}
20
29
30
+ fn serialize_attributes < S > ( x : & Vec < HtmlPluginAttribute > , s : S ) -> Result < S :: Ok , S :: Error >
31
+ where
32
+ S : Serializer ,
33
+ {
34
+ let mut map = s. serialize_map ( Some ( x. len ( ) ) ) ?;
35
+ for attr in x {
36
+ let attr_value = attr. attr_value . to_owned ( ) . unwrap_or ( "true" . to_string ( ) ) ;
37
+ map. serialize_entry ( & attr. attr_name , & attr_value) ?;
38
+ }
39
+ map. end ( )
40
+ }
41
+
42
+ fn deserialize_attributes < ' de , D > ( d : D ) -> Result < Vec < HtmlPluginAttribute > , D :: Error >
43
+ where
44
+ D : Deserializer < ' de > ,
45
+ {
46
+ struct DataVisitor ;
47
+
48
+ impl < ' de > Visitor < ' de > for DataVisitor {
49
+ type Value = Vec < HtmlPluginAttribute > ;
50
+
51
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
52
+ formatter. write_str ( "html attributes" )
53
+ }
54
+
55
+ fn visit_map < A > ( self , mut access : A ) -> Result < Self :: Value , A :: Error >
56
+ where
57
+ A : MapAccess < ' de > ,
58
+ {
59
+ let mut res = vec ! [ ] ;
60
+
61
+ while let Some ( ( k, v) ) = access. next_entry :: < String , Option < String > > ( ) ? {
62
+ res. push ( HtmlPluginAttribute {
63
+ attr_name : k,
64
+ attr_value : v. filter ( |value| value != "true" ) ,
65
+ } ) ;
66
+ }
67
+
68
+ Ok ( res)
69
+ }
70
+ }
71
+
72
+ d. deserialize_map ( DataVisitor )
73
+ }
74
+
21
75
impl HTMLPluginTag {
22
76
pub fn create_style ( href : & str , append_to : HtmlInject ) -> HTMLPluginTag {
23
77
HTMLPluginTag {
@@ -112,7 +166,7 @@ impl HTMLPluginTag {
112
166
HTMLPluginTag {
113
167
tag_name : "title" . to_string ( ) ,
114
168
void_tag : true ,
115
- content : Some ( title. to_string ( ) ) ,
169
+ inner_html : Some ( title. to_string ( ) ) ,
116
170
..Default :: default ( )
117
171
}
118
172
}
0 commit comments