From 8407ebd208327cd179792ff7180f1577242c2be7 Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Thu, 10 Dec 2020 21:04:19 +0100 Subject: [PATCH] add transformEntities option (ref #105) --- README.md | 6 ++++++ index.js | 18 +++++++++------- test/tests-jsx.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 317c3ac..a105915 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/index.js b/index.js index 004e080..cc33ff6 100644 --- a/index.js +++ b/index.js @@ -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); }; }; @@ -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( @@ -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); diff --git a/test/tests-jsx.js b/test/tests-jsx.js index b53549d..59d07fa 100644 --- a/test/tests-jsx.js +++ b/test/tests-jsx.js @@ -4634,6 +4634,59 @@ test('foo>', { } ] }); +test('foo>', { + "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>", + "raw": "foo>" + } + ] + } + } + ] +}, { +}, { + transformEntities: false +}); test('function*it(){yield }', { "type": "Program", "start": 0,