*
* @file
*/
+use MediaWiki\MediaWikiServices;
/**
* This class is a collection of static functions that serve two purposes:
// Silly XML.
return substr( $start, 0, -1 ) . '/>';
} else {
- return "$start$contents" . self::closeElement( $element );
+ return $start . $contents . self::closeElement( $element );
}
}
// consistency and better compression.
$element = strtolower( $element );
+ // Some people were abusing this by passing things like
+ // 'h1 id="foo" to $element, which we don't want.
+ if ( strpos( $element, ' ' ) !== false ) {
+ wfWarn( __METHOD__ . " given element name with space '$element'" );
+ }
+
// Remove invalid input types
if ( $element == 'input' ) {
$validTypes = [
}
/**
- * Output a "<script>" tag with the given contents.
+ * Output an HTML script tag with the given contents.
*
- * @todo do some useful escaping as well, like if $contents contains
- * literal "</script>" or (for XML) literal "]]>".
+ * It is unsupported for the contents to contain the sequence `<script` or `</script`
+ * (case-insensitive). This ensures the script can be terminated easily and consistently.
+ * It is the responsibility of the caller to avoid such character sequence by escaping
+ * or avoiding it. If found at run-time, the contents are replaced with a comment, and
+ * a warning is logged server-side.
*
* @param string $contents JavaScript
- * @param string $nonce Nonce for CSP header, from OutputPage::getCSPNonce()
+ * @param string|null $nonce Nonce for CSP header, from OutputPage::getCSPNonce()
* @return string Raw HTML
*/
public static function inlineScript( $contents, $nonce = null ) {
if ( $nonce !== null ) {
$attrs['nonce'] = $nonce;
} else {
- if ( ContentSecurityPolicy::isEnabled( RequestContext::getMain()->getConfig() ) ) {
+ if ( ContentSecurityPolicy::isNonceRequired( RequestContext::getMain()->getConfig() ) ) {
wfWarn( "no nonce set on script. CSP will break it" );
}
}
- if ( preg_match( '/[<&]/', $contents ) ) {
- $contents = "/*<![CDATA[*/$contents/*]]>*/";
+ if ( preg_match( '/<\/?script/i', $contents ) ) {
+ wfLogWarning( __METHOD__ . ': Illegal character sequence found in inline script.' );
+ $contents = '/* ERROR: Invalid script */';
}
return self::rawElement( 'script', $attrs, $contents );
* "<script src=foo.js></script>".
*
* @param string $url
- * @param string $nonce Nonce for CSP header, from OutputPage::getCSPNonce()
+ * @param string|null $nonce Nonce for CSP header, from OutputPage::getCSPNonce()
* @return string Raw HTML
*/
public static function linkedScript( $url, $nonce = null ) {
if ( $nonce !== null ) {
$attrs['nonce'] = $nonce;
} else {
- if ( ContentSecurityPolicy::isEnabled( RequestContext::getMain()->getConfig() ) ) {
+ if ( ContentSecurityPolicy::isNonceRequired( RequestContext::getMain()->getConfig() ) ) {
wfWarn( "no nonce set on script. CSP will break it" );
}
}
* @return string of HTML representing a box.
*/
private static function messageBox( $html, $className, $heading = '' ) {
- if ( $heading ) {
+ if ( $heading !== '' ) {
$html = self::element( 'h2', [], $heading ) . $html;
}
return self::rawElement( 'div', [ 'class' => $className ], $html );
* @return array
*/
public static function namespaceSelectorOptions( array $params = [] ) {
- global $wgContLang;
-
$options = [];
if ( !isset( $params['exclude'] ) || !is_array( $params['exclude'] ) ) {
// Value is provided by user, the name shown is localized for the user.
$options[$params['all']] = wfMessage( 'namespacesall' )->text();
}
- // Add all namespaces as options (in the content language)
- $options += $wgContLang->getFormattedNamespaces();
+ if ( $params['in-user-lang'] ?? false ) {
+ global $wgLang;
+ $lang = $wgLang;
+ } else {
+ $lang = MediaWikiServices::getInstance()->getContentLanguage();
+ }
+ // Add all namespaces as options
+ $options += $lang->getFormattedNamespaces();
$optionsOut = [];
// Filter out namespaces below 0 and massage labels
// main we don't use "" but the user message describing it (e.g. "(Main)" or "(Article)")
$nsName = wfMessage( 'blanknamespace' )->text();
} elseif ( is_int( $nsId ) ) {
- $nsName = $wgContLang->convertNamespace( $nsId );
+ $nsName = $lang->convertNamespace( $nsId );
}
$optionsOut[$nsId] = $nsName;
}
if ( $isXHTML ) { // XHTML5
// XML MIME-typed markup should have an xml header.
// However a DOCTYPE is not needed.
- $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n";
+ $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
// Add the standard xmlns
$attribs['xmlns'] = 'http://www.w3.org/1999/xhtml';
$attribs["xmlns:$tag"] = $ns;
}
} else { // HTML5
- // DOCTYPE
$ret .= "<!DOCTYPE html>\n";
}