Reviewed by Darin.
[WebKit-https.git] / WebCore / dom / Range.cpp
index 7f51e2c2bd948fe3fdf0430bdee901380e957f60..2e4419e4bd0318c0d3cef2044101912493b20bf7 100644 (file)
@@ -674,7 +674,17 @@ PassRefPtr<DocumentFragment> Range::processContents ( ActionType action, Excepti
             }
         }
         else if (m_startContainer->nodeType() == Node::PROCESSING_INSTRUCTION_NODE) {
-            // ### operate just on data ?
+            if (action == EXTRACT_CONTENTS || action == CLONE_CONTENTS) {
+                RefPtr<ProcessingInstruction> c = static_pointer_cast<ProcessingInstruction>(m_startContainer->cloneNode(true));
+                c->setData(c->data().substring(m_startOffset, m_endOffset - m_startOffset), ec);
+                fragment->appendChild(c.release(), ec);
+            }
+            if (action == EXTRACT_CONTENTS || action == DELETE_CONTENTS) {
+                ProcessingInstruction* pi= static_cast<ProcessingInstruction*>(m_startContainer.get());
+                String data(pi->data());
+                data.remove(m_startOffset, m_endOffset - m_startOffset);
+                pi->setData(data, ec);
+            }
         }
         else {
             Node *n = m_startContainer->firstChild();
@@ -735,8 +745,16 @@ PassRefPtr<DocumentFragment> Range::processContents ( ActionType action, Excepti
             }
         }
         else if (m_startContainer->nodeType() == Node::PROCESSING_INSTRUCTION_NODE) {
-            // ### operate just on data ?
-            // leftContents = ...
+            if (action == EXTRACT_CONTENTS || action == CLONE_CONTENTS) {
+                RefPtr<ProcessingInstruction> c = static_pointer_cast<ProcessingInstruction>(m_startContainer->cloneNode(true));
+                c->setData(c->data().substring(m_startOffset), ec);
+                leftContents = c.release();
+            }
+            if (action == EXTRACT_CONTENTS || action == DELETE_CONTENTS) {
+                ProcessingInstruction* pi= static_cast<ProcessingInstruction*>(m_startContainer.get());
+                String data(pi->data());
+                pi->setData(data.left(m_startOffset), ec);
+            }
         }
         else {
             if (action == EXTRACT_CONTENTS || action == CLONE_CONTENTS)
@@ -797,9 +815,16 @@ PassRefPtr<DocumentFragment> Range::processContents ( ActionType action, Excepti
                 m_startContainer->document()->updateLayout();
             }
         }
-        else if (m_startContainer->nodeType() == Node::PROCESSING_INSTRUCTION_NODE) {
-            // ### operate just on data ?
-            // rightContents = ...
+        else if (m_endContainer->nodeType() == Node::PROCESSING_INSTRUCTION_NODE) {
+            if (action == EXTRACT_CONTENTS || action == CLONE_CONTENTS) {
+                RefPtr<ProcessingInstruction> c = static_pointer_cast<ProcessingInstruction>(m_endContainer->cloneNode(true));
+                c->setData(c->data().left(m_endOffset), ec);
+                rightContents = c.release();
+            }
+            if (action == EXTRACT_CONTENTS || action == DELETE_CONTENTS) {
+                ProcessingInstruction* pi= static_cast<ProcessingInstruction*>(m_endContainer.get());
+                pi->setData(pi->data().substring(m_endOffset), ec);
+            }
         }
         else {
             if (action == EXTRACT_CONTENTS || action == CLONE_CONTENTS)
@@ -1115,11 +1140,11 @@ bool Range::isDetached() const
     return m_detached;
 }
 
-void Range::checkNodeWOffset( Node *n, int offset, ExceptionCode& ec) const
+void Range::checkNodeWOffset(Node* n, int offset, ExceptionCode& ec) const
 {
-    if( offset < 0 ) {
+    if (offset < 0)
         ec = INDEX_SIZE_ERR;
-    }
+        // no return here
 
     switch (n->nodeType()) {
         case Node::ENTITY_NODE:
@@ -1130,16 +1155,15 @@ void Range::checkNodeWOffset( Node *n, int offset, ExceptionCode& ec) const
         case Node::TEXT_NODE:
         case Node::COMMENT_NODE:
         case Node::CDATA_SECTION_NODE:
-            if ( (unsigned)offset > static_cast<CharacterData*>(n)->length() )
+            if ((unsigned)offset > static_cast<CharacterData*>(n)->length())
                 ec = INDEX_SIZE_ERR;
             break;
         case Node::PROCESSING_INSTRUCTION_NODE:
-            // ### are we supposed to check with just data or the whole contents?
-            if ( (unsigned)offset > static_cast<ProcessingInstruction*>(n)->data().length() )
+            if ((unsigned)offset > static_cast<ProcessingInstruction*>(n)->data().length())
                 ec = INDEX_SIZE_ERR;
             break;
         default:
-            if ( (unsigned)offset > n->childNodeCount() )
+            if ((unsigned)offset > n->childNodeCount())
                 ec = INDEX_SIZE_ERR;
             break;
     }