Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eselement

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eselement

DOM-like wrapper around the Javascript AST

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20
increased by900%
Maintainers
1
Weekly downloads
 
Created
Source

eselement

npm version Build Status Coverage Status Dependency Status License

DOM-like wrapper around the Javascript AST

Install

$ npm install eselement --save

Example

// amd2cjs.js
// convert AMD modules to CommonJS.
var fs = require("fs");

var eselement = require("eselement");

var content = fs.readFileSync(process.argv[2])
  , program = eselement.createElement(content);

var define = program.querySelector("CallExpression[callee.name='define']");

if (define) {
  var arr = define.arguments[0]
      , fn = define.arguments[1];

  var libs = arr.elements.map(function (e) { return e.value })
    , names = fn.params.map(function (p) { return p.name; })
    , body = fn.body.body;

  define.parentElement.removeChild(define);

  libs.forEach(function (lib, i) {
    var el = names[i] ? eselement.createElement("var " + names[i] + " = require('" + lib + "')")
                        : eselement.createElement("require('" + lib + "')");
    program.appendChild("body", el);
  });

  program.appendChild("body", body);

  console.log(program.outerCode());
}
$ cat amd_module.js
define(["crypto"], function (crypto) {
  var ciphers = crypto.getCiphers();
  console.log(ciphers);
});

$ node amd2cjs.js amd_module.js
var crypto = require('crypto');
var ciphers = crypto.getCiphers();
console.log(ciphers);

API

createElement(string || object)

Creates and element from either Javascript code or Mozilla Parser AST.

Element

Attributes

All attributes from the AST are copied onto the Element, for example an element of type Program will have an attribute body.

Element.parentElement

A reference to the Elements parent, if it has no parent it's null.

Element.parentAttribute

A string denoting the attribute on the parent in which this element is in.

Methods

Element.childElements()

Get all children of an element.

Element.firstChild()

Get the first child of an element.

Element.lastChild()

Get the last child of an element.

Element.querySelectorAll(selector)

Select elements using esquery.

Element.querySelector(selector)

Select the first matching element using esquery.

Element.setAttribute(attribute, value)

Sets the attribute attribute to value. Does checking to see if value is of the correct type for attribute.

Element.appendChild(attribute, child)

Append child to element attribute attribute.

Element.removeChild(child)

Remove child from element.

Element.replaceChild(newChild, oldChild)

Replace oldChild with newChild in element.

Element.cloneElement()

Return a clone of element.

Element.outerAST()

Return the AST for element.

Element.outerCode()

Return the javascript code for element.

Keywords

FAQs

Package last updated on 08 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc