fix: parsing

This commit is contained in:
hypercross 2026-03-01 10:05:56 +08:00
parent fdaf79e7ed
commit ff091e2f31
1 changed files with 10 additions and 14 deletions

View File

@ -1,5 +1,6 @@
import { createStore } from "solid-js/store";
import type { TrackerItem, TrackerAttribute, TrackerCommand } from "../types";
import {parseEmmet} from "../utils";
export interface TrackerStore {
items: TrackerItem[];
@ -18,32 +19,27 @@ const [tracker, setTracker] = createStore<TrackerStore>({
* - tag - tag
* - tag.class - tag class
* - .class - class
* - [attr=value] -
*/
export function findTrackerIndex(emmet: string): number {
const trimmed = emmet.trim();
// 解析 #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('.') : [];
const {tag, id, classes, attributes} = parseEmmet(emmet);
return tracker.items.findIndex(item => {
// 匹配 tag
if (tag && item.tag !== tag) return false;
// 匹配 id
if (id && item.id !== id) return false;
// 匹配所有 classes
if (classes.length > 0) {
for (const cls of classes) {
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;
});
}