@birthday8/doc-mcp 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,118 @@
1
+ """
2
+ 测试严格验证器
3
+ """
4
+
5
+ from html_validator_strict import StrictHTMLValidator, ValidationError, strict_validator
6
+
7
+ # 测试1: 不允许的标签
8
+ print("=== 测试1: 不允许的标签 ===")
9
+ html1 = """<!DOCTYPE html>
10
+ <html>
11
+ <body>
12
+ <p>正常段落</p>
13
+ <script>alert('hello')</script>
14
+ <p>另一个段落</p>
15
+ </body>
16
+ </html>"""
17
+ result1 = strict_validator.validate(html1, strict_mode=False)
18
+ print(f"验证通过: {result1.is_valid}")
19
+ print(f"错误数: {len(result1.errors)}")
20
+ for error in result1.errors:
21
+ print(f" - {error['code']}: {error['message']}")
22
+ print()
23
+
24
+ # 测试2: 不允许的属性
25
+ print("=== 测试2: 不允许的属性 ===")
26
+ html2 = """<!DOCTYPE html>
27
+ <html>
28
+ <body>
29
+ <img src="test.jpg" alt="测试" onclick="alert('click')" />
30
+ </body>
31
+ </html>"""
32
+ result2 = strict_validator.validate(html2, strict_mode=False)
33
+ print(f"验证通过: {result2.is_valid}")
34
+ print(f"错误数: {len(result2.errors)}")
35
+ for error in result2.errors:
36
+ print(f" - {error['code']}: {error['message']}")
37
+ print()
38
+
39
+ # 测试3: 错误的颜色格式
40
+ print("=== 测试3: 错误的颜色格式 ===")
41
+ html3 = """<!DOCTYPE html>
42
+ <html>
43
+ <body>
44
+ <p style="color: red;">红色文本</p>
45
+ <p style="color: rgb(255,0,0);">RGB颜色</p>
46
+ <p style="color: #FF0000;">正确的颜色</p>
47
+ </body>
48
+ </html>"""
49
+ result3 = strict_validator.validate(html3, strict_mode=False)
50
+ print(f"验证通过: {result3.is_valid}")
51
+ print(f"错误数: {len(result3.errors)}")
52
+ for error in result3.errors:
53
+ print(f" - {error['code']}: {error['message']}")
54
+ print()
55
+
56
+ # 测试4: 正确的HTML
57
+ print("=== 测试4: 正确的HTML ===")
58
+ html4 = """<!DOCTYPE html>
59
+ <html lang="zh-CN">
60
+ <head>
61
+ <meta charset="UTF-8">
62
+ </head>
63
+ <body>
64
+ <h1>标题</h1>
65
+ <p><strong>加粗</strong>文本</p>
66
+ <p style="color: #FF0000;">红色文本</p>
67
+ <img src="test.jpg" alt="测试" />
68
+ </body>
69
+ </html>"""
70
+ result4 = strict_validator.validate(html4, strict_mode=False)
71
+ print(f"验证通过: {result4.is_valid}")
72
+ print(f"错误数: {len(result4.errors)}")
73
+ print(f"警告数: {len(result4.warnings)}")
74
+ if result4.warnings:
75
+ for warning in result4.warnings:
76
+ print(f" - {warning['code']}: {warning['message']}")
77
+ print()
78
+
79
+ # 测试5: 错误的嵌套
80
+ print("=== 测试5: 错误的嵌套 ===")
81
+ html5 = """<!DOCTYPE html>
82
+ <html>
83
+ <body>
84
+ <p>段落包含<div>块级元素</div></p>
85
+ </body>
86
+ </html>"""
87
+ result5 = strict_validator.validate(html5, strict_mode=False)
88
+ print(f"验证通过: {result5.is_valid}")
89
+ print(f"错误数: {len(result5.errors)}")
90
+ for error in result5.errors:
91
+ print(f" - {error['code']}: {error['message']}")
92
+ print()
93
+
94
+ # 测试6: 测试example.html
95
+ print("=== 测试6: 测试example.html ===")
96
+ import os
97
+
98
+ example_path = os.path.join("sample", "example.html")
99
+ if os.path.exists(example_path):
100
+ with open(example_path, "r", encoding="utf-8") as f:
101
+ html6 = f.read()
102
+ result6 = strict_validator.validate(html6, strict_mode=False)
103
+ print(f"验证通过: {result6.is_valid}")
104
+ print(f"错误数: {len(result6.errors)}")
105
+ print(f"警告数: {len(result6.warnings)}")
106
+ if result6.errors:
107
+ print("错误详情:")
108
+ for error in result6.errors:
109
+ print(f" - {error['code']}: {error['message']}")
110
+ if result6.warnings:
111
+ print("警告详情:")
112
+ for warning in result6.warnings:
113
+ print(f" - {warning['code']}: {warning['message']}")
114
+ else:
115
+ print("example.html 不存在")
116
+ print()
117
+
118
+ print("✅ 所有测试完成!")