Repository: nadar/quill-delta-parser (php)

Issue:
Rendering inline listeners priority
**Describe the bug**
When rendering inline items the priority messes with the content, sometimes even deleting content

**The delta code which generates the Problem**
```
{"ops": [
  {"attributes": {"italic": true},"insert": "italic text"},
  {"attributes": {"link": "https://www.test.com/","bold": true},"insert": "link"},
  {"insert": "\n"}
]}
```
**The expected html output the delta should produce**
```
<p><em>italic text</em><a href="https://www.test.com/" target="_blank"><strong>link</strong></a></p>
```

**Expected behavior**
I expect the order of the inserts to be honored

**Additional context**
This bug also happens with other inline items;
I would like to help fix it but am unsure of the desired direction; It feels like we need to revisit the entire order of how inline things are rendered at the moment. Did you notice this before? Do you have a desired direction? Or do you consider this behavior as intended?

If you update output.php on line 40 & 41
```
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
```
link will work on the demo page

Pull request description:
fix issue with sort index
this closes #16 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2716ea5..54e9445 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file.
 
 This project adheres to [Semantic Versioning](http://semver.org/).
 
+## 1.3.3
+
++ [#16](https://github.com/nadar/quill-delta-parser/issues/16) Fixed bug with sorting index of inline elements when using prepend.
+
 ## 1.3.2 (3. July 2019)
+
 + [#15](https://github.com/nadar/quill-delta-parser/pull/15) Fixed bug when header contains (partial) formatting and line before header contains formatting
 
 ## 1.3.1 (6. June 2019)
diff --git a/output.php b/output.php
index ee08eff..53af4c9 100644
--- a/output.php
+++ b/output.php
@@ -37,8 +37,8 @@ EOT;
 <html lang="en">
     <head>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
-    <link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet">
-    <script src="https://cdn.quilljs.com/1.0.0/quill.js"></script>
+    <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
+    <script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
     </head>
 <body>
diff --git a/src/InlineListener.php b/src/InlineListener.php
index d316005..2b47a40 100644
--- a/src/InlineListener.php
+++ b/src/InlineListener.php
@@ -56,7 +56,7 @@ abstract class InlineListener extends Listener
             if (!$next) {
                 throw new Exception("Unable to find a next element. Invalid DELTA on '{$pick->line->input}'. Maybe your delta code does not end with a newline?");
             }
-            $next->addPrepend($pick->line->input);
+            $next->addPrepend($pick->line->input, $pick->line);
         }
     }
 }
diff --git a/src/Line.php b/src/Line.php
index 93e01f5..b006c63 100644
--- a/src/Line.php
+++ b/src/Line.php
@@ -218,14 +218,16 @@ class Line
     /**
      * Add a new value to the prepend array.
      *
-     * Certain elements needs to prepend values into the next element.
-     *
+     * Certain elements needs to prepend values into the next element. The Line argument is required in order to 
+     * ensure the correct index for the prepend element.
+     * 
      * @param string $value The value to prepend.
+     * @param Line $line The line which does the prepend, this is used to ensure the correctly order index of the elements. {@since 1.3.3}
      * @return void
      */
-    public function addPrepend($value)
+    public function addPrepend($value, Line $line)
     {
-        $this->prepend[] = $value;
+        $this->prepend[$line->getIndex()] = $value;
     }
 
     /**
@@ -235,6 +237,7 @@ class Line
      */
     public function renderPrepend()
     {
+        ksort($this->prepend);
         return implode("", array_unique($this->prepend));
     }
 


Test patch:
diff --git a/tests/InlinePrependOrderTest.php b/tests/InlinePrependOrderTest.php
new file mode 100644
index 0000000..79714cc
--- /dev/null
+++ b/tests/InlinePrependOrderTest.php
@@ -0,0 +1,13 @@
+<?php
+namespace nadar\quill\tests;
+
+class InlinePrependOrderTest extends DeltaTestCase
+{
+    public $json = <<<'JSON'
+{"ops":[{"attributes":{"italic":true},"insert":"italic"},{"attributes":{"bold":true},"insert":"bold"},{"insert":"\n"}]}
+JSON;
+
+    public $html = <<<'EOT'
+<p><em>italic</em><strong>bold</strong></p>
+EOT;
+}
diff --git a/tests/PrependInlineIndexTest.php b/tests/PrependInlineIndexTest.php
new file mode 100644
index 0000000..dbf5a8a
--- /dev/null
+++ b/tests/PrependInlineIndexTest.php
@@ -0,0 +1,20 @@
+<?php
+namespace nadar\quill\tests;
+
+class PrependInlineIndexTest extends DeltaTestCase
+{
+    public $json = <<<'JSON'
+    {"ops":[
+        {"attributes":{"italic":true},"insert":"italic"},
+        {"attributes":{"bold":true,"link":"https://nadar.io"},"insert":"link"},
+        {"insert":"\n"}
+    ]}
+JSON;
+
+    public $html = <<<'EOT'
+<p>
+<em>italic</em>
+<a href="https://nadar.io" target="_blank"><strong>link</strong></a>
+</p>
+EOT;
+}
\ No newline at end of file