Skip to content

chrisfls/elm-html-convert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elm-html-convert

This is a hack ported from elm-test that allows converting Html to Value and String.

Post-processing

This library requires post-processing.

where:

var $kress95$elm_html_convert$Html$Convert$toValue = function (_v0) {
	return $elm$json$Json$Encode$string('You thought it was a JSON value, but it was me, Dio!');
};

replace with:

/*

import Elm.Kernel.Json exposing (wrap)

*/

// NOTE: this is duplicating constants also defined in Test.Internal.KernelConstants
//       so if you make any changes here, be sure to synchronize them there!
var virtualDomKernelConstants = {
  nodeTypeTagger: 4,
  nodeTypeThunk: 5,
  kids: "e",
  refs: "l",
  thunk: "m",
  node: "k",
  value: "a",
};

function forceThunks(vNode) {
  if (typeof vNode !== "undefined" && vNode.$ === "#2") {
    // This is a tuple (the kids : List (String, Html) field of a Keyed node); recurse into the right side of the tuple
    vNode.b = forceThunks(vNode.b);
  }
  if (
    typeof vNode !== "undefined" &&
    vNode.$ === virtualDomKernelConstants.nodeTypeThunk &&
    !vNode[virtualDomKernelConstants.node]
  ) {
    // This is a lazy node; evaluate it
    var args = vNode[virtualDomKernelConstants.thunk];
    vNode[virtualDomKernelConstants.node] =
      vNode[virtualDomKernelConstants.thunk].apply(args);
    // And then recurse into the evaluated node
    vNode[virtualDomKernelConstants.node] = forceThunks(
      vNode[virtualDomKernelConstants.node],
    );
  }
  if (
    typeof vNode !== "undefined" &&
    vNode.$ === virtualDomKernelConstants.nodeTypeTagger
  ) {
    // This is an Html.map; recurse into the node it is wrapping
    vNode[virtualDomKernelConstants.node] = forceThunks(
      vNode[virtualDomKernelConstants.node],
    );
  }
  if (
    typeof vNode !== "undefined" &&
    typeof vNode[virtualDomKernelConstants.kids] !== "undefined"
  ) {
    // This is something with children (either a node with kids : List Html, or keyed with kids : List (String, Html));
    // recurse into the children
    vNode[virtualDomKernelConstants.kids] =
      vNode[virtualDomKernelConstants.kids].map(forceThunks);
  }
  return vNode;
}

function _HtmlAsJson_toJson(html) {
  return _Json_wrap(forceThunks(html));
}

function _HtmlAsJson_eventHandler(event) {
  return event[virtualDomKernelConstants.value];
}

function _HtmlAsJson_taggerFunction(tagger) {
  return tagger.a;
}

function _HtmlAsJson_attributeToJson(attribute) {
  return _Json_wrap(attribute);
}

var $kress95$elm_html_convert$Html$Convert$toValue = _HtmlAsJson_toJson;

You can automatize this with expanded-elm.

About

Convert HTML to JSON value and string

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages