Skip to content

Commit

Permalink
Add Highlighter::decode, refs 1768, 2347 (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Mar 25, 2017
1 parent dbbf3d1 commit 8a9b94d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 9 deletions.
16 changes: 16 additions & 0 deletions includes/Highlighter.php
Expand Up @@ -77,6 +77,22 @@ public static function factory( $type, $language = null ) {
return new Highlighter( $type, $language );
}

/**
* @since 3.0
*
* @param string $text
*
* @return string
*/
public static function decode( $text ) {
// #2347, '[' is handled by the MediaWiki parser/sanitizer itself
return str_replace(
array( '&amp;', '&lt;', '&gt;', '&#160;', '<nowiki>', '</nowiki>' ),
array( '&', '<', '>', ' ', '', '' ),
$text
);
}

/**
* Returns html output
*
Expand Down
11 changes: 3 additions & 8 deletions src/GlobalFunctions.php
Expand Up @@ -5,6 +5,7 @@
use SMW\IntlNumberFormatter;
use SMW\SPARQLStore\SparqlDBConnectionProvider;
use SMW\ProcessingErrorMsgHandler;
use SMW\Highlighter;

/**
* Global functions specified and used by Semantic MediaWiki. In general, it is
Expand Down Expand Up @@ -124,17 +125,11 @@ function smwfEncodeMessages( array $messages, $type = 'warning', $seperator = '
$errorList = '<ul>' . implode( $seperator, $messages ) . '</ul>';
}

$errorList = str_replace(
array( '&amp;', '&lt;', '&gt;', '&#160;', '<nowiki>', '</nowiki>', '[', ),
array( '&', '<', '>', ' ', '', '', '&#x005B;' ),
$errorList
);

// Type will be converted internally
$highlighter = SMW\Highlighter::factory( $type );
$highlighter = Highlighter::factory( $type );
$highlighter->setContent( array (
'caption' => null,
'content' => $errorList
'content' => Highlighter::decode( $errorList )
) );

return $highlighter->getHtml();
Expand Down
113 changes: 113 additions & 0 deletions tests/phpunit/Integration/JSONScript/TestCases/p-0108.json
@@ -0,0 +1,113 @@
{
"description": "Test `#info`, `#ask` template output (#2347, `wgContLang=en`, `wgLang=en`)",
"setup": [
{
"namespace": "SMW_NS_PROPERTY",
"page": "Has page",
"contents": "[[Has type::Page]]"
},
{
"namespace": "SMW_NS_PROPERTY",
"page": "Has text",
"contents": "[[Has type::Text]]"
},
{
"namespace": "NS_TEMPLATE",
"page": "P0108.Ask.Output",
"contents": "<includeonly>{{{1}}} {{{2}}}</includeonly>"
},
{
"namespace": "NS_TEMPLATE",
"page": "P0108.Ask",
"contents": "<includeonly>{{#ask: [[Has page::P0108]] |?Has page |limit=1 |format=template |link=none |template=P0108.Ask.Output }}</includeonly>"
},
{
"namespace": "NS_TEMPLATE",
"page": "P0108",
"contents": "{{#info: message={{P0108.Ask}} }}"
},
{
"page": "Test/P0108/1",
"contents": "[[Has page::P0108]]"
},
{
"page": "Test/P0108/2",
"contents": "{{P0108}}"
},
{
"page": "Test/P0108/3",
"contents": "{{#set: Has text=Text with an [[Has page::annotation]] }}"
},
{
"page": "Test/P0108/4",
"contents": "{{#info: message={{#show: Test/P0108/3 |?Has text }} }}"
},
{
"page": "Test/P0108/5",
"contents": "{{#info: {{#show: Test/P0108/3 |?Has text }} }}"
}
],
"tests": [
{
"type": "parser",
"about": "#0 #info with template generate output including encoded (&#91;) on/off marker",
"subject": "Test/P0108/2",
"assert-output": {
"to-contain": [
"title=\"&amp;#91;&amp;#91;SMW::off]]Test/P0108/1 P0108&amp;#91;&amp;#91;SMW::on]]\">"
]
}
},
{
"type": "parser",
"about": "#1 $info + #show (no annotation is leaked via a text element)",
"subject": "Test/P0108/4",
"assert-store": {
"semantic-data": {
"strictPropertyValueMatch": false,
"propertyCount": 3,
"propertyKeys": [
"_ASK",
"_MDAT",
"_SKEY"
]
}
},
"assert-output": {
"to-contain": [
"title=\"Text with an annotation\">"
]
}
},
{
"type": "parser",
"about": "#2 $info + #show (no annotation is leaked via a text element)",
"subject": "Test/P0108/5",
"assert-store": {
"semantic-data": {
"strictPropertyValueMatch": false,
"propertyCount": 3,
"propertyKeys": [
"_ASK",
"_MDAT",
"_SKEY"
]
}
},
"assert-output": {
"to-contain": [
"title=\"Text with an annotation\">"
]
}
}
],
"settings": {
"wgContLang": "en",
"wgLang": "en"
},
"meta": {
"version": "2",
"is-incomplete": false,
"debug": false
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/Unit/DataValues/InfoLinksProviderTest.php
Expand Up @@ -215,7 +215,7 @@ public function testGetInfolinkTextOnStringValueWithServiceLinks() {
->will( $this->returnValue( $instance ) );

$this->assertContains(
'<div class="smwttcontent">&#x005B;SERVICELINK-B SERVICELINK-A]</div>',
'<div class="smwttcontent">[SERVICELINK-B SERVICELINK-A]</div>',
$instance->getInfolinkText( SMW_OUTPUT_WIKI )
);

Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/includes/HighlighterTest.php
Expand Up @@ -43,6 +43,14 @@ public function testGetTypeId( $type, $expected ) {
);
}

public function testDecode() {

$this->assertEquals(
'&<> ',
Highlighter::decode( '&amp;&lt;&gt;&#160;<nowiki></nowiki>' )
);
}

/**
* @dataProvider getTypeDataProvider
*/
Expand Down

0 comments on commit 8a9b94d

Please sign in to comment.