<?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>19003</bug_id>
          
          <creation_ts>2012-09-25 07:40:50 +0000</creation_ts>
          <short_desc>Bugs in toNativeLineEndings()</short_desc>
          <delta_ts>2012-10-17 00:11:32 +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>File API</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="Simon Pieters">zcorpan</reporter>
          <assigned_to name="Arun">arun</assigned_to>
          <cc>glenn</cc>
    
    <cc>public-webapps</cc>
          
          <qa_contact>public-webapps-bugzilla</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>74407</commentid>
    <comment_count>0</comment_count>
    <who name="Simon Pieters">zcorpan</who>
    <bug_when>2012-09-25 07:40:50 +0000</bug_when>
    <thetext>https://www.w3.org/Bugs/Public/show_bug.cgi?id=16733
http://dev.w3.org/2006/webapi/FileAPI/#convenienceAPI

The IDL should say:

Window implements LineEndings;
WorkerGlobalScope implements LineEndings;

And remove this:
[[
In environments where the global object is represented by a Window or WorkerGlobalScope object, the toNativeLineEndings method should be available.
]]

(assuming we want this in the global scope)

The algorithm seems bogus. It seems to assume that there will only be one &quot;line ending&quot; and that it is at the end of the string. It doesn&apos;t define what a &quot;line ending&quot; is. &quot;Expected by the underlying platform&quot; also seems unclear. It seems unexpected that this methods *adds* a line ending if there isn&apos;t one.

I would have expected that this method does this:

function toNativeLineEndings(string) {
  ...snip impl of WebIDL voodoo here...
  return string.replace(/(\r\n|\r|\n)/g, nativeLineEnding);
}

As for spec language, I would probably do this:

1. Let /input/ be /string/.
2. Let /position/ be a pointer into /input/, initially pointing at the start of the string.
3. Let /lines/ be an empty array.
4. While /position/ is not past the end of /input/:
    1. Collect a sequence of characters that are not U+000A or U+000D.
    2. Add the string collected in the previous step to /lines/.
    3. If the character at /position/ is U+000D, and the character at /position/+1 is U+000A, advance /position/ by two characters. Otherwise, advance /position/ to the next character.
5. Let /native line ending/ be a sequence of characters that represent a line ending according to the underlying platform&apos;s conventions, or U+000A if there is no particular convention. [issue: should we only allow \n and \r\n here?]
6. Let /output/ be the result of joining /lines/ with /native line ending/.
7. Return /output/.

where &quot;collect a sequence of characters&quot; is defined here: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#collect-a-sequence-of-characters

or here: http://dom.spec.whatwg.org/#collect-a-sequence-of-characters</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>74431</commentid>
    <comment_count>1</comment_count>
    <who name="Glenn Maynard">glenn</who>
    <bug_when>2012-09-25 15:09:44 +0000</bug_when>
    <thetext>Yeah, that&apos;s what I meant in https://www.w3.org/Bugs/Public/show_bug.cgi?id=16733#c21: convert the line endings in a string.

&gt; 3. Let /lines/ be an empty array.
&gt; 4. While /position/ is not past the end of /input/:
&gt;     1. Collect a sequence of characters that are not U+000A or U+000D.
&gt;     2. Add the string collected in the previous step to /lines/.
&gt;     3. If the character at /position/ is U+000D, and the character at
&gt; /position/+1 is U+000A, advance /position/ by two characters. Otherwise,
&gt; advance /position/ to the next character.
&gt; 6. Let /output/ be the result of joining /lines/ with /native line ending/.

This doesn&apos;t seem quite right--an input of &quot;\n&quot; results in &quot;&quot; (instead of eg. &quot;\r\n&quot;), because /lines/ becomes [&quot;&quot;].  Also, &quot;joining string with a string&quot; would need to be defined.

Maybe:

1. Let /position/ be a pointer into /string/, initially pointing at the start of
the string.
2. Let /result/ be the empty string.
3. Let /native line ending/ be the character U+000A, or the character U+000D followed by the character U+000A, as determined by the underlying platform&apos;s conventions.
4. /Line loop/: Collect a sequence of characters that are not U+000A or U+000D.
5. Add the string collected in the previous step to /result/.
6. If the character at /position/ is past the end of /string/, return /result/.
7. Add /native line ending/ to /result/.
8. If the character at /position/ is U+000D, and the character at
/position/+1 is U+000A, advance /position/ by two characters.  Otherwise,
advance /position/ to the next character.
9. Jump back to the step labeled /line loop/ in these steps.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>74437</commentid>
    <comment_count>2</comment_count>
    <who name="Arun">arun</who>
    <bug_when>2012-09-25 15:33:12 +0000</bug_when>
    <thetext>*Both* SimonP&apos;s formulation *and* Glenn&apos;s formulation in Comment 1 do not change a string without ANY line endings to a string with line endings (per the underlying platform).  Why is that?  Should one NOT assume that a developer calls this API on strings without line endings to add them in appropriately?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>74443</commentid>
    <comment_count>3</comment_count>
    <who name="Glenn Maynard">glenn</who>
    <bug_when>2012-09-25 16:08:58 +0000</bug_when>
    <thetext>Huh, what?  Why would you add line endings to a string that didn&apos;t have any?

The whole point is to convert the line endings in the string to the line endings of the platform.

s = open(&quot;file with unix line endings.txt&quot;).read()
s = s.replace(&quot;\n&quot;, &quot;\r\n&quot;)
open(&quot;file with DOS line endings.txt&quot;, &quot;w&quot;).write(s)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>74448</commentid>
    <comment_count>4</comment_count>
    <who name="Arun">arun</who>
    <bug_when>2012-09-25 16:43:49 +0000</bug_when>
    <thetext>I&apos;ve been thinking about this API incorrectly, without allowing for multiple pre-existing line endings.  I also assumed it would append a line ending to a single line parameter.  Both are wrong.  Your way is right, and your algorithm is better.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>74545</commentid>
    <comment_count>5</comment_count>
    <who name="Simon Pieters">zcorpan</who>
    <bug_when>2012-09-26 06:56:29 +0000</bug_when>
    <thetext>zewt, oops.

You version still has a bug though; the &quot;collect a sequence of characters&quot; algorithm expects a variable in the calling algorithm named /input/ (hence my step 1).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>74553</commentid>
    <comment_count>6</comment_count>
    <who name="Glenn Maynard">glenn</who>
    <bug_when>2012-09-26 14:06:13 +0000</bug_when>
    <thetext>A very weird algorithm.  That&apos;s like a function that modifies variables in its calling function, instead of using parameters.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>76466</commentid>
    <comment_count>7</comment_count>
    <who name="Arun">arun</who>
    <bug_when>2012-10-17 00:11:32 +0000</bug_when>
    <thetext>Done.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>