diff --git a/README.md b/README.md index 949c2bf..a0d5704 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Transpiles an Oniguruma pattern and returns a native JavaScript `RegExp`. function toRegExp( pattern: string, options?: Options -): RegExp; +): RegExp | EmulatedRegExp; ``` #### Type `Options` @@ -186,7 +186,7 @@ Supports all features of `default`, plus the following: *Default: `false`.* -Prevents use of advanced emulation strategies that rely on returning a `RegExp` subclass, resulting in certain patterns not being emulatable. +Disables advanced emulation strategies that rely on returning a `RegExp` subclass, resulting in certain patterns not being emulatable. ### `flags` @@ -229,7 +229,7 @@ Higher limits have no effect on regexes that don't use recursion, so you should *Default: `'ES2024'`.* -Sets the JavaScript language version for generated patterns and flags. Later targets allow faster processing, simpler generated source, and support for additional features. +Sets the JavaScript language version for the generated pattern and flags. Later targets allow faster processing, simpler generated source, and support for additional features.
More details diff --git a/demo/demo.css b/demo/demo.css index 588f1d1..5d893e2 100644 --- a/demo/demo.css +++ b/demo/demo.css @@ -14,7 +14,7 @@ body { main { background-color: rgba(255, 255, 255, 0.9); max-width: 65rem; - padding: 2.8vw 4.5vw; + padding: 4vh 4.5vw 3vw 4.5vw; margin: 0 auto; box-shadow: 0 0 0 10px #80c0ff; border-radius: 0 0 15px 15px; @@ -55,12 +55,69 @@ summary { label { margin-right: 0.5em; + position: relative; + display: inline-block; } label img { vertical-align: middle; } +label .tip { + visibility: hidden; + text-align: center; + padding: 4px 8px; + background-color: #c9e4ff; + border-radius: 6px; + position: absolute; + bottom: 130%; + left: 50%; + z-index: 1; +} + +label:hover .tip { + visibility: visible; +} + +label .tip:hover { + visibility: hidden; +} + +label .tip::after { + content: " "; + position: absolute; + top: 100%; + left: 50%; + margin-left: -5px; + border-width: 5px; + border-style: solid; + border-color: #c9e4ff transparent transparent transparent; +} + +label .tip-sm { + width: 130px; + margin-left: -65px; +} + +label .tip-md { + width: 170px; + margin-left: -85px; +} + +label .tip-lg { + width: 220px; + margin-left: -110px; +} + +label .tip-xl { + width: 320px; + margin-left: -160px; +} + +label .tip :is(code, kbd) { + background-color: #dceeff; +} + input[type='checkbox'] { width: 1.25em; height: 1.25em; diff --git a/demo/index.html b/demo/index.html index 7cec45e..81b73d1 100644 --- a/demo/index.html +++ b/demo/index.html @@ -23,14 +23,17 @@

Try it

@@ -42,6 +45,7 @@

Try it

target + JS version for generated pattern and flags

@@ -60,12 +65,14 @@

Try it

@@ -74,12 +81,14 @@

Try it

@@ -88,12 +97,14 @@

Try it

@@ -101,7 +112,7 @@

Try it


     
-    

The output shows the result of calling toRegExp. Oniguruma-To-ES functions toDetails and toOnigurumaAst can also be run from the console on this page, and you can pretty-print ASTs by passing them to printAst. +

See Readme: Options for more detailed explanations of each option. The output shows the result of calling toRegExp. Functions toDetails and toOnigurumaAst can be run from the console on this page, and you can pretty-print ASTs by passing them to printAst. diff --git a/src/index.js b/src/index.js index 16f9461..4b058ad 100644 --- a/src/index.js +++ b/src/index.js @@ -99,7 +99,7 @@ function toOnigurumaAst(pattern, options) { Transpiles an Oniguruma pattern and returns a native JavaScript `RegExp`. @param {string} pattern Oniguruma regex pattern. @param {Options} [options] -@returns {RegExp} +@returns {RegExp | EmulatedRegExp} */ function toRegExp(pattern, options) { const result = toDetails(pattern, options); diff --git a/src/options.js b/src/options.js index 74e8e6a..2b54372 100644 --- a/src/options.js +++ b/src/options.js @@ -29,8 +29,8 @@ function getOptions(options) { return { // Sets the level of emulation rigor/strictness. accuracy: 'default', - // Prevents use of advanced emulation strategies that rely on returning a `RegExp` subclass, - // resulting in certain patterns not being emulatable. + // Disables advanced emulation strategies that rely on returning a `RegExp` subclass, resulting + // in certain patterns not being emulatable. avoidSubclass: false, // Oniguruma flags; a string with `i`, `m`, and `x` in any order (all optional). Oniguruma's // `m` is equivalent to JavaScript's `s` (`dotAll`). @@ -42,8 +42,8 @@ function getOptions(options) { // Specifies the recursion depth limit. Supported values are integers `2` to `100` and `null`. // If `null`, any use of recursion results in an error. maxRecursionDepth: 6, - // Sets the JavaScript language version for generated patterns and flags. Later targets allow - // faster processing, simpler generated source, and support for additional features. + // Sets the JavaScript language version for the generated pattern and flags. Later targets + // allow faster processing, simpler generated source, and support for additional features. target: 'ES2024', // Leave disabled unless the regex will be used in a TextMate grammar processor that merges // backreferences across `begin` and `end` patterns. diff --git a/src/transform.js b/src/transform.js index 4db00f9..29d5f4b 100644 --- a/src/transform.js +++ b/src/transform.js @@ -590,10 +590,13 @@ function applySubclassStrategies(ast, accuracy) { alts[0].elements.length === 1 && (firstEl.type === AstTypes.CapturingGroup || firstEl.type === AstTypes.Group) && firstEl.alternatives.length === 1; - // First element within first group if the group doesn't contain top-level alternation, else just - // the first element of the pattern; a wrapper might be used to apply flags to the full pattern - const firstElIn = hasWrapperGroup ? firstEl.alternatives[0].elements[0] : firstEl; const singleAltIn = hasWrapperGroup ? firstEl.alternatives[0] : alts[0]; + // First el within first group if the group doesn't contain top-level alternation, else just the + // first el of the pattern; ex: a flag group might enclose the full pattern + const firstElIn = hasWrapperGroup ? singleAltIn.elements[0] : firstEl; + if (!firstElIn) { + return null; + } // ## Strategy `line_or_search_start`: Support leading `(^|\G)` and similar if (