index.vue 26.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
<!--
 * @Date: 2026-02-27
 * @Description: v-html 测试页面 - 验证 v-html 在 Taro 中是否可用
 *
 * @purpose
 *   验证 v-html 指令在 Taro + Vue3 小程序中是否能正常运行
 *   对比 index1.vue 中 mp-html 组件的绑定方式
 *
 * @test-cases
 *   1. v-html 渲染响应式变量
 *   2. 内容切换响应性
 *   3. HTML 标签解析
-->
<template>
  <view class="rich-text-test-page">
    <!-- 导航栏 -->
    <NavHeader title="v-html 测试" />

    <view class="test-container">
      <!-- 测试说明 -->
      <view class="test-info">
        <text class="test-title">测试项目:</text>
        <text class="test-item">1. v-html 渲染 {{ renderTest ? '✅' : '❌' }}</text>
        <text class="test-item">2. 内容切换 {{ contentSwitchTest ? '✅' : '❌' }}</text>
        <text class="test-item">3. HTML 解析 {{ htmlParseTest ? '✅' : '❌' }}</text>
      </view>

      <!-- v-html 渲染区域 -->
      <view class="v-html-container">
        <text class="container-title">v-html 绑定区域:</text>
        <view id="taro_html" v-html="testHtml" class="taro_html"></view>
      </view>

      <!-- 测试结果 -->
      <view class="test-results">
        <text class="result-title">测试结果:</text>
        <text class="result-item">当前内容: {{ currentContentType }}</text>
        <text class="result-item">内容长度: {{ contentLength }} 字符</text>
        <text class="result-item">切换次数: {{ switchCount }}</text>
        <text class="result-item">最后操作: {{ lastAction }}</text>
      </view>

      <!-- 切换测试内容 -->
      <view class="test-actions">
        <view class="action-btn primary" @tap="switchTestContent(1)">测试1: 纯文本</view>
        <view class="action-btn primary" @tap="switchTestContent(2)">测试2: HTML标签</view>
        <view class="action-btn primary" @tap="switchTestContent(3)">测试3: 图片</view>
        <view class="action-btn primary" @tap="switchTestContent(4)">测试4: 链接</view>
        <view class="action-btn warning" @tap="clearContent">清空内容</view>
        <view class="action-btn success" @tap="loadFromAPI">模拟API加载</view>
        <view class="action-btn danger" @tap="loadRealApiData">📄 真实API</view>
        <view class="action-btn info" @tap="loadRealApiDataWithDiv">📄 真实API(a→div)</view>
        <view class="action-btn" :class="transformEnabled ? 'enabled' : ''" @tap="toggleTransform">
          {{ transformEnabled ? '✅' : '⚪' }} 图片自动处理
        </view>
      </view>

      <!-- 对比说明 -->
      <view class="compare-note">
        <text class="note-title">与 index1.vue 对比:</text>
        <text class="note-item">• index1.vue 使用 &lt;mp-html&gt; 组件 + :content="testHtml"</text>
        <text class="note-item">• 本页使用 v-html="testHtml" 指令</text>
        <text class="note-item">• 两者都绑定响应式变量 testHtml</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, nextTick, watch } from 'vue'
import Taro, { definePageConfig } from '@tarojs/taro'
import NavHeader from '@/components/navigation/NavHeader.vue'
import { $ } from '@tarojs/extend'
import { useFileOperation } from '@/composables/useFileOperation'
// v-html 需要导入 Taro 的 HTML 样式
import '@tarojs/taro/html.css'

// 文件操作
const { viewFile } = useFileOperation()

// 配置页面
definePageConfig({
  navigationBarTitleText: 'v-html 测试'
})

// 测试状态
const renderTest = ref(false)
const contentSwitchTest = ref(false)
const htmlParseTest = ref(false)
const switchCount = ref(0)
const currentContentType = ref('未加载')
const contentLength = ref(0)
const lastAction = ref('页面初始化')
const transformEnabled = ref(false) // 是否启用 transformElement

// 测试 HTML 内容
const testHtml = ref('')

