|
| 1 | +import fs from 'fs' |
| 2 | +import * as parse5 from 'parse5' |
| 3 | +import { DOMImplementation, XMLSerializer } from '@xmldom/xmldom' |
| 4 | +import xpath from 'xpath' |
| 5 | +import Locator from '../locator.js' |
| 6 | +import { xpathLocator } from '../utils.js' |
| 7 | + |
| 8 | +export default async function query(locator, context, options = {}) { |
| 9 | + const html = options.file ? fs.readFileSync(options.file, 'utf8') : await readStdin() |
| 10 | + |
| 11 | + if (!html || !html.trim()) { |
| 12 | + console.error('codeceptq: no HTML input. Pipe HTML via stdin or use --file <path>.') |
| 13 | + process.exitCode = 2 |
| 14 | + return |
| 15 | + } |
| 16 | + |
| 17 | + let xpathExpr |
| 18 | + let contextExpr = null |
| 19 | + try { |
| 20 | + xpathExpr = buildXPath(locator, options) |
| 21 | + if (context) contextExpr = buildXPath(context, {}) |
| 22 | + } catch (err) { |
| 23 | + console.error(`codeceptq: cannot build XPath: ${err.message}`) |
| 24 | + process.exitCode = 2 |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + const { doc, source } = htmlToDoc(html) |
| 29 | + |
| 30 | + let nodes |
| 31 | + try { |
| 32 | + if (contextExpr) { |
| 33 | + const ctxNodes = toArray(xpath.select(contextExpr, doc)) |
| 34 | + const seen = new Set() |
| 35 | + nodes = [] |
| 36 | + for (const ctx of ctxNodes) { |
| 37 | + for (const m of toArray(xpath.select(xpathExpr, ctx))) { |
| 38 | + if (!seen.has(m)) { |
| 39 | + seen.add(m) |
| 40 | + nodes.push(m) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } else { |
| 45 | + nodes = toArray(xpath.select(xpathExpr, doc)) |
| 46 | + } |
| 47 | + } catch (err) { |
| 48 | + console.error(`codeceptq: XPath evaluation failed for "${xpathExpr}": ${err.message}`) |
| 49 | + process.exitCode = 2 |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + const limit = parseInt(options.limit, 10) || 20 |
| 54 | + const snippetLen = parseInt(options.snippet, 10) || 500 |
| 55 | + const truncated = nodes.slice(0, limit) |
| 56 | + const where = options.file || 'stdin' |
| 57 | + |
| 58 | + if (options.json) { |
| 59 | + process.stdout.write( |
| 60 | + JSON.stringify( |
| 61 | + { |
| 62 | + locator, |
| 63 | + context: context || null, |
| 64 | + xpath: xpathExpr, |
| 65 | + contextXPath: contextExpr, |
| 66 | + source: where, |
| 67 | + total: nodes.length, |
| 68 | + shown: truncated.length, |
| 69 | + matches: truncated.map(n => ({ |
| 70 | + line: n.__line ?? null, |
| 71 | + snippet: renderSnippet(n, source, snippetLen, options.full), |
| 72 | + })), |
| 73 | + }, |
| 74 | + null, |
| 75 | + 2, |
| 76 | + ) + '\n', |
| 77 | + ) |
| 78 | + } else { |
| 79 | + if (nodes.length === 0) { |
| 80 | + console.log(`No matches for ${quote(locator)}${context ? ` within ${quote(context)}` : ''} in ${where}`) |
| 81 | + console.log(`(xpath: ${xpathExpr})`) |
| 82 | + } else { |
| 83 | + const noun = nodes.length === 1 ? 'match' : 'matches' |
| 84 | + const more = nodes.length > truncated.length ? ` (showing first ${truncated.length})` : '' |
| 85 | + console.log(`${nodes.length} ${noun} for ${quote(locator)}${context ? ` within ${quote(context)}` : ''} in ${where}${more}`) |
| 86 | + console.log() |
| 87 | + truncated.forEach((node, i) => { |
| 88 | + const line = node.__line ?? '?' |
| 89 | + console.log(`${i + 1}. Line ${line}`) |
| 90 | + const snippet = renderSnippet(node, source, snippetLen, options.full) |
| 91 | + snippet.split('\n').forEach(l => console.log(' ' + l)) |
| 92 | + console.log() |
| 93 | + }) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (nodes.length === 0) process.exitCode = 1 |
| 98 | +} |
| 99 | + |
| 100 | +function buildXPath(input, options) { |
| 101 | + const literal = xpathLocator.literal(input) |
| 102 | + if (options.field) return Locator.field.byText(literal) |
| 103 | + if (options.click || options.clickable) return Locator.clickable.wide(literal) |
| 104 | + if (options.checkable) return Locator.checkable.byText(literal) |
| 105 | + if (options.select) { |
| 106 | + // Locator.select.byVisibleText is meant to be evaluated within a <select> |
| 107 | + // context (`./option`). Rewrite to descendant-of-document for standalone use. |
| 108 | + return Locator.select.byVisibleText(literal).replace(/\.\/(option|optgroup)/g, './/$1') |
| 109 | + } |
| 110 | + |
| 111 | + if (options.xpath) return new Locator({ xpath: input }).toXPath() |
| 112 | + if (options.css) return new Locator({ css: input }).toXPath() |
| 113 | + |
| 114 | + const loc = new Locator(input) |
| 115 | + if (loc.type === 'fuzzy') { |
| 116 | + return xpathLocator.combine([Locator.clickable.wide(literal), Locator.field.byText(literal)]) |
| 117 | + } |
| 118 | + return loc.toXPath() |
| 119 | +} |
| 120 | + |
| 121 | +function htmlToDoc(html) { |
| 122 | + const p5doc = parse5.parse(html, { sourceCodeLocationInfo: true }) |
| 123 | + const impl = new DOMImplementation() |
| 124 | + const doc = impl.createDocument(null, null, null) |
| 125 | + walkParse5(p5doc, doc, doc) |
| 126 | + return { doc, source: html } |
| 127 | +} |
| 128 | + |
| 129 | +function walkParse5(p5node, xmlParent, xmlDoc) { |
| 130 | + for (const child of p5node.childNodes || []) { |
| 131 | + const name = child.nodeName |
| 132 | + if (name === '#text') { |
| 133 | + if (child.value != null) { |
| 134 | + const t = xmlDoc.createTextNode(child.value) |
| 135 | + if (child.sourceCodeLocation) t.__line = child.sourceCodeLocation.startLine |
| 136 | + xmlParent.appendChild(t) |
| 137 | + } |
| 138 | + } else if (name === '#comment') { |
| 139 | + try { |
| 140 | + xmlParent.appendChild(xmlDoc.createComment(child.data || '')) |
| 141 | + } catch { |
| 142 | + // ignore comments xmldom rejects |
| 143 | + } |
| 144 | + } else if (name === '#documentType') { |
| 145 | + // skip doctype |
| 146 | + } else { |
| 147 | + const tagName = child.tagName || name |
| 148 | + let el |
| 149 | + try { |
| 150 | + el = xmlDoc.createElement(tagName) |
| 151 | + } catch { |
| 152 | + continue |
| 153 | + } |
| 154 | + for (const attr of child.attrs || []) { |
| 155 | + try { |
| 156 | + el.setAttribute(attr.name, attr.value) |
| 157 | + } catch { |
| 158 | + // ignore attrs xmldom rejects (namespaces, invalid names) |
| 159 | + } |
| 160 | + } |
| 161 | + const loc = child.sourceCodeLocation |
| 162 | + if (loc) { |
| 163 | + el.__line = loc.startLine |
| 164 | + el.__startOffset = loc.startOffset |
| 165 | + el.__endOffset = loc.endOffset |
| 166 | + el.__startTagEndOffset = loc.startTag ? loc.startTag.endOffset : loc.endOffset |
| 167 | + } |
| 168 | + xmlParent.appendChild(el) |
| 169 | + walkParse5(child, el, xmlDoc) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +function renderSnippet(node, source, snippetLen, full) { |
| 175 | + if (typeof node.__startOffset !== 'number') { |
| 176 | + try { |
| 177 | + return new XMLSerializer().serializeToString(node) |
| 178 | + } catch { |
| 179 | + return `<${node.nodeName || '?'}>` |
| 180 | + } |
| 181 | + } |
| 182 | + const start = node.__startOffset |
| 183 | + const end = node.__endOffset ?? start |
| 184 | + if (full) return source.slice(start, end) |
| 185 | + |
| 186 | + const tagEnd = node.__startTagEndOffset ?? end |
| 187 | + const openingTag = source.slice(start, tagEnd) |
| 188 | + if (end <= tagEnd) return openingTag |
| 189 | + |
| 190 | + const totalLen = end - start |
| 191 | + if (totalLen <= snippetLen) return source.slice(start, end) |
| 192 | + |
| 193 | + const remaining = Math.max(0, snippetLen - openingTag.length) |
| 194 | + if (remaining < 20) return openingTag + ' …' |
| 195 | + return openingTag + source.slice(tagEnd, tagEnd + remaining) + ' …' |
| 196 | +} |
| 197 | + |
| 198 | +function readStdin() { |
| 199 | + return new Promise((resolve, reject) => { |
| 200 | + if (process.stdin.isTTY) { |
| 201 | + resolve('') |
| 202 | + return |
| 203 | + } |
| 204 | + let data = '' |
| 205 | + process.stdin.setEncoding('utf8') |
| 206 | + process.stdin.on('data', chunk => (data += chunk)) |
| 207 | + process.stdin.on('end', () => resolve(data)) |
| 208 | + process.stdin.on('error', reject) |
| 209 | + }) |
| 210 | +} |
| 211 | + |
| 212 | +function toArray(v) { |
| 213 | + if (Array.isArray(v)) return v |
| 214 | + if (v == null || v === '' || typeof v === 'boolean' || typeof v === 'number') return [] |
| 215 | + return [v] |
| 216 | +} |
| 217 | + |
| 218 | +function quote(s) { |
| 219 | + return `'${String(s).replace(/'/g, "\\'")}'` |
| 220 | +} |
0 commit comments