Merge pull request #8584 from wernerkrauss/cleanup-convert

Cleanup Convert
This commit is contained in:
Robbie Averill 2018-11-07 21:51:13 +02:00 committed by GitHub
commit e7041cdfeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 52 deletions

View File

@ -70,9 +70,9 @@ class Convert
}
return $val;
} else {
return self::raw2att($val);
}
return self::raw2att($val);
}
/**
@ -93,16 +93,16 @@ class Convert
}
return $val;
} else {
return trim(
preg_replace(
'/_+/',
'_',
preg_replace('/[^a-zA-Z0-9\-_:.]+/', '_', $val)
),
'_'
);
}
return trim(
preg_replace(
'/_+/',
'_',
preg_replace('/[^a-zA-Z0-9\-_:.]+/', '_', $val)
),
'_'
);
}
/**
@ -119,9 +119,9 @@ class Convert
$val[$k] = self::raw2xml($v);
}
return $val;
} else {
return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
}
return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
}
/**
@ -137,14 +137,14 @@ class Convert
$val[$k] = self::raw2js($v);
}
return $val;
} else {
return str_replace(
// Intercepts some characters such as <, >, and & which can interfere
array("\\", '"', "\n", "\r", "'", "<", ">", "&"),
array("\\\\", '\"', '\n', '\r', "\\'", "\\x3c", "\\x3e", "\\x26"),
$val
);
}
return str_replace(
// Intercepts some characters such as <, >, and & which can interfere
array("\\", '"', "\n", "\r", "'", '<', '>', '&'),
array("\\\\", '\"', '\n', '\r', "\\'", "\\x3c", "\\x3e", "\\x26"),
$val
);
}
/**
@ -195,13 +195,13 @@ class Convert
$val[$k] = self::raw2sql($v, $quoted);
}
return $val;
} else {
if ($quoted) {
return DB::get_conn()->quoteString($val);
} else {
return DB::get_conn()->escapeString($val);
}
}
if ($quoted) {
return DB::get_conn()->quoteString($val);
}
return DB::get_conn()->escapeString($val);
}
/**
@ -233,14 +233,14 @@ class Convert
$val[$k] = self::xml2raw($v);
}
return $val;
} else {
// More complex text needs to use html2raw instead
if (strpos($val, '<') !== false) {
return self::html2raw($val);
} else {
return html_entity_decode($val, ENT_QUOTES, 'UTF-8');
}
}
// More complex text needs to use html2raw instead
if (strpos($val, '<') !== false) {
return self::html2raw($val);
}
return html_entity_decode($val, ENT_QUOTES, 'UTF-8');
}
/**
@ -332,7 +332,7 @@ class Convert
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) {
if (count($xml) === 0) {
return (string)$x;
} // for CDATA
$r = [];
@ -359,9 +359,9 @@ class Convert
{
if (preg_match('/^[a-z+]+\:\/\/[a-zA-Z0-9$-_.+?&=!*\'()%]+$/', $string)) {
return "<a style=\"white-space: nowrap\" href=\"$string\">$string</a>";
} else {
return $string;
}
return $string;
}
/**
@ -387,8 +387,8 @@ class Convert
$config = $defaultConfig;
}
$data = preg_replace("/<style([^A-Za-z0-9>][^>]*)?>.*?<\/style[^>]*>/is", "", $data);
$data = preg_replace("/<script([^A-Za-z0-9>][^>]*)?>.*?<\/script[^>]*>/is", "", $data);
$data = preg_replace("/<style([^A-Za-z0-9>][^>]*)?>.*?<\/style[^>]*>/is", '', $data);
$data = preg_replace("/<script([^A-Za-z0-9>][^>]*)?>.*?<\/script[^>]*>/is", '', $data);
if ($config['ReplaceBoldAsterisk']) {
$data = preg_replace('%<(strong|b)( [^>]*)?>|</(strong|b)>%i', '*', $data);
@ -412,7 +412,7 @@ class Convert
// Compress whitespace
if ($config['CompressWhitespace']) {
$data = preg_replace("/\s+/u", " ", $data);
$data = preg_replace("/\s+/u", ' ', $data);
}
// Parse newline tags
@ -421,9 +421,9 @@ class Convert
$data = preg_replace("/\s*<[Dd][Ii][Vv]([^A-Za-z0-9>][^>]*)?> */u", "\n\n", $data);
$data = preg_replace("/\n\n\n+/", "\n\n", $data);
$data = preg_replace("/<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data);
$data = preg_replace("/<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data);
$data = preg_replace("/<\/[Tt][Dd]([^A-Za-z0-9>][^>]*)?> */", " ", $data);
$data = preg_replace('/<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> */', "\n", $data);
$data = preg_replace('/<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> */', "\n", $data);
$data = preg_replace("/<\/[Tt][Dd]([^A-Za-z0-9>][^>]*)?> */", ' ', $data);
$data = preg_replace('/<\/p>/i', "\n\n", $data);
// Replace HTML entities

View File

@ -133,7 +133,7 @@ class ConvertTest extends SapphireTest
$this->assertEquals(
"That's absolutely correct",
Convert::html2raw($val7),
"Single quotes are decoded correctly"
'Single quotes are decoded correctly'
);
$val8 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ' . 'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ' . 'exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute ' . 'irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ' . 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia ' . 'deserunt mollit anim id est laborum.';
@ -280,7 +280,7 @@ PHP
protected function assertEqualsQuoted($expected, $actual)
{
$message = sprintf(
"Expected \"%s\" but given \"%s\"",
'Expected "%s" but given "%s"',
addcslashes($expected, "\r\n"),
addcslashes($actual, "\r\n")
);
@ -296,8 +296,8 @@ PHP
foreach (array("\r\n", "\r", "\n") as $nl) {
// Base case: no action
$this->assertEqualsQuoted(
"Base case",
Convert::nl2os("Base case", $nl)
'Base case',
Convert::nl2os('Base case', $nl)
);
// Mixed formats
@ -426,7 +426,7 @@ XML
// Test without doctype validation
$expected = array(
'result' => array(
"Now include SOME_SUPER_LONG_STRING lots of times to expand the in-memory size of this XML structure",
'Now include SOME_SUPER_LONG_STRING lots of times to expand the in-memory size of this XML structure',
array(
'long' => array(
array(
@ -607,11 +607,11 @@ XML
{
return [
[200, '200B'],
[(2 * 1024), '2K'],
[(512 * 1024 * 1024), '512M'],
[(512 * 1024 * 1024 * 1024), '512G'],
[(512 * 1024 * 1024 * 1024 * 1024), '512T'],
[(512 * 1024 * 1024 * 1024 * 1024 * 1024), '512P']
[2 * 1024, '2K'],
[512 * 1024 * 1024, '512M'],
[512 * 1024 * 1024 * 1024, '512G'],
[512 * 1024 * 1024 * 1024 * 1024, '512T'],
[512 * 1024 * 1024 * 1024 * 1024 * 1024, '512P']
];
}