// 真实 API 数据(来自 raw.md)
const realApiData = {
  id: 3185667,
  post_title: "IANG Profile入职指引",
  post_content: `<p><img src="https://cdn.ipadbiz.cn/space_3079606/微信图片_20260227170749_FhZ6SHYxM9yVKOPo1LeqVO25I35q.jpg" alt="微信图片_20260227170749.jpg" width="800" height="340" /></p>
<p>&nbsp;</p>
<p>📝IANG&nbsp;Profile入职指引💡</p>
<hr />
<p>&nbsp;</p>
<p>符合IANG&nbsp;计划的招募对象👥</p>
<p>1️⃣年龄:21-55岁</p>
<p>2️⃣持IANG、高才通B/C类 VISA及大学毕业或以上学历</p>
<p>3️⃣非香港永久性居民</p>
<p>4️⃣并非宏利前代理人</p>
<p>&nbsp;</p>
<hr />
<p>&nbsp;</p>
<p>📑📌IANG Profile需注意事项:</p>
<p>1.此计划提供18个月每月津贴(底薪),并设有3年约束期;每月津贴申请金额:本科毕业生每月津贴为港元 10,000-港元 20,000; 硕士毕业生每月津贴为港元 15,000-港元 25,000; 博士毕业生:每月津贴为港元 20,000-港元 30,000</p>
<p>2.如代理人业绩未达到该月的业绩规定,将不會获发放该月的每月津貼。代理人可于第三个、第六个、第九个、第十二个、第十五个及第十八个合約月份结束时达到相应的的业绩规定及课程规定(第四个合约月内完成财富管家课程),将可获补发未获发放的每月津贴;</p>
<p>3.若于第36个合约月内合约因任何原因被终止,需全数退还已收取的每月津贴;</p>
<p>4.若首 18 个合约月份代理人所销售保单续保率为55%以上及 80%以下,须按比例退回已收取的每月津贴;</p>
<p>5. 若首 12 个合约月份代理人所销售保单续保率为55%以上及 80%以下,须按比例退回已收取的特别业绩表现奖金 I;</p>
<p>6. 若首 13个合约月份至第24个合约月份代理人所销售保单续保率为55%以上及 80%以下,须按比例退回已收取的特别业绩表现奖金 II 。</p>
<p>&nbsp;</p>
<p>其余规定请查看以下公司IANG计划之规定文件!</p>
<p><a class="_file_list" style="display: inline-block; padding: 12px; border: 1px solid #e1e5e9; border-radius: 8px; margin: 8px; background: #f8f9fa; box-shadow: 0 2px 4px rgba(0,0,0,0.1); max-width: 400px; transition: all 0.3s ease; text-decoration: none; color: inherit; cursor: pointer; font-family: -apple-system,BlinkMacSystemFont,;" href="https://cdn.ipadbiz.cn/space_3079606/2-hk-prog_IANG_Profile_Program_C_Fj2lebJPQj71S8LhhOtgmcYVnpBD.pdf" target="_blank" rel="noopener"><span style="display: inline-block; font-size: 24px; margin-right: 12px; color: #6c757d; vertical-align: middle;"><img style="width: 30px; height: 30px; vertical-align: text-bottom;" src="https://cdn.ipadbiz.cn/img%2Ftinymce_icon%2Foffice-pdf.png" /></span><span style="display: inline-block; vertical-align: middle;"><span style="display: block; font-weight: 600; font-size: 14px; color: #2c3e50; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2;">2-hk-prog_IANG_Profile....pdf</span><span style="display: block; font-size: 12px; color: #6c757d; margin-top: 4px; line-height: 1.2;">384.34 KB</span></span></a></p>
<p>&nbsp;</p>
<p><a class="_file_list" style="display: inline-block; padding: 12px; border: 1px solid #e1e5e9; border-radius: 8px; margin: 8px; background: #f8f9fa; box-shadow: 0 2px 4px rgba(0,0,0,0.1); max-width: 400px; transition: all 0.3s ease; text-decoration: none; color: inherit; cursor: pointer; font-family: -apple-system,BlinkMacSystemFont,;" href="https://cdn.ipadbiz.cn/space_3079606/IANG Profile illustration_FriOxAVusDKilewqIqFo_OHdc84X.pdf" target="_blank" rel="noopener"><span style="display: inline-block; font-size: 24px; margin-right: 12px; color: #6c757d; vertical-align: middle;"><img style="width: 30px; height: 30px; vertical-align: text-bottom;" src="https://cdn.ipadbiz.cn/img%2Ftinymce_icon%2Foffice-pdf.png" /></span><span style="display: inline-block; vertical-align: middle;"><span style="display: block; font-weight: 600; font-size: 14px; color: #2c3e50; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2;">IANG Profile illustration.pdf</span><span style="display: block; font-size: 12px; color: #6c757d; margin-top: 4px; line-height: 1.2;">1.82 MB</span></span></a></p>
<p>&nbsp;</p>
<p><a class="_file_list" style="display: inline-block; padding: 12px; border: 1px solid #e1e5e9; border-radius: 8px; margin: 8px; background: #f8f9fa; box-shadow: 0 2px 4px rgba(0,0,0,0.1); max-width: 400px; transition: all 0.3s ease; text-decoration: none; color: inherit; cursor: pointer; font-family: -apple-system,BlinkMacSystemFont,;" href="https://cdn.ipadbiz.cn/space_3079606/IANG对数表_Fq3IQGJuM0SP4zAkKZDK803xVeQK.pdf" target="_blank" rel="noopener"><span style="display: inline-block; font-size: 24px; margin-right: 12px; color: #6c757d; vertical-align: middle;"><img style="width: 30px; height: 30px; vertical-align: text-bottom;" src="https://cdn.ipadbiz.cn/img%2Ftinymce_icon%2Foffice-pdf.png" /></span><span style="display: inline-block; vertical-align: middle;"><span style="display: block; font-weight: 600; font-size: 14px; color: #2c3e50; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2;">IANG对数表.pdf</span><span style="display: block; font-size: 12px; color: #6c757d; margin-top: 4px; line-height: 1.2;">65.73 KB</span></span></a></p>
<p>&nbsp;</p>
<hr />
<p>&nbsp;</p>
<p>📑📌IANG Profile入职资料</p>
<p>1.香港身份证</p>
<p>2.Visa</p>
<p>3.首次入境小票</p>
<p>4.申请VISA时登记的有效港澳通行证正反面或护照个人信息页</p>
<p>5.近三个月内由港府、香港银行、香港电话公司发出的【香港住宅英文地址证明】(注1️⃣)</p>
<p>6.毕业证及近三个月学信网学历认证,国外学历须提供留服认证或香港资历架构认证(注2️⃣)</p>
<p>7.卷一及卷三合格证明</p>
<p>8.香港电话号码(注3️⃣)</p>
<p>9.在港申请的香港银行卡</p>
<p>10.一寸证件照两张,底色无要求</p>
<p>11.简历</p>
<p>&nbsp;</p>
<hr />
<p>&nbsp;</p>
<p>⚠️入职资料注意事项:</p>
<p>1️⃣香港住宅英文地址证明,例如:水电煤气通知单、银行月结单或电话费月结单等;证券接单不可作为有效地址证明。</p>
<p>2️⃣艺术类,语言类或文科专业毕业,还需提供成绩单,且成绩单里包含中文、英语及数学成绩、成绩须达第二级/达E级或以上,IA看情况要求提供成绩认证。</p>
<p>3️⃣香港电话号码尽量不提供在内地申请的两地号码,两地号码在内地没办法收验证码,不便于办理挂牌手续。</p>
<p>&nbsp;</p>
<hr />
<p>&nbsp;</p>
<p>入职办理流程⤵️:</p>
<p>1.准招募递交入职申请后,由DD核实审批</p>
<p>2.审批完毕后,入职流程递交至AR部门,AR部门核实签证类型,没问题后Pass至AD部门</p>
<p>3.AD核实后,出Offer&nbsp;Letter</p>
<p>4.准招募签署完Offer&nbsp;Letter后,流程Pass至C&amp;B部门做资料综合审核及背景调查</p>
<p>5.审查完毕,C&amp;B同事帮准招募向保監提出挂牌申请</p>
<p>6.准招募收到保監发出的【保險中介一站通-啟動個人賬戶】郵件及公司發出的纸本AOP啟動碼</p>
<p>7.联系秘书填写一站通资料</p>
<p>8.公司审核一站通资料无误后,准招募支付挂牌费用(3年挂牌费为港元810)</p>
<p>9.挂牌后,公司生成专属Agent&nbsp;code,并发出代理人合约及奖金合约</p>
<p>10.准招募签署合约并递交给公司</p>
<p>11.收到公司配送的Welcome&nbsp;Kit,内含工牌、门卡及邮件激活密码</p>
<p>12.激活邮件及Manutouch账号后,可开始签单</p>
<p>&nbsp;</p>
<p><img src="https://cdn.ipadbiz.cn/space_3079606/微信图片_20260227171202_FhZfGYQAKK_04-E_Td5TzNosjlXm.jpg" alt="微信图片_20260227171202.jpg" width="1200" height="596" /></p>
<hr />
<p>&nbsp;</p>
<p>⭐入职需完成课程✅:</p>
<p>1.【新人课】,完成后方可发第1期底薪,建议在递交完入职资料,等待出Code过程中,联系秘书报名,最迟在入职第一个合约月内完成,完成后方可发第1个月底薪。</p>
<p>2.【财富管家课程】,入职后4个月内需上完,否则会影响底薪及佣金发放,建议出code后,尽快安排时间联系秘书报名。</p>
<p>&nbsp;</p>
<p>🕹️签署合约后,每月需交给公司200元/月行政费!</p>
<hr />
<p>&nbsp;</p>
<p>入职资料Sample</p>
<p><a class="_file_list" style="display: inline-block; padding: 12px; border: 1px solid #e1e5e9; border-radius: 8px; margin: 8px; background: #f8f9fa; box-shadow: 0 2px 4px rgba(0,0,0,0.1); max-width: 400px; transition: all 0.3s ease; text-decoration: none; color: inherit; cursor: pointer; font-family: -apple-system,BlinkMacSystemFont,;" href="https://cdn.ipadbiz.cn/space_3079606/需提供的部分个人资料SAMPLE(不含流水SAMPLE)_扫描版_lkAqOTlyK-jZ7RbBN43oiG5QPfr-.pdf" target="_blank" rel="noopener"><span style="display: inline-block; font-size: 24px; margin-right: 12px; color: #6c757d; vertical-align: middle;"><img style="width: 30px; height: 30px; vertical-align: text-bottom;" src="https://cdn.ipadbiz.cn/img%2Ftinymce_icon%2Foffice-pdf.png" /></span><span style="display: inline-block; vertical-align: middle;"><span style="display: block; font-weight: 600; font-size: 14px; color: #2c3e50; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2;">需提供的部分个人资料SAMPLE(不含流水S....pdf</span><span style="display: block; font-size: 12px; color: #6c757d; margin-top: 4px; line-height: 1.2;">101.91 MB</span></span></a></p>
<p>&nbsp;</p>`
}

// 测试内容模板
const testContents = {
  1: {
    name: '纯文本',
    content: `
      <h1>测试1:纯文本渲染</h1>
      <p>这是一段纯文本内容,测试 v-html 是否能正确渲染基本的 HTML 结构。</p>
      <p><strong>加粗文本</strong>和<em>斜体文本</em>测试。</p>
    `
  },
  2: {
    name: 'HTML标签',
    content: `
      <h1>测试2:HTML 标签解析</h1>
      <h2>二级标题</h2>
      <h3>三级标题</h3>
      <ul>
        <li>列表项 1</li>
        <li>列表项 2</li>
        <li>列表项 3</li>
      </ul>
      <p>段落测试:<span style="color: red;">红色文字</span>和<span style="color: blue;">蓝色文字</span></p>
    `
  },
  3: {
    name: '图片',
    content: `
      <h1>测试3:图片渲染</h1>
      <p>点击图片测试长按预览功能:</p>
      <p class="h5-p">
        <img class="h5-img" src="https://cdn.ipadbiz.cn/space_3079606/微信图片_20260227170749_FhZ6SHYxM9yVKOPo1LeqVO25I35q.jpg" alt="测试图片" style="max-width: 100%;" />
      </p>
    `
  },
  4: {
    name: '链接',
    content: `
      <h1>测试4:链接渲染</h1>
      <p>测试链接是否正确渲染:</p>
      <p>
        <a href="https://www.baidu.com">百度链接</a> |
        <a href="https://www.github.com">GitHub 链接</a>
      </p>
      <p>PDF 链接测试:
        <a href="https://cdn.ipadbiz.cn/space_3079606/需提供的部分个人资料SAMPLE(不含流水SAMPLE)_扫描版_lkAqOTlyK-jZ7RbBN43oiG5QPfr-.pdf">PDF 文档</a>
      </p>
    `
  }
}

// 切换测试内容
const switchTestContent = (index) => {
  const template = testContents[index]
  if (template) {
    testHtml.value = template.content
    currentContentType.value = template.name
    contentLength.value = template.content.length
    switchCount.value++
    lastAction.value = `切换到测试${index}: ${template.name}`
    contentSwitchTest.value = true

    // 绑定图片长按事件
    nextTick(() => {
      bindImageEvents()
    })
  }
}

// 清空内容
const clearContent = () => {
  testHtml.value = ''
  currentContentType.value = '已清空'
  contentLength.value = 0
  lastAction.value = '清空内容'
}

// 模拟 API 加载
const loadFromAPI = () => {
  lastAction.value = '模拟 API 加载中...'
  Taro.showLoading({ title: '加载中...' })

  setTimeout(() => {
    // 模拟 API 返回的数据
    const apiData = `
      <div class="api-content">
        <h1>从 API 加载的内容</h1>
        <p>这是模拟从后端 API 获取的富文本内容。</p>
        <p>加载时间:${new Date().toLocaleString()}</p>
        <p class="h5-p">
          <img class="h5-img" src="https://cdn.ipadbiz.cn/space_3079606/微信图片_20260227170749_FhZ6SHYxM9yVKOPo1LeqVO25I35q.jpg" alt="API图片" />
        </p>
      </div>
    `
    testHtml.value = apiData
    currentContentType.value = 'API 数据'
    contentLength.value = apiData.length
    lastAction.value = 'API 加载完成'
    Taro.hideLoading()

    nextTick(() => {
      bindImageEvents()
    })
  }, 1000)
}

// 加载真实 API 数据(原始版本,包含 <a> 标签)
const loadRealApiData = () => {
  lastAction.value = '加载真实 API 数据(原始)...'
  Taro.showLoading({ title: '加载中...' })

  setTimeout(() => {
    testHtml.value = realApiData.post_content
    currentContentType.value = `真实文章(原始<a>): ${realApiData.post_title}`
    contentLength.value = realApiData.post_content.length
    switchCount.value++
    lastAction.value = `加载文章 ID: ${realApiData.id} (原始<a>标签)`
    Taro.hideLoading()

    nextTick(() => {
      bindImageEvents()
    })

    Taro.showToast({
      title: '原始 <a> 标签',
      icon: 'none'
    })
  }, 500)
}

// 切换 transformElement
const toggleTransform = () => {
  transformEnabled.value = !transformEnabled.value

  if (transformEnabled.value) {
    // 启用 transformElement
    Taro.options.html.transformElement = (el) => {
      const nodeName = el.nodeName?.toLowerCase() || ''
      const tagName = el.tagName?.toLowerCase() || ''

      // 处理 img/image 标签
      if (nodeName === 'img' || tagName === 'img' || nodeName === 'image' || tagName === 'image') {
        const src = el.getAttribute('src') || ''

        // 检查父元素链
        let parent = el.parentElement
        let isInsideLink = false
        let parentChain = []

        while (parent) {
          const pName = parent.nodeName?.toLowerCase() || ''
          const pDataIsLink = parent.getAttribute?.('data-is-link')
          const pClassList = parent.classList?.value || ''

          parentChain.push(pName)

          if (pName === 'a' || pDataIsLink === 'true' || pClassList.includes('_file_list')) {
            isInsideLink = true
            break
          }

          parent = parent.parentElement
        }

        if (isInsideLink) {
          // 在链接内,不处理(图标),但必须返回元素让它显示
          return el // 直接返回,不做任何修改
        }

        // 不在链接内,处理(内容图片)
        el.setAttribute('mode', 'widthFix')
        el.setAttribute('style', 'width: 100%;')
      }

      // 所有元素都必须返回,否则会被过滤掉不显示
      return el
    }

    lastAction.value = '已启用 transformElement(外部图片自动 widthFix)'
    Taro.showToast({
      title: '已启用图片自动处理',
      icon: 'success'
    })

    // 重新加载当前内容以应用 transform
    if (testHtml.value) {
      const currentContent = testHtml.value
      testHtml.value = ''
      nextTick(() => {
        testHtml.value = currentContent
      })
    }
  } else {
    // 禁用 transformElement
    Taro.options.html.transformElement = undefined

    lastAction.value = '已禁用 transformElement'
    Taro.showToast({
      title: '已禁用图片自动处理',
      icon: 'none'
    })

    // 重新加载当前内容
    if (testHtml.value) {
      const currentContent = testHtml.value
      testHtml.value = ''
      nextTick(() => {
        testHtml.value = currentContent
      })
    }
  }
}

// 加载真实 API 数据(<a> 替换为 <div> 版本)
const loadRealApiDataWithDiv = () => {
  lastAction.value = '加载真实 API 数据(a→div)...'
  Taro.showLoading({ title: '加载中...' })

  setTimeout(() => {
    // 将 <a> 标签替换为 <div> 标签,保留 href 为 data-href
    let content = realApiData.post_content

    // 替换 HTML 实体
    content = content.replace(/&nbsp;/g, ' ')         // 空格
    content = content.replace(/&amp;/g, '&')          // &
    content = content.replace(/&lt;/g, '<')           // <
    content = content.replace(/&gt;/g, '>')           // >
    content = content.replace(/&quot;/g, '"')         // "
    content = content.replace(/&apos;/g, "'")         // '

    // 替换 <a ... href="..."> 为 <div ... data-href="..." data-is-link="true">
    content = content.replace(/<a\s+/g, '<div data-is-link="true" ')
    content = content.replace(/href=/g, 'data-href=')
    content = content.replace(/<\/a>/g, '</div>')

    // 统计 data-href 数量
    const hrefCount = (content.match(/data-href=/g) || []).length

    testHtml.value = content
    currentContentType.value = `真实文章(<a>→<div>): ${realApiData.post_title}`
    contentLength.value = content.length
    switchCount.value++
    lastAction.value = `加载文章 ID: ${realApiData.id} (a标签已替换为div, href=${hrefCount})`
    Taro.hideLoading()

    nextTick(() => {
      bindImageEvents()
      // 绑定文件链接点击事件
      bindFileLinkEvents()
    })

    Taro.showToast({
      title: `已绑定 ${hrefCount} 个文件链接`,
      icon: 'success'
    })
  }, 500)
}

// 绑定图片长按预览事件(类似 activityDetail 页面的实现)
const bindImageEvents = () => {
  const imgs = $('#taro_html').children('.h5-p').children('.h5-img')

  imgs.forEach(function (img) {
    $(img).on('longpress', function () {
      const src = $(img).attr('src')

      Taro.previewImage({
        urls: [src],
        current: src,
        indicator: 'default',
        loop: false,
        success: res => {
          lastAction.value = '图片预览触发'
        },
        fail: err => {
          // 预览失败
        }
      })
    })
  })

  htmlParseTest.value = imgs.length > 0
}

// 绑定文件链接点击事件
const bindFileLinkEvents = () => {
  // 先检查 #taro_html 容器是否存在
  const container = $('#taro_html')

  // 检查带 data-is-link 属性的 div
  const fileLinks = container.find('div[data-is-link="true"]')

  // 如果找不到,尝试用 class 查找
  if (fileLinks.length === 0) {
    const classLinks = container.find('._file_list')

    // 如果用 class 找到了,绑定到这些元素上
    classLinks.each(function (idx, el) {
      const $el = $(el)
      const dataHref = $el.attr('data-href')

      if (dataHref) {
        $el.on('tap', function () {
          const fileName = $el.find('span span span').first().text() || 'document.pdf'
          lastAction.value = `打开文件: ${fileName}`
          viewFile({
            downloadUrl: dataHref,
            fileName: fileName
          })
        })
      }
    })

    return classLinks.length
  }

  // 原始逻辑:使用 data-is-link 查找
  fileLinks.forEach(function (link) {
    const $link = $(link)
    const href = $link.attr('data-href')

    $link.on('tap', function () {
      const fileName = $link.find('span span span').first().text() || 'document.pdf'

      if (href) {
        lastAction.value = `打开文件: ${fileName}`

        // 使用 useFileOperation 打开文件
        viewFile({
          downloadUrl: href,
          fileName: fileName
        })
      }
    })
  })

  return fileLinks.length
}

