import { SanitizeStrategy } from '../core/sanitize-strategy';
/**
 * CSS / style sanitization strategy.
 *
 * - Strips all HTML elements
 * - Preserves text nodes
 * - Blocks dangerous CSS patterns (expression(), javascript:)
 */
export class StyleSanitizeStrategy extends SanitizeStrategy {
  constructor() {
    super(...arguments);
    this.allowedTags = new Set();
    this.preserveTextContent = true;
  }
  /**
   * @param {Node} node - Node to sanitize.
   * @param {Document} doc - document reference.
   * @returns {Nullable<Node>} - Sanitized node
   */
  execute(node, doc) {
    var _a;
    switch (node.nodeType) {
      case Node.TEXT_NODE: {
        const css = (_a = node.textContent) !== null && _a !== void 0 ? _a : '';
        // Strip dangerous CSS patterns
        return this.isSafeCss(css) ? doc.createTextNode(css) : null;
      }
      case Node.ELEMENT_NODE: {
        // Recurse into children but discard the element itself
        const fragment = doc.createDocumentFragment();
        node.childNodes.forEach((child) => {
          const clean = this.execute(child, doc);
          if (clean)
            fragment.appendChild(clean);
        });
        return fragment;
      }
      default:
        return null;
    }
  }
  /**
   * @param {string} css - CSS string.
   * @returns {boolean} - True or false
   */
  isSafeCss(css) {
    // Block known XSS patterns
    return !/expression\s*\(|javascript:|url\s*\(/i.test(css);
  }
}
