Skip to content

Support HTML entities in JSX text/attributes #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,15 @@ module.exports = grammar({

// Should not contain new lines and should not start or end with a space
jsx_text: _ => choice(
/[^{}<>\n ]([^{}<>\n]*[^{}<>\n ])?/,
/[^{}<>\n& ]([^{}<>\n&]*[^{}<>\n& ])?/,
/\/\/[^\n]*/,
),

// An entity can be named, numeric (decimal), or numeric (hexadecimal). The
// longest entity name is 29 characters long, and the HTML spec says that
// no more will ever be added.
jsx_html_character_reference: _ => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});/,

jsx_expression: $ => seq(
'{',
optional(choice(
Expand All @@ -617,6 +622,7 @@ module.exports = grammar({

_jsx_child: $ => choice(
$.jsx_text,
$.jsx_html_character_reference,
$._jsx_element,
$.jsx_expression,
),
Expand Down Expand Up @@ -676,8 +682,36 @@ module.exports = grammar({
)),
),

jsx_attribute_string: $ => choice(
seq(
'"',
repeat(choice(
alias($.unescaped_double_jsx_attribute_string_fragment, $.string_fragment),
$.jsx_html_character_reference,
)),
'"',
),
seq(
'\'',
repeat(choice(
alias($.unescaped_single_jsx_attribute_string_fragment, $.string_fragment),
$.jsx_html_character_reference,
)),
'\'',
),
),

// Workaround to https://github.com/tree-sitter/tree-sitter/issues/1156
// We give names to the token() constructs containing a regexp
// so as to obtain a node in the CST.
//
unescaped_double_jsx_attribute_string_fragment: _ => token.immediate(prec(1, /[^"&]+/)),

// same here
unescaped_single_jsx_attribute_string_fragment: _ => token.immediate(prec(1, /[^'&]+/)),

_jsx_attribute_value: $ => choice(
$.string,
alias($.jsx_attribute_string, $.string),
$.jsx_expression,
$._jsx_element,
),
Expand Down Expand Up @@ -907,12 +941,6 @@ module.exports = grammar({
// Primitives
//

// Here we tolerate unescaped newlines in double-quoted and
// single-quoted string literals.
// This is legal in typescript as jsx/tsx attribute values (as of
// 2020), and perhaps will be valid in javascript as well in the
// future.
//
string: $ => choice(
seq(
'"',
Expand All @@ -936,10 +964,10 @@ module.exports = grammar({
// We give names to the token() constructs containing a regexp
// so as to obtain a node in the CST.
//
unescaped_double_string_fragment: _ => token.immediate(prec(1, /[^"\\]+/)),
unescaped_double_string_fragment: _ => token.immediate(prec(1, /[^"\\\n]+/)),

// same here
unescaped_single_string_fragment: _ => token.immediate(prec(1, /[^'\\]+/)),
unescaped_single_string_fragment: _ => token.immediate(prec(1, /[^'\\\n]+/)),

escape_sequence: _ => token.immediate(seq(
'\\',
Expand Down
118 changes: 113 additions & 5 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading