fix: parsing
This commit is contained in:
parent
fdaf79e7ed
commit
ff091e2f31
|
|
@ -1,5 +1,6 @@
|
||||||
import { createStore } from "solid-js/store";
|
import { createStore } from "solid-js/store";
|
||||||
import type { TrackerItem, TrackerAttribute, TrackerCommand } from "../types";
|
import type { TrackerItem, TrackerAttribute, TrackerCommand } from "../types";
|
||||||
|
import {parseEmmet} from "../utils";
|
||||||
|
|
||||||
export interface TrackerStore {
|
export interface TrackerStore {
|
||||||
items: TrackerItem[];
|
items: TrackerItem[];
|
||||||
|
|
@ -18,32 +19,27 @@ const [tracker, setTracker] = createStore<TrackerStore>({
|
||||||
* - tag - 匹配 tag
|
* - tag - 匹配 tag
|
||||||
* - tag.class - 匹配 tag 和 class
|
* - tag.class - 匹配 tag 和 class
|
||||||
* - .class - 匹配 class
|
* - .class - 匹配 class
|
||||||
|
* - [attr=value] - 匹配属性
|
||||||
*/
|
*/
|
||||||
export function findTrackerIndex(emmet: string): number {
|
export function findTrackerIndex(emmet: string): number {
|
||||||
const trimmed = emmet.trim();
|
const {tag, id, classes, attributes} = parseEmmet(emmet);
|
||||||
|
|
||||||
// 解析 #id
|
|
||||||
if (trimmed.startsWith('#')) {
|
|
||||||
const id = trimmed.slice(1).split('.')[0];
|
|
||||||
return tracker.items.findIndex(item => item.id === id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析 tag 和 classes
|
|
||||||
const match = trimmed.match(/^([^.#]+)?(?:#([^.#]+))?(?:\.(.+))?$/);
|
|
||||||
if (!match) return -1;
|
|
||||||
|
|
||||||
const tag = match[1];
|
|
||||||
const classes = match[3] ? match[3].split('.') : [];
|
|
||||||
|
|
||||||
return tracker.items.findIndex(item => {
|
return tracker.items.findIndex(item => {
|
||||||
// 匹配 tag
|
// 匹配 tag
|
||||||
if (tag && item.tag !== tag) return false;
|
if (tag && item.tag !== tag) return false;
|
||||||
|
// 匹配 id
|
||||||
|
if (id && item.id !== id) return false;
|
||||||
// 匹配所有 classes
|
// 匹配所有 classes
|
||||||
if (classes.length > 0) {
|
if (classes.length > 0) {
|
||||||
for (const cls of classes) {
|
for (const cls of classes) {
|
||||||
if (!item.classes.includes(cls)) return false;
|
if (!item.classes.includes(cls)) return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 匹配属性
|
||||||
|
for(const attrName in attributes){
|
||||||
|
const attr = attributes[attrName];
|
||||||
|
if (item.attributes[attrName] !== attr) return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue