Skip to content

Engine: <%# herb:strict false %> directive #1160

Description

@marcoroth

Summary

Add support for a file-level directive to control strict mode in Herb::Engine.

Background

The engine already supports a strict option that gets passed to the parser:

# lib/herb/engine.rb
@strict = properties.fetch(:strict, false)
parse_result = ::Herb.parse(input, track_whitespace: true, strict: @strict)

What strict mode controls

In the parser (src/parser.c), strict mode affects HTML tag matching behavior:

  • strict: true - Reports errors for omitted closing tags (e.g., <p>text without </p>)
  • strict: false - Silently handles omitted closing tags without errors (following HTML5 optional closing tag rules)

This is useful because HTML5 allows optional closing tags for elements like <p>, <li>, <td>, etc.

Proposed Directive Format

<%# herb:strict false %>  <!-- disable strict mode for this file -->
<%# herb:strict true %>   <!-- enable strict mode for this file -->

Use Cases

  1. Legacy templates - Files with valid HTML5 that omit optional closing tags
  2. Migration - Gradually enable strict mode across a codebase
  3. Third-party templates - Templates that can't be easily modified but are valid HTML

Relationship to Security Directive

This is separate from <%# herb:security false %> (#1161):

Directive Controls
herb:strict Parser strictness (omitted closing tag errors)
herb:security Security validation (injection pattern errors)

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineHerb engine and Rails template compilationfeatureNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions