Main Content

mlreportgen.dom.TemplateHTMLStyle Class

Namespace: mlreportgen.dom

Parse existing styles within HTML template

Since R2024a

Description

Use this class to create a Document Object Model (DOM) representation of an existing style in an HTML template. This class generates a style sheet entry in HTML style sheets only. PDF and DOCX template style sheets ignore this class. This class enables you to see what styles already exist in a template.

This class represents an HTML style defined by an HTML template (.htmtx or .htmt) file. Opening an HTML template creates an array containing an instance of this class for each style defined by the template file. You can access the styles by using the TemplateStyles property of the style sheet stored in the Stylesheet property. Use this class to view and modify the CSS selector and formats for an HTML style. You can remove or replace HTML styles using the removeStyle and replaceStyle methods of the template style sheet.

The mlreportgen.dom.TemplateHTMLStyle class is a handle class.

Class Attributes

HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

example

style = TemplateHTMLStyle(selector,rawFormats) creates an HTML style and sets the Selector and RawFormats properties to the values of selector and rawFormats, respectively. Use this constructor to define HTML styles that use more complex CSS selectors or formats that do not have equivalent DOM classes.

Properties

expand all

Name of the style as parsed from the value of the Selector property, specified as a character vector or string scalar.

Attributes:

SetAccess
private
NonCopyable
true

Data Types: char | string

CSS selector for the style, specified as a string scalar. This value includes the class name and element name, and other selector patterns such as "nth-child". For example, a table style, "exampleTable", that formats even rows of the table would set the Selector property to "tr:nth-child(even).exampleTable".

Attributes:

NonCopyable
true

Data Types: string

CSS properties and values of the style, specified as a string scalar. You must separate each CSS property and value pair by using a semicolon. For example, the Selector property for a style that formats color and font weight would be: "color:blue; font-weight:bold;".

Attributes:

NonCopyable
true

Data Types: string

The class ignores this property.

Tag for mlreportgen.dom.TemplateHTMLStyle object, specified as a character vector or string scalar. The DOM API generates a session-unique tag as part of the creation of this object. The generated tag has the form CLASS:ID, where CLASS is the object class and ID is the value of the Id property of the object. Specify your own tag value to help you identify where to look when an issue occurs during document generation.

Attributes:

NonCopyable
true

Data Types: char | string

Object identifier for mlreportgen.dom.TemplateHTMLStyle object, specified as a character vector or string scalar. The DOM API generates a session-unique identifier when it creates the document element object. You can specify your own value for Id.

Attributes:

NonCopyable
true

Data Types: char | string

Examples

collapse all

View an existing style in an HTML template, modify that style, and replace the existing style with the modified style.

Create a HTML Template and Examine the Default Style

Create a new template based on the default HTML template and examine the styles for the table of contents.

Import the DOM API namespace so that you do not have to use fully qualified names.

import mlreportgen.dom.*

Create a Template using the default HTML template.

t = Template("myTemplate","html");

Open the template and get the stylesheet.

open(t);
stylesheet = t.Stylesheet
stylesheet = 
  TemplateStylesheet with properties:

     TemplateStyles: [1×31 mlreportgen.dom.TemplateHTMLStyle]
         TextStyles: []
    ParagraphStyles: []
       LinkedStyles: []
         ListStyles: []
        TableStyles: []
                Tag: 'dom.TemplateStylesheet:759'
                 Id: '759'

Get the styles with stylename "toc".

tocStyles = getStyle(stylesheet,"toc");

Look at the first of the table of content's style and note that the list style under RawFormats is currently set to none.

tocStyles(1)
ans = 
  TemplateHTMLStyle with properties:

          Name: 'toc'
      Selector: 'ol.toc > li'
    RawFormats: ' list-style:none; '
       Formats: []
           Tag: 'dom.TemplateHTMLStyle:760'
            Id: '760'

Modify Table of Contents to Use Roman Numerals for Sections

Modify the new template by changing the table of contents styles to use Roman numerals instead of the default.

This pattern matches rules that start with "list-style" and end with "none;".

pat = "list-style" + wildcardPattern("Except",";") + "none;";

Replace "list-style-type: none" and "list-style: none" with "list-style-type: upper-roman".

for tocStyle = tocStyles
    tocStyle.RawFormats = replace(tocStyle.RawFormats,pat,"list-style-type:upper-roman;");
end

Create a new style that sets the font used by the table of contents to Courier New.

newHTMLStyle = TemplateHTMLStyle("ol.toc a","font-family:""Courier New"";");
addStyle(stylesheet,newHTMLStyle);

Close the template.

close(t);

Version History

Introduced in R2024a