// 监听 testHtml 变化
watch(testHtml, (_newVal, _oldVal) => {
  renderTest.value = true
})

// 初始化
onMounted(() => {
  // 加载默认测试内容
  switchTestContent(1)
})
</script>

<style lang="less">
.rich-text-test-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.test-container {
  padding: 32rpx;
}

.test-info {
  background: #fff;
  padding: 24rpx;
  border-radius: 12rpx;
  margin-bottom: 24rpx;
}

.test-title {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 16rpx;
}

.test-item {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin: 8rpx 0;
}

.v-html-container {
  background: #fff;
  padding: 24rpx;
  border-radius: 12rpx;
  margin-bottom: 24rpx;
  min-height: 200rpx;
}

.container-title {
  display: block;
  font-size: 28rpx;
  color: #999;
  margin-bottom: 16rpx;
}

.taro_html {
  // Taro html.css 样式
  :deep(.h5-p) {
    margin: 16rpx 0;
  }

  :deep(.h5-img) {
    max-width: 100%;
    display: block;
  }

  :deep(h1) {
    font-size: 48rpx;
    font-weight: bold;
    margin: 32rpx 0 16rpx;
  }

  :deep(h2) {
    font-size: 40rpx;
    font-weight: bold;
    margin: 24rpx 0 12rpx;
  }

  :deep(h3) {
    font-size: 36rpx;
    font-weight: bold;
    margin: 20rpx 0 12rpx;
  }

  :deep(p) {
    font-size: 28rpx;
    line-height: 1.6;
    margin: 12rpx 0;
    color: #333;
  }

  :deep(ul) {
    padding-left: 48rpx;
    margin: 16rpx 0;
  }

  :deep(li) {
    font-size: 28rpx;
    line-height: 1.6;
    margin: 8rpx 0;
  }

  :deep(a) {
    color: #1989fa;
    text-decoration: underline;
  }

  :deep(strong) {
    font-weight: bold;
  }

  :deep(em) {
    font-style: italic;
  }

  :deep(span) {
    font-size: 28rpx;
  }

  // 文件链接卡片样式
  :deep(._file_list) {
    display: inline-block !important;
    padding: 12px;
    border: 1px solid #e1e5e9;
    border-radius: 8px;
    margin: 8px;
    background: #f8f9fa;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    max-width: 400px;
    transition: all 0.3s ease;
    text-decoration: none;
    color: inherit;
    cursor: pointer;
    width: 100%;
    box-sizing: border-box;
  }
}

.test-results {
  background: #fff;
  padding: 24rpx;
  border-radius: 12rpx;
  margin-bottom: 24rpx;
}

.result-title {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 16rpx;
}

.result-item {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin: 8rpx 0;
}

.test-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
  margin-bottom: 24rpx;
}

.action-btn {
  padding: 16rpx 32rpx;
  border-radius: 8rpx;
  font-size: 28rpx;
  text-align: center;
  background: #f0f0f0;
  color: #333;

  &.primary {
    background: #1989fa;
    color: #fff;
  }

  &.warning {
    background: #ff976a;
    color: #fff;
  }

  &.success {
    background: #07c160;
    color: #fff;
  }

  &.danger {
    background: #ee0a24;
    color: #fff;
    font-weight: bold;
  }

  &.info {
    background: #7232dd;
    color: #fff;
  }

  &.enabled {
    background: #07c160;
    color: #fff;
  }
}

.compare-note {
  background: #f0f9ff;
  padding: 24rpx;
  border-radius: 12rpx;
  border-left: 8rpx solid #1989fa;
}

.note-title {
  display: block;
  font-size: 28rpx;
  font-weight: bold;
  color: #1989fa;
  margin-bottom: 16rpx;
}

.note-item {
  display: block;
  font-size: 24rpx;
  color: #666;
  margin: 8rpx 0;
  line-height: 1.5;
}
</style>