syntax-tree / hast-util-select Goto Github PK
View Code? Open in Web Editor NEWutility to add `querySelector`, `querySelectorAll`, and `matches` support for hast
Home Page: https://unifiedjs.com
License: MIT License
utility to add `querySelector`, `querySelectorAll`, and `matches` support for hast
Home Page: https://unifiedjs.com
License: MIT License
https://runkit.com/quarterto/6397058cb17f2300086ba513
import {h} from 'hastscript'
import {selectAll} from 'hast-util-select'
selectAll('img:not(:first-child)`, h('body', [
h('img', { src: 'https://example.com/one' }),
h('p', ['text']),
h('img', { src: 'https://example.com/two' }),
]))
it selects the second img
Error: Cannot use `:first-child` without parent
this is because :not
is implemented using matches
, which doesn't support parent-sensitive pseudos
node@14, node@16
No response
No response
No response
Greetings!
I use this tool, with custom html like tree.
When i use a custom tag where "tagName": "title", i cannot get children tags, because they are stringified.
I know this tool is for standard html, but i like to use for my custom data structures.
So i cannot use "title" named tag, and i dont know which tag has similar problems.
Maybe this is the required behaviour for standard usage, and not fixable, but i just wanted to write here, if anybody would use custom tags, this could happen.
5.0.4
import { selectAll } from "hast-util-select";
import { s } from "hastscript";
const tree = s("svg", [
s("circle", { fill: "#333" }),
s("circle", { fill: "none" }),
s("circle", { fill: "none" }),
s("circle"),
s("rect", { fill: "#666666" }),
s("rect", { fill: "none" }),
s("rect", { fill: "none" }),
s("rect", { fill: "none" }),
s("rect"),
]);
const result = selectAll("circle:not([fill]), rect:not([fill])", tree);
// Should select just 1 circle, 1 rect.
console.log(result);
// But we get:
// [
// { type: 'element', tagName: 'circle', properties: {}, children: [] },
// {
// type: 'element',
// tagName: 'rect',
// properties: { fill: '#666666' },
// children: []
// },
// {
// type: 'element',
// tagName: 'rect',
// properties: { fill: 'none' },
// children: []
// },
// {
// type: 'element',
// tagName: 'rect',
// properties: { fill: 'none' },
// children: []
// },
// {
// type: 'element',
// tagName: 'rect',
// properties: { fill: 'none' },
// children: []
// },
// { type: 'element', tagName: 'rect', properties: {}, children: [] }
// ]
It should just 1 circle and 1 rect.
It selects a bunch of elements that don't match the query.
[email protected], [email protected]
No response
No response
No response
Are you open to adding an index.d.ts file?
I made a d.ts for this - that I have been using. Happy to PR it back to the project.
VS Code Intelisense to show expected types.
I could submit directly to DefinitelyTyped - but I think their bots may ping you anyway. (Not sure, never PRd to DT
before)
This does not work:
st.deepEqual(
select(
'h1 + p',
u('root', [
u('text', '\n'),
h('p', 'Alpha'),
u('text', '\n'),
h('h1', 'Bravo'),
u('text', '\n'),
h('p', 'Charlie'),
u('text', '\n'),
h('p', 'Delta'),
u('text', '\n'),
h('div', [h('p', 'Echo')])
])
),
h('p', 'Charlie'),
'should return adjacent sibling (whitespace)'
)
It does work if there is no inter-element whitespace.
Affects +
, but probably also ~
and >
Hello - I try use it for search all elements
var body = '<!DOCTYPE html><html lang="en"><p></p></html>';
var h = require('hastscript');
var selectAll = require('hast-util-select').selectAll;
console.log(
selectAll(
'p',
h('html', body)
)
);
console.log(
selectAll(
'p',
body
)
);
console.log(
selectAll(
'p',
h(body)
)
);
but I get everytime only
[]
[]
[]
Q can you help?
Bruno
hast-util-select@6
https://jsbin.com/lihidasupa/1/edit?html,console
Element should be found
Element isn't found
chromium@Version 121.0.6167.85 (Official Build) Arch Linux (64-bit)
No response
No response
No response
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.