Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(842)

Side by Side Diff: Source/core/html/HTMLElement.cpp

Issue 105693009: Move insertAdjacentHTML to Element (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Correct base. Should not include patches it depends on. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/html/HTMLElement.h ('k') | Source/core/html/HTMLElement.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo bile.com/) 5 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo bile.com/)
6 * Copyright (C) 2011 Motorola Mobility. All rights reserved. 6 * Copyright (C) 2011 Motorola Mobility. All rights reserved.
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 parent->replaceChild(newChild.release(), this, exceptionState); 445 parent->replaceChild(newChild.release(), this, exceptionState);
446 446
447 RefPtr<Node> node = next ? next->previousSibling() : 0; 447 RefPtr<Node> node = next ? next->previousSibling() : 0;
448 if (!exceptionState.hadException() && node && node->isTextNode()) 448 if (!exceptionState.hadException() && node && node->isTextNode())
449 mergeWithNextTextNode(node.release(), exceptionState); 449 mergeWithNextTextNode(node.release(), exceptionState);
450 450
451 if (!exceptionState.hadException() && prev && prev->isTextNode()) 451 if (!exceptionState.hadException() && prev && prev->isTextNode())
452 mergeWithNextTextNode(prev.release(), exceptionState); 452 mergeWithNextTextNode(prev.release(), exceptionState);
453 } 453 }
454 454
455 Node* HTMLElement::insertAdjacent(const String& where, Node* newChild, Exception State& exceptionState)
456 {
457 // In Internet Explorer if the element has no parent and where is "beforeBeg in" or "afterEnd",
458 // a document fragment is created and the elements appended in the correct o rder. This document
459 // fragment isn't returned anywhere.
460 //
461 // This is impossible for us to implement as the DOM tree does not allow for such structures,
462 // Opera also appears to disallow such usage.
463
464 if (equalIgnoringCase(where, "beforeBegin")) {
465 if (ContainerNode* parent = this->parentNode()) {
466 parent->insertBefore(newChild, this, exceptionState);
467 if (!exceptionState.hadException())
468 return newChild;
469 }
470 return 0;
471 }
472
473 if (equalIgnoringCase(where, "afterBegin")) {
474 insertBefore(newChild, firstChild(), exceptionState);
475 return exceptionState.hadException() ? 0 : newChild;
476 }
477
478 if (equalIgnoringCase(where, "beforeEnd")) {
479 appendChild(newChild, exceptionState);
480 return exceptionState.hadException() ? 0 : newChild;
481 }
482
483 if (equalIgnoringCase(where, "afterEnd")) {
484 if (ContainerNode* parent = this->parentNode()) {
485 parent->insertBefore(newChild, nextSibling(), exceptionState);
486 if (!exceptionState.hadException())
487 return newChild;
488 }
489 return 0;
490 }
491
492 // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alte rnative.
493 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + wher e + "') is not one of 'beforeBegin', 'afterBegin', 'beforeEnd', or 'afterEnd'.") ;
494 return 0;
495 }
496
497 Element* HTMLElement::insertAdjacentElement(const String& where, Element* newChi ld, ExceptionState& exceptionState) 455 Element* HTMLElement::insertAdjacentElement(const String& where, Element* newChi ld, ExceptionState& exceptionState)
498 { 456 {
499 if (!newChild) { 457 if (!newChild) {
500 // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alternative. 458 // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alternative.
501 exceptionState.throwTypeError("The node provided is null."); 459 exceptionState.throwTypeError("The node provided is null.");
502 return 0; 460 return 0;
503 } 461 }
504 462
505 Node* returnValue = insertAdjacent(where, newChild, exceptionState); 463 Node* returnValue = insertAdjacent(where, newChild, exceptionState);
506 return toElement(returnValue); 464 return toElement(returnValue);
507 } 465 }
508 466
509 // Step 1 of http://domparsing.spec.whatwg.org/#insertadjacenthtml()
510 static Element* contextElementForInsertion(const String& where, Element* element , ExceptionState& exceptionState)
511 {
512 if (equalIgnoringCase(where, "beforeBegin") || equalIgnoringCase(where, "aft erEnd")) {
513 ContainerNode* parent = element->parentNode();
514 if (!parent || !parent->isElementNode()) {
515 exceptionState.throwDOMException(NoModificationAllowedError, "The el ement has no parent.");
516 return 0;
517 }
518 return toElement(parent);
519 }
520 if (equalIgnoringCase(where, "afterBegin") || equalIgnoringCase(where, "befo reEnd"))
521 return element;
522 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + wher e + "') is not one of 'beforeBegin', 'afterBegin', 'beforeEnd', or 'afterEnd'.") ;
523 return 0;
524 }
525
526 void HTMLElement::insertAdjacentHTML(const String& where, const String& markup, ExceptionState& exceptionState)
527 {
528 RefPtr<Element> contextElement = contextElementForInsertion(where, this, exc eptionState);
529 if (!contextElement)
530 return;
531
532 RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, contextElement.get(), AllowScriptingContent, "insertAdjacentHTML", exceptionStat e);
533 if (!fragment)
534 return;
535 insertAdjacent(where, fragment.get(), exceptionState);
536 }
537
538 void HTMLElement::insertAdjacentText(const String& where, const String& text, Ex ceptionState& exceptionState) 467 void HTMLElement::insertAdjacentText(const String& where, const String& text, Ex ceptionState& exceptionState)
539 { 468 {
540 RefPtr<Text> textNode = document().createTextNode(text); 469 RefPtr<Text> textNode = document().createTextNode(text);
541 insertAdjacent(where, textNode.get(), exceptionState); 470 insertAdjacent(where, textNode.get(), exceptionState);
542 } 471 }
543 472
544 void HTMLElement::applyAlignmentAttributeToStyle(const AtomicString& alignment, MutableStylePropertySet* style) 473 void HTMLElement::applyAlignmentAttributeToStyle(const AtomicString& alignment, MutableStylePropertySet* style)
545 { 474 {
546 // Vertical alignment with respect to the current baseline of the text 475 // Vertical alignment with respect to the current baseline of the text
547 // right or left means floating images. 476 // right or left means floating images.
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 #ifndef NDEBUG 1004 #ifndef NDEBUG
1076 1005
1077 // For use in the debugger 1006 // For use in the debugger
1078 void dumpInnerHTML(WebCore::HTMLElement*); 1007 void dumpInnerHTML(WebCore::HTMLElement*);
1079 1008
1080 void dumpInnerHTML(WebCore::HTMLElement* element) 1009 void dumpInnerHTML(WebCore::HTMLElement* element)
1081 { 1010 {
1082 printf("%s\n", element->innerHTML().ascii().data()); 1011 printf("%s\n", element->innerHTML().ascii().data());
1083 } 1012 }
1084 #endif 1013 #endif
OLDNEW
« no previous file with comments | « Source/core/html/HTMLElement.h ('k') | Source/core/html/HTMLElement.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698