Sha256: 3620dfe971118af3215dd663d461bbc3e63d39363ac729b01322b7a4468785c0

Contents?: true

Size: 1.32 KB

Versions: 27

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true
module Haml
  class Compiler
    class CommentCompiler
      def compile(node, &block)
        if node.value[:conditional]
          compile_conditional_comment(node, &block)
        else
          compile_html_comment(node, &block)
        end
      end

      private

      def compile_html_comment(node, &block)
        if node.children.empty?
          [:html, :comment, compile_text(node)]
        else
          [:html, :comment, yield(node)]
        end
      end

      def compile_conditional_comment(node, &block)
        condition = node.value[:conditional]
        if node.value[:conditional] =~ /\A\[(\[*[^\[\]]+\]*)\]/
          condition = $1
        end

        content =
          if node.children.empty?
            compile_text(node)
          else
            yield(node)
          end
        [:html, :condcomment, condition, content, node.value[:revealed]]
      end

      def compile_text(node)
        text =
          if node.value[:parse]
            # Just always escaping the result for safety. We could respect
            # escape_html, but I don't see any use case for it.
            [:escape, true, [:dynamic, node.value[:text]]]
          else
            [:static, node.value[:text]]
          end
        [:multi, [:static, ' '], text, [:static, ' ']]
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
haml-6.3.0 lib/haml/compiler/comment_compiler.rb
haml-6.2.5 lib/haml/compiler/comment_compiler.rb
haml-6.2.4 lib/haml/compiler/comment_compiler.rb
haml-6.2.3 lib/haml/compiler/comment_compiler.rb
haml-6.2.2 lib/haml/compiler/comment_compiler.rb
haml-6.2.1 lib/haml/compiler/comment_compiler.rb
haml-6.2.0 lib/haml/compiler/comment_compiler.rb
haml-6.1.4-java lib/haml/compiler/comment_compiler.rb
haml-6.1.4 lib/haml/compiler/comment_compiler.rb
haml-6.1.3-java lib/haml/compiler/comment_compiler.rb
haml-6.1.3 lib/haml/compiler/comment_compiler.rb
haml-6.1.2-java lib/haml/compiler/comment_compiler.rb
haml-6.1.2 lib/haml/compiler/comment_compiler.rb
haml-6.1.1-java lib/haml/compiler/comment_compiler.rb
haml-6.1.1 lib/haml/compiler/comment_compiler.rb
haml-6.1.0-java lib/haml/compiler/comment_compiler.rb
haml-6.1.0 lib/haml/compiler/comment_compiler.rb
haml-6.0.12-java lib/haml/compiler/comment_compiler.rb
haml-6.0.12 lib/haml/compiler/comment_compiler.rb
haml-6.0.11-java lib/haml/compiler/comment_compiler.rb