Replacing XML nodes using perl and Mojo::DOM - perl

I would like to exchange node in an XML file using Mojo::DOM.
I'm pretty sure it is possible but I didn't find a way yet.
Given the following XML:
my $xml = q~
<html>
<div>
<p>1</p>
<p>2</p>
<img />
</div>
</html>
~;
I would like to remove the div and instead insert a body tag, so that the result looks like this:
my $xml = q~
<html>
<body>
<p>1</p>
<p>2</p>
<img />
</body>
</html>
~;
I thought about replace, but I didn't find an example where the replacement is the $dom of the replaced tag.

It's very simple to just find the <div> element and use the tag method to change its tag
This program demonstrates. The CSS selector html > div finds the (first) <div> element that is a child of an <html> element
use strict;
use warnings;
use Mojo::DOM;
my $xml = q~
<html>
<div>
<p>1</p>
<p>2</p>
<img />
</div>
</html>
~;
my $dom = Mojo::DOM->new($xml);
$dom->at('html > div')->tag('body');
print $dom, "\n";
output
<html>
<body>
<p>1</p>
<p>2</p>
<img>
</body>
</html>

Related

perl, mason, working and passing variables

I'm trying to pass a 'mason' variable into a Perl autohandler script that runs before the HTML.
Is this possible.
<!--- MASON -->
<html>
<body>
<%init>
my $sub_headline = 'this is text';
</%init>
###HEADER###
</body>
</html>
<!--- AUTOHANDLER --->
my $m_header = '<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<h3 class="m-portlet__head-text">###TITLE###</h3>
</div>
<p>$sub_headline</p>
</div><!-- /.caption -->
</div><!-- /.head -->';
$html =~ s/###HEADER###/${m_header}/i;
Ended up making a component for the header and adding a condition for the passed in arg. Then pirnt the correct HTML.

why this code hide the whole Div when I use childNodes[x] method?

I want to only hide the P0 paragraph using childNodes[x] . I wonder how it works because it hides the whole div with in this code:
<html>
<body>
<div id="myDiv">
<p>P0</p>
<p>P1</p>
</div>
<button onclick="hideFn();">hide</button>
<script>
function hideFn()
{
document.childNodes[0].childNodes[1].childNodes[1].style.display = "none";
}
</script>
</body>
</html>
</html>
You easily could have found the reason yourself by simply doing the traversal step by step:
document
the document
.childNodes[0]
the documentElement, also known as the root node (<html>)
.childNodes[1]
the <body>
.childNodes[1]
the <div>

How can I remove an attribute from all DOM elements with Mojolicious?

I want to remove the bgcolor attribute from all elements of a page I am scraping via Mojolicious.
My attempt has been the following:
$dom->all_contents->each(sub { $_->attr('bgcolor' => undef) });
but this seems not to work.
How do I do it right?
The following uses Mojo::DOM to delete the bgcolor attribute for every node:
use strict;
use warnings;
use Mojo::DOM;
my $dom = Mojo::DOM->new(do {local $/; <DATA>});
for my $node ($dom->find('*')->each) {
delete $node->{bgcolor};
}
print $dom;
__DATA__
<html>
<head>
<title>Hello background color</title>
</head>
<body bgcolor="white">
<h1>Hello world</h1>
<table>
<tr><td bgcolor="blue">blue</td></tr>
<tr><td bgcolor="green">green</td></tr>
</table>
</body>
</html>
Outputs:
<html>
<head>
<title>Hello background color</title>
</head>
<body>
<h1>Hello world</h1>
<table>
<tr><td>blue</td></tr>
<tr><td>green</td></tr>
</table>
</body>
</html>
Notes:
It's possible to use CSS Selectors to limit the returned nodes to only those containing the specific attribute:
for my $node ($dom->find('[bgcolor]')->each) {
One can also let Mojo handle the iteration like the following:
$dom->find('*')->each(sub {
delete $_->{bgcolor};
});
As I understand it, the DOM attribute you're looking for isn't bgcolor but background-color, the css variety. bgcolor fell out of popularity a while ago, in favor of defining classes and using CSS to set the styling on an object (including its background color). Try background-color instead.

PHP 5.3 write contents to plain text

I am trying to make a PHP script write into a plain text file. I have done this before and it worked just fine. But it's not working this time for some reason.
Here is the HTML I am using:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/css/feedback.css" >
<title>Questions, Comments, Suggestions</title>
</head>
<body>
<p class="title">Questions, comments, and suggestions here!</p>
<form method="post" name="userFeedback" action="/submit.php">
<textarea id="comments" placeholder="Leave a comment or review here..."></textarea>
<textarea id="name" placeholder="Your name here"></textarea>
<textarea id="contact" placeholder="Put any means of contact you want to here (optional)"></textarea>
<br>
<input class="enter" type="submit" value="Enter">
</form>
</body>
</html>
All I want to do with this is to print out whatever is entered onto a plain .txt file with PHP 5.3. Here is the code:
$data = ($_POST["comments"] ." || ". $_POST["name"] ." || ". $_POST["contact"]);
$data = strip_tags($data);
$file = "feedback.txt";
$f = fopen($file, "a+");
fwrite($f, $data . "\n" . "\n");
fclose($f);
header ( 'Location: index.html' );
Please remember that I am using 5.3. I'm sure there's a simple error in here somewhere. Can someone help me with this? Thank you in advance!
We got it! Turns out that the PHP $_POST method looks for the "name" attribute and not the "id".

Why does Lift escape this <lift:bind> value?

I have (roughly) this LIFT-ified HTML in my default template:
<html>
<head>
<title>FooBar Application | <lift:bind name="page-title"/></title>
</head>
<body>
<h1><lift:bind name="page-title" /></h1>
<div id="page-content">
<lift:bind name="page-content" />
</div>
</body>
</html>
...and then this in my main template:
<lift:surround at="page-content">
<lift:bind-at name="page-title">Home</lift:bind-at>
</lift>
...which give me this in the generated HTML:
<html>
<head>
<title>FooBar Application | <lift:bind name="page-title"/></title>
</head>
<body>
<h1>Home</h1>
<div id="page-content">
</div>
</body>
</html>
Why is the <lift:bind> tag in the <title> getting escaped, and the one in the <body><h2> not? And how do I prevent that from happening?
Are the pages defined through SiteMap? As was mentioned before, <title> can be a special case, and it is interpreted in several places - some of which might be doing the escaping. If you can, I'd try setting the page title in one of two ways:
Through the Sitemap you can use the Title Loc Param as referenced here: Dynamic title with Lift
You can also have something like: <title data-lift="PageTitle"></title> to have it invoke a snippet called page-title. Where, the snippet would be something like:
class PageTitle {
def render = "*" #> "FooBar Application | Home"
}