| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /**
- * 修复 list-search-actions 内误嵌套查询项 / 未闭合 / 重复 toolbar 的问题
- */
- const fs = require('fs')
- const path = require('path')
- const targets = process.argv.slice(2)
- if (!targets.length) {
- console.error('Usage: node repair-list-search-corruption.js <vue-file> [...]')
- process.exit(1)
- }
- function extractButtons(html) {
- return (html.match(/<el-button[\s\S]*?<\/el-button>/g) || []).map((btn) =>
- btn.replace(/\stype="primary"/, ' type="cyan"')
- )
- }
- function repair(content) {
- let next = content
- // list-search-actions 内只有嵌套查询项 + 紧跟 </el-form> + 可选 list-toolbar
- next = next.replace(
- /<el-form-item class="list-search-actions">\s*((?:<el-form-item\b[\s\S]*?<\/el-form-item>\s*)+)<\/el-form>([\s\S]*?)(?=<el-table|<\/el-card|<el-tabs|<div class="batch|<el-row(?![^>]*list-toolbar))/g,
- (full, nested, tail) => {
- const toolbar = tail.match(/<el-row\b[\s\S]*?list-toolbar[\s\S]*?<\/el-row>/)
- const buttons = toolbar ? extractButtons(toolbar[0]) : []
- const tailWithoutToolbar = toolbar ? tail.replace(toolbar[0], '') : tail
- if (!buttons.length) {
- return `${nested}\n</el-form>${tailWithoutToolbar}`
- }
- return `${nested}<el-form-item class="list-search-actions">\n ${buttons.join(
- '\n '
- )}\n </el-form-item>\n</el-form>${tailWithoutToolbar}`
- }
- )
- // 仅嵌套查询项、无按钮:去掉 list-search-actions 包裹
- next = next.replace(
- /<el-form-item class="list-search-actions">\s*((?:<el-form-item\b[\s\S]*?<\/el-form-item>\s*)+)<\/el-form>/g,
- '$1\n</el-form>'
- )
- return next
- }
- for (const file of targets) {
- const abs = path.resolve(file)
- const original = fs.readFileSync(abs, 'utf8')
- const fixed = repair(original)
- if (fixed !== original) {
- fs.writeFileSync(abs, fixed, 'utf8')
- console.log('repaired:', abs)
- } else {
- console.log('unchanged:', abs)
- }
- }
|