Module org.htmlunit

Class HtmxTwoZeroSevenScriptPreProcessor

java.lang.Object
org.htmlunit.javascript.preprocessor.HtmxTwoZeroSevenScriptPreProcessor
All Implemented Interfaces:
ScriptPreProcessor

public class HtmxTwoZeroSevenScriptPreProcessor extends Object implements ScriptPreProcessor
A ScriptPreProcessor implementation that applies compatibility patches to htmx 2.0.3 - 2.0.10 versions of the htmx JavaScript library.

This preprocessor rewrites certain ECMAScript syntax constructs (such as spread operator usage in array push and for-of loops over array copies) to equivalent ES5-compatible code. This is necessary because these features are not yet supported by the JavaScript engine htmlunit-corejs (Rhino).

The class can be chained with other ScriptPreProcessor instances via its constructor.

Supported patches include:

  • Replacing result.push(...toArray(...)) with result.push.apply(result, toArray(...))
  • Replacing result.push(...findAttributeTargets(...)) with result.push.apply(result, findAttributeTargets(...))
  • Replacing for (const preservedElt of [...pantry.children]) with for (const preservedElt of Array.from(pantry.children))
  • Similar replacements for minified htmx scripts (e.g., htmx.min.js)

Usage Example:

 try (WebClient webClient = new WebClient()) {
     webClient.setScriptPreProcessor(new HtmxTwoZeroSevenScriptPreProcessor());
     // use webClient as needed
 }
 
Author:
Ronald Brill
See Also:
  • Constructor Details

    • HtmxTwoZeroSevenScriptPreProcessor

      public HtmxTwoZeroSevenScriptPreProcessor()
      Ctor.
    • HtmxTwoZeroSevenScriptPreProcessor

      public HtmxTwoZeroSevenScriptPreProcessor(ScriptPreProcessor nextScriptPreProcessor)
      Ctor.
      Parameters:
      nextScriptPreProcessor - the next ScriptPreProcessor
  • Method Details

    • preProcess

      public String preProcess(HtmlPage htmlPage, String sourceCode, String sourceName, int lineNumber, HtmlElement htmlElement)
      Pre process the specified source code in the context of the given page.
      Specified by:
      preProcess in interface ScriptPreProcessor
      Parameters:
      htmlPage - the page
      sourceCode - the code to execute
      sourceName - a name for the chunk of code that is going to be executed (used in error messages)
      lineNumber - the line number of the source code
      htmlElement - the HTML element that will act as the context
      Returns:
      the source code after pre processing