htmlbeautifier 0.0.12 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7aaf76d4d6d04d9e89603730cb13417bf03a0321
4
- data.tar.gz: 56808b31dc24878165d2bd3c55cf43b93fd893de
3
+ metadata.gz: 47d8a66494066ec486f778eb743569aa10eda648
4
+ data.tar.gz: 964230bf12d44b7e71733b1d0c902864d9c879d1
5
5
  SHA512:
6
- metadata.gz: 0168d0c22faff926c2af24939434dea45fb35b5b95451efd207819f7401aedc32dec9a00a9bbda8a7e423be2e49af6ce3d0296a844ee69bb34593c18cc777080
7
- data.tar.gz: 90c25b3b9628e98a4edddfc7069ce610d31570cfafe97ce6d2586683dbfdc633c7f12afc3bc7290966fc1dcee86c8499d3277f2c6a88dc8b43f19b9872f42f9f
6
+ metadata.gz: 6f3421ac4478a9e60a9ef959ce88f62b2d08a0ee246118b8913f54f5c7b4dc396b64596348b8ae5b79905a101b4dfa1a05a79ba7eb9ecb97813dba5f6832c061
7
+ data.tar.gz: 93bcc5f3cb49700c15977d4609e7f3ec1d66722dec0d5c33085459a69cd23b9d41cd9b6e23d5780f31cb672aef5c4e1b26b0fdd8d843dbc5b4ad37115ab48f2b
data/README.md CHANGED
@@ -1,16 +1,8 @@
1
1
  HTML Beautifier
2
2
  ===============
3
3
 
4
- A normaliser/beautifier for HTML that also understands embedded Ruby. Ideal for tidying up Rails templates.
5
-
6
- Usage
7
- -----
8
-
9
- htmlbeautifier file1 [file2 ...]
10
-
11
- or
12
-
13
- htmlbeautifier < input > output
4
+ A normaliser/beautifier for HTML that also understands embedded Ruby.
5
+ Ideal for tidying up Rails templates.
14
6
 
15
7
  What it does
16
8
  ------------
@@ -23,5 +15,36 @@ What it does
23
15
  * Indents after block-opening embedded Ruby (if, do etc.)
24
16
  * Outdents before closing Ruby blocks
25
17
  * Outdents elsif and then indents again
26
- * Indents the left-hand margin of JavaScript and CSS blocks to match the indentation level of the code
18
+ * Indents the left-hand margin of JavaScript and CSS blocks to match the
19
+ indentation level of the code
20
+
21
+ Usage
22
+ -----
23
+
24
+ ### From the command line
25
+
26
+ To update files in-place:
27
+
28
+ ``` sh
29
+ $ htmlbeautifier file1 [file2 ...]
30
+ ```
31
+
32
+ or to operate on standard input and output:
33
+
34
+ ``` sh
35
+ $ htmlbeautifier < input > output
36
+ ```
37
+
38
+ ## In your code
39
+
40
+ ```ruby
41
+ require 'htmlbeautifier'
42
+
43
+ beautiful = HtmlBeautifier.beautify(messy)
44
+ ```
45
+
46
+ You can also specify the number of spaces to indent (the default is 2):
27
47
 
48
+ ```ruby
49
+ beautiful = HtmlBeautifier.beautify(messy, tab_stops: 4)
50
+ ```
data/Rakefile CHANGED
@@ -1,9 +1,9 @@
1
- require 'rake/testtask'
1
+ require "rspec/core/rake_task"
2
2
 
3
- Rake::TestTask.new(:test) do |t|
4
- t.libs << 'test'
5
- t.pattern = 'test/test_*.rb'
6
- t.verbose = true
3
+ desc "Run the specs."
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ t.verbose = false
7
7
  end
8
8
 
9
- task :default => :test
9
+ task :default => [:spec]
@@ -4,10 +4,7 @@ require 'optparse'
4
4
  require 'fileutils'
5
5
 
6
6
  def beautify(name, input, output, options)
7
- beautifier = HtmlBeautifier::Beautifier.new(output)
8
- beautifier.tab_stops = options[:tab_stops]
9
- beautifier.scan(input)
10
- output << "\n"
7
+ output.puts HtmlBeautifier.beautify(input, options)
11
8
  rescue => e
12
9
  raise "Error parsing #{name}: #{e}"
13
10
  end
@@ -18,6 +15,9 @@ parser = OptionParser.new do |opts|
18
15
  opts.on("-t", "--tab-stops NUMBER", Integer, "Set number of tab stops") do |num|
19
16
  options[:tab_stops] = num
20
17
  end
18
+ opts.on("-e", "--stop-on-errors", "Stop with an error on problems in source") do |num|
19
+ options[:stop_on_errors] = num
20
+ end
21
21
  opts.on("-h", "--help", "Display this help message and exit") do
22
22
  puts opts
23
23
  exit
@@ -1,2 +1,20 @@
1
- require 'htmlbeautifier/beautifier'
1
+ require 'htmlbeautifier/builder'
2
+ require 'htmlbeautifier/html_parser'
2
3
  require 'htmlbeautifier/version'
4
+
5
+ module HtmlBeautifier
6
+
7
+ # Returns a beautified HTML/HTML+ERB document as a String.
8
+ # html must be an object that responds to +#to_s+.
9
+ #
10
+ # Available options are:
11
+ # tab_stops - an integer for the number of spaces to indent, default 2
12
+ # stop_on_errors - raise an exception on a badly-formed document. Default
13
+ # is false, i.e. continue to process the rest of the document.
14
+ #
15
+ def self.beautify(html, options = {})
16
+ ''.tap { |output|
17
+ HtmlParser.new.scan html.to_s, Builder.new(output, options)
18
+ }
19
+ end
20
+ end
@@ -12,22 +12,36 @@ module HtmlBeautifier
12
12
  }x
13
13
  ELEMENT_CONTENT = %r{ (?:[^<>]|<%.*?%>)* }mx
14
14
 
15
- def initialize(output, tab_stops)
15
+ DEFAULT_OPTIONS = {
16
+ tab_stops: 2,
17
+ stop_on_errors: false
18
+ }
19
+
20
+ def initialize(output, options = {})
21
+ options = DEFAULT_OPTIONS.merge(options)
16
22
  @level = 0
17
23
  @new_line = false
18
- @tab = ' ' * tab_stops
24
+ @tab = ' ' * options[:tab_stops]
25
+ @stop_on_errors = options[:stop_on_errors]
19
26
  @output = output
20
27
  @empty = true
21
28
  @ie_cc_levels = []
22
29
  end
23
30
 
31
+ private
32
+
33
+ def error(text)
34
+ return unless @stop_on_errors
35
+ raise RuntimeError, text
36
+ end
37
+
24
38
  def indent
25
39
  @level += 1
26
40
  end
27
41
 
28
42
  def outdent
29
- @level -= 1
30
- raise "Outdented too far" if @level < 0
43
+ error "Extraneous closing tag" if @level == 0
44
+ @level = [@level - 1, 0].max
31
45
  end
32
46
 
33
47
  def emit(s)
@@ -107,8 +121,11 @@ module HtmlBeautifier
107
121
  end
108
122
 
109
123
  def close_ie_cc(e)
110
- raise "Unclosed conditional comment" if @ie_cc_levels.empty?
111
- @level = @ie_cc_levels.pop
124
+ if @ie_cc_levels.empty?
125
+ error "Unclosed conditional comment"
126
+ else
127
+ @level = @ie_cc_levels.pop
128
+ end
112
129
  emit e
113
130
  end
114
131
 
@@ -1,8 +1,8 @@
1
1
  module HtmlBeautifier #:nodoc:
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0
3
+ MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 12
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlbeautifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-30 00:00:00.000000000 Z
11
+ date: 2015-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
27
41
  description: A normaliser/beautifier for HTML that also understands embedded Ruby.
28
42
  email: pbattley@gmail.com
29
43
  executables:
@@ -35,16 +49,10 @@ files:
35
49
  - Rakefile
36
50
  - bin/htmlbeautifier
37
51
  - lib/htmlbeautifier.rb
38
- - lib/htmlbeautifier/beautifier.rb
39
52
  - lib/htmlbeautifier/builder.rb
40
53
  - lib/htmlbeautifier/html_parser.rb
41
54
  - lib/htmlbeautifier/parser.rb
42
55
  - lib/htmlbeautifier/version.rb
