Skip to content

Commit

Permalink
option to not escape </script> and </style>
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 19, 2022
1 parent 47dd4de commit 4cc9999
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 2 deletions.
78 changes: 78 additions & 0 deletions internal/bundler_tests/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3549,6 +3549,84 @@ func TestLegalCommentsManyEndOfFile(t *testing.T) {
})
}

func TestLegalCommentsEscapeSlashScriptAndStyleEndOfFile(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/project/entry.js": `import "js-pkg"; a /*! </script> */`,
"/project/node_modules/js-pkg/index.js": `x /*! </script> */`,

"/project/entry.css": `@import "css-pkg"; a { b: c } /*! </style> */`,
"/project/node_modules/css-pkg/index.css": `x { y: z } /*! </style> */`,
},
entryPaths: []string{"/project/entry.js", "/project/entry.css"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
MinifyWhitespace: true,
LegalComments: config.LegalCommentsEndOfFile,
},
})
}

func TestLegalCommentsEscapeSlashScriptAndStyleExternal(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/project/entry.js": `import "js-pkg"; a /*! </script> */`,
"/project/node_modules/js-pkg/index.js": `x /*! </script> */`,

"/project/entry.css": `@import "css-pkg"; a { b: c } /*! </style> */`,
"/project/node_modules/css-pkg/index.css": `x { y: z } /*! </style> */`,
},
entryPaths: []string{"/project/entry.js", "/project/entry.css"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
MinifyWhitespace: true,
LegalComments: config.LegalCommentsExternalWithoutComment,
},
})
}

func TestLegalCommentsNoEscapeSlashScriptEndOfFile(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/project/entry.js": `import "js-pkg"; a /*! </script> */`,
"/project/node_modules/js-pkg/index.js": `x /*! </script> */`,

"/project/entry.css": `@import "css-pkg"; a { b: c } /*! </style> */`,
"/project/node_modules/css-pkg/index.css": `x { y: z } /*! </style> */`,
},
entryPaths: []string{"/project/entry.js", "/project/entry.css"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
MinifyWhitespace: true,
LegalComments: config.LegalCommentsEndOfFile,
UnsupportedJSFeatures: compat.InlineScript,
},
})
}

func TestLegalCommentsNoEscapeSlashStyleEndOfFile(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/project/entry.js": `import "js-pkg"; a /*! </script> */`,
"/project/node_modules/js-pkg/index.js": `x /*! </script> */`,

"/project/entry.css": `@import "css-pkg"; a { b: c } /*! </style> */`,
"/project/node_modules/css-pkg/index.css": `x { y: z } /*! </style> */`,
},
entryPaths: []string{"/project/entry.js", "/project/entry.css"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
MinifyWhitespace: true,
LegalComments: config.LegalCommentsEndOfFile,
UnsupportedCSSFeatures: compat.InlineStyle,
},
})
}

func TestLegalCommentsManyLinked(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
84 changes: 84 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,50 @@ c {
/*! Copyright notice 1 */
/*! Copyright notice 2 */

================================================================================
TestLegalCommentsEscapeSlashScriptAndStyleEndOfFile
---------- /out/entry.js ----------
x;a;
/*! <\/script> */
/*! Bundled license information:

js-pkg/index.js:
(*! <\/script> *)
*/

---------- /out/entry.css ----------
x{y:z}a{b:c}
/*! <\/style> */
/*! Bundled license information:

css-pkg/index.css:
(*! <\/style> *)
*/

================================================================================
TestLegalCommentsEscapeSlashScriptAndStyleExternal
---------- /out/entry.js.LEGAL.txt ----------
/*! </script> */

Bundled license information:

js-pkg/index.js:
/*! </script> */

---------- /out/entry.js ----------
x;a;

---------- /out/entry.css.LEGAL.txt ----------
/*! </style> */

Bundled license information:

css-pkg/index.css:
/*! </style> */

---------- /out/entry.css ----------
x{y:z}a{b:c}

================================================================================
TestLegalCommentsExternal
---------- /out/entry.js.LEGAL.txt ----------
Expand Down Expand Up @@ -2208,6 +2252,46 @@ export {
}
}

================================================================================
TestLegalCommentsNoEscapeSlashScriptEndOfFile
---------- /out/entry.js ----------
x;a;
/*! </script> */
/*! Bundled license information:

js-pkg/index.js:
(*! </script> *)
*/

---------- /out/entry.css ----------
x{y:z}a{b:c}
/*! <\/style> */
/*! Bundled license information:

css-pkg/index.css:
(*! <\/style> *)
*/

================================================================================
TestLegalCommentsNoEscapeSlashStyleEndOfFile
---------- /out/entry.js ----------
x;a;
/*! <\/script> */
/*! Bundled license information:

js-pkg/index.js:
(*! <\/script> *)
*/

---------- /out/entry.css ----------
x{y:z}a{b:c}
/*! </style> */
/*! Bundled license information:

css-pkg/index.css:
(*! </style> *)
*/

================================================================================
TestLegalCommentsNone
---------- /out/entry.js ----------
Expand Down
3 changes: 3 additions & 0 deletions internal/helpers/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ seekBackwardToNewline:
}

func EscapeClosingTag(text string, slashTag string) string {
if slashTag == "" {
return text
}
i := strings.Index(text, "</")
if i < 0 {
return text
Expand Down
12 changes: 10 additions & 2 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5041,7 +5041,11 @@ func (c *linkerContext) generateChunkJS(chunkIndex int, chunkWaitGroup *sync.Wai

// Make sure the file ends with a newline
j.EnsureNewlineAtEnd()
c.maybeAppendLegalComments(c.options.LegalComments, legalCommentList, chunk, &j, "/script")
slashTag := "/script"
if c.options.UnsupportedJSFeatures.Has(compat.InlineScript) {
slashTag = ""
}
c.maybeAppendLegalComments(c.options.LegalComments, legalCommentList, chunk, &j, slashTag)

if len(c.options.JSFooter) > 0 {
j.AddString(c.options.JSFooter)
Expand Down Expand Up @@ -5420,7 +5424,11 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa

// Make sure the file ends with a newline
j.EnsureNewlineAtEnd()
c.maybeAppendLegalComments(c.options.LegalComments, legalCommentList, chunk, &j, "/style")
slashTag := "/style"
if c.options.UnsupportedCSSFeatures.Has(compat.InlineStyle) {
slashTag = ""
}
c.maybeAppendLegalComments(c.options.LegalComments, legalCommentList, chunk, &j, slashTag)

if len(c.options.CSSFooter) > 0 {
j.AddString(c.options.CSSFooter)
Expand Down

0 comments on commit 4cc9999

Please sign in to comment.