als-document
Version:
A powerful HTML parser & DOM manipulation library for both backend and frontend.
65 lines (62 loc) • 2.45 kB
JavaScript
const { describe, it } = require('node:test')
const assert = require('node:assert')
const { Query } = require('../index')
const q1 = 'html>body>div.tabs~.some[type $= "some"][test]>p+div>.some-id .tab-content~input[disabled] div.some'
const s1 = Query.get(q1)[0]
describe('query tests',() => {
it('Check target',() => {
const expected = 'div.some'
let {tag,classList} = s1
assert(`${tag}.${classList[0]}` === expected)
})
it('Check ancestors length',() => {
assert(s1.ancestors.length === 2)
})
it('Check prev group',() => {
const expected = s1.ancestors[0].group.split('+')[0]
assert(s1.ancestors[0].prev.group === expected)
})
it('Check parents length',() => {
assert(s1.ancestors[0].parents.length === 1)
})
it('Check prev any, prev and classList',() => {
assert(s1.ancestors[0].prev.prevAny.classList[0] === 'tabs')
})
it('Check attributes length',() => {
assert(s1.ancestors[0].prev.parents[0].attribs.length === 2)
})
it('Check attributes fn $',() => {
assert(s1.ancestors[0].prev.parents[0].attribs[0].check('test and some') === true)
})
it('Check attributes fn *=',() => {
assert(s1.ancestors[0].prev.parents[0].attribs[0].check('some') === true)
})
it('Check attributes fn *=',() => {
let s = Query.get('[test*="some value"]')[0]
assert(s.attribs[0].check('some value test') === true)
})
it('Check attributes fn ~= prase instead single word',() => {
let s = Query.get('[test~="some value"]')[0]
assert(s.attribs[0].check('some value test') === false)
})
it('Check attributes fn ~=',() => {
let s = Query.get('[test~="value"]')[0]
assert(s.attribs[0].check('some value test') === true)
})
it('Check attributes fn ^=',() => {
let s = Query.get('[test^="some"]')[0]
assert(s.attribs[0].check('some value test') === true)
})
it('Check attributes fn |= with few words',() => {
let s = Query.get('[test|="some value test"]')[0]
assert(s.attribs[0].check('some') === false)
})
it('Check attributes fn |=word',() => {
let s = Query.get('[test|="some"]')[0]
assert(s.attribs[0].check('some') === true)
})
it('Check attributes fn |=word-word',() => {
let s = Query.get('[test|="some"]')[0]
assert(s.attribs[0].check('some-value') === true)
})
})