1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
# encoding: utf-8
# frozen_string_literal: true
require "helper"
class TestHtml5Serialize < Nokogiri::TestCase
# https://github.com/web-platform-tests/wpt/blob/master/html/syntax/serializing-html-fragments/initial-linefeed-pre.html
def initial_linefeed_pre
@initial_linefeed_pre ||= begin
html = <<~EOF.gsub(/^ /, "").freeze
<!DOCTYPE html>
<div id="outer">
<div id="inner">
<pre id="pre1">
x</pre>
<pre id="pre2">
x</pre>
<textarea id="textarea1">
x</textarea>
<textarea id="textarea2">
x</textarea>
<listing id="listing1">
x</listing>
<listing id="listing2">
x</listing>
</div>
</div>
EOF
Nokogiri::HTML5(html)
end
@initial_linefeed_pre
end
def test_initial_linefeed_pre_outer
expected = %{\n<div id="inner">\n<pre id="pre1">x</pre>\n<pre id="pre2">\nx</pre>\n<textarea id="textarea1">x</textarea>\n<textarea id="textarea2">\nx</textarea>\n<listing id="listing1">x</listing>\n<listing id="listing2">\nx</listing>\n</div>\n}
outer = initial_linefeed_pre.xpath('//div[@id="outer"]')[0]
refute_nil(outer)
assert_equal(expected, outer.inner_html)
end
def test_initial_linefeed_pre_inner
expected = %{\n<pre id="pre1">x</pre>\n<pre id="pre2">\nx</pre>\n<textarea id="textarea1">x</textarea>\n<textarea id="textarea2">\nx</textarea>\n<listing id="listing1">x</listing>\n<listing id="listing2">\nx</listing>\n}
inner = initial_linefeed_pre.at('//div[@id="inner"]')
refute_nil(inner)
assert_equal(expected, inner.inner_html)
end
["pre", "textarea", "listing"].each do |tag|
define_method("test_initial_linefeed_#{tag}1".to_sym) do
elem = initial_linefeed_pre.at("//*[@id=\"#{tag}1\"]")
refute_nil elem
assert_equal "x", elem.inner_html
end
define_method("test_initial_linefeed_#{tag}2".to_sym) do
elem = initial_linefeed_pre.at("//*[@id=\"#{tag}2\"]")
refute_nil elem
assert_equal "\nx", elem.inner_html
end
end
# https://github.com/web-platform-tests/wpt/blob/master/html/syntax/serializing-html-fragments/outerHTML.html
ELEMENTS_WITH_END_TAG = [
"a",
"abbr",
"address",
"article",
"aside",
"audio",
"b",
"bdi",
"bdo",
"blockquote",
"body",
"button",
"canvas",
"caption",
"cite",
"code",
"colgroup",
"command",
"datalist",
"dd",
"del",
"details",
"dfn",
"dialog",
"div",
"dl",
"dt",
"em",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hgroup",
"html",
"i",
"iframe",
"ins",
"kbd",
"label",
"legend",
"li",
"map",
"mark",
"menu",
"meter",
"nav",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"pre",
"progress",
"q",
"rp",
"rt",
"ruby",
"s",
"samp",
"script",
"section",
"select",
"small",
"span",
"strong",
"style",
"sub",
"summary",
"sup",
"table",
"tbody",
"td",
"textarea",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"u",
"ul",
"var",
"video",
"data",
].freeze
ELEMENTS_WITHOUT_END_TAG = [
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]
ELEMENTS_WITH_END_TAG.each do |tag|
define_method("test_outer_html_#{tag}".to_sym) do
doc = Nokogiri::HTML5::Document.new
child = Nokogiri::XML::Element.new(tag, doc)
assert_equal "<#{tag}></#{tag}>", child.serialize(save_with: 0)
end
end
ELEMENTS_WITHOUT_END_TAG.each do |tag|
define_method("test_outer_html_#{tag}".to_sym) do
doc = Nokogiri::HTML5::Document.new
child = Nokogiri::XML::Element.new(tag, doc)
assert_equal "<#{tag}>", child.serialize(save_with: 0)
end
end
# https://github.com/web-platform-tests/wpt/blob/master/html/syntax/serializing-html-fragments/serializing.html
def serializing_test_data
@serializing_test_data ||= begin
html = <<~EOF.gsub(/ /, "")
<!DOCTYPE html>
<div id="test" style="display:none">
<span></span>
<span><a></a></span>
<span><a b=c></a></span>
<span><a b='c'></a></span>
<span><a b='&'></a></span>
<span><a b=' '></a></span>
<span><a b='"'></a></span>
<span><a b="<"></a></span>
<span><a b=">"></a></span>
<span><svg xlink:href="a"></svg></span>
<span><svg xmlns:svg="test"></svg></span>
<span>a</span>
<span>&</span>
<span> </span>
<span><</span>
<span>></span>
<span>"</span>
<span><style><&></style></span>
<span><script type="test"><&></script></span>
<script type="test"><&></script>
<span><xmp><&></xmp></span>
<span><iframe><&></iframe></span>
<span><noembed><&></noembed></span>
<span><noframes><&></noframes></span>
<span><noscript><&></noscript></span>
<span><!--data--></span>
<span><a><b><c></c></b><d>e</d><f><g>h</g></f></a></span>
<span b=c></span>
</div>
EOF
Nokogiri::HTML5(html).xpath("/html/body/div/*")
end
@serializing_test_data
end
EXPECTED = [
["", "<span></span>"],
["<a></a>", "<span><a></a></span>"],
["<a b=\"c\"></a>", "<span><a b=\"c\"></a></span>"],
["<a b=\"c\"></a>", "<span><a b=\"c\"></a></span>"],
["<a b=\"&\"></a>", "<span><a b=\"&\"></a></span>"],
["<a b=\" \"></a>", "<span><a b=\" \"></a></span>"],
["<a b=\""\"></a>", "<span><a b=\""\"></a></span>"],
["<a b=\"<\"></a>", "<span><a b=\"<\"></a></span>"],
["<a b=\">\"></a>", "<span><a b=\">\"></a></span>"],
["<svg xlink:href=\"a\"></svg>", "<span><svg xlink:href=\"a\"></svg></span>"],
["<svg xmlns:svg=\"test\"></svg>", "<span><svg xmlns:svg=\"test\"></svg></span>"],
["a", "<span>a</span>"],
["&", "<span>&</span>"],
[" ", "<span> </span>"],
["<", "<span><</span>"],
[">", "<span>></span>"],
["\"", "<span>\"</span>"],
["<style><&></style>", "<span><style><&></style></span>"],
["<script type=\"test\"><&><\/script>", "<span><script type=\"test\"><&><\/script></span>"],
["<&>", "<script type=\"test\"><&><\/script>"],
["<xmp><&></xmp>", "<span><xmp><&></xmp></span>"],
["<iframe><&></iframe>", "<span><iframe><&></iframe></span>"],
["<noembed><&></noembed>", "<span><noembed><&></noembed></span>"],
["<noframes><&></noframes>", "<span><noframes><&></noframes></span>"],
["<noscript><&></noscript>", "<span><noscript><&></noscript></span>"],
["<!--data-->", "<span><!--data--></span>"],
["<a><b><c></c></b><d>e</d><f><g>h</g></f></a>", "<span><a><b><c></c></b><d>e</d><f><g>h</g></f></a></span>"],
["", "<span b=\"c\"></span>"],
].freeze
DOM_TESTS = [
["Attribute in the XML namespace",
lambda do
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
svg = Nokogiri::XML::Element.new("svg", doc)
span.add_child(svg)
svg.add_namespace("xml", "http://www.w3.org/XML/1998/namespace")
svg["xml:foo"] = "test"
span
end,
'<svg xml:foo="test"></svg>',
'<span><svg xml:foo="test"></svg></span>',],
["Attribute in the XML namespace with the prefix not set to xml:",
lambda do
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
svg = Nokogiri::XML::Element.new("svg", doc)
span.add_child(svg)
svg["abc:foo"] = "test"
ns = svg.add_namespace("xml", "http://www.w3.org/XML/1998/namespace")
svg.attribute("abc:foo").namespace = ns
span
end,
'<svg xml:foo="test"></svg>',
'<span><svg xml:foo="test"></svg></span>',],
["Non-'xmlns' attribute in the xmlns namespace",
lambda do
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
svg = Nokogiri::XML::Element.new("svg", doc)
span.add_child(svg)
svg.add_namespace("xmlns", "http://www.w3.org/2000/xmlns/")
svg["xmlns:foo"] = "test"
span
end,
'<svg xmlns:foo="test"></svg>',
'<span><svg xmlns:foo="test"></svg></span>',],
["'xmlns' attribute in the xmlns namespace",
lambda do
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
svg = Nokogiri::XML::Element.new("svg", doc)
span.add_child(svg)
svg.add_namespace("xmlns", "http://www.w3.org/2000/xmlns/")
svg["xmlns"] = "test"
span
end,
'<svg xmlns="test"></svg>',
'<span><svg xmlns="test"></svg></span>',],
["Attribute in non-standard namespace",
lambda do
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
svg = Nokogiri::XML::Element.new("svg", doc)
span.add_child(svg)
svg.add_namespace("abc", "fake_ns")
svg["abc:def"] = "test"
span
end,
'<svg abc:def="test"></svg>',
'<span><svg abc:def="test"></svg></span>',],
["<span> starting with U+000A",
lambda do
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
text = Nokogiri::XML::Text.new("\x0A", doc)
span.add_child(text)
span
end,
"\x0A",
"<span>\x0A</span>",],
# TODO: Processing instructions
]
TEXT_ELEMENTS = ["pre", "textarea", "listing"]
TEXT_TESTS = [
["<%text> context starting with U+000A",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
elem = Nokogiri::XML::Element.new(tag, doc)
text = Nokogiri::XML::Text.new("\x0A", doc)
elem.add_child(text)
elem
end,
"\x0A",
"<%text>\x0A</%text>",],
["<%text> context not starting with U+000A",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
elem = Nokogiri::XML::Element.new(tag, doc)
text = Nokogiri::XML::Text.new("a\x0A", doc)
elem.add_child(text)
elem
end,
"a\x0A",
"<%text>a\x0A</%text>",],
["<%text> non-context starting with U+000A",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
elem = Nokogiri::XML::Element.new(tag, doc)
span = Nokogiri::XML::Element.new("span", doc)
text = Nokogiri::XML::Text.new("\x0A", doc)
elem.add_child(text)
span.add_child(elem)
span
end,
"<%text>\x0A</%text>",
"<span><%text>\x0A</%text></span>",],
["<%text> non-context not starting with U+000A",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
elem = Nokogiri::XML::Element.new(tag, doc)
span = Nokogiri::XML::Element.new("span", doc)
text = Nokogiri::XML::Text.new("a\x0A", doc)
elem.add_child(text)
span.add_child(elem)
span
end,
"<%text>a\x0A</%text>",
"<span><%text>a\x0A</%text></span>",],
]
VOID_ELEMENTS = [
"area", "base", "basefont", "bgsound", "br", "col", "embed",
"frame", "hr", "img", "input", "keygen", "link",
"meta", "param", "source", "track", "wbr",
]
VOID_TESTS = [
["Void context node",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
Nokogiri::XML::Element.new(tag, doc)
end,
"",
"<%void>",],
["void as first child with following siblings",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
span.add_child(Nokogiri::XML::Element.new(tag, doc))
span.add_child(Nokogiri::XML::Element.new("a", doc))
.add_child(Nokogiri::XML::Text.new("test", doc))
span.add_child(Nokogiri::XML::Element.new("b", doc))
span
end,
"<%void><a>test</a><b></b>",
"<span><%void><a>test</a><b></b></span>",],
["void as second child with following siblings",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
span.add_child(Nokogiri::XML::Element.new("a", doc))
.add_child(Nokogiri::XML::Text.new("test", doc))
span.add_child(Nokogiri::XML::Element.new(tag, doc))
span.add_child(Nokogiri::XML::Element.new("b", doc))
span
end,
"<a>test</a><%void><b></b>",
"<span><a>test</a><%void><b></b></span>",],
["void as last child with preceding siblings",
lambda do |tag|
doc = Nokogiri::HTML5::Document.new
span = Nokogiri::XML::Element.new("span", doc)
span.add_child(Nokogiri::XML::Element.new("a", doc))
.add_child(Nokogiri::XML::Text.new("test", doc))
span.add_child(Nokogiri::XML::Element.new("b", doc))
span.add_child(Nokogiri::XML::Element.new(tag, doc))
span
end,
"<a>test</a><b></b><%void>",
"<span><a>test</a><b></b><%void></span>",],
]
# Generate tests
def self.cross_map(a1, a2)
rv = []
a1.each do |a1_elem|
a2.each do |a2_elem|
rv << yield(a1_elem, a2_elem)
end
end
end
EXPECTED.each_with_index do |item, i|
define_method("test_serializing_html_innerHTML_#{i}".to_sym) do
assert_equal item[0], serializing_test_data[i].inner_html
end
define_method("test_serializing_html_outerHTML_#{i}".to_sym) do
assert_equal item[1], serializing_test_data[i].serialize
end
end
DOM_TESTS.each do |test|
define_method("test_serializing_dom_innerHTML_#{test[0]}".to_sym) do
elem = test[1].call
refute_nil elem
assert_equal test[2], elem.inner_html
end
define_method("test_serializing_dom_outerHTML_#{test[0]}".to_sym) do
elem = test[1].call
refute_nil elem
assert_equal test[3], elem.serialize
end
end
cross_map(TEXT_TESTS, TEXT_ELEMENTS) do |test_data, tag|
define_method("test_serializing_text_innerHTML_#{test_data[0].gsub("%text", tag)}".to_sym) do
assert_equal test_data[2].gsub("%text", tag), test_data[1].call(tag).inner_html
end
define_method("test_serialization_text_outerHTML_#{test_data[0].gsub("%text", tag)}".to_sym) do
assert_equal test_data[3].gsub("%text", tag), test_data[1].call(tag).serialize
end
end
cross_map(VOID_TESTS, VOID_ELEMENTS) do |test_data, tag|
define_method("test_serializing_void_innerHTML_#{test_data[0]}_#{tag}".to_sym) do
assert_equal test_data[2].gsub("%void", tag), test_data[1].call(tag).inner_html
end
define_method("test_serialization_void_outerHTML_#{test_data[0]}_#{tag}".to_sym) do
assert_equal test_data[3].gsub("%void", tag), test_data[1].call(tag).serialize
end
end
end if Nokogiri.uses_gumbo?
|