Skip to content

Commit

Permalink
Merge branch 'pr136-block-level'
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Feb 7, 2018
2 parents 6ca00aa + e5d0c7b commit 9b13194
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
Features:

* Support HTML5 `<main>` tag. #133 (Thanks, @MothOnMars!)
* Recognize HTML5 block elements. #136 (Thanks, @MothOnMars!)
* Support SVG `<symbol>` tag. #131 (Thanks, @baopham!)
* Support for whitelisting CSS functions, initially just `calc`. #122/#123 (Thanks, @NikoRoberts!)

Expand Down
87 changes: 81 additions & 6 deletions lib/loofah/elements.rb
Expand Up @@ -2,13 +2,88 @@

module Loofah
module Elements
# Block elements in HTML4
STRICT_BLOCK_LEVEL = Set.new %w[address blockquote center dir div dl
fieldset form h1 h2 h3 h4 h5 h6 hr isindex menu noframes
noscript ol p pre table ul]
STRICT_BLOCK_LEVEL_HTML4 = Set.new %w[
address
blockquote
center
dir
div
dl
fieldset
form
h1
h2
h3
h4
h5
h6
hr
isindex
menu
noframes
noscript
ol
p
pre
table
ul
]

# The following elements may also be considered block-level elements since they may contain block-level elements
LOOSE_BLOCK_LEVEL = Set.new %w[dd dt frameset li tbody td tfoot th thead tr]
# https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
STRICT_BLOCK_LEVEL_HTML5 = Set.new %w[
address
article
aside
blockquote
canvas
dd
div
dl
dt
fieldset
figcaption
figure
footer
form
h1
h2
h3
h4
h5
h6
header
hgroup
hr
li
main
nav
noscript
ol
output
p
pre
section
table
tfoot
ul
video
]

STRICT_BLOCK_LEVEL = STRICT_BLOCK_LEVEL_HTML4 + STRICT_BLOCK_LEVEL_HTML5

# The following elements may also be considered block-level
# elements since they may contain block-level elements
LOOSE_BLOCK_LEVEL = Set.new %w[dd
dt
frameset
li
tbody
td
tfoot
th
thead
tr
]

BLOCK_LEVEL = STRICT_BLOCK_LEVEL + LOOSE_BLOCK_LEVEL
end
Expand Down
14 changes: 12 additions & 2 deletions test/integration/test_html.rb
Expand Up @@ -19,11 +19,16 @@ class IntegrationTestHtml < Loofah::TestCase
end

context "#to_text" do
it "add newlines before and after block elements" do
it "add newlines before and after html4 block elements" do
html = Loofah.fragment "<div>tweedle<h1>beetle</h1>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end

it "add newlines before and after html5 block elements" do
html = Loofah.fragment "<div>tweedle<section>beetle</section>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end

it "remove extraneous whitespace" do
html = Loofah.fragment "<div>tweedle\n\n\t\n\s\nbeetle</div>"
assert_equal "\ntweedle\n\nbeetle\n", html.to_text
Expand All @@ -47,11 +52,16 @@ class IntegrationTestHtml < Loofah::TestCase
end

context "#to_text" do
it "add newlines before and after block elements" do
it "add newlines before and after html4 block elements" do
html = Loofah.document "<div>tweedle<h1>beetle</h1>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end

it "add newlines before and after html5 block elements" do
html = Loofah.document "<div>tweedle<section>beetle</section>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end

it "remove extraneous whitespace" do
html = Loofah.document "<div>tweedle\n\n\t\n\s\nbeetle</div>"
assert_equal "\ntweedle\n\nbeetle\n", html.to_text
Expand Down

0 comments on commit 9b13194

Please sign in to comment.