thagenbeek / phpquery

Automatically exported from code.google.com/p/phpquery
0 stars 0 forks source link

Ajax returns with <p> tags? #130

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
If you echo some plain text (or JSON..) and send it as an AJAX response on
an output buffered page, it comes back with <p> tags?

Can't seem to solve the problem!?

Original issue reported on code.google.com by robinson...@gmail.com on 25 Aug 2009 at 10:41

GoogleCodeExporter commented 9 years ago
I'm experiencing something like this as well. I have some new text that I am 
sticking into an H1 node with pq($item)->html("Hello World"); but the node 
returns as <h1><p>Hello World</p></h1>. 

Original comment by Joseph.R...@gmail.com on 1 Oct 2009 at 1:11

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Yeah, I've also seen this same problem - surely we must be doing something 
wrong???

Original comment by robinson...@gmail.com on 2 Oct 2009 at 4:41

GoogleCodeExporter commented 9 years ago
In my case, I was working with some html fragments such as "hello world" and
trying to insert them into the dom under an <h1> tag. After investigating I 
think the
php dom needs a top level element in the fragment in order to insert it into 
the dom
and it uses <p> in the absence of anything else. If I try to insert the fragment
"<span>hello world</span>" it doesn't add the <p> tags. 

Original comment by Joseph.R...@gmail.com on 2 Oct 2009 at 8:45

GoogleCodeExporter commented 9 years ago
So how do we handle HTML fragments?

Is it possible to create a new document and manipulate some HTML without 
altering
it's conent / adding charsets / wrapping <p>'s ??

Original comment by robinson...@gmail.com on 2 Oct 2009 at 9:54

GoogleCodeExporter commented 9 years ago
I'm still trying to figure that out. I think the problem is more fundamental 
than
phpQuery as the pq()->html() function ultimately uses php dom to do the work 
and it
is the php dom function that is adding the <p> tags. From the perspective of 
the dom
I can understand why it doesn't like fragments without a root level element. The
fragment is illegal xml and the dom does what it can to conform it. 

I have an idea that I am exploring: insert the fragment with a dummy tag, say
<pqfix>, save the whole dom to html, find and replace the dummy tag with an 
empty
string, and then save the html again. This is a hacky kludge but the result is
exactly what I want. 

How this helps ajax returns, I'm not sure. Maybe save the html fragment to a 
string
as the final step in the php, do a find/replace for the dummy tag, and then 
send the
string back as the ajax response?

Original comment by Joseph.R...@gmail.com on 2 Oct 2009 at 11:40

GoogleCodeExporter commented 9 years ago
Just a confirmation, the solution I proposed in comment 6 does work. Wrapping 
the
html fragment in a dummy tag and then string replacing the output of pq works 
just fine.

Original comment by Joseph.R...@gmail.com on 4 Oct 2009 at 11:40

GoogleCodeExporter commented 9 years ago
There is some code within PHPQuery itself that adds in '<fake></fake>' elements,
perhaps this is something similar that just isn't completed?

Tobiasz, any chance of a reply?? How can we use PHPQuery without it wrapping 
<p> tags
around things?

Original comment by robinson...@gmail.com on 8 Oct 2009 at 3:10

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I'm sorry but i cant reproduce it in any way. I've added new test case for this 
issue
with r392. I've also checked this with ->load() and against trunk (i'm working 
on dev
branch).

Indeed there's <fake> root support and quite more other things in 
DOMDocumentWrapper
to make everything works fine (mostly for XML documents).

Only place where text nodes aren't supported on beginning of the string is pq()
function, which threats them as selectors.

Please check code from new TC, and post one which allows you to reproduce this 
issue.

Original comment by tobiasz....@gmail.com on 10 Oct 2009 at 9:08