| 18 | } |
| 19 | |
| 20 | function getTagNameNode(state: EditorState, from: number, to: number): SyntaxNode | null { |
| 21 | const tree = syntaxTree(state); |
| 22 | const positions = from === to ? [from, from - 1] : [from, to, to - 1]; |
| 23 | |
| 24 | for (const pos of positions) { |
| 25 | if (pos < 0 || pos > state.doc.length) continue; |
| 26 | |
| 27 | for (const assoc of [-1, 1] as const) { |
| 28 | let node: SyntaxNode | null = tree.resolveInner(pos, assoc); |
| 29 | for (; node; node = node.parent) { |
| 30 | if (node.name === "TagName") return node; |
| 31 | if ( |
| 32 | node.name === "OpenTag" || |
| 33 | node.name === "CloseTag" || |
| 34 | node.name === "SelfClosingTag" || |
| 35 | node.name === "Element" || |
| 36 | node.type.isTop |
| 37 | ) { |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | function readNode(doc: Text, node: SyntaxNode): string { |
| 48 | return doc.sliceString(node.from, node.to); |