cgross / grunt-dom-munger

Grunt task to read and manipulate HTML with CSS selectors.
MIT License
93 stars 40 forks source link

Adhere to Cheerio's XML mode when xmlMode option is specified #4

Open jcdarwin opened 11 years ago

jcdarwin commented 11 years ago

Cheerio exposes an option "xmlMode: true", which should be used to distinguish between writing out HTML ( $.html() ) and XML ( $.xml() ).

Therefore, in method processFile:

updatedContents = $.html();  

Should become:

if (options.xmlMode) {
        updatedContents = $.xml();
} else {
        updatedContents = $.html();  
}
cgross commented 11 years ago

I'm trying to understand the use-case you're hitting. I don't mind making the addition but I was under the assumption that cheerio would endeavor to write the document back out with the same tags as before (plus whatever your modifications were). Is it not doing this or is there some similar kind of issue?

jcdarwin commented 11 years ago

The problem I'm having is that I'm appending some elements to an XML file, and unfortunately some other elements are ending up malformed.

In particular I start out with:

and, without this change, end up with:

(note the lack of the self closing /).

Actually, although this suggested change fixes this problem, I strike another problem with Cheerio (which the xmlMode doesn't help with) -- for another element, I start out with:

2012-08-20T03:18:06Z

and end up with:

2012-08-20T03:18:06Z

(i.e. it drops the entire closing tag).

There looks to be something buried in cheerio that specifically treats meta tags incorrectly -- I've yet to find the time to hunt it down but will do so in the next day or two.

Jason

On Tue, Aug 20, 2013 at 2:45 AM, Chris Gross notifications@github.comwrote:

I'm trying to understand the use-case you're hitting. I don't mind making the addition but I was under the assumption that cheerio would endeavor to write the document back out with the same tags as before (plus whatever your modifications were). Is it not doing this or is there some similar kind of issue?

— Reply to this email directly or view it on GitHubhttps://github.com/cgross/grunt-dom-munger/issues/4#issuecomment-22877065 .

inta commented 10 years ago

Could we get this fixed? If cheerio would just write back the document unchanged, that would not be a problem, but it is removing all slashes from self closing elements (if you use $.html). That will break XHTML and polyglot documents.

A one liner like: var updatedContents = (options.xmlMode) ? $.xml() : $.html(); would suffice …