Extension:TemplateStyles

The TemplateStyles extension introduces a  tag to specify that a stylesheet should be loaded from a wiki page. Placing this in a template allows the template to have custom styles without having to place them in .

For instructions on how to use the extension as an editor on a wiki, see .

Usage
First, the CSS page must be created. By default any subpage in the Template namespace with a title ending in ".css" will be created with the "Sanitized CSS"  if it contains no syntax errors.

The set of namespaces may be adjusted with  , or  may be used on any page. Then, in the template's wikitext, add the  tag to load the styles.

The CSS saved using the "Sanitized CSS" content model must meet strict validity requirements: invalid CSS, unrecognized at-rules, and unrecognized or unsupported properties or property values cannot be saved. If invalid CSS is somehow saved anyway, the offending constructs will be removed when the CSS is output to the browser.

The value of the <tvar|1> </> attribute on the tag is the title of the page, defaulting to the Template namespace. For example, <tvar|1></> will load the page "<tvar|2>Template:Example/styles.css</>". This will fail if that page does not exist or has a content model other than "Sanitized CSS".

Styles can be scoped within the page by using the optional <tvar|1> </> parameter to the tag, e.g. <tvar|2></> would scope the styles loaded to any <tvar|3></> inside the main parsed content. Any CSS simple selector sequence can be used for the <tvar|wrap> </> parameter. This is intended to allow side-by-side comparison of live and sandbox versions of a template.

Use of sanitized CSS is tracked like transclusion of templates, and will show up as a transclusion on <tvar|1></>.

Caveats

 * Styles added by TemplateStyles are scoped into <tvar|1> </> to avoid tampering with the user interface outside of the main parsed content.


 * To use TemplateStyles to style something like w:MediaWiki:Protectedpagetext, you would need to enclose the message's contents in <tvar|1></>.


 * The styles should be written to target specific CSS classes, and anything that generates elements with those classes should be sure to also include the styles itself rather than relying on some other template to have done so.


 * Styles included by a template can currently affect content on the page outside of the content generated by that template, but this ability may be removed in the future and should not be relied upon.
 * Including styles on a template that affects contents outside of that template, will cause those styles to not be applied when editing a section that doesn't contain that template. Example: including styles on an infobox that affect all tables of the page, when editing a section that doesn't contain the infobox, those tables won't be styled when previewing that section.


 * TemplateStyles allows few non-standardized CSS properties. Requests to support additional properties should be [<tvar|url>https://phabricator.wikimedia.org/maniphest/task/edit/form/1/?tags=TemplateStyles,css-sanitizer</> filed in Phabricator in the css-sanitizer and TemplateStyles projects].


 * Requests should include links to standards-track documents (e.g. on w3.org) describing the syntax of the properties being requested, and an analysis of current browser support for the properties (e.g. a link to a caniuse.com page about the properties).


 * Vendor-prefixed properties (e.g. anything starting with <tvar|1> </>, <tvar|2> </>, or <tvar|3> </>) are likely to be declined if they're not needed for 4>Special:MyLanguage/Compatibility#Browser support matrix</>|modern browsers.


 * <tvar|1> </> rules must use a <tvar|2> </> prefixed with "TemplateStyles". This should largely prevent redefining fonts used elsewhere in the document.


 * To target styles based on skins, use a selector such as <tvar|1> </>; specification of the <tvar|2> </> element is required and must be followed by a descendant combinator (i.e. the space). Other classes on the <tvar|1> </> or <tvar|2> </> elements may be targeted in the same manner.

Other dependencies
<tvar|1> </> should be configured to use no tidying or <tvar|2>RemexHtml</>. If used with any of the Raggett drivers, a <tvar|1></> tag in the middle of a paragraph (including in an inline template) will cause tidy to break the paragraph at that point. The other drivers have not been tested for this issue.

Potential errors
If you experience an error when importing pages from another wiki, for example, it may help to enable <tvar|1></> in your <tvar|2> </> to determine if you are experiencing any of the errors below.


 * This error may come up when attempting to import a man>Special:MyLanguage/Manual:CSS</>|wiki CSS page or when help>Special:MyLanguage/Help:ChangeContentModel</>|changing the content model of a page to "santized-css". There is, as of April 16, 2020, an 2>phab:T215713</>|open bug in the extension distributor which requires <tvar|1> </> to be run in the extension subdirectory even when the extension is not installed from Git.  See <tvar|1>Topic:Ukv6pdo96a8qfur2</> for details.
 * Some people get this error on a wiki when importing pages. This extension provides that content model.  Install TemplateStyles to fix your import.
 * Some people get this error on a wiki when importing pages. This extension provides that content model.  Install TemplateStyles to fix your import.
 * Some people get this error on a wiki when importing pages. This extension provides that content model.  Install TemplateStyles to fix your import.