Skip to content

Commit

Permalink
avoid printing "</style" in CSS code (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 8, 2021
1 parent 8095e75 commit 3fbc735
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Changelog

## Unreleased

* Avoid the sequence `</style` in CSS output ([#1509](https://github.com/evanw/esbuild/issues/1509))

The CSS code generator now avoids generating the character sequence `</style` in case you want to embed the CSS output in a `<style>...</style>` tag inside HTML:

```css
/* Original code */
a:after {
content: "</style>";
}

/* Old output */
a:after {
content: "</style>";
}

/* New output */
a:after {
content: "<\/style>";
}
```

This mirrors how the JS code generator similarly avoids the character sequence `</script`.

## 0.12.19

* Add support for CSS source maps ([#519](https://github.com/evanw/esbuild/issues/519))
Expand Down
7 changes: 7 additions & 0 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package css_printer

import (
"fmt"
"strings"
"unicode/utf8"

"github.com/evanw/esbuild/internal/ast"
Expand Down Expand Up @@ -485,6 +486,12 @@ func (p *printer) printQuotedWithQuote(text string, quote byte) {
escape = escapeBackslash
}

case '/':
// Avoid generating the sequence "</style" in CSS code
if i >= 1 && text[i-1] == '<' && strings.HasPrefix(text[i+1:], "style") {
escape = escapeBackslash
}

default:
if (p.options.ASCIIOnly && c >= 0x80) || c == '\uFEFF' {
escape = escapeHex
Expand Down
5 changes: 5 additions & 0 deletions internal/css_printer/css_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func TestStringQuote(t *testing.T) {
expectPrintedString(t, "f\nG", "\"f\\aG\"")
expectPrintedString(t, "f\x01o", "\"f\x01o\"")
expectPrintedString(t, "f\to", "\"f\to\"")

expectPrintedString(t, "</script>", "\"</script>\"")
expectPrintedString(t, "</style>", "\"<\\/style>\"")
expectPrintedString(t, "</style", "\"<\\/style\"")
expectPrintedString(t, ">/style", "\">/style\"")
}

func TestURLQuote(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (p *printer) printUnquotedUTF16(text []uint16, quote rune) {
js = append(js, "\\\\"...)

case '/':
// Avoid generating the sequence "</script" in JS code
if i >= 2 && text[i-2] == '<' && i+6 <= len(text) && js_lexer.UTF16EqualsString(text[i:i+6], "script") {
js = append(js, '\\')
}
Expand Down

0 comments on commit 3fbc735

Please sign in to comment.