htmldom — Simplified html or xml handle in nodejs
var HtmlDom = require('htmldom');
install
npm install htmldom --save
test
npm test
online demo
http://douzi8.github.io/htmldom/
browserify
Exports HtmlDom to front, then uglify it
browserify htmldom.js -s HtmlDom > htmldom.front.js
uglifyjs htmldom.front.js -o htmldom.front.js
API
Constructor(code, options)
- {string}
code
html string - [object]
options
var html = new HtmlDom('<div>1</div>');
var $ = html.$;
$('div').addClass('test').attr('k', 'v');
console.log(html.html());
var xml = new HtmlDom('<?xml version="1.0" encoding="utf-8" ?><tag><item></item></tag>')
dom
The structure of html dom, it's an array with object item, list item type
html.dom
{
type: 'documentType',
value: '<!doctype html>'
}
{
type: 'tag',
name: 'div',
isVoid: false,
attributes: {
id: 'test'
},
children: [],
parent: {
}
}
{
type: 'text',
value: 'welcome'
}
{
type: 'comment',
value: 'header comment',
isIEHack: false
}
$(selector)
- {string}
selector
w3c selector, support list
- element
- *
- element > element
- element + element
- element ~ element
- #id
- .class
- [attribute]
- [attribute=value]
- [attribute^=value]
- [attribute$=value]
- [attribute~=value]
- [attribtue*=value]
var $ = html.$;
$('div .class a')
$('.item > *')
$('div + p')
$('.item ~ p')
$('.item > a')
$('.wrap .item')
$('#id li')
$('[title]').attr('key', 'value').addClass('cls').removeClass('cls2');
support jQuery method list
$('').length;
$('')[0] // first element
$('')[1] // second
$('').hasClass('cls');
$('').addClass('cls');
$('').addClass('cls1 cls2');
$('').removeClass() // remove all class
$('').removeClass('one') // remove one
$('').removeClass('one two') // remove multiple
$('').attr('key', 'value')
$('').attr('key', function(index,oldValue) {});
$('').attr({});
$('').attr('key', null)
<div data-true="true" data-false="false" data-null="null"
data-obj='{"key": "value"}' data-array="[1, 2]"
data-string="word"></div>
$('div').data('true') === true
$('div').data('null') === null
$('div').data('obj') // {key: "value" }
$('div').data('id', 5) // set data
$('').parent()
$('').parent('.cls')
$('').html()
$('').html('12')
- append(content)
- prepend(content)
- before(content)
- after(content)
$('').append('<h3>title');
$('').before('<h3>title');
$('').remove();
$('').css('height');
$('').css('height', '200px');
$('').css('height', null);
$('').css({
});
$('div').find('.item > a')
$('').filter('[data-id=1]')
$('').filter(function(index) {
return $(this[index]).attr('data-id') == 1;
});
$('').eq(0) // first element
$('').eq(-1) // last element
- each(function(index, item) {})
$('').each(function(index, item) {
var $item = $(item);
});
html()
If you want get html string fast, choose this api, it's only output origin html code
html.html()
html.html({ selfClosed: true })
stringify()
- {object}
options
- {boolean}
[options.booleanAttributes=false]
remove boolean attribute
<input disabled="disabled"> => <input disbaled>
- {boolean}
[options.removeAttributeQuotes=false]
<div id="test"></div> => <div id=test></div>
- {boolean}
[options.selfClosed]
Is tag closed with '/' - {boolean}
[options.removeJsType=true]
<script type="text/javascript"></script> => <script></script>
- {boolean}
[options.removeCssType=true]
<style type="text/css"></style> => <style></style>
- {array}
[options.jsCodeType]
js code type
<script type="text/config">
var a = 4;
</script>
- {array}
[options.templateType]
html code type
<script type="text/template">
<div>
</div>
</script>
- {object}
[options.cssdom]
Use cssdom uglify css code with style tag
<style>a { color:red; } </style> => <style>a{color:#f00}</style>
- {object}
[options.uglifyJs]
Use uglify-js uglify js code with script tag and inline events
<script> var a = 5; </script> => ...
<div onclick="return false"></div> => <div onclick="return !1"></div>
html.stringify({
booleanAttributes: true,
templateType: ['text/template'],
selfClosed: false,
uglifyJs: {}
});
beautify()
- {object}
options
- {string}
[options.indent=' ']
code indent - {boolean}
[options.selfClosed]
Is tag closed with '/' - {object}
[options.cssdom]
Use cssdom beautify css code - {object}
[options.jsBeautify]
Use js-beautify beautify js code
html.beautify({
indent: ' ',
selfClosed: false,
jsBeautify: {}
});