Skip to content
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

Add transformEntities option #120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ acorn.Parser.extend(jsx({ allowNamespaces: false }))

Note that by default `allowNamespaces` is enabled for spec compliancy.

By default, HTML entities are parsed and transformed within strings. To disable this functionality (and treat HTML entities as plain text), you can provide `transformEntities: false` as an option:

```javascript
acorn.Parser.extend(jsx({ transformEntities: false }))
```

## License

This plugin is issued under the [MIT license](./LICENSE).
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ module.exports = function(options) {
return function(Parser) {
return plugin({
allowNamespaces: options.allowNamespaces !== false,
allowNamespacedObjects: !!options.allowNamespacedObjects
allowNamespacedObjects: !!options.allowNamespacedObjects,
transformEntities: options.transformEntities !== false
}, Parser);
};
};
Expand Down Expand Up @@ -130,12 +131,6 @@ function plugin(options, Parser) {
out += this.input.slice(chunkStart, this.pos);
return this.finishToken(tok.jsxText, out);

case 38: // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
break;

case 62: // '>'
case 125: // '}'
this.raise(
Expand All @@ -144,6 +139,15 @@ function plugin(options, Parser) {
(ch === 62 ? ">" : "}") + "` or " + "`{\"" + this.input[this.pos] + "\"}" + "`?"
);

case 38: // '&' (NOTE: must come directly BEFORE default case)
if (options.transformEntities) {
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
break;
}
/* intentional fallthrough */

default:
if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos);
Expand Down
53 changes: 53 additions & 0 deletions test/tests-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4634,6 +4634,59 @@ test('<A>foo&gt;</A>', {
}
]
});
test('<A>foo&gt;</A>', {
"type": "Program",
"start": 0,
"end": 14,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 14,
"expression": {
"type": "JSXElement",
"start": 0,
"end": 14,
"openingElement": {
"type": "JSXOpeningElement",
"start": 0,
"end": 3,
"attributes": [],
"name": {
"type": "JSXIdentifier",
"start": 1,
"end": 2,
"name": "A"
},
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start": 10,
"end": 14,
"name": {
"type": "JSXIdentifier",
"start": 12,
"end": 13,
"name": "A"
}
},
"children": [
{
"type": "JSXText",
"start": 3,
"end": 10,
"value": "foo&gt;",
"raw": "foo&gt;"
}
]
}
}
]
}, {
}, {
transformEntities: false
});
test('function*it(){yield <a></a>}', {
"type": "Program",
"start": 0,
Expand Down