1
0
mirror of synced 2025-12-07 22:18:17 +08:00

feat(console): $x utility

This commit is contained in:
surunzi
2019-11-21 17:21:57 +08:00
parent ba9efd14e7
commit b1f4157f10
3 changed files with 56 additions and 1 deletions

View File

@@ -2741,3 +2741,16 @@ const p = wrap(escape, function(fn, text) {
});
p('You & Me'); // -> '<p>You &amp; Me</p>'
```
## xpath
Select elements using xpath, IE is not supported.
|Name |Type |Desc |
|------|------|---------------|
|xpath |string|Xpath |
|return|array |Target elements|
```javascript
xpath('//html/body'); // -> [body]
```

View File

@@ -20,7 +20,8 @@ import {
keys,
last,
throttle,
raf
raf,
xpath
} from '../lib/util'
let id = 0
@@ -73,6 +74,9 @@ export default class Logger extends Emitter {
$$() {
return toArr(document.querySelectorAll.apply(document, arguments))
},
$x(path) {
return xpath(path)
},
clear: () => {
this.clear()
},

View File

@@ -8952,4 +8952,42 @@ export var wrap = _.wrap = (function (exports) {
return exports;
})({});
/* ------------------------------ xpath ------------------------------ */
export var xpath = _.xpath = (function (exports) {
/* Select elements using xpath, IE is not supported.
*
* |Name |Type |Desc |
* |------|------|---------------|
* |xpath |string|Xpath |
* |return|array |Target elements|
*/
/* example
* xpath('//html/body'); // -> [body]
*/
/* typescript
* export declare function xpath(xpath: string): HTMLElement[];
*/
exports = function(xpath) {
var ret = [];
var nodesSnapshot = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
for (var i = 0; i < nodesSnapshot.snapshotLength; i++) {
ret.push(nodesSnapshot.snapshotItem(i));
}
return ret;
};
return exports;
})({});
export default _;