Giter Club home page Giter Club logo

minidom's Introduction

minidom

A small JavaScript DOM. Implements DOM level 1, with textContent from level 3 and innerHTML and outerHTML getters. Only supports HTML documents.

Usage

var minidom = require("minidom");

var document = minidom('<!doctype><html><head><title>Hello</title><body><h1>Hi!</h1></body></html>');

expect(document.getElementsByTagName("h1")[0].textContent).toEqual("Hi!");

You can also access the DOM implementation used internally:

var dom = require("minidom/dom");

var document = minidom();
expect(document instanceof dom.Node).toBeTruthy();

Differences with JSDom

JSDom minidom
Runs Javascript in the page context Yes No
CSSOM Yes No
Loads remote resources Yes No
HTML5 parsing algorithm No Yes*
Runs in the browser No Yes**
Awesome Yes Yes

Basically minidom does a lot less, but often it's all you need.

* This means that the DOM representation is the same as you would get in the browser, but may not be suitable if you wish to preserve as much of the original formatting as possible. For example parsing <body>hi</body><head><title>hello</title> with minidom results in a document that looks like <html><head></head><body>hi<title>hello</title></body></html> , where as JSDom outputs <html><body>hi</body><head><title>hello</title></head></html>.

** This is probably very cool, although I have no idea why.

Supported API

Properties marked ⃠  are read-only

Node

  • ELEMENT_NODE
  • ATTRIBUTE_NODE
  • TEXT_NODE
  • CDATA_SECTION_NODE
  • ENTITY_REFERENCE_NODE
  • ENTITY_NODE
  • PROCESSING_INSTRUCTION_NODE
  • COMMENT_NODE
  • DOCUMENT_NODE
  • DOCUMENT_TYPE_NODE
  • DOCUMENT_FRAGMENT_NODE
  • NOTATION_NODE
  • children
  • nodeValue
  • parentNode
  • nodeName
  • attributes
  • firstChild
  • ownerDocument
  • readonly
  • lastChild
  • childNodes
  • nextSibling
  • previousSibling
  • insertBefore(/* Node */ newChild, /* Node*/ refChild)
  • replaceChild(/* Node */ newChild, /* Node */ oldChild)
  • removeChild(/* Node */ oldChild)
  • appendChild(/* Node */ newChild)
  • hasChildNodes()
  • cloneNode(/* bool */ deep, fn)
  • normalize()
  • toString()
  • raise(type, message, data)
  • textContent

Document (inherits from Node)

  • nodeType
  • contentType
  • doctype
  • documentElement
  • implementation
  • nodeName
  • tagName
  • nodeValue
  • attributes
  • ownerDocument
  • readonly
  • createElement(/* string */ tagName)
  • createDocumentFragment()
  • createTextNode(/* string */ data)
  • createComment(/* string */ data)
  • createCDATASection(/* string */ data)
  • createProcessingInstruction(/* string */ target, /* string */ data)
  • createAttribute(/* string */ name)
  • createEntityReference(/* string */ name)
  • createEntityNode(/* string */ name)
  • createNotationNode(/* string */ name, /* string */ publicId, /* string */ systemId)
  • appendChild(/* Node */ arg)
  • removeChild(/* Node */ arg)
  • getElementsByTagName(/* string */ name)
  • outerHTML

Element (inherits from Node)

  • nodeValue
  • tagName
  • nodeType
  • attributes
  • getAttribute(/* string */ name)
  • setAttribute(/* string */ name, /* string */ value)
  • removeAttribute(/* string */ name)
  • getAttributeNode(/* string */ name)
  • setAttributeNode(/* Attr */ newAttr)
  • removeAttributeNode(/* Attr */ oldAttr)
  • getElementsByTagName(/* string */ name)
  • outerHTML
  • innerHTML

Thanks

Made possible with large excerpts from JSDom, and the excellent parse5 implementation of the HTML5 parsing algorithm.

License

MIT license. See LICENSE.md for details.

minidom's People

Contributors

mightyiam avatar stuk avatar thibaultzanini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minidom's Issues

minidom might be doing entities wrong

e.g., document.createTextNode("&hellip;") might be interpreting the entity instead of encoding. not sure. But there is definitely a difference in behavior between minidom and browser dom
in my usage at least.

from @kriskowal

Keep history, diff, etc.

How would you keep a history of changes in a minidom?

  • Make minidom manipulations (appendChild, etc.)
  • Register current minidom as a point in history (a history array of diffs?)
  • Revert to the last point in history (thus able to revert back to any point in history) (apply last diff?)

Node 0.12 problem

When running some tests, I ran into an issue parsing a trivial DOM on Node 0.12, which was resolved by backtracking to Node 0.10. The issue manifested as an "unable to set length" error for a property that only has a getter. The trace originates at Array.prototype.slice.call(this._childNodes, &c in insertBefore.

createRange support?

Is anyone aware of a minidom extension that supports createRange?
Is there interest in adding it to minidom?
Or should I simply create a package that depends on minidom and extends it?

Set node.localName

Hey,

That's from DOM 2, but having localName available (even if as an alias to tagName) would make Minidom compatible with the Xpath library out of the box. I think. :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.