Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pac
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tools
pac
Commits
9965af0d
Commit
9965af0d
authored
Sep 03, 2026
by
luoqi
Browse files
Options
Browse Files
Download
Plain Diff
merge: fix/strip-html-double-encoded → test(双重编码剥不干净)
parents
7b0124bb
1fe0a5a9
Pipeline
#3662
failed in 0 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
13 deletions
+40
-13
apps/pac-service/src/modules/sync/transforms/operators/derive.op.ts
+25
-13
apps/pac-service/tests/friday-return-visit.spec.ts
+15
-0
No files found.
apps/pac-service/src/modules/sync/transforms/operators/derive.op.ts
View file @
9965af0d
...
@@ -136,19 +136,31 @@ function evalExpr(expr: DeriveExpr, row: Row): unknown {
...
@@ -136,19 +136,31 @@ function evalExpr(expr: DeriveExpr, row: Row): unknown {
* - 收尾清理连续空行/首尾空白;全部剥完只剩空白 → 返回 null(空壳不入库)
* - 收尾清理连续空行/首尾空白;全部剥完只剩空白 → 返回 null(空壳不入库)
*/
*/
export
function
stripHtml
(
input
:
string
):
string
|
null
{
export
function
stripHtml
(
input
:
string
):
string
|
null
{
const
text
=
input
// 单轮:剥标签 → 还原实体。实体还原可能**再露出字面标签** —— 宿主实测存在双重编码:
// 块级结束/换行标签先转成换行,保住段落边界
// '<p><span…><p>阿斯蒂芬撒的发生</p></span></p>'
.
replace
(
/<
\s
*br
\s
*
\/?\s
*>/gi
,
'
\
n'
)
// 外层是真标签、内层是被转义的标签,剥一轮后 <p> 还原成 <p> 留在结果里。
.
replace
(
/<
\/\s
*
(
p|div|li|tr|h
[
1-6
]
|blockquote
)\s
*>/gi
,
'
\
n'
)
// 故做**有界多轮**:还原后若又出现标签则再剥一轮,最多 2 轮 —— 不做无界循环,
// 其余标签一律剥掉
// 避免构造出的畸形输入(每轮都产生新标签)把摄入卡死。
.
replace
(
/<
[^
>
]
*>/g
,
''
)
const
once
=
(
v
:
string
):
string
=>
// 实体还原(& 放最后,避免 &lt; 被二次解码成 <)
v
.
replace
(
/ /gi
,
' '
)
// 块级结束/换行标签先转成换行,保住段落边界
.
replace
(
/</gi
,
'<'
)
.
replace
(
/<
\s
*br
\s
*
\/?\s
*>/gi
,
'
\
n'
)
.
replace
(
/>/gi
,
'>'
)
.
replace
(
/<
\/\s
*
(
p|div|li|tr|h
[
1-6
]
|blockquote
)\s
*>/gi
,
'
\
n'
)
.
replace
(
/"/gi
,
'"'
)
// 其余标签一律剥掉
.
replace
(
/'/g
,
"'"
)
.
replace
(
/<
[^
>
]
*>/g
,
''
)
.
replace
(
/&/gi
,
'&'
)
// 实体还原(& 放最后,避免 &lt; 被二次解码成 <)
.
replace
(
/ /gi
,
' '
)
.
replace
(
/</gi
,
'<'
)
.
replace
(
/>/gi
,
'>'
)
.
replace
(
/"/gi
,
'"'
)
.
replace
(
/'/g
,
"'"
)
.
replace
(
/&/gi
,
'&'
);
let
text
=
once
(
input
);
// 第 2 轮只在确实又露出标签时才跑(绝大多数行一轮即净,不做无谓开销)
if
(
/<
[^
>
]
+>/
.
test
(
text
))
text
=
once
(
text
);
text
=
text
// 连续空行压成一个,首尾清干净
// 连续空行压成一个,首尾清干净
.
replace
(
/
[
\t]
+
\n
/g
,
'
\
n'
)
.
replace
(
/
[
\t]
+
\n
/g
,
'
\
n'
)
.
replace
(
/
\n{2,}
/g
,
'
\
n'
)
.
replace
(
/
\n{2,}
/g
,
'
\
n'
)
...
...
apps/pac-service/tests/friday-return-visit.spec.ts
View file @
9965af0d
...
@@ -168,5 +168,20 @@ describe('FRIDAY 回访摄入', () => {
...
@@ -168,5 +168,20 @@ describe('FRIDAY 回访摄入', () => {
test
(
'&lt; 不被二次解码成 <(实体还原顺序)'
,
()
=>
{
test
(
'&lt; 不被二次解码成 <(实体还原顺序)'
,
()
=>
{
expect
(
stripHtml
(
'&lt;'
)).
toBe
(
'<'
);
expect
(
stripHtml
(
'&lt;'
)).
toBe
(
'<'
);
});
});
test
(
'⭐ 双重编码:剥完外层标签、还原实体后又露出字面标签 → 再剥一轮'
,
()
=>
{
// 线上真实数据(customer_return_visit id=266151):外层是真标签,内层是被转义的标签。
// 首轮导入时这条**没被剥干净**,落库后仍是 "<p>阿斯蒂芬撒的发生</p>" —— 由此加的有界第二轮。
const
real
=
'<p><span style="color:#000000"><span style="font-size:13px">'
+
'<span style="background-color:#ffffff"><p>阿斯蒂芬撒的发生</p></span></span></span></p>'
;
expect
(
stripHtml
(
real
)).
toBe
(
'阿斯蒂芬撒的发生'
);
});
test
(
'只剥两轮,不做无界循环(畸形输入不能卡死摄入)'
,
()
=>
{
// 每轮都能再生出标签的构造输入:两轮后仍有残留是可接受的,关键是**必然终止**
const
nested
=
'&amp;lt;p&amp;gt;x'
;
expect
(
typeof
stripHtml
(
nested
)).
toBe
(
'string'
);
});
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment