def _process_element(self, element: ContentElement): """Filter ISD element style properties""" element_styles = list(element.iter_styles()) for style_prop in element_styles: value = element.get_style(style_prop) default_value = self.style_property_default_values.get(style_prop) parent = element.parent() if parent is not None and style_prop.is_inherited: # If the parent style property value has not been removed, it means # the value is not set to default, and so that the child style property # value may have been "forced" to the default value, so let's skip it. parent_value = parent.get_style(style_prop) if parent_value is not None and parent_value is not value: continue # Remove the style property if its value is default (and if it is not inherited) if default_value is not None and value == default_value: element.set_style(style_prop, None) for child in element: self._process_element(child)
def from_model( ctx: TTMLElement.WritingContext, model_element: model.ContentElement ) -> typing.Optional[et.Element]: '''Returns the TTML element corresponding to the model element `model_element`. `ctx` contains state information used in the process. ''' if isinstance(model_element, model.Body): imsc_class = BodyElement elif isinstance(model_element, model.Div): imsc_class = DivElement elif isinstance(model_element, model.P): imsc_class = PElement elif isinstance(model_element, model.Span): imsc_class = SpanElement elif isinstance(model_element, model.Br): imsc_class = BrElement elif isinstance(model_element, model.Ruby): imsc_class = RubyElement elif isinstance(model_element, model.Rb): imsc_class = RbElement elif isinstance(model_element, model.Rt): imsc_class = RtElement elif isinstance(model_element, model.Rbc): imsc_class = RbcElement elif isinstance(model_element, model.Rtc): imsc_class = RtcElement elif isinstance(model_element, model.Region): imsc_class = RegionElement else: return None xml_element = imsc_class.make_ttml_element() if (model_element.parent() is None and model_element.get_space() is model.WhiteSpaceHandling.PRESERVE) or \ (model_element.parent() is not None and model_element.parent().get_space() != model_element.get_space()): imsc_attr.XMLSpaceAttribute.set(xml_element, model_element.get_space()) if imsc_class.has_region: if model_element.get_region() is not None: imsc_attr.RegionAttribute.set(xml_element, model_element.get_region().get_id()) if imsc_class.has_timing: if model_element.get_begin() is not None: imsc_attr.BeginAttribute.set(ctx.temporal_context, xml_element, model_element.get_begin()) if model_element.get_end() is not None: imsc_attr.EndAttribute.set(ctx.temporal_context, xml_element, model_element.get_end()) if model_element.get_id() is not None: imsc_attr.XMLIDAttribute.set(xml_element, model_element.get_id()) if imsc_class.has_styles: ContentElement.from_model_style_properties(model_element, xml_element) ContentElement.from_model_animation(ctx, model_element, xml_element) if imsc_class.has_children: last_child_element = None for child in iter(model_element): if isinstance(child, model.Text): if last_child_element is None: xml_element.text = child.get_text() else: last_child_element.tail = child.get_text() child_element = ContentElement.from_model(ctx, child) if child_element is not None: xml_element.append(child_element) last_child_element = child_element return xml_element