Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help importing NodeJS-Module "htmlparser2" in SmartMobileStudio

I tried to use the htmlparser2 module (installed via npm install htmlparser2) in SmartMobileStudio. The module itself works well in direct javascript (using a slightly changed sample from htmlparser2's Homepage):

var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
    onopentag: function(name, attribs){
        console.log(name);
        console.log(attribs);
    }
});
parser.write("<img src='image1.jpg'>");
parser.end();

Using SMS (2.0.0.9 Beta) I tried to import the module like this:

unit NodeJS.htmlparser2;

interface

uses
  NodeJS.Core,
  NodeJS.events;

type
  TOnTag = procedure(name: string; attribs: Variant);
  TOnText = procedure(text: string);

  JParser = class external(NodeJS.events.JNodeEventEmitter)
    procedure write(s: string);
    procedure &end;
  end;

  Jhtmlparser_Exports = class external
  public
    function Parser(onopentag: TOnTag; ontext: TOnText; onclosetag: TOnTag): JParser;
  end;

function htmlparser2: Jhtmlparser_Exports;

implementation

function htmlparser2: Jhtmlparser_Exports;
begin
  result := Jhtmlparser_Exports( require("htmlparser2") );
end;

end.

I changed the project generated by the Node.js-New-Project-Template like this:

[...]

procedure TServer.Run;
begin
  var htmlparser := NodeJS.htmlparser2.htmlparser2;

  var parser := htmlparser.Parser(
   procedure (Name: string; Attribs: Variant)
   begin
     console_.log([Name]);
     console_.log([Attribs]);
   end,
   nil,
   nil);

  parser.write("<img src='image1.jpg'>");
  parser.end();
end;

The Problem is that the emitted code is not correct, but nearly:

[...]
parser = htmlparser.Parser(function (Name$3, Attribs) {
   console_().log([Name$3].slice());
   console_().log([Attribs].slice());
},null,null);
[...]

This works:

[...]
parser = new htmlparser.Parser({onopentag: function (Name$3, Attribs) {
   console_().log([Name$3].slice());
   console_().log([Attribs].slice());
}});
[...]

The difference is the "new" keyword and the named event callback "onopentag". What do I need to write to generate working js code?

like image 452
Steffen Binas Avatar asked Nov 29 '25 19:11

Steffen Binas


1 Answers

By the way, I found a better solution: use a class reference (JParserClass = class of JParser)

  JParser = class external(NodeJS.events.JNodeEventEmitter)
    constructor Create(onopentag: TOnTag; ontext: TOnText; onclosetag: TOnTag);
    procedure write(s: string);
    procedure &end;
  end;
  JParserClass = class of JParser;

  Jhtmlparser_Exports = class external
  public
    Parser: JParserClass;
  end;

Then you can create the class like this:

  var parser := new (htmlparser2.Parser)(

Or like this:

  var parserclass = htmlparser2.Parser;
  var parser := new parserclass(

Or:

  var parserclass = htmlparser2.Parser;
  var parser := parserclass.Create(
like image 175
André Avatar answered Dec 02 '25 08:12

André



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!