Add an option to strip <script> elements

Partially addresses issue #11.
This commit is contained in:
Vytautas Šaltenis 2013-04-13 22:57:16 +03:00
parent b79e720a36
commit fb923cdb78
2 changed files with 17 additions and 1 deletions

View File

@ -28,6 +28,7 @@ const (
HTML_SKIP_STYLE // skip embedded <style> elements HTML_SKIP_STYLE // skip embedded <style> elements
HTML_SKIP_IMAGES // skip embedded images HTML_SKIP_IMAGES // skip embedded images
HTML_SKIP_LINKS // skip all links HTML_SKIP_LINKS // skip all links
HTML_SKIP_SCRIPT // skip embedded <script> elements
HTML_SAFELINK // only link to trusted protocols HTML_SAFELINK // only link to trusted protocols
HTML_TOC // generate a table of contents HTML_TOC // generate a table of contents
HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents) HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents)
@ -460,6 +461,9 @@ func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) {
if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") { if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
return return
} }
if options.flags&HTML_SKIP_SCRIPT != 0 && isHtmlTag(text, "script") {
return
}
out.Write(text) out.Write(text)
} }

View File

@ -70,8 +70,20 @@ func TestRawHtmlTag(t *testing.T) {
"zz <STYLE>p {}</STYLE>\n", "zz <STYLE>p {}</STYLE>\n",
"<p>zz p {}</p>\n", "<p>zz p {}</p>\n",
"<SCRIPT>alert()</SCRIPT>\n",
"<p>alert()</p>\n",
"zz <SCRIPT>alert()</SCRIPT>\n",
"<p>zz alert()</p>\n",
"zz <script>alert()</script>\n",
"<p>zz alert()</p>\n",
" <script>alert()</script>\n",
"<p>alert()</p>\n",
} }
doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE) doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SKIP_SCRIPT)
} }
func TestEmphasis(t *testing.T) { func TestEmphasis(t *testing.T) {