void Range::surroundContents( const Node &newParent )
{
    checkCommon();

    if( newParent.isNull() )
        return;

    Node start = startContainer();
    if( newParent.ownerDocument() != start.ownerDocument() )
        throw DOMException( DOMException::WRONG_DOCUMENT_ERR );
    
    if( newParent.nodeType() == Node::ATTRIBUTE_NODE ||
        newParent.nodeType() == Node::ENTITY_NODE ||
        newParent.nodeType() == Node::NOTATION_NODE ||
        newParent.nodeType() == Node::DOCUMENT_TYPE_NODE ||
        newParent.nodeType() == Node::DOCUMENT_NODE ||
        newParent.nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
        throw RangeException( RangeException::INVALID_NODE_TYPE_ERR );

    // revisit: if you set a range without optimizing it (trimming) the following exception might be
    // thrown incorrectly
    Node realStart = (start.nodeType() == Node::TEXT_NODE)? start.parentNode() : start;
    Node end = endContainer();
    Node realEnd = (end.nodeType() == Node::TEXT_NODE)? end.parentNode() : end;
    if( realStart != realEnd )
        throw RangeException( RangeException::BAD_BOUNDARYPOINTS_ERR );

    DocumentFragment fragment = extractContents();
    insertNode( newParent );
    // BIC: to avoid this const_cast newParent shouldn't be const
    //(const_cast<Node>(newParent)).appendChild( fragment );
    ((Node)(newParent)).appendChild( fragment );
    selectNode( newParent );
}
Example #2
0
void StyleElement::process(Element* e)
{
    if (!e || !e->inDocument())
        return;

    unsigned resultLength = 0;
    for (Node* c = e->firstChild(); c; c = c->nextSibling()) {
        Node::NodeType nodeType = c->nodeType();
        if (nodeType == Node::TEXT_NODE || nodeType == Node::CDATA_SECTION_NODE || nodeType == Node::COMMENT_NODE)
            resultLength += c->nodeValue().length();
    }
    UChar* text;
    String sheetText = String::createUninitialized(resultLength, text);

    UChar* p = text;
    for (Node* c = e->firstChild(); c; c = c->nextSibling()) {
        Node::NodeType nodeType = c->nodeType();
        if (nodeType == Node::TEXT_NODE || nodeType == Node::CDATA_SECTION_NODE || nodeType == Node::COMMENT_NODE) {
            String nodeValue = c->nodeValue();
            unsigned nodeLength = nodeValue.length();
            memcpy(p, nodeValue.characters(), nodeLength * sizeof(UChar));
            p += nodeLength;
        }
    }
    ASSERT(p == text + resultLength);

    createSheet(e, sheetText);
}
Example #3
0
NodeList
Element::getElementsByTagName (const DOMString& name)
{
    NodeList elements;
    
    for (size_t i = 0; i < _children.length(); i++) {
        Node* node = _children.item(i);

        if (node->nodeType() == Node::ELEMENT_NODE) {
            if (((Element*)node)->tagName() == Utils::toUpper(name)) {
                elements.insert(node);
            }
        }
    }

    for (size_t i = 0; i < _children.length(); i++) {
        Node* node = _children.item(i);

        if (node->nodeType() == Node::ELEMENT_NODE) {
            NodeList subElements = ((Element*)node)->getElementsByTagName(name);

            for (size_t h = 0; h < subElements.length(); h++) {
                elements.insert(subElements.item(h));
            }
        }
    }

    return elements;
}
Example #4
0
void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
    m_title = "";
    for (Node* c = firstChild(); c != 0; c = c->nextSibling())
        if (c->nodeType() == TEXT_NODE || c->nodeType() == CDATA_SECTION_NODE)
            m_title += c->nodeValue();
    if (inDocument())
        document()->setTitle(m_title, this);
    HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
}
void HTMLTitleElement::childrenChanged()
{
    HTMLElement::childrenChanged();
    m_title = "";
    for (Node* c = firstChild(); c != 0; c = c->nextSibling())
        if (c->nodeType() == TEXT_NODE || c->nodeType() == CDATA_SECTION_NODE)
            m_title += c->nodeValue();
    if (inDocument())
        document()->setTitle(m_title, this);
}
Example #6
0
Value LocationPath::evaluate() const
{
    EvaluationContext& evaluationContext = Expression::evaluationContext();
    EvaluationContext backupContext = evaluationContext;
    // http://www.w3.org/TR/xpath/
    // Section 2, Location Paths:
    // "/ selects the document root (which is always the parent of the document element)"
    // "A / by itself selects the root node of the document containing the context node."
    // In the case of a tree that is detached from the document, we violate
    // the spec and treat / as the root node of the detached tree.
    // This is for compatibility with Firefox, and also seems like a more
    // logical treatment of where you would expect the "root" to be.
    Node* context = evaluationContext.node.get();
    if (m_absolute && context->nodeType() != Node::DOCUMENT_NODE)  {
        if (context->inDocument())
            context = context->ownerDocument();
        else
            context = context->highestAncestor();
    }

    NodeSet nodes;
    nodes.append(context);
    evaluate(nodes);
    
    evaluationContext = backupContext;
    return Value(nodes, Value::adopt);
}
Example #7
0
void MarkupAccumulator::appendStartMarkup(StringBuilder& result, Node& node, Namespaces* namespaces)
{
    switch (node.nodeType()) {
    case Node::TEXT_NODE:
        appendText(result, toText(node));
        break;
    case Node::COMMENT_NODE:
        appendComment(result, toComment(node).data());
        break;
    case Node::DOCUMENT_NODE:
        appendXMLDeclaration(result, toDocument(node));
        break;
    case Node::DOCUMENT_FRAGMENT_NODE:
        break;
    case Node::DOCUMENT_TYPE_NODE:
        appendDocumentType(result, toDocumentType(node));
        break;
    case Node::PROCESSING_INSTRUCTION_NODE:
        appendProcessingInstruction(result, toProcessingInstruction(node).target(), toProcessingInstruction(node).data());
        break;
    case Node::ELEMENT_NODE:
        appendElement(result, toElement(node), namespaces);
        break;
    case Node::CDATA_SECTION_NODE:
        appendCDATASection(result, toCDATASection(node).data());
        break;
    case Node::ATTRIBUTE_NODE:
        ASSERT_NOT_REACHED();
        break;
    }
}
Example #8
0
String OptionElement::collectOptionInnerText(const Element* element)
{
    String text;
    Node* n = element->firstChild();
    while (n) {
        if (n->nodeType() == Node::TEXT_NODE || n->nodeType() == Node::CDATA_SECTION_NODE)
            text += n->nodeValue();

        // skip script content
        if (n->isElementNode() && toScriptElement(static_cast<Element*>(n)))
            n = n->traverseNextSibling(element);
        else
            n = n->traverseNextNode(element);
    }
    return text;
}
void Range::insertNode( const Node &newNode )
{
    checkCommon();

    if( newNode.nodeType() == Node::ATTRIBUTE_NODE ||
        newNode.nodeType() == Node::ENTITY_NODE ||
        newNode.nodeType() == Node::NOTATION_NODE ||
        newNode.nodeType() == Node::DOCUMENT_NODE ||
        newNode.nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
        throw RangeException( RangeException::INVALID_NODE_TYPE_ERR);

    if( newNode.ownerDocument() != startContainer().ownerDocument() )
        throw DOMException( DOMException::WRONG_DOCUMENT_ERR );

    impl->insertNode(newNode);
}
Example #10
0
File: Element.cpp Project: 119/vdc
Element* Element::getChildElementNS(const XMLString& namespaceURI, const XMLString& localName) const
{
	Node* pNode = firstChild();
	while (pNode && !(pNode->nodeType() == Node::ELEMENT_NODE && pNode->namespaceURI() == namespaceURI && pNode->localName() == localName))
		pNode = pNode->nextSibling();
	return static_cast<Element*>(pNode);
}
bool DOMPatchSupport::innerPatchNode(Digest* oldDigest, Digest* newDigest, ExceptionState& exceptionState)
{
    if (oldDigest->m_sha1 == newDigest->m_sha1)
        return true;

    Node* oldNode = oldDigest->m_node;
    Node* newNode = newDigest->m_node;

    if (newNode->nodeType() != oldNode->nodeType() || newNode->nodeName() != oldNode->nodeName())
        return m_domEditor->replaceChild(oldNode->parentNode(), newNode, oldNode, exceptionState);

    if (oldNode->nodeValue() != newNode->nodeValue()) {
        if (!m_domEditor->setNodeValue(oldNode, newNode->nodeValue(), exceptionState))
            return false;
    }

    if (!oldNode->isElementNode())
        return true;

    // Patch attributes
    Element* oldElement = toElement(oldNode);
    Element* newElement = toElement(newNode);
    if (oldDigest->m_attrsSHA1 != newDigest->m_attrsSHA1) {
        // FIXME: Create a function in Element for removing all properties. Take in account whether did/willModifyAttribute are important.
        if (oldElement->hasAttributesWithoutUpdate()) {
            while (oldElement->attributeCount()) {
                const Attribute& attribute = oldElement->attributeAt(0);
                if (!m_domEditor->removeAttribute(oldElement, attribute.localName(), exceptionState))
                    return false;
            }
        }

        // FIXME: Create a function in Element for copying properties. cloneDataFromElement() is close but not enough for this case.
        if (newElement->hasAttributesWithoutUpdate()) {
            AttributeCollection attributes = newElement->attributes();
            AttributeCollection::const_iterator end = attributes.end();
            for (AttributeCollection::const_iterator it = attributes.begin(); it != end; ++it) {
                if (!m_domEditor->setAttribute(oldElement, it->name().localName(), it->value(), exceptionState))
                    return false;
            }
        }
    }

    bool result = innerPatchChildren(oldElement, oldDigest->m_children, newDigest->m_children, exceptionState);
    m_unusedNodesMap.remove(newDigest->m_sha1);
    return result;
}
Example #12
0
bool DOMPatchSupport::innerPatchNode(Digest* oldDigest, Digest* newDigest, ExceptionCode& ec)
{
    if (oldDigest->m_sha1 == newDigest->m_sha1)
        return true;

    Node* oldNode = oldDigest->m_node;
    Node* newNode = newDigest->m_node;

    if (newNode->nodeType() != oldNode->nodeType() || newNode->nodeName() != oldNode->nodeName())
        return m_domEditor->replaceChild(oldNode->parentNode(), newNode, oldNode, ec);

    if (oldNode->nodeValue() != newNode->nodeValue()) {
        if (!m_domEditor->setNodeValue(oldNode, newNode->nodeValue(), ec))
            return false;
    }

    if (oldNode->nodeType() != Node::ELEMENT_NODE)
        return true;

    // Patch attributes
    Element* oldElement = static_cast<Element*>(oldNode);
    Element* newElement = static_cast<Element*>(newNode);
    if (oldDigest->m_attrsSHA1 != newDigest->m_attrsSHA1) {
        // FIXME: Create a function in Element for removing all properties. Take in account whether did/willModifyAttribute are important.
        if (oldElement->hasAttributesWithoutUpdate()) {
            while (oldElement->attributeCount()) {
                Attribute* attr = oldElement->attributeItem(0);
                if (!m_domEditor->removeAttribute(oldElement, attr->localName(), ec))
                    return false;
            }
        }

        // FIXME: Create a function in Element for copying properties. setAttributesFromElement() is close but not enough for this case.
        if (newElement->hasAttributesWithoutUpdate()) {
            size_t numAttrs = newElement->attributeCount();
            for (size_t i = 0; i < numAttrs; ++i) {
                const Attribute* attribute = newElement->attributeItem(i);
                if (!m_domEditor->setAttribute(oldElement, attribute->name().localName(), attribute->value(), ec))
                    return false;
            }
        }
    }

    bool result = innerPatchChildren(oldElement, oldDigest->m_children, newDigest->m_children, ec);
    m_unusedNodesMap.remove(newDigest->m_sha1);
    return result;
}
Example #13
0
Node* DTDMap::getNamedItem(const XMLString& name) const
{
	Node* pCur = _pDocumentType->firstChild();
	while (pCur)
	{
		if (pCur->nodeType() == _type && pCur->nodeName() == name)
			return pCur;
		pCur = pCur->nextSibling();
	}
	return pCur;
}
Example #14
0
String createFullMarkup(const Node& node)
{
    // FIXME: This is never "for interchange". Is that right?
    String markupString = createMarkup(node, IncludeNode, 0);

    Node::NodeType nodeType = node.nodeType();
    if (nodeType != Node::DOCUMENT_NODE && nodeType != Node::DOCUMENT_TYPE_NODE)
        markupString = documentTypeString(node.document()) + markupString;

    return markupString;
}
Example #15
0
unsigned long DTDMap::length() const
{
	unsigned long n = 0;
	Node* pCur = _pDocumentType->firstChild();
	while (pCur)
	{
		if (pCur->nodeType() == _type) ++n;
		pCur = pCur->nextSibling();
	}
	return n;
}
Example #16
0
static inline bool isChildTypeAllowed(ContainerNode* newParent, Node* child)
{
    if (!child->isDocumentFragment())
        return newParent->childTypeAllowed(child->nodeType());

    for (Node* node = child->firstChild(); node; node = node->nextSibling()) {
        if (!newParent->childTypeAllowed(node->nodeType()))
            return false;
    }
    return true;
}
Example #17
0
  std::string Node::getRawData(bool includeName) const
  {
	
	std::string s, v = data();

	bool hasV = find_if(v.begin(), v.end(), std::not1(std::ptr_fun(isspace))) != v.end();

	if (includeName) {
		s += "<" + name();
		unsigned int attCnt = 0;
		for( unsigned int i = 0; i < getNodeCount(); ++i )
		{
			Node *node = getNode( i );
			if( node->nodeType() == Attribute) {
				s += " " + node->name() + "='"+ node->asString() + "'";
				attCnt++;
			}
		}
		if (attCnt == getNodeCount()) // have not elements
		{
			if (!hasV) s += "/>";
			else s += ">" + v + "</" + name() + ">\r\n";
			return s;
		}
		else 
		{
			s += ">\r\n";
			if (hasV) s += v + "\r\n";
		}
	}
	for( unsigned int i = 0; i < getNodeCount(); ++i )
	{
		Node *node = getNode( i );
		if( node->nodeType() == Element) 
			s += node->getRawData();
	}
	if (includeName) s += "</" + name() + ">\r\n";

	return s;
}
Example #18
0
File: Element.cpp Project: 119/vdc
void Element::normalize()
{
	Node* pCur = firstChild();
	while (pCur)
	{
		if (pCur->nodeType() == Node::ELEMENT_NODE)
		{
			pCur->normalize();
		}
		else if (pCur->nodeType() == Node::TEXT_NODE)
		{
			Node* pNext = pCur->nextSibling();
			while (pNext && pNext->nodeType() == Node::TEXT_NODE)
			{
				static_cast<Text*>(pCur)->appendData(pNext->nodeValue());
				removeChild(pNext);
				pNext = pCur->nextSibling();
			}
		}
		pCur = pCur->nextSibling();
	}
}
Example #19
0
static Node * getRootNode(Document * document)
{
    NodeListPtr children = document->childNodes();
    for (size_t i = 0; i < children->length(); ++i)
    {
        Node * child = children->item(i);
        /// Besides the root element there can be comment nodes on the top level.
        /// Skip them.
        if (child->nodeType() == Node::ELEMENT_NODE)
            return child;
    }

    throw Poco::Exception("No root node in document");
}
Example #20
0
bool Node::lookupAttribute( const std::string &name, std::string &data ) const
{
    for( unsigned int i = 0; i < getNodeCount(); ++i )
    {
        Node *node = getNode( i );
        if( node->nodeType() != Attribute || node->name() != name )
            continue;

        data = node->data();
        return true;
    }

    return false;
}
Example #21
0
static bool findNodesSurroundingContext(Document* document, RefPtr<Node>& nodeBeforeContext, RefPtr<Node>& nodeAfterContext)
{
    for (Node* node = document->firstChild(); node; node = NodeTraversal::next(node)) {
        if (node->nodeType() == Node::COMMENT_NODE && static_cast<CharacterData*>(node)->data() == fragmentMarkerTag) {
            if (!nodeBeforeContext)
                nodeBeforeContext = node;
            else {
                nodeAfterContext = node;
                return true;
            }
        }
    }
    return false;
}
Example #22
0
bool InspectorStyleSheet::inlineStyleSheetText(String* result) const
{
    if (!m_pageStyleSheet)
        return false;

    Node* ownerNode = m_pageStyleSheet->ownerNode();
    if (!ownerNode || ownerNode->nodeType() != Node::ELEMENT_NODE)
        return false;
    Element* ownerElement = static_cast<Element*>(ownerNode);
    if (ownerElement->tagName().lower() != "style")
        return false;
    *result = ownerElement->innerText();
    return true;
}
void MarkupAccumulator::appendStartMarkup(StringBuilder& result, Node& node, Namespaces* namespaces)
{
    switch (node.nodeType()) {
    case Node::TEXT_NODE:
        appendText(result, toText(node));
        break;
    case Node::ELEMENT_NODE:
        appendElement(result, toElement(node), namespaces);
        break;
    default:
        m_formatter.appendStartMarkup(result, node, namespaces);
        break;
    }
}
void ExtensionPointService::handleExtensions(Bundle::ConstPtr pBundle, std::istream& istr, GenericHandler handler, Direction dir)
{
	Poco::XML::DOMParser parser;
	parser.setFeature(Poco::XML::XMLReader::FEATURE_NAMESPACES, false);
	parser.setFeature(Poco::XML::DOMParser::FEATURE_FILTER_WHITESPACE, true);
	Poco::XML::InputSource source(istr);
	AutoPtr<Document> pDoc(parser.parse(&source));
	Node* pRoot = pDoc->firstChild();
	while (pRoot && pRoot->nodeName() != EXTENSIONS_ELEM)
	{
		pRoot = pRoot->nextSibling();
	}
	if (pRoot)
	{
		Node* pNode = (dir == DIR_FORWARD ? pRoot->firstChild() : pRoot->lastChild());
		while (pNode)
		{
			if (pNode->nodeType() == Node::ELEMENT_NODE)
			{
				if (pNode->nodeName() == EXTENSION_ELEM)
				{
					Element* pElem = static_cast<Element*>(pNode);
					const std::string& id = pElem->getAttribute(POINT_ATTR);
					if (!id.empty())
					{
						(this->*handler)(pBundle, id, pElem);
					}
					else
					{
						_logger.error(std::string("No point attribute found in extension element of bundle ")
							+ pBundle->symbolicName());
					}
				}
				else
				{
					_logger.warning(std::string("Unsupported element '")
						+ pNode->nodeName()
						+ "' found in "
						+ EXTENSIONS_XML
						+ " of bundle "
						+ pBundle->symbolicName());
				}
			}

			pNode = (dir == DIR_FORWARD ? pNode->nextSibling() : pNode->previousSibling());
		}
	}
	else throw Poco::NotFoundException("No extensions element found");
}
Example #25
0
void JSNode::setTextContent(JSC::ExecState* exec, JSC::JSValue value)
{
    Node* imp = static_cast<Node*>(impl());
    String nodeValue = valueToStringWithNullCheck(exec, value);

    if (imp->nodeType() == Node::ATTRIBUTE_NODE) {
        Element* ownerElement = static_cast<Attr*>(impl())->ownerElement();
        if (ownerElement && !allowSettingSrcToJavascriptURL(exec, ownerElement, imp->nodeName(), nodeValue))
            return;
    }

    ExceptionCode ec = 0;
    imp->setTextContent(nodeValue, ec);
    setDOMException(exec, ec);
}
Example #26
0
Node* DTDMap::item(unsigned long index) const
{
	unsigned long n = 0;
	Node* pCur = _pDocumentType->firstChild();
	while (pCur)
	{
		if (pCur->nodeType() == _type)
		{
			if (n == index) return pCur;
			++n;
		}
		pCur = pCur->nextSibling();
	}
	return pCur;
}
JSValue JSNamedNodeMap::setNamedItemNS(ExecState* exec, const ArgList& args)
{
    NamedNodeMap* imp = static_cast<NamedNodeMap*>(impl());
    ExceptionCode ec = 0;
    Node* newNode = toNode(args.at(0));

    if (newNode && newNode->nodeType() == Node::ATTRIBUTE_NODE && imp->element()) {
        if (!allowSettingSrcToJavascriptURL(exec, imp->element(), newNode->nodeName(), newNode->nodeValue()))
            return jsNull();
    }

    JSValue result = toJS(exec, globalObject(), WTF::getPtr(imp->setNamedItemNS(newNode, ec)));
    setDOMException(exec, ec);
    return result;
}
Example #28
0
JSValue JSNode::appendChild(ExecState* exec, const ArgList& args)
{
    Node* imp = static_cast<Node*>(impl());
    if (imp->nodeType() == Node::ATTRIBUTE_NODE && isAttrFrameSrc(static_cast<Attr*>(impl())->ownerElement(), imp->nodeName())) {
        setDOMException(exec, NOT_SUPPORTED_ERR);
        return jsNull();
    }

    ExceptionCode ec = 0;
    bool ok = imp->appendChild(toNode(args.at(0)), ec, true);
    setDOMException(exec, ec);
    if (ok)
        return args.at(0);
    return jsNull();
}
Example #29
0
Node *ProjectTree::nodeForFile(const FileName &fileName)
{
    Node *node = nullptr;
    for (const Project *project : SessionManager::projects()) {
        if (ProjectNode *projectNode = project->rootProjectNode()) {
            projectNode->forEachGenericNode([&](Node *n) {
                if (n->filePath() == fileName) {
                    // prefer file nodes
                    if (!node || (node->nodeType() != NodeType::File && n->nodeType() == NodeType::File))
                        node = n;
                }
            });
        }
    }
    return node;
}
Example #30
0
String OptionElement::collectOptionInnerText(const Element* element)
{
    /// M: @{
    StringBuilder text;
    for (Node* node = element->firstChild(); node; ) {
        if (node->isTextNode() || node->nodeType() == Node::CDATA_SECTION_NODE)
            text.append(node->nodeValue());
        // Text nodes inside script elements are not part of the option text.
        if (node->isElementNode() && toScriptLoaderIfPossible(toElement(node)))
            node = NodeTraversal::nextSkippingChildren(*node, element);
        else
            node = NodeTraversal::next(*node, element);
    }
    return text.toString();
    /// @}
}