43
- - test/test_executable.rb
44
- - test/test_helper.rb
45
- - test/test_html_beautifier_integration.rb
46
- - test/test_html_beautifier_regression.rb
47
- - test/test_parser.rb
48
56
  homepage: http://github.com/threedaymonk/htmlbeautifier
49
57
  licenses:
50
58
  - MIT
@@ -57,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
65
  requirements:
58
66
  - - ">="
59
67
  - !ruby/object:Gem::Version
60
- version: '0'
68
+ version: 1.9.2
61
69
  required_rubygems_version: !ruby/object:Gem::Requirement
62
70
  requirements:
63
71
  - - ">="
@@ -1,23 +0,0 @@
1
- require 'htmlbeautifier/html_parser'
2
- require 'htmlbeautifier/builder'
3
-
4
- module HtmlBeautifier
5
- class Beautifier
6
- attr_accessor :tab_stops
7
-
8
- # Create a new Beautifier.
9
- # output should be an object that responds to <<
10
- # i.e. a String or an IO
11
- def initialize(output)
12
- self.tab_stops = 2
13
- @output = output
14
- end
15
-
16
- # Process an HTML/HTML+ERB document
17
- # html should be a string
18
- def scan(html)
19
- @parser = HtmlParser.new
20
- @parser.scan html.strip, Builder.new(@output, self.tab_stops)
21
- end
22
- end
23
- end
@@ -1,66 +0,0 @@
1
- require 'test_helper'
2
- require 'shellwords'
3
- require 'fileutils'
4
-
5
- class TestExecutable < Test::Unit::TestCase
6
- def path_to(*partial)
7
- File.join(File.expand_path('../..', __FILE__), *partial)
8
- end
9
-
10
- def command
11
- 'ruby -I%s %s' % [
12
- escape(path_to('lib')),
13
- escape(path_to('bin', 'htmlbeautifier'))
14
- ]
15
- end
16
-
17
- def escape(s)
18
- Shellwords.escape(s)
19
- end
20
-
21
- def test_should_beautify_a_file_in_place
22
- FileUtils.mkdir_p path_to('tmp')
23
- input = "<p>\nfoo\n</p>"
24
- expected = "<p>\n foo\n</p>\n"
25
- path = path_to('tmp', 'in-place.html')
26
- File.open(path, 'w') do |f|
27
- f.write input
28
- end
29
-
30
- system '%s %s' % [command, escape(path)]
31
-
32
- output = File.read(path)
33
- assert_equal expected, output
34
- end
35
-
36
- def test_should_beautify_a_file_from_stdin_to_stdout
37
- FileUtils.mkdir_p path_to('tmp')
38
- input = "<p>\nfoo\n</p>"
39
- expected = "<p>\n foo\n</p>\n"
40
- in_path = path_to('tmp', 'input.html')
41
- out_path = path_to('tmp', 'output.html')
42
- File.open(in_path, 'w') do |f|
43
- f.write input
44
- end
45
-
46
- system '%s < %s > %s' % [command, escape(in_path), escape(out_path)]
47
-
48
- output = File.read(out_path)
49
- assert_equal expected, output
50
- end
51
-
52
- def test_should_allow_configurable_number_of_tab_stops
53
- FileUtils.mkdir_p path_to('tmp')
54
- input = "<p>\nfoo\n</p>"
55
- expected = "<p>\n foo\n</p>\n"
56
- path = path_to('tmp', 'in-place.html')
57
- File.open(path, 'w') do |f|
58
- f.write input
59
- end
60
-
61
- system '%s --tab-stops=3 %s' % [command, escape(path)]
62
-
63
- output = File.read(path)
64
- assert_equal expected, output
65
- end
66
- end
@@ -1,19 +0,0 @@
1
- require 'test/unit'
2
- lib = File.expand_path('../../lib', __FILE__)
3
- $:.unshift lib unless $:.include?(lib)
4
-
5
- module HtmlBeautifierTestUtilities
6
- def code(str)
7
- str = str.gsub(/\A\n|\n\s*\Z/, '')
8
- indentation = str[/\A +/]
9
- lines = str.split(/\n/)
10
- lines.map{ |line| line.sub(/^#{indentation}/, '') }.join("\n")
11
- end
12
-
13
- def assert_beautifies(expected, source)
14
- actual = ''
15
- beautifier = HtmlBeautifier::Beautifier.new(actual)
16
- beautifier.scan(source)
17
- assert expected == actual, "Expected:\n#{expected}\nbut was:\n#{actual}"
18
- end
19
- end
@@ -1,117 +0,0 @@
1
- require 'test_helper'
2
- require 'htmlbeautifier/beautifier'
3
-
4
- class TestHtmlBeautifierIntegration < Test::Unit::TestCase
5
- include HtmlBeautifierTestUtilities
6
-
7
- def test_should_correctly_indent_mixed_document
8
- source = code(%q(
9
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
10
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
11
- <head>
12
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
13
- <script src="/javascripts/prototype.js" type="text/javascript"></script>
14
- <link rel="stylesheet" type="text/css" href="/stylesheets/screen.css" media="screen"/>
15
- <!--[if IE 6]>
16
- <link rel="stylesheet" href="/stylesheets/screen_ie6.css" type="text/css" />
17
- <![endif]-->
18
- <title>Title Goes Here</title>
19
- <script type="text/javascript" charset="utf-8">
20
- doSomething();
21
- </script>
22
- </head>
23
- <body>
24
- <div id="something">
25
- <h1>
26
- Heading 1
27
- </h1>
28
- </div>
29
- <div id="somethingElse"><p>Lorem Ipsum</p>
30
- <% if @x %>
31
- <% @ys.each do |y| %>
32
- <p>
33
- <%= h y %>
34
- </p>
35
- <% end %>
36
- <% elsif @z %>
37
- <hr />
38
- <% end %>
39
- </div>
40
- <table>
41
- <colgroup>
42
- <col style="width: 50%;">
43
- <col style="width: 50%;">
44
- </colgroup>
45
- <tbody>
46
- <tr><td>First column</td></tr><tr>
47
- <td>Second column</td></tr>
48
- </tbody>
49
- </table>
50
- </body>
51
- </html>
52
- ))
53
- expected = code(%q(
54
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
55
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
56
- <head>
57
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
58
- <script src="/javascripts/prototype.js" type="text/javascript"></script>
59
- <link rel="stylesheet" type="text/css" href="/stylesheets/screen.css" media="screen"/>
60
- <!--[if IE 6]>
61
- <link rel="stylesheet" href="/stylesheets/screen_ie6.css" type="text/css" />
62
- <![endif]-->
63
- <title>Title Goes Here</title>
64
- <script type="text/javascript" charset="utf-8">
65
- doSomething();
66
- </script>
67
- </head>
68
- <body>
69
- <div id="something">
70
- <h1>
71
- Heading 1
72
- </h1>
73
- </div>
74
- <div id="somethingElse">
75
- <p>Lorem Ipsum</p>
76
- <% if @x %>
77
- <% @ys.each do |y| %>
78
- <p>
79
- <%= h y %>
80
- </p>
81
- <% end %>
82
- <% elsif @z %>
83
- <hr />
84
- <% end %>
85
- </div>
86
- <table>
87
- <colgroup>
88
- <col style="width: 50%;">
89
- <col style="width: 50%;">
90
- </colgroup>
91
- <tbody>
92
- <tr>
93
- <td>First column</td>
94
- </tr>
95
- <tr>
96
- <td>Second column</td>
97
- </tr>
98
- </tbody>
99
- </table>
100
- </body>
101
- </html>
102
- ))
103
- assert_beautifies expected, source
104
- end
105
-
106
- def test_should_raise_an_error_with_the_source_line_of_an_illegal_outdent
107
- begin
108
- HtmlBeautifier::Beautifier.new('').scan("<html>\n</html>\n</html>")
109
- rescue Exception => e
110
- @exception = e
111
- end
112
- assert_equal RuntimeError, @exception.class
113
- assert_match /outdent/i, @exception.message
114
- assert_match /line 3/i, @exception.message
115
- end
116
-
117
- end
@@ -1,345 +0,0 @@
1
- require 'test_helper'
2
- require 'htmlbeautifier/beautifier'
3
-
4
- class HtmlBeautifierRegressionTest < Test::Unit::TestCase
5
-
6
- include HtmlBeautifierTestUtilities
7
-
8
- def test_should_ignore_html_fragments_in_embedded_code
9
- source = code(%q(
10
- <div>
11
- <%= a[:b].gsub("\n","<br />\n") %>
12
- </div>
13
- ))
14
- expected = code(%q(
15
- <div>
16
- <%= a[:b].gsub("\n","<br />\n") %>
17
- </div>
18
- ))
19
- assert_beautifies expected, source
20
- end
21
-
22
- def test_should_indent_scripts
23
- source = code(%q(
24
- <script>
25
- function(f) {
26
- g();
27
- return 42;
28
- }
29
- </script>
30
- ))
31
- expected = code(%q(
32
- <script>
33
- function(f) {
34
- g();
35
- return 42;
36
- }
37
- </script>
38
- ))
39
- assert_beautifies expected, source
40
- end
41
-
42
- def test_should_remove_blank_lines_around_scripts
43
- source = code(%q(
44
- <script>
45
-
46
- f();
47
-
48
- </script>
49
- ))
50
- expected = code(%q(
51
- <script>
52
- f();
53
- </script>
54
- ))
55
- assert_beautifies expected, source
56
- end
57
-
58
- def test_should_remove_trailing_space_from_script_lines
59
- source = %Q(<script>\n f(); \n</script>)
60
- expected = %Q(<script>\n f();\n</script>)
61
- assert_beautifies expected, source
62
- end
63
-
64
- def test_should_skip_over_empty_scripts
65
- source = %q(<script src="/foo.js" type="text/javascript" charset="utf-8"></script>)
66
- expected = source
67
- assert_beautifies expected, source
68
- end
69
-
70
- def test_should_ignore_case_of_script
71
- source = code(%q(
72
- <SCRIPT>
73
-
74
- // code
75
-
76
- </SCRIPT>
77
- ))
78
- expected = code(%q(
79
- <SCRIPT>
80
- // code
81
- </SCRIPT>
82
- ))
83
- assert_beautifies expected, source
84
- end
85
-
86
- def test_should_indent_styles
87
- source = code(%q(
88
- <style>
89
- .foo{ margin: 0; }
90
- .bar{
91
- padding: 0;
92
- margin: 0;
93
- }
94
- </style>
95
- ))
96
- expected = code(%q(
97
- <style>
98
- .foo{ margin: 0; }
99
- .bar{
100
- padding: 0;
101
- margin: 0;
102
- }
103
- </style>
104
- ))
105
- assert_beautifies expected, source
106
- end
107
-
108
- def test_should_remove_blank_lines_around_styles
109
- source = code(%q(
110
- <style>
111
-
112
- .foo{ margin: 0; }
113
-
114
- </style>
115
- ))
116
- expected = code(%q(
117
- <style>
118
- .foo{ margin: 0; }
119
- </style>
120
- ))
121
- assert_beautifies expected, source
122
- end
123
-
124
- def test_should_remove_trailing_space_from_style_lines
125
- source = %Q(<style>\n .foo{ margin: 0; } \n</style>)
126
- expected = %Q(<style>\n .foo{ margin: 0; }\n</style>)
127
- assert_beautifies expected, source
128
- end
129
-
130
- def test_should_ignore_case_of_style
131
- source = code(%q(
132
- <STYLE>
133
-
134
- .foo{ margin: 0; }
135
-
136
- </STYLE>
137
- ))
138
- expected = code(%q(
139
- <STYLE>
140
- .foo{ margin: 0; }
141
- </STYLE>
142
- ))
143
- assert_beautifies expected, source
144
- end
145
-
146
- def test_should_indent_divs_containing_standalone_elements
147
- source = code(%q(
148
- <div>
149
- <div>
150
- <img src="foo" alt="" />
151
- </div>
152
- <div>
153
- <img src="foo" alt="" />
154
- </div>
155
- </div>
156
- ))
157
- expected = source
158
- assert_beautifies expected, source
159
- end
160
-
161
- def test_should_not_break_line_on_embedded_code_within_script_opening_element
162
- source = '<script src="<%= path %>" type="text/javascript"></script>'
163
- expected = source
164
- assert_beautifies expected, source
165
- end
166
-
167
- def test_should_not_break_line_on_embedded_code_within_normal_element
168
- source = '<img src="<%= path %>" alt="foo" />'
169
- expected = source
170
- assert_beautifies expected, source
171
- end
172
-
173
- def test_should_indent_inside_IE_conditional_comments
174
- source = code(%q(
175
- <!--[if IE 6]>
176
- <link rel="stylesheet" href="/stylesheets/ie6.css" type="text/css" />
177
- <![endif]-->
178
- <!--[if IE 5]>
179
- <link rel="stylesheet" href="/stylesheets/ie5.css" type="text/css" />
180
- <![endif]-->
181
- ))
182
- expected = code(%q(
183
- <!--[if IE 6]>
184
- <link rel="stylesheet" href="/stylesheets/ie6.css" type="text/css" />
185
- <![endif]-->
186
- <!--[if IE 5]>
187
- <link rel="stylesheet" href="/stylesheets/ie5.css" type="text/css" />
188
- <![endif]-->
189
- ))
190
- assert_beautifies expected, source
191
- end
192
-
193
- def test_should_outdent_else
194
- source = code(%q(
195
- <% if @x %>
196
- Foo
197
- <% else %>
198
- Bar
199
- <% end %>
200
- ))
201
- expected = code(%q(
202
- <% if @x %>
203
- Foo
204
- <% else %>
205
- Bar
206
- <% end %>
207
- ))
208
- assert_beautifies expected, source
209
- end
210
-
211
- def test_should_indent_with_hyphenated_erb_tags
212
- source = code(%q(
213
- <%- if @x -%>
214
- <%- @ys.each do |y| -%>
215
- <p>Foo</p>
216
- <%- end -%>
217
- <%- elsif @z -%>
218
- <hr />
219
- <%- end -%>
220
- ))
221
- expected = code(%q(
222
- <%- if @x -%>
223
- <%- @ys.each do |y| -%>
224
- <p>Foo</p>
225
- <%- end -%>
226
- <%- elsif @z -%>
227
- <hr />
228
- <%- end -%>
229
- ))
230
- assert_beautifies expected, source
231
- end
232
-
233
- def test_should_not_indent_comments
234
- source = code(%q(
235
- <!-- This is a comment -->
236
- <!-- So is this -->
237
- ))
238
- assert_beautifies source, source
239
- end
240
-
241
- def test_should_not_indent_conditional_comments
242
- source = code(%q(
243
- <!--[if lt IE 7]><html lang="en-us" class="ie6"><![endif]-->
244
- <!--[if IE 7]><html lang="en-us" class="ie7"><![endif]-->
245
- <!--[if IE 8]><html lang="en-us" class="ie8"><![endif]-->
246
- <!--[if gt IE 8]><!--><html lang="en-us"><!--<![endif]-->
247
- <body>
248
- </body>
249
- </html>
250
- ))
251
- assert_beautifies source, source
252
- end
253
-
254
- def test_should_not_indent_doctype
255
- source = code(%q(
256
- <!DOCTYPE html>
257
- <html>
258
- </html>
259
- ))
260
- assert_beautifies source, source
261
- end
262
-
263
- def test_should_not_indent_html_void_elements
264
- source = code(%q(
265
- <meta>
266
- <input id="id">
267
- <br>
268
- ))
269
- assert_beautifies source, source
270
- end
271
-
272
- def test_should_ignore_case_of_void_elements
273
- source = code(%q(
274
- <META>
275
- <INPUT id="id">
276
- <BR>
277
- ))
278
- assert_beautifies source, source
279
- end
280
-
281
- def test_should_not_parse_colgroup_as_standalone
282
- source = code(%q(
283
- <colgroup>
284
- <col style="width: 50%;">
285
- </colgroup>
286
- ))
287
- assert_beautifies source, source
288
- end
289
-
290
- def test_should_not_modify_pre_content
291
- source = code(%q(
292
- <div>
293
- <pre> Preformatted text
294
-
295
- should <em>not be </em>
296
- modified,
297
- ever!
298
-
299
- </pre>
300
- </div>
301
- ))
302
- assert_beautifies source, source
303
- end
304
-
305
- def test_should_add_newline_after_block_elements
306
- source = code(%q(
307
- <section><h1>Title</h1><p>Lorem <em>ipsum</em></p>
308
- <ol>
309
- <li>First</li><li>Second</li></ol>
310
- </section>
311
- ))
312
- expected = code(%(
313
- <section>
314
- <h1>Title</h1>
315
- <p>Lorem <em>ipsum</em></p>
316
- <ol>
317
- <li>First</li>
318
- <li>Second</li>
319
- </ol>
320
- </section>
321
- ))
322
- assert_beautifies expected, source
323
- end
324
-
325
- def test_should_add_newlines_around_pre_element
326
- source = %(<section><pre>puts "Allons-y!"</pre></section>)
327
- expected = code(%(
328
- <section>
329
- <pre>puts "Allons-y!"</pre>
330
- </section>
331
- ))
332
- assert_beautifies expected, source
333
- end
334
-
335
- def test_should_add_newline_after_br_element
336
- source = %(<p>Lorem ipsum<br>dolor sit<br />amet,<br/>consectetur.</p>)
337
- expected = code(%(
338
- <p>Lorem ipsum<br>
339
- dolor sit<br />
340
- amet,<br/>
341
- consectetur.</p>
342
- ))
343
- assert_beautifies expected, source
344
- end
345
- end
@@ -1,82 +0,0 @@
1
- require 'test_helper'
2
- require 'htmlbeautifier/parser'
3
-
4
- class TestParser < Test::Unit::TestCase
5
-
6
- class Receiver
7
- attr_reader :sequence
8
-
9
- def initialize
10
- @sequence = []
11
- end
12
-
13
- def method_missing(method, *params)
14
- @sequence << [method, params]
15
- end
16
- end
17
-
18
- def setup
19
- # HtmlBeautifier::Parser.debug_block{ |match, method| puts("#{match.inspect} => #{method}") }
20
- end
21
-
22
- def test_should_dispatch_matching_sequence
23
- receiver = Receiver.new
24
- parser = HtmlBeautifier::Parser.new { |p|
25
- p.map %r{foo}, :foo
26
- p.map %r{bar\s*}, :bar
27
- p.map %r{\s+}, :whitespace
28
- }
29
- parser.scan('foo bar ', receiver)
30
- assert_equal [[:foo, ['foo']], [:whitespace, [' ']], [:bar, ['bar ']]], receiver.sequence
31
- end
32
-
33
- def test_should_send_parenthesized_components_as_separate_parameters
34
- receiver = Receiver.new
35
- parser = HtmlBeautifier::Parser.new { |p|
36
- p.map %r{(foo)\((.*?)\)}, :foo
37
- }
38
- parser.scan('foo(bar)', receiver)
39
- assert_equal [[:foo, ['foo', 'bar']]], receiver.sequence
40
- end
41
-
42
- class SourceTrackingReceiver < Receiver
43
- attr_reader :sources_so_far
44
- attr_reader :source_line_numbers
45
-
46
- def initialize(parser)
47
- @sources_so_far = []
48
- @source_line_numbers = []
49
- @parser = parser
50
- super()
51
- end
52
-
53
- def append_new_source_so_far(*ignored)
54
- @sources_so_far << @parser.source_so_far
55
- end
56
-
57
- def append_new_source_line_number(*ignored)
58
- @source_line_numbers << @parser.source_line_number
59
- end
60
- end
61
-
62
- def test_should_give_source_so_far
63
- parser = HtmlBeautifier::Parser.new { |p|
64
- p.map %r{(M+)}m, :append_new_source_so_far
65
- p.map %r{([\s\n]+)}m, :space_or_newline
66
- }
67
- receiver = SourceTrackingReceiver.new(parser)
68
- parser.scan("M MM MMM", receiver)
69
- assert_equal ['M', 'M MM', 'M MM MMM'], receiver.sources_so_far
70
- end
71
-
72
- def test_should_give_source_line_number
73
- parser = HtmlBeautifier::Parser.new{ |p|
74
- p.map %r{(M+)}m, :append_new_source_line_number
75
- p.map %r{([\s\n]+)}m, :space_or_newline
76
- }
77
- receiver = SourceTrackingReceiver.new(parser)
78
- parser.scan("M \n\nMM\nMMM", receiver)
79
- assert_equal [1, 3, 4], receiver.source_line_numbers
80
- end
81
-
82
- end