Coder Social home page Coder Social logo

html2dom's Introduction

html2dom

JavaScript library to parse HTML mark-up and convert it into DOM calls.

More and more people use JavaScript to manipulate the DOM. Naturally this can pose a security risk as soon as user input is involved: Embedding user input can easily lead to Cross Site Scripting (XSS). Having a clean JavaScript codebase that seperates HTML elements and text can mitigating that risk by treating user data as non-HTML content. html2dom helps you to to enforce a stricter seperation betwwen HTML and text. Using the library will enable you to transform your big fat assignment to innerHTML into multiple simple DOM calls. A useful side-effect is, that browsers won't have to run a HTML parser on your long HTML strings which might even improve your website performance.

Example 1: Turn HTML into JavaScript that calls DOM Methods

See this HTML?

<p id="greeting">Hello <b>World</b></p>

What if we wanted add this to a document using JavaScript? Let's use html2dom for that!

var h2d = html2dom.parse('<p id="greeting">Hello <b>World</b></p>');
console.log(h2d);

This will give us:

var docFragment = document.createDocumentFragment(); // contains all gathered nodes
var greeting = document.createElement('P');
greeting.setAttribute("id", "greeting");
docFragment.appendChild(greeting);
var text = document.createTextNode("Hello ");
greeting.appendChild(text);
var b = document.createElement('B');
greeting.appendChild(b);
var text_0 = document.createTextNode("World");
b.appendChild(text_0);

Example 2: Let's make our JavaScript code generate us some HTML!

Sometimes, you might want to check for yourself what happens when you throw the supplied JavaScript code into a script tag and preview the HTML that is being generated (the demo does that).

And, do you know what? We also use that for our tests, to see if the JavaScript we get out of html2dom renders the HTML we have supplied in the first place. Nifty, eh?

var inp = '<p id="greeting">Hello <b>World</b></p>';
var js = html2dom.parse(inp);
html2dom.dom2html(js, function callback(res) {
	console.log("Input:", inp);
	console.log("Result:", res);
});

And hooray, this results is:

Input: <p id="greeting">Hello <b>World</b></p>
Result: <p id="greeting">Hello <b>World</b></p>

Demo?

Demo.

Unit Tests?

Tests.

License

Mozilla Public License

html2dom's People

Contributors

mozfreddyb avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

html2dom's Issues

Replace whitespace/new lines prior to generation to make more clean JS code

remove redundant whitespaces where applicable

html2dom emits code for text nodes that might contain redundant whitespaces, e.g.

var text = document.createTextNode(" \n          TITLE  ");

The problem here is that we do not know the real context. In most cases, we can just replace multiple whitespaces with a single space. But if the node's parent (or any other direct ancestor) is a <pre> tag or a tag with a css white-space attribute that is set to either pre, pre-wrap or pre-line, then we are doing it wrong.

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.