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

femdom

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

femdom

Stringify HTML elements to JSON and domify them back to your page.

  • 0.0.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

FEMDOM

Note: this package was intended as a joke a long time ago but it should actually work perfectly fine.

FEMDOM is a simple FORMATTER / ENCODER / MARKUPIFIER for DOCUMENT OBJECT MODEL nodes. Easy interconversion between DOM nodes, EcmaScript object and JSON strings.

JOI | JAVASCRIPT OBJECT INTERCONVERSION

Easily convert EcmaScript HTML Nodes to JSON and back.

Acquire

Simple as that.

npm i femdom 
/* Node */
const femdom = require('femdom')
/* Browser */
<script src="femdom.js"></script>

Methods

FEMDOM has three methods.

  • objectify(htmlNode)
  • domify(object)
  • stringify(object or htmlNode, indentation)

OBJECTIFY

Isolates only the nodeName, nodeValue, childNodes and attributes of a DOM node. Returns 'abstraction' of the HTML element as regular EcmaScript object.

<div id="women" class="fancy">
... some user info here ...
</div>
/* Find element with id = 'women' */
let women = document.getElementById('women')
let result = FEMDOM.objectify(women)

Your result is an abstract representation of whatever is inside your HTML element. It could look something like this:

var result = {
   "nodeType": 1,
   "tagName": "div",
   "attributes": [
      [
         "id",
         "women"
      ]
   ],
   "childNodes": [
      {
         "nodeType": 1,
         "tagName": "table",
         "attributes": [
            [
               "class",
               "table"
            ]
         ],
         "childNodes": [
            {
               "nodeType": 1,
               "tagName": "caption",
               "attributes": [],
               "childNodes": [
                  {
                     "nodeType": 3,
                     "nodeName": "#text",
                     "nodeValue": "Women",
                     "childNodes": []
                  }
               ]
            },
            {
               "nodeType": 1,
               "tagName": "thead",
               "attributes": [],
               "childNodes": [
                  {
                     "nodeType": 1,
                     "tagName": "tr",
                     "attributes": [],
                     "childNodes": [
                        {
                           "nodeType": 1,
                           "tagName": "th",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "Name",
                                 "childNodes": []
                              }
                           ]
                        },
                        {
                           "nodeType": 1,
                           "tagName": "th",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "Age",
                                 "childNodes": []
                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "nodeType": 1,
               "tagName": "tbody",
               "attributes": [],
               "childNodes": [
                  {
                     "nodeType": 1,
                     "tagName": "tr",
                     "attributes": [],
                     "childNodes": [
                        {
                           "nodeType": 1,
                           "tagName": "td",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "Kylie Star",
                                 "childNodes": []
                              }
                           ]
                        },
                        {
                           "nodeType": 1,
                           "tagName": "td",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "@kyliestar",
                                 "childNodes": []
                              }
                           ]
                        }
                     ]
                  },
                  {
                     "nodeType": 1,
                     "tagName": "tr",
                     "attributes": [],
                     "childNodes": [
                        {
                           "nodeType": 1,
                           "tagName": "td",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "Princess Rene",
                                 "childNodes": []
                              }
                           ]
                        },
                        {
                           "nodeType": 1,
                           "tagName": "td",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "@worshiprenee",
                                 "childNodes": []
                              }
                           ]
                        }
                     ]
                  },
                  {
                     "nodeType": 1,
                     "tagName": "tr",
                     "attributes": [],
                     "childNodes": [
                        {
                           "nodeType": 1,
                           "tagName": "td",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "Amai Liu",
                                 "childNodes": []
                              }
                           ]
                        },
                        {
                           "nodeType": 1,
                           "tagName": "td",
                           "attributes": [],
                           "childNodes": [
                              {
                                 "nodeType": 3,
                                 "nodeName": "#text",
                                 "nodeValue": "@amailiu",
                                 "childNodes": []
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}
/* Observe that when using jQuery, you simply do this */
FEMDOM.objectify(
    $('#women')[0] // the zero means first result of this query
)

DOMIFY

As the name suggests, domify() will successfully render your exported string or object back to the DOM.

/* Basically dupliates our user table */
document.body.appendChild(
    FEMDOM.domify(result)
)

STRINGIFY

Simply wraps JSON.stringify() and returns a JSON string representation. The second argument for indentation is optional and is the same as the third argument of JSON.stringify. You can stringify both objectified nodes and HTML nodes directly, it can tell the difference.

/* This will internally simply call JSON stringify on your result object */
let jsonString = FEMDOM.stringify(result)

/* Stringify HTML element directly */
FEMDOM.stringify(
    document.querySelector('#stuff')
)

Keywords

FAQs

Package last updated on 21 Jun 2018

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