Improve auto-paragraph to preserve newlines and handle edge-cases better.
[htmlpurifier.git] / tests / HTMLPurifier / Strategy / MakeWellFormed_InjectorTest.php
blob8a2ebb83b0a592032ba7cea0a860e7a8b336e089
1 <?php
3 class HTMLPurifier_Strategy_MakeWellFormed_InjectorTest extends HTMLPurifier_StrategyHarness
6 function setUp() {
7 parent::setUp();
8 $this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
9 $this->config->set('AutoFormat', 'AutoParagraph', true);
10 $this->config->set('AutoFormat', 'Linkify', true);
11 $this->config->set('AutoFormat', 'RemoveEmpty', true);
12 generate_mock_once('HTMLPurifier_Injector');
15 function testEndNotification() {
16 $mock = new HTMLPurifier_InjectorMock();
17 $mock->skip = false;
18 $b = new HTMLPurifier_Token_End('b');
19 $b->start = new HTMLPurifier_Token_Start('b');
20 $mock->expectAt(0, 'notifyEnd', array($b));
21 $i = new HTMLPurifier_Token_End('i');
22 $i->start = new HTMLPurifier_Token_Start('i');
23 $mock->expectAt(1, 'notifyEnd', array($i));
24 $mock->expectCallCount('notifyEnd', 2);
25 $this->config->set('AutoFormat', 'AutoParagraph', false);
26 $this->config->set('AutoFormat', 'Linkify', false);
27 $this->config->set('AutoFormat', 'Custom', array($mock));
28 $this->assertResult('<i><b>asdf</b>', '<i><b>asdf</b></i>');
31 function testErrorRequiredElementNotAllowed() {
32 $this->config->set('HTML', 'Allowed', '');
33 $this->expectError('Cannot enable AutoParagraph injector because p is not allowed');
34 $this->expectError('Cannot enable Linkify injector because a is not allowed');
35 $this->assertResult('Foobar');
38 function testErrorRequiredAttributeNotAllowed() {
39 $this->config->set('HTML', 'Allowed', 'a,p');
40 $this->expectError('Cannot enable Linkify injector because a.href is not allowed');
41 $this->assertResult('<p>http://example.com</p>');
44 function testOnlyAutoParagraph() {
45 $this->assertResult(
46 'Foobar',
47 '<p>Foobar</p>'
51 function testParagraphWrappingOnlyLink() {
52 $this->assertResult(
53 'http://example.com',
54 '<p><a href="http://example.com">http://example.com</a></p>'
58 function testParagraphWrappingNodeContainingLink() {
59 $this->assertResult(
60 '<b>http://example.com</b>',
61 '<p><b><a href="http://example.com">http://example.com</a></b></p>'
65 function testParagraphWrappingPoorlyFormedNodeContainingLink() {
66 $this->assertResult(
67 '<b>http://example.com',
68 '<p><b><a href="http://example.com">http://example.com</a></b></p>'
72 function testTwoParagraphsContainingOnlyOneLink() {
73 $this->assertResult(
74 "http://example.com\n\nhttp://dev.example.com",
75 '<p><a href="http://example.com">http://example.com</a></p>
77 <p><a href="http://dev.example.com">http://dev.example.com</a></p>'
81 function testParagraphNextToDivWithLinks() {
82 $this->assertResult(
83 'http://example.com <div>http://example.com</div>',
84 '<p><a href="http://example.com">http://example.com</a> </p>
86 <div><a href="http://example.com">http://example.com</a></div>'
90 function testRealisticLinkInSentence() {
91 $this->assertResult(
92 'This URL http://example.com is what you need',
93 '<p>This URL <a href="http://example.com">http://example.com</a> is what you need</p>'
97 function testParagraphAfterLinkifiedURL() {
98 $this->assertResult(
99 "http://google.com
101 <b>b</b>",
102 "<p><a href=\"http://google.com\">http://google.com</a></p>
104 <p><b>b</b></p>"
108 function testEmptyAndParagraph() {
109 // This is a fairly degenerate case, but it demonstrates that
110 // the two don't error out together, at least.
111 // Change this behavior!
112 $this->assertResult(
113 "<p>asdf
115 asdf<b></b></p>
117 <p></p><i></i>",
118 "<p>asdf</p>
120 <p>asdf</p>
128 function testRewindAndParagraph() {
129 // perhaps change the behavior of this?
130 $this->assertResult(
131 "bar
133 <p><i></i>
135 </p>
137 foo",
138 "<p>bar</p>
140 <p></p>
142 <p>foo</p>"