1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

The default quote character can now be set in the options hash. Also changes the quote-choosing

behavior, which speeds up the engine.


git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@105 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-10-30 06:10:26 +00:00
parent 5d9c510928
commit 3e0156a858
6 changed files with 27 additions and 8 deletions

View file

@ -85,9 +85,10 @@ closing tags for any element.
==== {}
Brackets represent a Ruby hash that is used for specifying the attributes of an
element. It is literally evaluated as a Ruby hash, so logic will work in it. At
the moment, though, it doesn't see local variables. The hash is placed after
the tag is defined. For example:
element. It is literally evaluated as a Ruby hash, so logic will work in it, and
local variables may be used. Quote characters within the attribute will be replaced
by appropriate escape sequences. The hash is placed after the tag is defined. For
example:
%head{ :name => "doc_head" }
%script{ 'type' => "text/" + "javascript", :src => "javascripts/script_#{2 + 7}" }

View file

@ -18,6 +18,7 @@ module Haml
# Creates a new buffer.
def initialize(options = {})
@options = options
@quote_escape = options[:attr_wrapper] == '"' ? """ : "'"
@buffer = ""
@one_liner_pending = false
end
@ -150,9 +151,12 @@ module Haml
end
result = attributes.sort.collect do |a,v|
unless v.nil?
first_quote_type = v.to_s.scan(/['"]/).first
quote_type = (first_quote_type == "'") ? '"' : "'"
"#{a.to_s}=#{quote_type}#{v.to_s}#{quote_type}"
v = v.to_s
attr_wrapper = @options[:attr_wrapper]
if v.include? attr_wrapper
v = v.gsub(attr_wrapper, @quote_escape)
end
"#{a.to_s}=#{attr_wrapper}#{v}#{attr_wrapper}"
end
end
result = result.compact.join(' ')

View file

@ -97,10 +97,17 @@ module Haml
# [<tt>:precompiled</tt>] A string containing a precompiled Haml template.
# If this is passed, <tt>template</tt> is ignored
# and no precompilation is done.
#
# [<tt>:attr_wrapper</tt>] The character that should wrap element attributes.
# This defaults to <tt>'</tt> (an apostrophe). Characters
# of this type within the attributes will be escaped
# (e.g. by replacing them with <tt>&apos;</tt>) if
# the character is an apostrophe or a quotation mark.
def initialize(template, options = {})
@options = {
:suppress_eval => false
:suppress_eval => false,
:attr_wrapper => "'"
}.merge options
@precompiled = @options[:precompiled]
@ -117,7 +124,7 @@ module Haml
# a string.
def to_html(scope = Object.new, &block)
@scope_object = scope
@buffer = Haml::Buffer.new
@buffer = Haml::Buffer.new(@options)
# Compile the @precompiled buffer
compile &block

View file

@ -17,6 +17,11 @@ class EngineTest < Test::Unit::TestCase
assert_equal("", render("= 'Hello'", :suppress_eval => true))
end
def test_attr_wrapper
assert_equal("<p strange=*attrs*>\n</p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
assert_equal("<p escaped=\"quo&quot;te\">\n</p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
end
# This is ugly because Hashes are unordered; we don't always know the order
# in which attributes will be returned.
# There is probably a better way to do this.

View file

@ -3,6 +3,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<strong apos='Foo&apos;s bar!'>Boo!</strong>
<!-- Short comment -->
<!--
This is a really long comment look how long it is it should be on a line of its own don't you think?

View file

@ -3,6 +3,7 @@
!!! 1.1 Strict
!!! Strict foo bar
!!! FRAMESET
%strong{:apos => "Foo's bar!"} Boo!
/ Short comment
/ This is a really long comment look how long it is it should be on a line of its own don't you think?
/