Example #1
0
/* static */
void GeckoUtils::GatherTextUnder(nsIDOMNode* aNode, nsString& aResult) 
{
  nsAutoString text;
  nsCOMPtr<nsIDOMNode> node;
  aNode->GetFirstChild(getter_AddRefs(node));
  PRUint32 depth = 1;
  while (node && depth) {
    nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(node));
    PRUint16 nodeType;
    node->GetNodeType(&nodeType);
    if (charData && nodeType == nsIDOMNode::TEXT_NODE) {
      // Add this text to our collection.
      text += NS_LITERAL_STRING(" ");
      nsAutoString data;
      charData->GetData(data);
      text += data;
    }
    else {
      nsCOMPtr<nsIDOMHTMLImageElement> img(do_QueryInterface(node));
      if (img) {
        nsAutoString altText;
        img->GetAlt(altText);
        if (!altText.IsEmpty()) {
          text = altText;
          break;
        }
      }
    }
    
    // Find the next node to test.
    PRBool hasChildNodes;
    node->HasChildNodes(&hasChildNodes);
    if (hasChildNodes) {
      nsCOMPtr<nsIDOMNode> temp = node;
      temp->GetFirstChild(getter_AddRefs(node));
      depth++;
    }
    else {
      nsCOMPtr<nsIDOMNode> nextSibling;
      node->GetNextSibling(getter_AddRefs(nextSibling));
      if (nextSibling)
        node = nextSibling;
      else {
        nsCOMPtr<nsIDOMNode> parentNode;
        node->GetParentNode(getter_AddRefs(parentNode));
        if (!parentNode)
          node = nsnull;
        else {
          nsCOMPtr<nsIDOMNode> nextSibling2;
          parentNode->GetNextSibling(getter_AddRefs(nextSibling2));
          node = nextSibling2;
          depth--;
        }
      }
    }
  }
  
  text.CompressWhitespace();
  aResult = text;
}
Example #2
0
NativeString Clipboard::pasteString()
{
	// 1
    XEvent e = checkEvent();

	// If we're being given a list of targets (possible conversions)
	if( e.xselection.target != XA_TARGETS ) {
		log::error("Target ", e.xselection.target, " should be XA_TARGETS ", XA_TARGETS);
		return {};
	}
    auto prop = readPropertyFromSel(e);
    if( ! prop ) {
		return {};
    }

	// request the STRING type
	Atom targetToBeRequested = prop.get("STRING");
	if( targetToBeRequested == None ) {
		log::error("No matching datatypes for clipboard!");
		return {};
	}
	DEBUG_CLIP("Now requesting type " << resourceManager.getAtomName(targetToBeRequested));
	XConvertSelection(resourceManager->dpy, sel, targetToBeRequested, sel, window.get(), CurrentTime);
	XFlush(resourceManager->dpy);

	// 2
    XEvent e2 = checkEvent();

    NativeString result;
	if( e2.xselection.target == targetToBeRequested ) {
	    if( auto prop = readPropertyFromSel(e2) ) {
			result.str.resize(prop.charSize());
			std::copy_n(prop.charData(),result.str.size(),result.str.begin());
	    }
	}
	return result;
}