Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1432445
StylesheetSanitizer.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
StylesheetSanitizer.php
View Options
<?php
/**
* @file
* @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
*/
namespace
Wikimedia\CSS\Sanitizer
;
use
Wikimedia\CSS\Grammar\MatcherFactory
;
use
Wikimedia\CSS\Objects\CSSObject
;
use
Wikimedia\CSS\Objects\RuleList
;
use
Wikimedia\CSS\Objects\Stylesheet
;
use
Wikimedia\CSS\Util
;
/**
* Sanitizes a CSS stylesheet or rule list
* @see https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#css-stylesheets
*/
class
StylesheetSanitizer
extends
Sanitizer
{
/** @var RuleSanitizer[] */
protected
$ruleSanitizers
;
/**
* @param RuleSanitizer[] $ruleSanitizers Sanitizers to test rules. For
* each rule in the sheet, the first sanitizer that handles that rule gets
* to sanitize it.
*/
public
function
__construct
(
array
$ruleSanitizers
=
[]
)
{
$this
->
setRuleSanitizers
(
$ruleSanitizers
);
}
/**
* Create and return a default StylesheetSanitizer.
* @note This method exists more to be an example of how to put everything
* together than to be used directly.
* @return StylesheetSanitizer
*/
public
static
function
newDefault
()
{
// First, we need a matcher factory for the stuff all the sanitizers
// will need.
$matcherFactory
=
MatcherFactory
::
singleton
();
// This is the sanitizer for a single "property: value", that gets used by
// StyleRuleSanitizer and various others.
$propertySanitizer
=
new
StylePropertySanitizer
(
$matcherFactory
);
// These are sanitizers for different types of rules that can appear in
// stylesheets and can be nested inside @media and @supports blocks.
// The keys in the array aren't used for anything by the library, but
// may help humans reading it.
$ruleSanitizers
=
[
'style'
=>
new
StyleRuleSanitizer
(
$matcherFactory
->
cssSelectorList
(),
$propertySanitizer
),
'@font-face'
=>
new
FontFaceAtRuleSanitizer
(
$matcherFactory
),
'@keyframes'
=>
new
KeyframesAtRuleSanitizer
(
$matcherFactory
,
$propertySanitizer
),
'@page'
=>
new
PageAtRuleSanitizer
(
$matcherFactory
,
$propertySanitizer
),
'@media'
=>
new
MediaAtRuleSanitizer
(
$matcherFactory
->
cssMediaQueryList
()
),
'@supports'
=>
new
SupportsAtRuleSanitizer
(
$matcherFactory
,
[
'declarationSanitizer'
=>
$propertySanitizer
,
]
),
];
// Inject the above list into the @media and @supports sanitizers.
$ruleSanitizers
[
'@media'
]->
setRuleSanitizers
(
$ruleSanitizers
);
$ruleSanitizers
[
'@supports'
]->
setRuleSanitizers
(
$ruleSanitizers
);
// Now we can put together the StylesheetSanitizer
return
new
StylesheetSanitizer
(
$ruleSanitizers
+
[
// Note there's intentionally no "@charset" sanitizer, as that at-rule
// was removed in the Editor's Draft in favor of special handling
// in the parser.
'@import'
=>
new
ImportAtRuleSanitizer
(
$matcherFactory
,
[
'declarationSanitizer'
=>
$propertySanitizer
,
]
),
'@namespace'
=>
new
NamespaceAtRuleSanitizer
(
$matcherFactory
),
]
);
}
/**
* Access the list of rule sanitizers
* @return RuleSanitizer[]
*/
public
function
getRuleSanitizers
()
{
return
$this
->
ruleSanitizers
;
}
/**
* Set the list of rule sanitizers
* @param RuleSanitizer[] $ruleSanitizers
*/
public
function
setRuleSanitizers
(
array
$ruleSanitizers
)
{
Util
::
assertAllInstanceOf
(
$ruleSanitizers
,
RuleSanitizer
::
class
,
'$ruleSanitizers'
);
$this
->
ruleSanitizers
=
$ruleSanitizers
;
}
/** @inheritDoc */
protected
function
doSanitize
(
CSSObject
$object
)
{
$isSheet
=
$object
instanceof
Stylesheet
;
if
(
$isSheet
)
{
'@phan-var Stylesheet $object'
;
$object
=
$object
->
getRuleList
();
}
if
(
!
$object
instanceof
RuleList
)
{
$this
->
sanitizationError
(
'expected-stylesheet'
,
$object
);
return
null
;
}
$ret
=
$this
->
sanitizeRules
(
$this
->
ruleSanitizers
,
$object
);
if
(
$isSheet
)
{
$ret
=
new
Stylesheet
(
$ret
);
}
return
$ret
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 21:45 (1 d, 3 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
30/f9/2738dff3fa7b544e5ffa9a48c80b
Default Alt Text
StylesheetSanitizer.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment