Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Dom.nodeFilter type is wrong #96

Closed
Coobaha opened this issue Apr 21, 2018 · 3 comments
Closed

Dom.nodeFilter type is wrong #96

Coobaha opened this issue Apr 21, 2018 · 3 comments

Comments

@Coobaha
Copy link

Coobaha commented Apr 21, 2018

https://bucklescript.github.io/bucklescript/api/Dom.html#TYPEnodeFilter

It is typed as record.

let _ = createNodeIteratorWithWhatToShowFilter(el, DomRe.WhatToShow.(many([_Element, _Attribute])), NodeFilter.make((_) => 0), document);

So it compiles to [() => 0] instead of { acceptNode: () => 0 };

BS issue rescript-lang/rescript-compiler#2764

@bobzhang
Copy link
Contributor

is it correct to type it as < .. > Js.t ?

@Coobaha
Copy link
Author

Coobaha commented Apr 22, 2018

@bobzhang Maybe but it has defined contract please check example from mdn

The NodeIterator.filter read-only method returns a NodeFilter object, that is an object implement an acceptNode(node) method, used to screen nodes.

NodeFilter

It also has this constants that acceptNode should return

NodeFilter.FILTER_ACCEPT
NodeFilter.FILTER_REJECT
NodeFilter.FILTER_SKIP

I have this as a workaround in my project:

module NodeFilter = {
  [@bs.val] [@bs.scope "NodeFilter"]
  external filterAccept : int = "FILTER_ACCEPT";
  [@bs.val] [@bs.scope "NodeFilter"]
  external filterReject : int = "FILTER_REJECT";
  [@bs.val] [@bs.scope "NodeFilter"]
  external filterSkip : int = "FILTER_SKIP";
  type acceptNode = Dom.element => int;
  [@bs.deriving abstract]
  type t = {acceptNode};
  external castToDomNodeFilter : t => Dom.nodeFilter = "%identity";
};

Document.createNodeIteratorWithWhatToShowFilter(
  parent,
  WhatToShow._Text,
  NodeFilter.(
    t(~acceptNode=node => filterAccept)
    |> castToDomNodeFilter
  ),
  document,
);

@yawaramin
Copy link
Collaborator

This should be fixed with BuckleScript's records-as-objects representation. if not we can reopen.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants