<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>24208</bug_id>
          
          <creation_ts>2014-01-06 00:49:24 +0000</creation_ts>
          <short_desc>Please handle the case of XML attributes with namespaces but no prefix</short_desc>
          <delta_ts>2014-10-13 23:46:29 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebAppsWG</product>
          <component>DOM Parsing and Serialization</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Victor Costan">costan</reporter>
          <assigned_to name="Travis Leithead [MSFT]">travil</assigned_to>
          <cc>mike</cc>
    
    <cc>www-dom</cc>
          
          <qa_contact>public-webapps-bugzilla</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>98017</commentid>
    <comment_count>0</comment_count>
    <who name="Victor Costan">costan</who>
    <bug_when>2014-01-06 00:49:24 +0000</bug_when>
    <thetext>The DOM Core API setAttributeNS allows the creation of attributes that have a non-null namespace URI, but no prefix.

A minimal example follows below.
var doc = (new DOMParser()).parseFromString(&quot;&lt;test/&gt;&quot;, &quot;text/xml&quot;, null);
doc.documentElement.setAttributeNS(&quot;http://www.example.com/&quot;, &quot;attr&quot;, &quot;value&quot;);

The XML serialization algorithm specified below does not seem to handle this case.
https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#dfn-concept-serialize-xml

(searching for &quot;XML serialization of the attributes&quot; shows the relevant paragraph)

Firefox and Internet Explorer handle this case by generating prefixes for the attributes.

Blink and WebKit do not yet handle this case, but I&apos;m working on it. http://crbug.com/248044</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>101507</commentid>
    <comment_count>1</comment_count>
    <who name="Travis Leithead [MSFT]">travil</who>
    <bug_when>2014-02-27 19:05:10 +0000</bug_when>
    <thetext>var doc = (new DOMParser()).parseFromString(&quot;&lt;test xmlns:a1=&apos;other&apos;&gt;&lt;child/&gt;&lt;/test&gt;&quot;, &quot;text/xml&quot;, null);
doc.documentElement.firstChild.setAttributeNS(&quot;http://www.example.com/&quot;, &quot;attr&quot;, &quot;value&quot;);
doc.documentElement.firstChild.setAttributeNS(&quot;http://www.another.example.com/&quot;, &quot;attr&quot;, &quot;value&quot;);

Firefox carefully manages the auto-generation of namespace numbering, so I&apos;ll need to spec that.

var doc = (new DOMParser()).parseFromString(&quot;&lt;test&gt;&lt;child/&gt;&lt;/test&gt;&quot;, &quot;text/xml&quot;, null);
doc.documentElement.firstChild.setAttributeNS(&quot;http://www.w3.org/2000/xmlns/&quot;, &quot;xmlns:test&quot;, &quot;http://www.example.com/&quot;);
doc.documentElement.firstChild.setAttributeNS(&quot;http://www.example.com/&quot;, &quot;attr&quot;, &quot;value&quot;);

This case falls down in IE--it doesn&apos;t seem to pickup the dynamically added &quot;text&quot; prefix and perform the association. This case needs to be handled as well.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>101508</commentid>
    <comment_count>2</comment_count>
    <who name="Travis Leithead [MSFT]">travil</who>
    <bug_when>2014-02-27 19:08:24 +0000</bug_when>
    <thetext>I&apos;m on the fence about what auto-generation naming system to use.

Firefox is more concise with &quot;a&quot; + index forming the generated prefix.

IE is more descriptive with &quot;NS&quot; + index, which is somewhat self-describing.

I like IE&apos;s generated prefix, but not the uppercase characters. If I go that route than no browser will align with the spec, though it would be easy for either of them to make the namespace generation string change, I imagine.

If I go with the firefox generated string, then they form the reference implementation.

Any preferences?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>101521</commentid>
    <comment_count>3</comment_count>
    <who name="Victor Costan">costan</who>
    <bug_when>2014-02-27 19:55:29 +0000</bug_when>
    <thetext>Thank you very much for looking into this, Travis!

I have a slight preference for lowercase &quot;ns&quot; as well, and that&apos;s what my work-in-progress patch uses for Blink. I&apos;m willing to implement what you end up spec&apos;ing though.

FWIW, in my implementation, I&apos;m not using sequential indices. Instead, the indices are based on an internal hash function. If the specification mandates sequential indices, I hope that it will do so in a way that does not require a quadratic-time algorithm. For bonus points, try not adding any memory overhead for DOMs that set their own prefixes for attributes with namespaces.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>101563</commentid>
    <comment_count>4</comment_count>
    <who name="Travis Leithead [MSFT]">travil</who>
    <bug_when>2014-02-27 23:53:12 +0000</bug_when>
    <thetext>(In reply to Victor Costan from comment #3)
&gt; I have a slight preference for lowercase &quot;ns&quot; as well, and that&apos;s what my
&gt; work-in-progress patch uses for Blink. I&apos;m willing to implement what you end
&gt; up spec&apos;ing though.

Cool, I&apos;ll go with &quot;ns&quot; then as a prefix...

&gt; FWIW, in my implementation, I&apos;m not using sequential indices. Instead, the
&gt; indices are based on an internal hash function. If the specification
&gt; mandates sequential indices, I hope that it will do so in a way that does
&gt; not require a quadratic-time algorithm. For bonus points, try not adding any
&gt; memory overhead for DOMs that set their own prefixes for attributes with
&gt; namespaces.

So, your indexes will be random? How many digits will you use to represent the result of the hash? 

Given a local incrementing counter approach, two unique serializations of different parts of a DOM are at risk of defining the same prefixes for different namespaces. I&apos;m not sure that&apos;s a problem unless you end up wanting to combine these serialized strings together (in which case you&apos;d need to de-dup the prefixes).

Another potential issue is that prefixes will be assignned based on the order they are encountered in an element&apos;s attributes collection (and this attribute order may be different based on different UAs).

Perhaps it would be safer (and more resilient) to have the spec define an implementation-specific algorithm that generates a unique number for a given namespace (the hashing protocol you described), and that the number&lt;-&gt;namespace mapping must be consistent for this namespaceURI wherever it is encountered (and not already associated with a prefix). That way you solve the first-mentioned problem, and don&apos;t imply an order in the second problem. An implementation-specific algorithm could use an incrementing number, or a 10-digit hash--the spec requirement might only describe the &quot;ns&quot; prefix part.

On the other hand all that might be overkill for this rather edge-case scenario.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102459</commentid>
    <comment_count>5</comment_count>
    <who name="Victor Costan">costan</who>
    <bug_when>2014-03-17 16:02:26 +0000</bug_when>
    <thetext>I&apos;m terribly sorry for answering to this so late! My inbox ate your reply, and I just happened to check on this bug today.

The hash function that I&apos;m relying on right now is consistent within a browser process, but may change across platforms and browser versions.

I like your approach for having a resilient spec, and have one minor mention. It might be better to allow the hash code to be up to 20 digits in length, to allow UAs to use 64-bit hashes.

I agree that this might be overkill. You can always spec that serialization should throw an exception in this case, and I&apos;d be happy to implement just that :) I don&apos;t think this should break too much of the Web, because right now Chrome and Safari basically generate invalid XML in this case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102793</commentid>
    <comment_count>6</comment_count>
    <who name="Travis Leithead [MSFT]">travil</who>
    <bug_when>2014-03-23 00:11:27 +0000</bug_when>
    <thetext>OK, took a stab at this. Had to make some significant refactoring of the algorithm, so make sure you review it all in context.

Unfortunately, I couldn&apos;t avoid a multi-pass through an element&apos;s attribute list--hopefully you&apos;ll understand why when reading the steps. An implementation can probably optimize this though :-).

Finally, I went with the easiest to spec approach of a single incrementing integer (it only has scope within the execution of the algorithm, so that multiple independent serializations may generate the overlapping &quot;generated prefixes&quot;. I think this is OK, because Firefox and IE do not enforce any kind of global consistency here and the web is just fine (so I think this is already a supreme edge-case).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>103122</commentid>
    <comment_count>7</comment_count>
    <who name="Travis Leithead [MSFT]">travil</who>
    <bug_when>2014-03-28 21:15:20 +0000</bug_when>
    <thetext>Note: the update was done across three commits:
  https://dvcs.w3.org/hg/innerhtml/rev/fb9edcfb8f5f
  https://dvcs.w3.org/hg/innerhtml/rev/f9b5a818ef99
  https://dvcs.w3.org/hg/innerhtml/rev/fa768c710fba</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>113059</commentid>
    <comment_count>8</comment_count>
    <who name="Travis Leithead [MSFT]">travil</who>
    <bug_when>2014-10-13 23:46:29 +0000</bug_when>
    <thetext>*** Bug 24210 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>