Add tests for parsing sections of HTML defined as containing CDATA.
David Dorward [Wed, 21 Dec 2011 18:07:54 +0000 (18:07 +0000)]
lib/HTML/Zoom.pm
t/parse_cdata.t [new file with mode: 0644]

index df4d3a0..053f458 100644 (file)
@@ -778,6 +778,8 @@ John Napiorkowski
 
 Robert Buels
 
+David Dorward
+
 =head1 COPYRIGHT
 
 Copyright (c) 2010-2011 the HTML::Zoom L</AUTHOR> and L</CONTRIBUTORS>
diff --git a/t/parse_cdata.t b/t/parse_cdata.t
new file mode 100644 (file)
index 0000000..81e95fe
--- /dev/null
@@ -0,0 +1,97 @@
+use strictures 1;
+use HTML::Zoom;
+use Test::More ;# skip_all => "Totally doesn't work yet";
+
+# Test that contant of elements defined as containing intrinsic CDATA are not
+# selected as elements
+
+# NB: This tests HTML parsing rules. XHTML is different.
+
+my $template = <<HTML;
+<!DOCTYPE html>
+<html lang=en-gb>
+    <meta charset=utf-8>
+    <title>Test</title>
+    <style>
+        /* <textarea>Unmodified</textarea> */
+    </style>
+    </head>
+    <body>
+        <p>Unmodified</p>
+        <textarea>Unmodified</textarea>
+        <script>
+            if (1) {
+                document.write('<p>');
+            } else {
+                document.write('<div>');
+            }
+            document.write('hello, world');
+            if (1) {
+                document.write('</p>');
+            } else {
+                document.write('</div>');
+            }
+        </script>
+HTML
+
+my $expected_p = <<HTML;
+<!DOCTYPE html>
+<html lang=en-gb>
+    <meta charset=utf-8>
+    <title>Test</title>
+    <style>
+        /* <textarea>Unmodified</textarea> */
+    </style>
+    </head>
+    <body>
+        <p>Unmodified</p>
+        <textarea>Unmodified</textarea>
+        <script>
+            if (1) {
+                document.write('<p>');
+            } else {
+                document.write('<div>');
+            }
+            document.write('hello, world');
+            if (1) {
+                document.write('</p>');
+            } else {
+                document.write('</div>');
+            }
+        </script>
+HTML
+
+my $expected_t = <<HTML;
+<!DOCTYPE html>
+<html lang=en-gb>
+    <meta charset=utf-8>
+    <title>Test</title>
+    <style>
+        /* <textarea>Unmodified</textarea> */
+    </style>
+    </head>
+    <body>
+        <p>Unmodified</p>
+        <textarea>Replaced</textarea>
+        <script>
+            if (1) {
+                document.write('<p>');
+            } else {
+                document.write('<div>');
+            }
+            document.write('hello, world');
+            if (1) {
+                document.write('</p>');
+            } else {
+                document.write('</div>');
+            }
+        </script>
+HTML
+
+my $replaced_p = HTML::Zoom->from_html($template)->select('p')->replace_content('Replaced')->to_html;
+is($replaced_p, $expected_p, "Script element parsed as CDATA");
+
+my $replaced_t = HTML::Zoom->from_html($template)->select('textarea')->replace_content('Replaced')->to_html;
+is($replaced_t, $expected_t, "Style element parsed as CDATA");
+
+done_testing;