first commit

This commit is contained in:
aschwarz
2023-02-27 10:20:09 +01:00
commit 09ee4a8728
2309 changed files with 449255 additions and 0 deletions

View File

@ -0,0 +1,577 @@
<?php
class HTMLPurifier_Injector_AutoParagraphTest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
$this->config->set('AutoFormat.AutoParagraph', true);
}
public function testSingleParagraph()
{
$this->assertResult(
'Foobar',
'<p>Foobar</p>'
);
}
public function testSingleMultiLineParagraph()
{
$this->assertResult(
'Par 1
Par 1 still',
'<p>Par 1
Par 1 still</p>'
);
}
public function testTwoParagraphs()
{
$this->assertResult(
'Par1
Par2',
"<p>Par1</p>
<p>Par2</p>"
);
}
public function testTwoParagraphsWithLotsOfSpace()
{
$this->assertResult(
'Par1
Par2',
'<p>Par1</p>
<p>Par2</p>'
);
}
public function testTwoParagraphsWithInlineElements()
{
$this->assertResult(
'<b>Par1</b>
<i>Par2</i>',
'<p><b>Par1</b></p>
<p><i>Par2</i></p>'
);
}
public function testSingleParagraphThatLooksLikeTwo()
{
$this->assertResult(
'<b>Par1
Par2</b>',
'<p><b>Par1
Par2</b></p>'
);
}
public function testAddParagraphAdjacentToParagraph()
{
$this->assertResult(
'Par1<p>Par2</p>',
'<p>Par1</p>
<p>Par2</p>'
);
}
public function testParagraphUnclosedInlineElement()
{
$this->assertResult(
'<b>Par1',
'<p><b>Par1</b></p>'
);
}
public function testPreservePreTags()
{
$this->assertResult(
'<pre>Par1
Par1</pre>'
);
}
public function testIgnoreTrailingWhitespace()
{
$this->assertResult(
'Par1
',
'<p>Par1</p>
'
);
}
public function testDoNotParagraphBlockElements()
{
$this->assertResult(
'Par1
<div>Par2</div>
Par3',
'<p>Par1</p>
<div>Par2</div>
<p>Par3</p>'
);
}
public function testParagraphTextAndInlineNodes()
{
$this->assertResult(
'Par<b>1</b>',
'<p>Par<b>1</b></p>'
);
}
public function testPreserveLeadingWhitespace()
{
$this->assertResult(
'
Par',
'
<p>Par</p>'
);
}
public function testPreserveSurroundingWhitespace()
{
$this->assertResult(
'
Par
',
'
<p>Par</p>
'
);
}
public function testParagraphInsideBlockNode()
{
$this->assertResult(
'<div>Par1
Par2</div>',
'<div><p>Par1</p>
<p>Par2</p></div>'
);
}
public function testParagraphInlineNodeInsideBlockNode()
{
$this->assertResult(
'<div><b>Par1</b>
Par2</div>',
'<div><p><b>Par1</b></p>
<p>Par2</p></div>'
);
}
public function testNoParagraphWhenOnlyOneInsideBlockNode()
{
$this->assertResult('<div>Par1</div>');
}
public function testParagraphTwoInlineNodesInsideBlockNode()
{
$this->assertResult(
'<div><b>Par1</b>
<i>Par2</i></div>',
'<div><p><b>Par1</b></p>
<p><i>Par2</i></p></div>'
);
}
public function testPreserveInlineNodesInPreTag()
{
$this->assertResult(
'<pre><b>Par1</b>
<i>Par2</i></pre>'
);
}
public function testSplitUpInternalsOfPTagInBlockNode()
{
$this->assertResult(
'<div><p>Foo
Bar</p></div>',
'<div><p>Foo</p>
<p>Bar</p></div>'
);
}
public function testSplitUpInlineNodesInPTagInBlockNode()
{
$this->assertResult(
'<div><p><b>Foo</b>
<i>Bar</i></p></div>',
'<div><p><b>Foo</b></p>
<p><i>Bar</i></p></div>'
);
}
public function testNoParagraphSingleInlineNodeInBlockNode()
{
$this->assertResult( '<div><b>Foo</b></div>' );
}
public function testParagraphInBlockquote()
{
$this->assertResult(
'<blockquote>Par1
Par2</blockquote>',
'<blockquote><p>Par1</p>
<p>Par2</p></blockquote>'
);
}
public function testNoParagraphBetweenListItem()
{
$this->assertResult(
'<ul><li>Foo</li>
<li>Bar</li></ul>'
);
}
public function testParagraphSingleElementWithSurroundingSpace()
{
$this->assertResult(
'<div>
Bar
</div>',
'<div>
<p>Bar</p>
</div>'
);
}
public function testIgnoreExtraSpaceWithLeadingInlineNode()
{
$this->assertResult(
'<b>Par1</b>a
Par2',
'<p><b>Par1</b>a</p>
<p>Par2</p>'
);
}
public function testAbsorbExtraEndingPTag()
{
$this->assertResult(
'Par1
Par2</p>',
'<p>Par1</p>
<p>Par2</p>'
);
}
public function testAbsorbExtraEndingDivTag()
{
$this->assertResult(
'Par1
Par2</div>',
'<p>Par1</p>
<p>Par2</p>'
);
}
public function testDoNotParagraphSingleSurroundingSpaceInBlockNode()
{
$this->assertResult(
'<div>
Par1
</div>'
);
}
public function testBlockNodeTextDelimeterInBlockNode()
{
$this->assertResult(
'<div>Par1
<div>Par2</div></div>',
'<div><p>Par1</p>
<div>Par2</div></div>'
);
}
public function testBlockNodeTextDelimeterWithoutDoublespaceInBlockNode()
{
$this->assertResult(
'<div>Par1
<div>Par2</div></div>'
);
}
public function testBlockNodeTextDelimeterWithoutDoublespace()
{
$this->assertResult(
'Par1
<div>Par2</div>',
'<p>Par1
</p>
<div>Par2</div>'
);
}
public function testTwoParagraphsOfTextAndInlineNode()
{
$this->assertResult(
'Par1
<b>Par2</b>',
'<p>Par1</p>
<p><b>Par2</b></p>'
);
}
public function testLeadingInlineNodeParagraph()
{
$this->assertResult(
'<img /> Foo',
'<p><img /> Foo</p>'
);
}
public function testTrailingInlineNodeParagraph()
{
$this->assertResult(
'<li>Foo <a>bar</a></li>'
);
}
public function testTwoInlineNodeParagraph()
{
$this->assertResult(
'<li><b>baz</b><a>bar</a></li>'
);
}
public function testNoParagraphTrailingBlockNodeInBlockNode()
{
$this->assertResult(
'<div><div>asdf</div><b>asdf</b></div>'
);
}
public function testParagraphTrailingBlockNodeWithDoublespaceInBlockNode()
{
$this->assertResult(
'<div><div>asdf</div>
<b>asdf</b></div>',
'<div><div>asdf</div>
<p><b>asdf</b></p></div>'
);
}
public function testParagraphTwoInlineNodesAndWhitespaceNode()
{
$this->assertResult(
'<b>One</b> <i>Two</i>',
'<p><b>One</b> <i>Two</i></p>'
);
}
public function testNoParagraphWithInlineRootNode()
{
$this->config->set('HTML.Parent', 'span');
$this->assertResult(
'Par
Par2'
);
}
public function testInlineAndBlockTagInDivNoParagraph()
{
$this->assertResult(
'<div><code>bar</code> mmm <pre>asdf</pre></div>'
);
}
public function testInlineAndBlockTagInDivNeedingParagraph()
{
$this->assertResult(
'<div><code>bar</code> mmm
<pre>asdf</pre></div>',
'<div><p><code>bar</code> mmm</p>
<pre>asdf</pre></div>'
);
}
public function testTextInlineNodeTextThenDoubleNewlineNeedsParagraph()
{
$this->assertResult(
'<div>asdf <code>bar</code> mmm
<pre>asdf</pre></div>',
'<div><p>asdf <code>bar</code> mmm</p>
<pre>asdf</pre></div>'
);
}
public function testUpcomingTokenHasNewline()
{
$this->assertResult(
'<div>Test<b>foo</b>bar<b>bing</b>bang
boo</div>',
'<div><p>Test<b>foo</b>bar<b>bing</b>bang</p>
<p>boo</p></div>'
);
}
public function testEmptyTokenAtEndOfDiv()
{
$this->assertResult(
'<div><p>foo</p>
</div>',
'<div><p>foo</p>
</div>'
);
}
public function testEmptyDoubleLineTokenAtEndOfDiv()
{
$this->assertResult(
'<div><p>foo</p>
</div>',
'<div><p>foo</p>
</div>'
);
}
public function testTextState11Root()
{
$this->assertResult('<div></div> ');
}
public function testTextState11Element()
{
$this->assertResult(
"<div><div></div>
</div>");
}
public function testTextStateLikeElementState111NoWhitespace()
{
$this->assertResult('<div><p>P</p>Boo</div>', '<div><p>P</p>Boo</div>');
}
public function testElementState111NoWhitespace()
{
$this->assertResult('<div><p>P</p><b>Boo</b></div>', '<div><p>P</p><b>Boo</b></div>');
}
public function testElementState133()
{
$this->assertResult(
"<div><b>B</b><pre>Ba</pre>
Bar</div>",
"<div><b>B</b><pre>Ba</pre>
<p>Bar</p></div>"
);
}
public function testElementState22()
{
$this->assertResult(
'<ul><li>foo</li></ul>'
);
}
public function testElementState311()
{
$this->assertResult(
'<p>Foo</p><b>Bar</b>',
'<p>Foo</p>
<p><b>Bar</b></p>'
);
}
public function testAutoClose()
{
$this->assertResult(
'<p></p>
<hr />'
);
}
public function testErrorNeeded()
{
$this->config->set('HTML.Allowed', 'b');
$this->expectError('Cannot enable AutoParagraph injector because p is not allowed');
$this->assertResult('<b>foobar</b>');
}
public function testParentElement()
{
$this->config->set('HTML.Allowed', 'p,ul,li');
$this->assertResult('Foo<ul><li>Bar</li></ul>', "<p>Foo</p>\n\n<ul><li>Bar</li></ul>");
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,37 @@
<?php
class HTMLPurifier_Injector_DisplayLinkURITest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
$this->config->set('AutoFormat.DisplayLinkURI', true);
}
public function testBasicLink()
{
$this->assertResult(
'<a href="http://malware.example.com">Don\'t go here!</a>',
'<a>Don\'t go here!</a> (http://malware.example.com)'
);
}
public function testEmptyLink()
{
$this->assertResult(
'<a>Don\'t go here!</a>',
'<a>Don\'t go here!</a>'
);
}
public function testEmptyText()
{
$this->assertResult(
'<a href="http://malware.example.com"></a>',
'<a></a> (http://malware.example.com)'
);
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,65 @@
<?php
class HTMLPurifier_Injector_LinkifyTest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
$this->config->set('AutoFormat.Linkify', true);
}
public function testLinkifyURLInRootNode()
{
$this->assertResult(
'http://example.com',
'<a href="http://example.com">http://example.com</a>'
);
}
public function testLinkifyURLInInlineNode()
{
$this->assertResult(
'<b>http://example.com</b>',
'<b><a href="http://example.com">http://example.com</a></b>'
);
}
public function testBasicUsageCase()
{
$this->assertResult(
'This URL http://example.com is what you need',
'This URL <a href="http://example.com">http://example.com</a> is what you need'
);
}
public function testIgnoreURLInATag()
{
$this->assertResult(
'<a>http://example.com/</a>'
);
}
public function testNeeded()
{
$this->config->set('HTML.Allowed', 'b');
$this->expectError('Cannot enable Linkify injector because a is not allowed');
$this->assertResult('http://example.com/');
}
public function testExcludes()
{
$this->assertResult('<a><span>http://example.com</span></a>');
}
public function testRegexIsSmart()
{
$this->assertResult('http://example.com/foo.', '<a href="http://example.com/foo">http://example.com/foo</a>.');
$this->assertResult('“http://example.com/foo”', '“<a href="http://example.com/foo">http://example.com/foo</a>”');
$this->assertResult('“http://example.com”', '“<a href="http://example.com">http://example.com</a>”');
$this->assertResult('(http://example.com/f(o)o)', '(<a href="http://example.com/f(o)o">http://example.com/f(o)o</a>)');
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,68 @@
<?php
class HTMLPurifier_Injector_PurifierLinkifyTest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
$this->config->set('AutoFormat.PurifierLinkify', true);
$this->config->set('AutoFormat.PurifierLinkify.DocURL', '#%s');
}
public function testNoTriggerCharacer()
{
$this->assertResult('Foobar');
}
public function testTriggerCharacterInIrrelevantContext()
{
$this->assertResult('20% off!');
}
public function testPreserveNamespace()
{
$this->assertResult('%Core namespace (not recognized)');
}
public function testLinkifyBasic()
{
$this->assertResult(
'%Namespace.Directive',
'<a href="#Namespace.Directive">%Namespace.Directive</a>'
);
}
public function testLinkifyWithAdjacentTextNodes()
{
$this->assertResult(
'This %Namespace.Directive thing',
'This <a href="#Namespace.Directive">%Namespace.Directive</a> thing'
);
}
public function testLinkifyInBlock()
{
$this->assertResult(
'<div>This %Namespace.Directive thing</div>',
'<div>This <a href="#Namespace.Directive">%Namespace.Directive</a> thing</div>'
);
}
public function testPreserveInATag()
{
$this->assertResult(
'<a>%Namespace.Directive</a>'
);
}
public function testNeeded()
{
$this->config->set('HTML.Allowed', 'b');
$this->expectError('Cannot enable PurifierLinkify injector because a is not allowed');
$this->assertResult('%Namespace.Directive');
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,120 @@
<?php
class HTMLPurifier_Injector_RemoveEmptyTest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
$this->config->set('AutoFormat.RemoveEmpty', true);
}
public function testPreserve()
{
$this->assertResult('<b>asdf</b>');
}
public function testRemove()
{
$this->assertResult('<b></b>', '');
}
public function testRemoveWithSpace()
{
$this->assertResult('<b> </b>', '');
}
public function testRemoveWithAttr()
{
$this->assertResult('<b class="asdf"></b>', '');
}
public function testRemoveIdAndName()
{
$this->assertResult('<a id="asdf" name="asdf"></a>', '');
}
public function testPreserveColgroup()
{
$this->assertResult('<colgroup></colgroup>');
}
public function testPreserveId()
{
$this->config->set('Attr.EnableID', true);
$this->assertResult('<a id="asdf"></a>');
}
public function testPreserveName()
{
$this->config->set('Attr.EnableID', true);
$this->assertResult('<a name="asdf"></a>');
}
public function testRemoveNested()
{
$this->assertResult('<b><i></i></b>', '');
}
public function testRemoveNested2()
{
$this->assertResult('<b><i><u></u></i></b>', '');
}
public function testRemoveNested3()
{
$this->assertResult('<b> <i> <u> </u> </i> </b>', '');
}
public function testRemoveNbsp()
{
$this->config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
$this->assertResult('<b>&nbsp;</b>', '');
}
public function testRemoveNbspMix()
{
$this->config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
$this->assertResult('<b>&nbsp; &nbsp;</b>', '');
}
public function testRemoveLi()
{
$this->assertResult("<ul><li>\n\n\n</li></ul>", '');
}
public function testDontRemoveNbsp()
{
$this->config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
$this->assertResult('<td>&nbsp;</b>', "<td>\xC2\xA0</td>");
}
public function testRemoveNbspExceptionsSpecial()
{
$this->config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
$this->config->set('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions', 'b');
$this->assertResult('<b>&nbsp;</b>', "<b>\xC2\xA0</b>");
}
public function testRemoveIframe()
{
$this->config->set('HTML.SafeIframe', true);
$this->assertResult('<iframe></iframe>', '');
}
public function testNoRemoveIframe()
{
$this->config->set('HTML.SafeIframe', true);
$this->assertResult('<iframe src="http://google.com"></iframe>', '');
}
public function testRemoveDisallowedIframe()
{
$this->config->set('HTML.SafeIframe', true);
$this->config->set('URI.SafeIframeRegexp', '%^http://www.youtube.com/embed/%');
$this->assertResult('<iframe src="http://google.com"></iframe>', '');
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,112 @@
<?php
class HTMLPurifier_Injector_RemoveSpansWithoutAttributesTest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
$this->config->set('HTML.Allowed', 'span[class],div,p,strong,em');
$this->config->set('AutoFormat.RemoveSpansWithoutAttributes', true);
}
public function testSingleSpan()
{
$this->assertResult(
'<span>foo</span>',
'foo'
);
}
public function testSingleSpanWithAttributes()
{
$this->assertResult(
'<span class="bar">foo</span>',
'<span class="bar">foo</span>'
);
}
public function testSingleNestedSpan()
{
$this->assertResult(
'<p><span>foo</span></p>',
'<p>foo</p>'
);
}
public function testSingleNestedSpanWithAttributes()
{
$this->assertResult(
'<p><span class="bar">foo</span></p>',
'<p><span class="bar">foo</span></p>'
);
}
public function testSpanWithChildren()
{
$this->assertResult(
'<span>foo <strong>bar</strong> <em>baz</em></span>',
'foo <strong>bar</strong> <em>baz</em>'
);
}
public function testSpanWithSiblings()
{
$this->assertResult(
'<p>before <span>inside</span> <strong>after</strong></p>',
'<p>before inside <strong>after</strong></p>'
);
}
public function testNestedSpanWithSiblingsAndChildren()
{
$this->assertResult(
'<p>a <span>b <em>c</em> d</span> e</p>',
'<p>a b <em>c</em> d e</p>'
);
}
public function testNestedSpansWithoutAttributes()
{
$this->assertResult(
'<span>one<span>two<span>three</span></span></span>',
'onetwothree'
);
}
public function testDeeplyNestedSpan()
{
$this->assertResult(
'<div><div><div><span class="a">a <span>b</span> c</span></div></div></div>',
'<div><div><div><span class="a">a b c</span></div></div></div>'
);
}
public function testSpanWithInvalidAttributes()
{
$this->assertResult(
'<p><span snorkel buzzer="emu">foo</span></p>',
'<p>foo</p>'
);
}
public function testNestedAlternateSpans()
{
$this->assertResult(
'<span>a <span class="x">b <span>c <span class="y">d <span>e <span class="z">f
</span></span></span></span></span></span>',
'a <span class="x">b c <span class="y">d e <span class="z">f
</span></span></span>'
);
}
public function testSpanWithSomeInvalidAttributes()
{
$this->assertResult(
'<p><span buzzer="emu" class="bar">foo</span></p>',
'<p><span class="bar">foo</span></p>'
);
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,106 @@
<?php
/**
* This test is kinda weird, because it doesn't test the full safe object
* functionality, just a small section of it. Or maybe it's actually the right
* way.
*/
class HTMLPurifier_Injector_SafeObjectTest extends HTMLPurifier_InjectorHarness
{
public function setup()
{
parent::setup();
// there is no AutoFormat.SafeObject directive
$this->config->set('AutoFormat.Custom', array(new HTMLPurifier_Injector_SafeObject()));
$this->config->set('HTML.Trusted', true);
}
public function testPreserve()
{
$this->assertResult(
'<b>asdf</b>'
);
}
public function testRemoveStrayParam()
{
$this->assertResult(
'<param />',
''
);
}
public function testEditObjectParam()
{
$this->assertResult(
'<object></object>',
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></object>'
);
}
public function testIgnoreStrayParam()
{
$this->assertResult(
'<object><param /></object>',
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></object>'
);
}
public function testIgnoreDuplicates()
{
$this->assertResult(
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></object>'
);
}
public function testIgnoreBogusData()
{
$this->assertResult(
'<object><param name="allowscriptaccess" value="always" /><param name="allowNetworking" value="always" /></object>',
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></object>'
);
}
public function testIgnoreInvalidData()
{
$this->assertResult(
'<object><param name="foo" value="bar" /></object>',
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></object>'
);
}
public function testKeepValidData()
{
$this->assertResult(
'<object><param name="movie" value="bar" /></object>',
'<object data="bar"><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /><param name="movie" value="bar" /></object>'
);
}
public function testNested()
{
$this->assertResult(
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /><object></object></object>',
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /><object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></object></object>'
);
}
public function testNotActuallyNested()
{
$this->assertResult(
'<object><p><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /></p></object>',
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /><p></p></object>'
);
}
public function testCaseInsensitive()
{
$this->assertResult(
'<object><param name="allowScriptAccess" value="never" /><param name="allowNetworking" value="internal" /><param name="flashVars" value="a" /><param name="FlashVars" value="b" /></object>'
);
}
}
// vim: et sw=4 sts=4