XMLSerializer::serializeToString() doesn't recognize empty elements

VERIFIED FIXED in mozilla0.9

Status

()

defect
P3
trivial
VERIFIED FIXED
19 years ago
19 years ago

People

(Reporter: taras.tielkes, Assigned: hjtoi-bugzilla)

Tracking

Trunk
mozilla0.9
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [fixinhand, fixes 45552 too])

Attachments

(8 attachments)

Reporter

Description

19 years ago
var xmlDoc = document.implementation.createDocument("", "x", null);
alert( new XMLSerializer().serializeToString( xmlDoc ) );
-------------------------------------------------------------
This will show "<x></x>".

This is absolutely correct, but it would be nice if XMLSerializer would 
recognize that an element is empty, and output an empty element tag. This will 
save some space with larger documents with a lot of empty elements.(Trivial 
though, but looks nice)
Target Milestone: --- → mozilla0.9
I have a fix, will attach.
Status: NEW → ASSIGNED
jst pointed out that we already have content serializers in layout. I removed
code duplication by calling the implementation in layout dir. While doing this I
noticed several bugs in the implementation there, namely:

- empty elements are not output as empty
- HTML tags get uppercased
- default namespace somehow gets assigned the prefix "xmlns" for output
- HTML tags get the HTML namespace declaration attribute twice
- script tag contents do not get output in testcase 1

My patches 1/2, 2/2 and 3/3 applied together fix all but the last two problems.
They are still suboptimal (too many string operations to my liking, and if
someone actually declared namespace prefix "xmlns" we would not output that).

This fix also fixes bug 45552.
Whiteboard: [fixinhand, fixes 45552 too]
I am taking all XMLExtras bugs to make it easier to see what I am working on...
Assignee: vidur → heikki
Status: ASSIGNED → NEW
PIs seem to be missing as well.
The last patch to layout/base enables ConfirmPrefix again. Now I know the
purpose: it is needed in case some script was moving elements around, for instance.

Also, because HTML elements do not really understand namespaces, they do not
report the namespace prefix properly - rather the name contains the prefix and
the actual name. There are quite a few hacks to work around this.

I have opened new bugs to track the issues that I have found while working with
this:

- bug 63559 DOMParser loses HTML element attributes
- bug 63558 XMLSerializer does not serialize starting XML PI
- bug 63560 XMLSerializer does not output html:script contents
- bug 63561 XMLSerializer does not output comments and PIs after document
element close

Comment 14

19 years ago
r=vidur. We should get the document encoder to work on free-standing subtrees as
well.
Bug 63568 opened to track the issue of serializing free-standing subtrees.
Status: NEW → ASSIGNED
sr=jst for the patches 1/2, 2/2 and the last patch.

I have one coding style nitpick about the last patch, code like this:

  if (!content) return NS_ERROR_FAILURE;

I'd prefer seeing that either either broken up on two lines or:

  NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);

... but I'd accept the patch as is if you don't wanna change that.

I think the namespace code could be cleaned up alot once our HTML elements use
nsINodeInfo so that prefixes and namespaces are always correctly preserved.
Fixed.
Status: ASSIGNED → RESOLVED
Last Resolved: 19 years ago
Resolution: --- → FIXED

Comment 18

19 years ago
Marking verified per last comments.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.