1
- use anyhow:: { Error , Result } ;
1
+ use anyhow:: { bail , Result } ;
2
2
use lol_html:: { element, html_content:: Element , HtmlRewriter , Settings } ;
3
3
4
+ #[ derive( Clone , Debug , Default ) ]
5
+ pub struct DocumentOptions {
6
+ /// Ignore self-closing script warnings
7
+ pub allow_self_closing_script : bool ,
8
+ }
9
+
4
10
/// A wrapper for Html modifications, and rewrites.
5
11
#[ derive( Debug ) ]
6
12
pub struct Document ( Vec < u8 > ) ;
@@ -14,33 +20,26 @@ impl AsRef<[u8]> for Document {
14
20
impl Document {
15
21
/// Create a new document
16
22
///
17
- /// Note: if this is not a valid HTML document, it will fail later on.
18
- // In case we want to add more things to check in the future, we can take in the config as an
19
- // argument, and `anyhow::Result<()>` could be
20
- // replaced with an `std::result::Result<(), MyErrorEnum>` in case we want to handle the error
21
- // case differently depending on the error variant.
22
- pub fn new ( data : impl Into < Vec < u8 > > , ignore_script_error : bool ) -> Result < Self > {
23
+ /// This will fail if a non-valid HTML is provided or a self-closing script element is found
24
+ /// and the self-closing script element is not allowed in the options.
25
+ pub fn new ( data : impl Into < Vec < u8 > > , options : DocumentOptions ) -> Result < Self > {
23
26
let doc = Self ( data. into ( ) ) ;
24
27
25
- // Check for self closed script tags such as "<script.../>"
26
- doc. select ( "script[data-trunk] " , |el| {
28
+ // Check for self- closed script tags such as "<script.../>"
29
+ doc. select ( "script" , |el| {
27
30
if el. is_self_closing ( ) {
28
- const SELF_CLOSED_SCRIPT : & str = concat ! (
29
- "Self closing script tag found. " ,
30
- r#"Replace the self closing script tag ("<script .../>") with a normally closed one such as "<script ...></script>"."# ,
31
- "\n For more information please take a look at https://github.com/trunk-rs/trunk/discussions/771."
32
- ) ;
33
-
34
- if ignore_script_error {
35
- tracing:: warn!( "{}" , SELF_CLOSED_SCRIPT ) ;
31
+ if options. allow_self_closing_script {
32
+ tracing:: warn!( "Self-closing script tag found (allowed by configuration)" ) ;
36
33
}
37
34
else {
38
- return Err ( Error :: msg (
39
- format ! ( "{}\n {}" ,
40
- SELF_CLOSED_SCRIPT ,
41
- r#"In case this is a false positive the "--ignore-script-error" flag can be used to issue a warning instead."#
42
- )
43
- ) )
35
+ bail ! (
36
+ r#"Self-closing script tag found.
37
+
38
+ Replace the self-closing script tag ("<script .../>") with a normally closed one such as "<script ...></script>".
39
+ For more information, please take a look at https://github.com/trunk-rs/trunk/discussions/771."
40
+
41
+ In case this is a false positive, the "--allow-self-closing-script" flag can be used to issue a warning instead."#
42
+ )
44
43
}
45
44
}
46
45
Ok ( ( ) )
@@ -147,19 +146,25 @@ impl Document {
147
146
mod test {
148
147
use super :: * ;
149
148
149
+ /// Run some basic tests with a spec-compliant HTML file.
150
+ ///
151
+ /// The focus is on the `<script>` element, and around other self-closing elements. If a
152
+ /// self-closing script tag is being used which, according to the spec should not be used, bad
153
+ /// things may happen. This test is there to test our expectation towards a spec-compliant file.
150
154
#[ test]
151
155
fn test_script_spec ( ) {
152
156
let mut doc = Document :: new (
153
157
r#"
154
158
<html>
155
159
<head>
160
+ <link/>
156
161
<script href="test"></script>
157
162
<link>
158
163
</head>
159
164
<body></body>
160
165
</html>
161
166
"# ,
162
- false ,
167
+ Default :: default ( ) ,
163
168
)
164
169
. expect ( "this is valid HTML" ) ;
165
170
@@ -173,6 +178,7 @@ mod test {
173
178
r#"
174
179
<html>
175
180
<head>
181
+ <link/>
176
182
<script href="test"><span>here</span></script>
177
183
<link>
178
184
</head>
@@ -182,9 +188,17 @@ mod test {
182
188
) ;
183
189
}
184
190
191
+ /// Ensure we get an error for any self-closing script tag
185
192
#[ test]
186
193
fn test_self_closing_script_tag ( ) {
187
- let doc = Document :: new ( "<script data-trunk/>" , false ) ;
194
+ let doc = Document :: new ( "<script/>" , Default :: default ( ) ) ;
195
+ assert ! ( doc. is_err( ) ) ;
196
+ }
197
+
198
+ /// Ensure we get an error for a self-closing trunk script tag.
199
+ #[ test]
200
+ fn test_self_closing_trunk_script_tag ( ) {
201
+ let doc = Document :: new ( "<script data-trunk/>" , Default :: default ( ) ) ;
188
202
assert ! ( doc. is_err( ) ) ;
189
203
}
190
204
}
0 commit comments