htmldom — Simplified html or xml handle in nodejs
const createHtmlDom = require('htmldom')
let $ = createHtmlDom('<div><button>1</button><a href="https">2</a></div>')
$('*').each((index, item) => {
let $item = $(item)
})
$('div button').addClass('title').attr('k', 'v')
$('a').attr('href')
$('div').find('a').attr('data-id', '5')
$.html()
install
npm install htmldom --save
test
npm run test
umd
Open test.html with browser
npm run umd
API
$(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]
let $ = createHtmlDom('html code')
$('*').each((index, item) => {
console.log(item)
let $el = $(item)
})
$('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({
k: 'v',
'data-id': 'v2',
k3: null
});
$('').attr('key', null)
$('').parent()
$('').parent('.cls')
$('').html()
$('').html('12')
$('div').outerHTML()
$('#id').clone()
- replaceWith(content)
- append(content)
- prepend(content)
- before(content)
- after(content)
$('div').replaceWith('<view></view>')
$('').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);
});
$.nodes
Get a dom tree
{
type: 'tag',
name: 'div',
attributes: {
class: 'title header',
id: 'test',
style: 'color:red;width:200px;'
},
parent: null,
children: [],
classList: new Set(['title', 'header']),
style: {
color: 'red',
width: '200px'
}
}
{
type: 'tag',
name: 'script',
tagType: 'rawTag',
textContent: 'alert(1)'
}
{
type: 'tag',
name: 'image',
attributes: { src: '' },
tagType: 'selfClosingTag'
}
{
type: 'tag',
name: 'input',
tagType: 'voidTag',
}
{
type: 'text',
data: 'text tag'
}
{
type: 'comment',
data: ' comemnt data '
}
$.root()
let $ = createHtmlDom('<div></div>')
$.root().prepend('<head></head>')
$.root().find('div')[0] === $('div')[0]
$.html()
$.html()
If you want get html string fast, choose this api, it's only output origin html code
$.html()
$.uglify()
$('script').each((index, item) => {
let type = $(item).attr('type')
if (type && type !== 'text/javascript') return
item.textContent = uglifyJs(item.textContent)
})
$('style').each((index, item) => {
let type = $(item).attr('type')
if (type && type !== 'text/css') return
item.textContent = uglifyCss(item.textContent)
})
$.uglify()
$.uglify({
removeAttributeQuotes: true
})
$.beautify()
- {object}
options
- {string}
[options.indent=' ']
code indent
$.beautify({
indent: ' '
});