Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4111779
HtmlHelperTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
HtmlHelperTest.php
View Options
<?php
namespace
MediaWiki\Tests\Unit
;
use
MediaWiki\Html\HtmlHelper
;
use
MediaWikiUnitTestCase
;
use
Wikimedia\RemexHtml\HTMLData
;
use
Wikimedia\RemexHtml\Serializer\SerializerNode
;
use
Wikimedia\RemexHtml\Tokenizer\PlainAttributes
;
/**
* @covers \MediaWiki\Html\HtmlHelper
*/
class
HtmlHelperTest
extends
MediaWikiUnitTestCase
{
public
function
testModifyElements
()
{
$shouldModifyCallback
=
static
function
(
SerializerNode
$node
)
{
return
$node
->
namespace
===
HTMLData
::
NS_HTML
&&
$node
->
name
===
'a'
&&
isset
(
$node
->
attrs
[
'href'
]
);
};
$modifyCallbackInPlace
=
static
function
(
SerializerNode
$node
)
{
$node
->
attrs
[
'href'
]
=
'https://tracker.org/'
.
$node
->
attrs
[
'href'
];
return
$node
;
};
$input
=
'<p>Foo <a href="https://example.org/">bar</a> baz</p>'
;
$expectedOutput
=
'<p>Foo <a href="https://tracker.org/https://example.org/">bar</a> baz</p>'
;
$output
=
HtmlHelper
::
modifyElements
(
$input
,
$shouldModifyCallback
,
$modifyCallbackInPlace
);
$this
->
assertSame
(
$expectedOutput
,
$output
);
$modifyCallbackNew
=
static
function
(
SerializerNode
$node
)
{
$href
=
'https://tracker.org/'
.
$node
->
attrs
[
'href'
];
$newNode
=
new
SerializerNode
(
$node
->
id
,
$node
->
parentId
,
$node
->
namespace
,
$node
->
name
,
new
PlainAttributes
(
[
'href'
=>
$href
]
),
$node
->
void
);
$node
->
attrs
[
'href'
]
=
'https://tracker.org/'
.
$node
->
attrs
[
'href'
];
return
$newNode
;
};
$output
=
HtmlHelper
::
modifyElements
(
$input
,
$shouldModifyCallback
,
$modifyCallbackNew
);
$this
->
assertSame
(
$expectedOutput
,
$output
);
// Check the "legacy compatibility" mode, for void elements like <link>
$input
=
"<link data-test='<style data-mw-deduplicate=
\"
\"
>bar</style>'>"
;
$shouldModifyCallback
=
static
function
(
SerializerNode
$node
)
{
return
false
;
};
// HTML5 output
$expectedOutput
=
'<link data-test="<style data-mw-deduplicate=" ">bar</style>">'
;
$output
=
HtmlHelper
::
modifyElements
(
$input
,
$shouldModifyCallback
,
$modifyCallbackInPlace
,
true
);
$this
->
assertSame
(
$expectedOutput
,
$output
);
// "Legacy" output
$expectedOutput
=
'<link data-test="<style data-mw-deduplicate=" ">bar</style>" />'
;
$output
=
HtmlHelper
::
modifyElements
(
$input
,
$shouldModifyCallback
,
$modifyCallbackInPlace
,
false
);
$this
->
assertSame
(
$expectedOutput
,
$output
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Tue, Aug 18, 22:32 (1 d, 2 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c5/73/0acbee2eaaf21518e1c6d5db498b
Default Alt Text
HtmlHelperTest.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment