forked from golang/net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
html: Added new ParseOptionLenientSelfClosing option [MOD]
This option slightly relaxes parsing rules about which elements can be self-closing to fix common issues with XHTML in EPUBs generated by XML tooling. See pgaskin/kepubify#45 and pgaskin/kepubify#28 for a few examples.
- Loading branch information
Showing
2 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// Copyright 2020 Patrick Gaskin. | ||
|
||
package html | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMod_ParseLenientSelfClosing(t *testing.T) { | ||
testModCase{ | ||
What: `Self closing title in head`, | ||
Original: `<!DOCTYPE html><html><head><title/></head><body><p>Test 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title></head><body><p>Test 1</p></body></html></title></head><body></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title></title></head><body><p>Test 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing title in head with elements around`, | ||
Original: `<!DOCTYPE html><html><head><base href="/"/><title/><meta charset="asd"/></head><body><p>Test 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><base href="/"/><title><meta charset="asd"/></head><body><p>Test 1</p></body></html></title></head><body></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><base href="/"/><title></title><meta charset="asd"/></head><body><p>Test 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing title in body`, | ||
Original: `<!DOCTYPE html><html><head></head><body><title/><p>Test 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head></head><body><title><p>Test 1</p></body></html></title></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head></head><body><title></title><p>Test 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing script in head`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title><script src="script.js"/></head><body><p>Test 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title><script src="script.js"></head><body><p>Test 1</p></body></html></script></head><body></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title><script src="script.js"></script></head><body><p>Test 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing script in head with elements around`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title><base href="/"/><script src="script.js"/><meta charset="asd"/></head><body><p>Test 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title><base href="/"/><script src="script.js"><meta charset="asd"/></head><body><p>Test 1</p></body></html></script></head><body></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title><base href="/"/><script src="script.js"></script><meta charset="asd"/></head><body><p>Test 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing script in body`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><script src="script.js"/><p>Test 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><script src="script.js"><p>Test 1</p></body></html></script></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><script src="script.js"></script><p>Test 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing a in body (simple)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <a id="test"/> 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <a id="test"> 1</a></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <a id="test"></a> 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing a in body (multiple)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <a id="test"/><a id="test1"/> 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <a id="test"></a><a id="test1"> 1</a></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <a id="test"></a><a id="test1"></a> 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing a in body (within text and escaped characters around multiple elements)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test ><<a id="test"/>>< 1<span>test</span></p><p>Test 2</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test ><<a id="test">>< 1<span>test</span></a></p><p><a id="test">Test 2</a></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test ><<a id="test"></a>>< 1<span>test</span></p><p>Test 2</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing a in body (within text and escaped characters around multiple elements + HTML5 unclosed formatting elements)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p><i>Test ><<a id="test"/>><b>< 1<span>test</span></p><p>Test 2</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p><i>Test ><<a id="test">><b>< 1<span>test</span></b></a></i></p><p><i><a id="test"><b>Test 2</b></a></i></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p><i>Test ><<a id="test"></a>><b>< 1<span>test</span></b></i></p><p><i><b>Test 2</b></i></p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing span in body (simple)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <span id="test"/> 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <span id="test"> 1</span></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <span id="test"></span> 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing span in body (multiple)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <span id="test"/><span id="test1"/> 1</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <span id="test"><span id="test1"> 1</span></span></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test <span id="test"></span><span id="test1"></span> 1</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing span in body (within text and escaped characters around multiple elements)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test ><<span id="test" />>< 1<span>test</span></p><p>Test 2</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test ><<span id="test">>< 1<span>test</span></span></p><p>Test 2</p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p>Test ><<span id="test"></span>>< 1<span>test</span></p><p>Test 2</p></body></html>`, | ||
}.Test(t) | ||
|
||
testModCase{ | ||
What: `Self closing span in body (within text and escaped characters around multiple elements + HTML5 unclosed formatting elements)`, | ||
Original: `<!DOCTYPE html><html><head><title>Title</title></head><body><p><i>Test ><<span id="test" />><b>< 1<span>test</span></p><p>Test 2</p></body></html>`, | ||
|
||
ParseOptsA: []ParseOption{ParseOptionLenientSelfClosing(false)}, | ||
RenderOptsA: nil, | ||
RenderedA: `<!DOCTYPE html><html><head><title>Title</title></head><body><p><i>Test ><<span id="test">><b>< 1<span>test</span></b></span></i></p><p><i><b>Test 2</b></i></p></body></html>`, | ||
|
||
ParseOptsB: []ParseOption{ParseOptionLenientSelfClosing(true)}, | ||
RenderOptsB: nil, | ||
RenderedB: `<!DOCTYPE html><html><head><title>Title</title></head><body><p><i>Test ><<span id="test"></span>><b>< 1<span>test</span></b></i></p><p><i><b>Test 2</b></i></p></body></html>`, | ||
}.Test(t) | ||
} |