Class BaseCSSStyleSheet

All Implemented Interfaces:
CSSStyleSheet<AbstractCSSRule>, Serializable, Cloneable, CSSStyleSheet, StyleSheet
Direct Known Subclasses:
BaseDocumentCSSStyleSheet, DOMCSSStyleSheet

public abstract class BaseCSSStyleSheet extends AbstractCSSStyleSheet
CSS Style Sheet Object Model implementation base class.
See Also:
  • Constructor Details

    • BaseCSSStyleSheet

      protected BaseCSSStyleSheet(String title, MediaQueryList media, AbstractCSSRule ownerRule, byte origin)
      Constructs a style sheet.
      Parameters:
      title - the advisory title.
      media - the media this sheet is for.
      ownerRule - the owner rule.
      origin - the sheet origin.
  • Method Details

    • copyAllTo

      protected void copyAllTo(BaseCSSStyleSheet myCopy)
    • copyFieldsTo

      protected void copyFieldsTo(BaseCSSStyleSheet myCopy)
    • copyRulesTo

      protected void copyRulesTo(BaseCSSStyleSheet myCopy)
    • getStyleSheetFactory

      public abstract BaseCSSStyleSheetFactory getStyleSheetFactory()
      Get the stylesheet factory used to produce this sheet.
      Specified by:
      getStyleSheetFactory in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      getStyleSheetFactory in class AbstractCSSStyleSheet
      Returns:
      the stylesheet factory.
    • getOwnerRule

      public AbstractCSSRule getOwnerRule()
      Specified by:
      getOwnerRule in interface CSSStyleSheet
      Specified by:
      getOwnerRule in class AbstractCSSStyleSheet
    • getOwnerNode

      public Node getOwnerNode()
    • getMedia

      public MediaQueryList getMedia()
      Description copied from interface: CSSStyleSheet
      Get the destination media for this sheet.
      Returns:
      the media query list.
    • setMedia

      protected void setMedia(MediaQueryList media) throws DOMException
      Description copied from class: AbstractStyleSheet
      Set the destination media for this sheet.
      Specified by:
      setMedia in class AbstractStyleSheet
      Parameters:
      media - the destination media.
      Throws:
      DOMException - if the media is invalid.
    • getOrigin

      public byte getOrigin()
      Description copied from class: AbstractCSSStyleSheet
      Get the origin of this sheet.
      Specified by:
      getOrigin in class AbstractCSSStyleSheet
      Returns:
      the origin of this sheet.
    • getCssRules

      public CSSRuleArrayList getCssRules()
      Description copied from interface: CSSStyleSheet
      Gets the collection of all CSS rules contained within the style sheet.
      Specified by:
      getCssRules in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      getCssRules in interface CSSStyleSheet
      Specified by:
      getCssRules in class AbstractCSSStyleSheet
      Returns:
      the list of all CSS rules contained within the style sheet.
    • insertRule

      public int insertRule(String rule, int index) throws DOMException
      Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
      Parameters:
      rule - The parsable text representing the rule. For rule sets this contains both the selector and the style declaration. For at-rules, this specifies both the at-identifier and the rule content.
      index - The index within the style sheet's rule list of the rule before which to insert the specified rule. If the specified index is equal to the length of the style sheet's rule collection, the rule will be added to the end of the style sheet.
      Returns:
      The index within the style sheet's rule collection of the newly inserted rule.
      Throws:
      DOMException - HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified index e.g. if an @import rule is inserted after a standard rule set or other at-rule.
      INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
      NO_MODIFICATION_ALLOWED_ERR: Raised if this style sheet is readonly.
      SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
    • addRule

      public void addRule(AbstractCSSRule cssrule) throws DOMException
      Inserts a rule in the current insertion point (generally after the last rule).
      Parameters:
      cssrule - the rule to be inserted.
      Throws:
      DOMException - NAMESPACE_ERR if the rule could not be added due to a namespace-related error.
    • registerNamespace

      protected void registerNamespace(CSSNamespaceRule nsrule)
      Description copied from class: AbstractCSSStyleSheet
      Register the namespace from the given namespace rule.
      Specified by:
      registerNamespace in class AbstractCSSStyleSheet
      Parameters:
      nsrule - the namespace rule.
    • unregisterNamespace

      protected void unregisterNamespace(String namespaceURI)
      Description copied from class: AbstractCSSStyleSheet
      Unregister the namespace corresponding to the given namespace URI.
      Specified by:
      unregisterNamespace in class AbstractCSSStyleSheet
      Parameters:
      namespaceURI - the namespace URI.
    • addLocalRule

      protected void addLocalRule(CSSRule cssrule)
      Inserts a local rule in the current insertion point (generally after the last rule).
      Parameters:
      cssrule - the rule to be inserted.
    • deleteRule

      public void deleteRule(int index) throws DOMException
      Deletes a rule from the style sheet.
      Parameters:
      index - The index within the style sheet's rule list of the rule to remove.
      Throws:
      DOMException - INDEX_SIZE_ERR: Raised if the specified index does not correspond to a rule in the style sheet's rule list.
      NAMESPACE_ERR: Raised if the rule is a namespace rule and this style sheet contains style rules with that namespace.
    • addStyleSheet

      public void addStyleSheet(AbstractCSSStyleSheet sheet)
      Adds the rules contained by the supplied style sheet, if that sheet is not disabled.

      If the provided sheet does not target all media, a media rule is created.

      Specified by:
      addStyleSheet in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      addStyleSheet in class AbstractCSSStyleSheet
      Parameters:
      sheet - the sheet whose rules are to be added.
    • createCounterStyleRule

      public CounterStyleRule createCounterStyleRule(String name) throws DOMException
      Description copied from interface: CSSStyleSheet
      Create a CSSCounterStyleRule compatible with this implementation.
      Parameters:
      name - the counter-style name.
      Returns:
      a CSSCounterStyleRule object.
      Throws:
      DOMException - if the name is invalid.
    • createFontFaceRule

      public FontFaceRule createFontFaceRule()
      Description copied from interface: CSSStyleSheet
      Create a CSS Font Face rule compatible with this implementation.
      Specified by:
      createFontFaceRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createFontFaceRule in class AbstractCSSStyleSheet
      Returns:
      a CSS Font Face rule object.
    • createFontFeatureValuesRule

      public FontFeatureValuesRule createFontFeatureValuesRule(String[] fontFamily)
      Description copied from interface: CSSStyleSheet
      Create a CSSFontFeatureValuesRule compatible with this implementation.
      Parameters:
      fontFamily - the font family.
      Returns:
      a CSSFontFeatureValuesRule object.
    • createImportRule

      public ImportRule createImportRule(MediaQueryList mediaList, String href)
      Description copied from interface: CSSStyleSheet
      Create a CSS import rule compatible with this implementation.
      Specified by:
      createImportRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createImportRule in class AbstractCSSStyleSheet
      Parameters:
      mediaList - a list of media types for which the new import rule may be used.
      href - the URI from which to import the sheet.
      Returns:
      a CSS import rule.
    • createKeyframesRule

      public KeyframesRule createKeyframesRule(String keyframesName)
      Description copied from interface: CSSStyleSheet
      Create a CSSKeyframesRule compatible with this implementation.
      Parameters:
      keyframesName - the name of the keyframes.
      Returns:
      a CSSKeyframesRule object.
    • createMarginRule

      public MarginRule createMarginRule(String name)
      Description copied from interface: CSSStyleSheet
      Create a CSS margin rule compatible with this implementation.
      Parameters:
      name - the margin rule name.
      Returns:
      a CSS margin rule.
    • createMediaRule

      public MediaRule createMediaRule(MediaQueryList mediaList)
      Description copied from interface: CSSStyleSheet
      Create a CSS media rule.
      Specified by:
      createMediaRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createMediaRule in class AbstractCSSStyleSheet
      Parameters:
      mediaList - a list of media types for the new rule.
      Returns:
      a CSS media rule.
    • createNamespaceRule

      public NamespaceRule createNamespaceRule(String prefix, String namespaceUri)
      Description copied from interface: CSSStyleSheet
      Create a CSS namespace rule compatible with this implementation.
      Parameters:
      prefix - the namespace prefix.
      namespaceUri - the namespace URI.
      Returns:
      a CSS namespace rule.
    • createPageRule

      public PageRule createPageRule()
      Description copied from interface: CSSStyleSheet
      Create a CSS page rule compatible with this implementation.
      Specified by:
      createPageRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createPageRule in class AbstractCSSStyleSheet
      Returns:
      a CSS page rule.
    • createPropertyRule

      public PropertyRule createPropertyRule(String name)
      Description copied from interface: CSSStyleSheet
      Create a CSS property rule compatible with this implementation.
      Parameters:
      name - the (unescaped) property name.
      Returns:
      a CSS property rule.
    • createStyleRule

      public StyleRule createStyleRule()
      Description copied from interface: CSSStyleSheet
      Create a CSS style rule.
      Specified by:
      createStyleRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createStyleRule in class AbstractCSSStyleSheet
      Returns:
      a CSS style rule.
    • createSupportsRule

      public SupportsRule createSupportsRule(BooleanCondition condition)
      Description copied from interface: CSSStyleSheet
      Create a CSSSupportsRule compatible with this implementation.
      Specified by:
      createSupportsRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createSupportsRule in class AbstractCSSStyleSheet
      Parameters:
      condition - the @supports condition.
      Returns:
      a CSSSupportsRule object.
    • createSupportsRule

      public SupportsRule createSupportsRule(String conditionText) throws DOMException
      Description copied from interface: CSSStyleSheet
      Create a CSSSupportsRule compatible with this implementation.
      Specified by:
      createSupportsRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createSupportsRule in class AbstractCSSStyleSheet
      Parameters:
      conditionText - a serialization of the @supports condition.
      Returns:
      a CSSSupportsRule object.
      Throws:
      DOMException - if the condition text could not be parsed.
    • createSupportsRule

      @Deprecated public SupportsRule createSupportsRule()
      Deprecated.
      Description copied from interface: CSSStyleSheet
      Create a CSSSupportsRule compatible with this implementation.
      Specified by:
      createSupportsRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createSupportsRule in class AbstractCSSStyleSheet
      Returns:
      a CSSSupportsRule object.
    • createViewportRule

      public ViewportRule createViewportRule()
      Description copied from interface: CSSStyleSheet
      Create a CSSViewportRule compatible with this implementation.
      Specified by:
      createViewportRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createViewportRule in class AbstractCSSStyleSheet
      Returns:
      a CSSViewportRule object.
    • createUnknownRule

      public UnknownRule createUnknownRule()
      Description copied from interface: CSSStyleSheet
      Create a CSS unknown rule.

      Its contents can be set with CSSRule.setCssText(String). Be careful to set a text that is compatible with a CSS rule, with an ending semicolon or balanced curly brackets, otherwise its serialization may break the style sheet serialization.

      Specified by:
      createUnknownRule in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createUnknownRule in class AbstractCSSStyleSheet
      Returns:
      a CSS unknown rule.
    • createStyleDeclaration

      protected BaseCSSStyleDeclaration createStyleDeclaration(BaseCSSDeclarationRule rule)
      Description copied from class: AbstractCSSStyleSheet
      Create a style declaration from the given declaration rule.
      Specified by:
      createStyleDeclaration in class AbstractCSSStyleSheet
      Parameters:
      rule - the declaration rule.
      Returns:
      the style declaration.
    • createStyleDeclaration

      public BaseCSSStyleDeclaration createStyleDeclaration()
      Description copied from interface: CSSStyleSheet
      Create a CSS style declaration compatible with this implementation.
      Specified by:
      createStyleDeclaration in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      createStyleDeclaration in class AbstractCSSStyleSheet
      Returns:
      a CSS style declaration.
    • hasRuleErrorsOrWarnings

      public boolean hasRuleErrorsOrWarnings()
      Description copied from interface: CSSStyleSheet
      Check whether this sheet contains rules that have errors or warnings reported to their handlers.
      Returns:
      true if this sheet contains rules that have errors or warnings.
    • getErrorHandler

      public SheetErrorHandler getErrorHandler()
      Description copied from interface: CSSStyleSheet
      Gets the error handler for this style sheet.
      Returns:
      the error handler.
    • getDocumentErrorHandler

      protected ErrorHandler getDocumentErrorHandler()
      Specified by:
      getDocumentErrorHandler in class AbstractStyleSheet
    • getType

      public String getType()
    • getNamespacePrefix

      protected String getNamespacePrefix(String uri)
      Gets the namespace prefix associated to the given URI.
      Specified by:
      getNamespacePrefix in class AbstractCSSStyleSheet
      Parameters:
      uri - the namespace URI string.
      Returns:
      the namespace prefix.
    • hasDefaultNamespace

      protected boolean hasDefaultNamespace()
      Has this style sheet defined a default namespace ?
      Specified by:
      hasDefaultNamespace in class AbstractCSSStyleSheet
      Returns:
      true if a default namespace was defined, false otherwise.
    • getDisabled

      public boolean getDisabled()
    • setDisabled

      public void setDisabled(boolean disabled)
    • getParentStyleSheet

      public AbstractCSSStyleSheet getParentStyleSheet()
      Specified by:
      getParentStyleSheet in interface StyleSheet
      Specified by:
      getParentStyleSheet in class AbstractCSSStyleSheet
    • setParentStyleSheet

      protected void setParentStyleSheet(AbstractCSSStyleSheet parent)
      Description copied from class: AbstractCSSStyleSheet
      Sets the parent style sheet.
      Specified by:
      setParentStyleSheet in class AbstractCSSStyleSheet
      Parameters:
      parent - the parent style sheet. Cannot be null.
    • getHref

      public String getHref()
    • setHref

      public void setHref(String href)
      Specified by:
      setHref in class AbstractCSSStyleSheet
    • loadStyleSheet

      public boolean loadStyleSheet(URL url, String referrerPolicy) throws DOMException, IOException
      Load the styles from url into this style sheet.
      Specified by:
      loadStyleSheet in class AbstractCSSStyleSheet
      Parameters:
      url - the url to load the style sheet from.
      referrerPolicy - the content of the referrerpolicy content attribute, if any, or the empty string.
      Returns:
      true if the NSAC parser reported no errors or fatal errors, false otherwise.
      Throws:
      DOMPolicyException - if the style sheet was served with an invalid content type.
      DOMException - if there is a serious problem parsing the style sheet.
      IOException - if a problem appears fetching the url contents.
    • getRulesForProperty

      public CSSRuleArrayList getRulesForProperty(String longhandPropertyName)
      Returns a list of rules that apply to a style where the given longhand property is set (either explicitly or through a shorthand).

      Grouping rules are scanned too, regardless of the medium or condition.

      Parameters:
      longhandPropertyName - the longhand property name.
      Returns:
      the list of rules, or null if no rules declare that property, or the property is a shorthand.
    • getSelectorsForProperty

      public Selector[] getSelectorsForProperty(String longhandPropertyName)
      Returns an array of selectors that apply to a style where the given longhand property is set (either explicitly or through a shorthand).

      Grouping rules are scanned too, regardless of the medium or condition.

      Parameters:
      longhandPropertyName - the longhand property name.
      Returns:
      the array of selectors, or null if no rules declare that property, or the property is a shorthand.
    • getSelectorsForPropertyValue

      public Selector[] getSelectorsForPropertyValue(String propertyName, String declaredValue)
      Returns an array of selectors that apply to a style where the given property was explicitly set to the given declared value.

      Media rules are scanned too, regardless of the specific medium.

      Beware that using this method with computed instead of declared values may not give the expected results.

      Parameters:
      propertyName - the property name.
      declaredValue - the property's declared value.
      Returns:
      the array of selectors, or null if no rules contain that property-value pair.
    • getFirstStyleRule

      public StyleRule getFirstStyleRule(SelectorList selectorList)
      Get the first style rule that exactly matches the given selector list, if any.

      Rules inside grouping rules are also searched.

      Specified by:
      getFirstStyleRule in class AbstractCSSStyleSheet
      Parameters:
      selectorList - the selector list.
      Returns:
      the first style rule that matches, or null if none.
    • getStyleRules

      public CSSRuleArrayList getStyleRules(Selector selector)
      Get the list of style rules that match the given selector.

      Rules inside grouping rules are also searched.

      Specified by:
      getStyleRules in class AbstractCSSStyleSheet
      Parameters:
      selector - the selector.
      Returns:
      the list of style rule that match, or null if none.
    • acceptStyleRuleVisitor

      public void acceptStyleRuleVisitor(Visitor<CSSStyleRule> visitor)
      Accept a style rule visitor.
      Specified by:
      acceptStyleRuleVisitor in class AbstractCSSStyleSheet
      Parameters:
      visitor - the visitor.
    • acceptDeclarationRuleVisitor

      public void acceptDeclarationRuleVisitor(Visitor<CSSDeclarationRule> visitor)
      Accept a declaration rule visitor.
      Specified by:
      acceptDeclarationRuleVisitor in class AbstractCSSStyleSheet
      Parameters:
      visitor - the visitor.
    • acceptDescriptorRuleVisitor

      public void acceptDescriptorRuleVisitor(Visitor<CSSDeclarationRule> visitor)
      Description copied from class: AbstractCSSStyleSheet
      Accept a descriptor rule visitor.

      This method scans for declaration rules that declare descriptors.

      Specified by:
      acceptDescriptorRuleVisitor in class AbstractCSSStyleSheet
      Parameters:
      visitor - the visitor.
    • getTargetMedium

      protected String getTargetMedium()
    • toMinifiedString

      public String toMinifiedString()
      Returns a minified parsable representation of the rule list of this sheet.

      Equivalent to getCssRules().toMinifiedString().

      Specified by:
      toMinifiedString in class AbstractStyleSheet
      Returns:
      a minified parsable representation of the rule list of this sheet.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toStyleString

      public String toStyleString()
      Description copied from class: AbstractStyleSheet
      Returns a serialization of this style sheet in the form of a STYLE element with its attributes and content.
      Specified by:
      toStyleString in class AbstractStyleSheet
      Returns:
      an HTML STYLE element representing this style sheet.
    • parseStyleSheet

      public boolean parseStyleSheet(Reader reader) throws DOMException, IOException
      Description copied from class: AbstractCSSStyleSheet
      Parses a style sheet.

      If the style sheet is not empty, the rules from the parsed source will be added at the end of the rule list, with the same origin as the rule with a highest precedence origin.

      Even if a specific media is set at the InputSource, this method does not alter the sheet's current media attribute.

      The comments shall be processed according to CSSStyleSheet.COMMENTS_AUTO.

      To create a sheet, see CSSStyleSheetFactory.createStyleSheet(String,MediaQueryList)

      Specified by:
      parseStyleSheet in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      parseStyleSheet in class AbstractCSSStyleSheet
      Parameters:
      reader - the character stream containing the CSS sheet.
      Returns:
      true if the NSAC parser reported no errors or fatal errors, false otherwise.
      Throws:
      DOMException - if raised by the error handler.
      IOException - if a problem is found reading the sheet.
    • parseStyleSheet

      public boolean parseStyleSheet(Reader reader, short commentMode) throws DOMException, IOException
      Parses a style sheet.

      If the style sheet is not empty, the rules from the parsed source will be added at the end of the rule list, with the same origin as the rule with a highest precedence origin.

      If commentMode is not COMMENTS_IGNORE, the comments preceding a rule shall be available through CSSRule.getPrecedingComments(), and if COMMENTS_AUTO was set also the trailing ones, through the method CSSRule.getTrailingComments().

      This method resets the state of this sheet's error handler.

      To create a sheet, see CSSStyleSheetFactory.createStyleSheet(String,MediaQueryList)

      Specified by:
      parseStyleSheet in interface CSSStyleSheet<AbstractCSSRule>
      Specified by:
      parseStyleSheet in class AbstractCSSStyleSheet
      Parameters:
      reader - the character stream containing the CSS sheet.
      commentMode - 0 if comments have to be ignored, 1 if all comments are considered as preceding a rule, 2 if the parser should try to figure out which comments are preceding and trailing a rule (auto mode).
      Returns:
      true if the SAC parser reported no errors or fatal errors, false otherwise.
      Throws:
      DOMException - if raised by the error handler.
      IOException - if a problem is found reading the sheet.