From cc32c09e2d3eb7c3f20cae8ef201ecd28d006d35 Mon Sep 17 00:00:00 2001 From: Nathan Davies Date: Fri, 7 Jan 2022 15:12:27 +0000 Subject: [PATCH] skip over noscript tags The contents of the noscript tag is not really considered part of the raw text of an HTML document. This patch skips over such tags and I've added a little test to ensure this is correct. --- html2text.go | 2 +- html2text_test.go | 4 ++++ testdata/utf8.html | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/html2text.go b/html2text.go index 657c3fb..e7727aa 100644 --- a/html2text.go +++ b/html2text.go @@ -291,7 +291,7 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error { ctx.isPre = false return err - case atom.Style, atom.Script, atom.Head: + case atom.Style, atom.Script, atom.Head, atom.Noscript : // Ignore the subtree. return nil diff --git a/html2text_test.go b/html2text_test.go index 452b45e..e28aa51 100644 --- a/html2text_test.go +++ b/html2text_test.go @@ -775,6 +775,10 @@ func TestIgnoreStylesScriptsHead(t *testing.T) { `Title`, "", }, + { + "", + "", + }, } for _, testCase := range testCases { diff --git a/testdata/utf8.html b/testdata/utf8.html index 53d401c..5d4b271 100755 --- a/testdata/utf8.html +++ b/testdata/utf8.html @@ -17,6 +17,12 @@

这本书的创作耗费了相当多的时间和精力。在成长的过程中,我在我的小房间里从未想过等待我的会是这样的战斗。在创作中,我的思想逐渐成熟;爱恋从分崩离析,到失而复得,世界冠军头衔从失之交臂,到囊中取物。如果说在我人生的第一个二十九年中,我学到了什么,那就是,我们永远无法预测结局,无论是重要的比赛、冒险,还是轰轰烈烈的爱情。我们唯一可以肯定的只有,出乎意料。不管我们做了多么万全的准备,在生活的真实场景中,我们总是会处于陌生的境地。我们也许会无法冷静,失去理智,感觉似乎整个世界都在针对我们。在这个时候,我们所要做的是要付出加倍的努力,要表现得比预想得更好。我认为,关键在于准备好随机应变,准备好在所能想象的高压下发挥出创造力。

读者朋友们,我非常希望你们在读过这本书后,可以得到启发,甚至会得到触动,从而能够根据各自的天赋与特长,去实现自己的梦想。这就是我写作此书的目的。我在字里行间所传达的理念曾经使我受益匪浅,我很希望它们可以为大家提供一个基本的框架和方向。如果我的方法言之有理,那么就请接受它,琢磨它,并加之自己的见解。忘记我的那些数字。真正的掌握需要通过自己发现一些最能够引起共鸣的信息,并将其彻底地融合进来,直至成为一体,这样我们才能随心所欲地驾驭它。

+ + \ No newline at end of file