2008-06-25 01:10:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class HTMLPurifier_Strategy_MakeWellFormed_InjectorTest extends HTMLPurifier_StrategyHarness
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
|
|
|
|
$this->config->set('AutoFormat', 'AutoParagraph', true);
|
|
|
|
$this->config->set('AutoFormat', 'Linkify', true);
|
2008-06-27 20:09:14 +00:00
|
|
|
$this->config->set('AutoFormat', 'RemoveEmpty', true);
|
2008-06-25 01:10:51 +00:00
|
|
|
generate_mock_once('HTMLPurifier_Injector');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
Implement Injector->handleEnd, with lots of refactoring for injector.
Previous design of injector streaming involved editability only to start, empty
and text tokens, because they could be safely modified without causing formedness
errors. By modifying notifyEnd to operate before MakeWellFormed's safeguards
kick into effect, it can be converted into a handle function, allowing for
arbitrary modification of end tags.
This change involved quite a bit of restructuring of the MakeWellFormed code,
including the moving of end of document tags to inside the loop, so rewinding
on those tags would be functional, increased reuse of the end tag codepath by
code that inserts end tags (as they could be changed out from under you), and
processToken modified to have an extra parameter to force re-processing of
a token if the original token was an end token.
We're not exactly sure if handleEnd works at this point, but the important
talking point about this refactoring is that nothing else broke. Also, a number
of convenience functions were moved from AutoParagraph to the Injector
supertype (specifically: forward, forwardToEndToken, backward, and current).
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
2008-10-01 04:54:51 +00:00
|
|
|
function testEndHandler() {
|
2008-06-25 01:10:51 +00:00
|
|
|
$mock = new HTMLPurifier_InjectorMock();
|
2008-06-27 20:09:14 +00:00
|
|
|
$b = new HTMLPurifier_Token_End('b');
|
2008-10-01 07:14:28 +00:00
|
|
|
$b->skip = array(0 => true);
|
2008-10-01 19:40:31 +00:00
|
|
|
$b->start = new HTMLPurifier_Token_Start('b');
|
|
|
|
$b->start->skip = array(0 => true, 1 => true);
|
Implement Injector->handleEnd, with lots of refactoring for injector.
Previous design of injector streaming involved editability only to start, empty
and text tokens, because they could be safely modified without causing formedness
errors. By modifying notifyEnd to operate before MakeWellFormed's safeguards
kick into effect, it can be converted into a handle function, allowing for
arbitrary modification of end tags.
This change involved quite a bit of restructuring of the MakeWellFormed code,
including the moving of end of document tags to inside the loop, so rewinding
on those tags would be functional, increased reuse of the end tag codepath by
code that inserts end tags (as they could be changed out from under you), and
processToken modified to have an extra parameter to force re-processing of
a token if the original token was an end token.
We're not exactly sure if handleEnd works at this point, but the important
talking point about this refactoring is that nothing else broke. Also, a number
of convenience functions were moved from AutoParagraph to the Injector
supertype (specifically: forward, forwardToEndToken, backward, and current).
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
2008-10-01 04:54:51 +00:00
|
|
|
$mock->expectAt(0, 'handleEnd', array($b));
|
2008-06-27 20:09:14 +00:00
|
|
|
$i = new HTMLPurifier_Token_End('i');
|
2008-10-01 19:40:31 +00:00
|
|
|
$i->start = new HTMLPurifier_Token_Start('i');
|
2008-10-01 07:14:28 +00:00
|
|
|
$i->skip = array(0 => true);
|
2008-10-01 19:40:31 +00:00
|
|
|
$i->start->skip = array(0 => true, 1 => true);
|
Implement Injector->handleEnd, with lots of refactoring for injector.
Previous design of injector streaming involved editability only to start, empty
and text tokens, because they could be safely modified without causing formedness
errors. By modifying notifyEnd to operate before MakeWellFormed's safeguards
kick into effect, it can be converted into a handle function, allowing for
arbitrary modification of end tags.
This change involved quite a bit of restructuring of the MakeWellFormed code,
including the moving of end of document tags to inside the loop, so rewinding
on those tags would be functional, increased reuse of the end tag codepath by
code that inserts end tags (as they could be changed out from under you), and
processToken modified to have an extra parameter to force re-processing of
a token if the original token was an end token.
We're not exactly sure if handleEnd works at this point, but the important
talking point about this refactoring is that nothing else broke. Also, a number
of convenience functions were moved from AutoParagraph to the Injector
supertype (specifically: forward, forwardToEndToken, backward, and current).
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
2008-10-01 04:54:51 +00:00
|
|
|
$mock->expectAt(1, 'handleEnd', array($i));
|
|
|
|
$mock->expectCallCount('handleEnd', 2);
|
|
|
|
$mock->setReturnValue('getRewind', false);
|
2008-06-25 01:10:51 +00:00
|
|
|
$this->config->set('AutoFormat', 'AutoParagraph', false);
|
|
|
|
$this->config->set('AutoFormat', 'Linkify', false);
|
|
|
|
$this->config->set('AutoFormat', 'Custom', array($mock));
|
|
|
|
$this->assertResult('<i><b>asdf</b>', '<i><b>asdf</b></i>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testErrorRequiredElementNotAllowed() {
|
|
|
|
$this->config->set('HTML', 'Allowed', '');
|
|
|
|
$this->expectError('Cannot enable AutoParagraph injector because p is not allowed');
|
|
|
|
$this->expectError('Cannot enable Linkify injector because a is not allowed');
|
|
|
|
$this->assertResult('Foobar');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testErrorRequiredAttributeNotAllowed() {
|
|
|
|
$this->config->set('HTML', 'Allowed', 'a,p');
|
|
|
|
$this->expectError('Cannot enable Linkify injector because a.href is not allowed');
|
|
|
|
$this->assertResult('<p>http://example.com</p>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testOnlyAutoParagraph() {
|
|
|
|
$this->assertResult(
|
|
|
|
'Foobar',
|
|
|
|
'<p>Foobar</p>'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testParagraphWrappingOnlyLink() {
|
|
|
|
$this->assertResult(
|
|
|
|
'http://example.com',
|
|
|
|
'<p><a href="http://example.com">http://example.com</a></p>'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testParagraphWrappingNodeContainingLink() {
|
|
|
|
$this->assertResult(
|
|
|
|
'<b>http://example.com</b>',
|
|
|
|
'<p><b><a href="http://example.com">http://example.com</a></b></p>'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testParagraphWrappingPoorlyFormedNodeContainingLink() {
|
|
|
|
$this->assertResult(
|
|
|
|
'<b>http://example.com',
|
|
|
|
'<p><b><a href="http://example.com">http://example.com</a></b></p>'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testTwoParagraphsContainingOnlyOneLink() {
|
|
|
|
$this->assertResult(
|
|
|
|
"http://example.com\n\nhttp://dev.example.com",
|
2008-08-10 04:32:29 +00:00
|
|
|
'<p><a href="http://example.com">http://example.com</a></p>
|
|
|
|
|
|
|
|
<p><a href="http://dev.example.com">http://dev.example.com</a></p>'
|
2008-06-25 01:10:51 +00:00
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testParagraphNextToDivWithLinks() {
|
|
|
|
$this->assertResult(
|
|
|
|
'http://example.com <div>http://example.com</div>',
|
2008-08-10 04:32:29 +00:00
|
|
|
'<p><a href="http://example.com">http://example.com</a> </p>
|
|
|
|
|
|
|
|
<div><a href="http://example.com">http://example.com</a></div>'
|
2008-06-25 01:10:51 +00:00
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testRealisticLinkInSentence() {
|
|
|
|
$this->assertResult(
|
|
|
|
'This URL http://example.com is what you need',
|
|
|
|
'<p>This URL <a href="http://example.com">http://example.com</a> is what you need</p>'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
function testParagraphAfterLinkifiedURL() {
|
|
|
|
$this->assertResult(
|
2008-08-10 04:32:29 +00:00
|
|
|
"http://google.com
|
|
|
|
|
|
|
|
<b>b</b>",
|
|
|
|
"<p><a href=\"http://google.com\">http://google.com</a></p>
|
|
|
|
|
|
|
|
<p><b>b</b></p>"
|
2008-06-25 01:10:51 +00:00
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-27 20:09:14 +00:00
|
|
|
function testEmptyAndParagraph() {
|
|
|
|
// This is a fairly degenerate case, but it demonstrates that
|
|
|
|
// the two don't error out together, at least.
|
2008-08-10 04:32:29 +00:00
|
|
|
// Change this behavior!
|
2008-06-27 20:09:14 +00:00
|
|
|
$this->assertResult(
|
2008-08-10 04:32:29 +00:00
|
|
|
"<p>asdf
|
|
|
|
|
|
|
|
asdf<b></b></p>
|
|
|
|
|
|
|
|
<p></p><i></i>",
|
|
|
|
"<p>asdf</p>
|
|
|
|
|
|
|
|
<p>asdf</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"
|
2008-06-27 20:09:14 +00:00
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-27 20:09:14 +00:00
|
|
|
function testRewindAndParagraph() {
|
|
|
|
$this->assertResult(
|
2008-08-10 04:32:29 +00:00
|
|
|
"bar
|
|
|
|
|
|
|
|
<p><i></i>
|
|
|
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
foo",
|
|
|
|
"<p>bar</p>
|
|
|
|
|
2008-10-01 07:14:28 +00:00
|
|
|
|
2008-08-10 04:32:29 +00:00
|
|
|
|
|
|
|
<p>foo</p>"
|
2008-06-27 20:09:14 +00:00
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 01:10:51 +00:00
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|