Exemplo n.º 1
0
bool KHTMLReader::parse_ul(DOM::Element e)
{
    _list_depth++;
    bool popstateneeded = false;
    for (DOM::Node items = e.firstChild();!items.isNull();items = items.nextSibling()) {
        if (items.nodeName().string().toLower() == "li") {
            if (popstateneeded) {
                popState();
                //popstateneeded = false;
            }
            pushNewState();
            startNewLayout();
            popstateneeded = true;
            _writer->layoutAttribute(state()->paragraph, "COUNTER", "numberingtype", "1");
            _writer->layoutAttribute(state()->paragraph, "COUNTER", "righttext", ".");
            if (e.tagName().string().toLower() == "ol") {
                _writer->layoutAttribute(state()->paragraph, "COUNTER", "type", "1");
                _writer->layoutAttribute(state()->paragraph, "COUNTER", "numberingtype", "1");
                _writer->layoutAttribute(state()->paragraph, "COUNTER", "righttext", ".");
            } else {
                _writer->layoutAttribute(state()->paragraph, "COUNTER", "type", "10");
                _writer->layoutAttribute(state()->paragraph, "COUNTER", "numberingtype", "");
                _writer->layoutAttribute(state()->paragraph, "COUNTER", "righttext", "");
            }
            _writer->layoutAttribute(state()->paragraph, "COUNTER", "depth", QString("%1").arg(_list_depth - 1));
        }
        parseNode(items);
    }
    if (popstateneeded)
        popState();
    _list_depth--;
    return false;
}
Exemplo n.º 2
0
void KHTMLReader::parseNode(DOM::Node node)
{

    // check if this is a text node.
    DOM::Text t = node;
    if (!t.isNull()) {
        _writer->addText(state()->paragraph, t.data().string(), 1, state()->in_pre_mode);
        return; // no children anymore...
    }

    // is this really needed ? it can't do harm anyway.
    state()->format = _writer->currentFormat(state()->paragraph, true);
    state()->layout = _writer->currentLayout(state()->paragraph);
    pushNewState();

    DOM::Element e = node;

    bool go_recursive = true;

    if (!e.isNull()) {
        // get the CSS information
        parseStyle(e);
        // get the tag information
        go_recursive = parseTag(e);
    }
    if (go_recursive) {
        for (DOM::Node q = node.firstChild(); !q.isNull(); q = q.nextSibling()) {
            parseNode(q);
        }
    }
    popState();


}
Exemplo n.º 3
0
static void clear_from(DOM::Node n) {
  DOM::Node p = n.parentNode();
  while(1) {
    DOM::Node next = n.nextSibling();
    p.removeChild(n);
    if(next.isNull())
      break;
    n = next;
  }
}
Exemplo n.º 4
0
void DOMTreeView::showRecursive(const DOM::Node &pNode, const DOM::Node &node, uint depth)
{
  DOMListViewItem *cur_item;
  DOMListViewItem *parent_item = m_itemdict.value(pNode.handle(), 0);

  if (depth > m_maxDepth) {
    m_maxDepth = depth;
  }

  if (depth == 0) {
    cur_item = new DOMListViewItem(node, m_listView);
    m_document = pNode.ownerDocument();
  } else {
    cur_item = new DOMListViewItem(node, parent_item);
  }

  //kDebug(90180) << node.nodeName().string() << " [" << depth << "]";
  cur_item = addElement (node, cur_item, false);
  m_listView->setItemExpanded(cur_item, depth < m_expansionDepth);

  if(node.handle()) {
    m_itemdict.insert(node.handle(), cur_item);
  }

  DOM::Node child = node.firstChild();
  if (child.isNull()) {
    DOM::HTMLFrameElement frame = node;
    if (!frame.isNull()) {
      child = frame.contentDocument().documentElement();
    } else {
      DOM::HTMLIFrameElement iframe = node;
      if (!iframe.isNull())
        child = iframe.contentDocument().documentElement();
    }
  }
  while(!child.isNull()) {
    showRecursive(node, child, depth + 1);
    child = child.nextSibling();
  }

  const DOM::Element element = node;
  if (!m_bPure) {
    if (!element.isNull() && !element.firstChild().isNull()) {
      if(depth == 0) {
	cur_item = new DOMListViewItem(node, m_listView, cur_item);
	m_document = pNode.ownerDocument();
      } else {
	cur_item = new DOMListViewItem(node, parent_item, cur_item);
      }
      //kDebug(90180) << "</" << node.nodeName().string() << ">";
      cur_item = addElement(element, cur_item, true);
//       m_listView->setItemExpanded(cur_item, depth < m_expansionDepth);
    }
  }
}
Exemplo n.º 5
0
void HTMLEnhancer::postUnenhanceNode(DOM::Node domNode)
{
	DOM::Node child, next;
	kNodeAttrs *attrs;

	if(domNode.isNull())
		return;

	//Try to remove the EmptyTextAsChildOfAChildlessElement Node first if present
	if(domNode.hasChildNodes())
	{
		child = domNode.firstChild();
		while(!child.isNull())
		{
			attrs = m_wkafkapart->getAttrs(child);
			next = child.nextSibling();
			if(attrs && attrs->specialBehavior() == kNodeAttrs::emptyTextAsChildOfAChildlessElement)
				kafkaCommon::removeDomNode(child);
			child = next;
		}
	}

	//Then try to remove the emptyTextSurroundingBlockElement* Nodes if present.
	if(!domNode.previousSibling().isNull())
	{
		attrs = m_wkafkapart->getAttrs(domNode.previousSibling());
		if(attrs && attrs->specialBehavior() == kNodeAttrs::emptyTextSurroundingBlockElementAtTheLeft)
			kafkaCommon::removeDomNode(domNode.previousSibling());
	}
	if(!domNode.nextSibling().isNull())
	{
		attrs = m_wkafkapart->getAttrs(domNode.nextSibling());
		if(attrs && attrs->specialBehavior() == kNodeAttrs::emptyTextSurroundingBlockElementAtTheRight)
			kafkaCommon::removeDomNode(domNode.nextSibling());
	}
}
Exemplo n.º 6
0
void ManipulateNodeCommand::remove()
{
  DOM::DocumentFragment frag = _node;

  if (frag.isNull()) {	// do a normal remove
    _node = _parent.removeChild(_node);

  } else {		// remove fragment nodes and recreate fragment
    DOM::DocumentFragment newfrag = _parent.ownerDocument().createDocumentFragment();

    for (DOM::Node i = frag.firstChild(); !i.isNull(); i = i.nextSibling()) {
      newfrag.appendChild(_parent.removeChild(i));
    }

    _node = newfrag;
  }
}
Exemplo n.º 7
0
bool KHTMLReader::parse_pre(DOM::Element e)
{
#if 0 // see Bug #74601 (normal): kword doesn't recognize PRE-tags in HTML
    //pushNewState();
    /// \todo set fixed width font
    DOM::HTMLElement htmlelement(e);
    if (! htmlelement.isNull())
        _writer->addText(state()->paragraph, htmlelement.innerHTML().string(), 1);
    startNewParagraph();
    //popState();
    return false; // children are already handled.
#else
    pushNewState();
    state()->in_pre_mode = true;
    for (DOM::Node q = e.firstChild(); !q.isNull(); q = q.nextSibling()) {
        parseNode(q); // parse everything...
    }
    popState();
    return false; // children are already handled.
#endif
}
Exemplo n.º 8
0
void DOMTreeView::slotShowTree(const DOM::Node &pNode)
{
  DOM::Node child;

  m_listView->clear();
  m_itemdict.clear();

  try
  {
    child = pNode.firstChild();
  }
  catch (DOM::DOMException &)
  {
    return;
  }

  while(!child.isNull()) {
    showRecursive(0, child, 0);
    child = child.nextSibling();
  }

  m_maxDepth--;
  //kdDebug(90180) << " Max Depth: " << m_maxDepth << endl;
}
Exemplo n.º 9
0
void DOMTreeView::saveRecursive(const DOM::Node &pNode, int indent)
{
  const QString nodeName(pNode.nodeName().string());
  QString text;
  QString strIndent;
  strIndent.fill(' ', indent);
  const DOM::Element element = static_cast<const DOM::Element>(pNode);

  text = strIndent;

  if ( !element.isNull() ) {
    if (nodeName.at(0)=='-') {
      /* Don't save khtml internal tags '-konq..'
       * Approximating it with <DIV>
       */
      text += "<DIV> <!-- -KONG_BLOCK -->";
    } else {
      text += "<" + nodeName;

      QString attributes;
      DOM::Attr attr;
      const DOM::NamedNodeMap attrs = element.attributes();
      unsigned long lmap = attrs.length();
      for( uint j=0; j<lmap; j++ ) {
	attr = static_cast<DOM::Attr>(attrs.item(j));
	attributes += " " + attr.name().string() + "=\"" + attr.value().string() + "\"";
      }
      if (!(attributes.isEmpty())){
	text += " ";
      }

      text += attributes.simplifyWhiteSpace();

      if(element.firstChild().isNull()) {
	text += "/>";
      } else {
	text += ">";
      }
    }
  } else {
    text = strIndent + pNode.nodeValue().string();
  }

  kdDebug(90180) << text << endl;
  if (!(text.isEmpty())) {
    (*m_textStream) << text << endl;
  }

  DOM::Node child = pNode.firstChild();
  while(!child.isNull()) {
    saveRecursive(child, indent+2);
    child = child.nextSibling();
  }

  if (!(element.isNull()) && (!(element.firstChild().isNull()))) {
    if (nodeName.at(0)=='-') {
      text = strIndent + "</DIV> <!-- -KONG_BLOCK -->";
    } else {
      text = strIndent + "</" + pNode.nodeName().string() + ">";
    }
    kdDebug(90180) << text << endl;
    (*m_textStream) << text << endl;
  }
}
Exemplo n.º 10
0
void HTMLEnhancer::postEnhanceNode(DOM::Node domNode)
{
	DOM::Node textNode;
	kNodeAttrs *props;
	QTag *qTag;
	bool isInline;

	if(domNode.isNull())
		return;

	//If domNode is a Block and there is no text around, and if domNode's parent can handle
	//text or a P tag, add an empty text DOM::Node
	// so that the user can access this area.
	qTag = QuantaCommon::tagFromDTD(m_wkafkapart->getCurrentDoc()->defaultDTD(),
		domNode.nodeName().string());
	isInline = kafkaCommon::isInline(domNode.nodeName().string());
	if(domNode.nodeType() == DOM::Node::ELEMENT_NODE &&
		(!isInline || (isInline && qTag && qTag->isSingle())))
	{
		qTag = QuantaCommon::tagFromDTD(m_wkafkapart->getNode(domNode.parentNode()));

		if((domNode.nextSibling().isNull() ||
			(!domNode.nextSibling().isNull() &&
			domNode.nextSibling().nodeType() == DOM::Node::ELEMENT_NODE &&
			!kafkaCommon::isInline(domNode.nextSibling().nodeName().string())))
			&& qTag && (qTag->isChild("#text", false) || qTag->isChild("p", false)) &&
			domNode.nodeName().string().lower() != "p")
		{
			textNode = kafkaCommon::createTextDomNode("",
				m_wkafkapart->getKafkaWidget()->document());
			props = m_wkafkapart->connectDomNodeToQuantaNode(textNode, 0L);
			props->setIsLinkedToNode(false);
			props->setSpecialBehavior(kNodeAttrs::emptyTextSurroundingBlockElementAtTheRight);
			kafkaCommon::insertDomNode(textNode, domNode.parentNode(),
				domNode.nextSibling());
		}

		if((domNode.previousSibling().isNull() || (!domNode.previousSibling().isNull() &&
			domNode.previousSibling().nodeType() == DOM::Node::ELEMENT_NODE &&
			!kafkaCommon::isInline(domNode.previousSibling().nodeName().string())))
			&& qTag && (qTag->isChild("#text", false) || qTag->isChild("p", false)) &&
			domNode.nodeName().string().lower() != "p")
		{
			textNode = kafkaCommon::createTextDomNode("",
				m_wkafkapart->getKafkaWidget()->document());
			props = m_wkafkapart->connectDomNodeToQuantaNode(textNode, 0L);
			props->setIsLinkedToNode(false);
			props->setSpecialBehavior(kNodeAttrs::emptyTextSurroundingBlockElementAtTheLeft);
			kafkaCommon::insertDomNode(textNode, domNode.parentNode(),
				domNode);
		}
	}

	//If domNode is an childless element, and if it can handle Text or a P tag,
	//add an empty text DOM::Node so that the
	//user can access this area.
	qTag = QuantaCommon::tagFromDTD(m_wkafkapart->getNode(domNode));
	if(domNode.nodeType() == DOM::Node::ELEMENT_NODE &&
		!domNode.hasChildNodes() && qTag && (qTag->isChild("#text", false) ||
		qTag->isChild("p", false)))
	{
		textNode = kafkaCommon::createTextDomNode("",
			m_wkafkapart->getKafkaWidget()->document());
		props = m_wkafkapart->connectDomNodeToQuantaNode(textNode, 0L);
		props->setIsLinkedToNode(false);
		props->setSpecialBehavior(kNodeAttrs::emptyTextAsChildOfAChildlessElement);
		kafkaCommon::insertDomNode(textNode, domNode);
	}
}
Exemplo n.º 11
0
bool KHTMLReader::parse_table(DOM::Element e)
{
    if (_writer->isInTable()) {
        // We are already inside of a table. Tables in tables are not supported
        // yet. So, just add that table-content as text.
        for (DOM::Node rows = e.firstChild().firstChild();!rows.isNull();rows = rows.nextSibling())
            if (!rows.isNull() && rows.nodeName().string().toLower() == "tr")
                for (DOM::Node cols = rows.firstChild();!cols.isNull();cols = cols.nextSibling())
                    if (!cols.isNull())
                        parseNode(cols);
        return false;
    }

    DOM::Element table_body = e.firstChild();
    if (table_body.isNull()) {
        // If the table_body is empty, we don't continue cause else
        // KHTML will throw a DOM::DOMException if we try to access
        // the null element.
        return true;
    }

    int tableno = _writer->createTable();
    int nrow = 0;
    int ncol = 0;
    bool has_borders = false;
    QColor bgcolor = parsecolor("#FFFFFF");

    if (!table_body.getAttribute("bgcolor").string().isEmpty())
        bgcolor = parsecolor(table_body.getAttribute("bgcolor").string());
    if ((e.getAttribute("border").string().toInt() > 0))
        has_borders = true;

    // fixme rewrite this proper
    //(maybe using computed sizes from khtml if thats once exported)
    for (DOM::Node rowsnode = table_body.firstChild();!rowsnode.isNull();rowsnode = rowsnode.nextSibling()) {
        DOM::Element rows = rowsnode;
        if (!rows.isNull() && rows.tagName().string().toLower() == "tr") {
            QColor obgcolor = bgcolor;
            if (!rows.getAttribute("bgcolor").string().isEmpty())
                bgcolor = parsecolor(rows.getAttribute("bgcolor").string());

            ncol = 0;
            for (DOM::Node colsnode = rows.firstChild();!colsnode.isNull();colsnode = colsnode.nextSibling()) {
                DOM::Element cols = colsnode;
                const QString nodename = cols.isNull() ? QString() : cols.nodeName().string().toLower();
                if (nodename == "td" || nodename == "th") {
                    QColor bbgcolor = bgcolor;
                    if (!cols.getAttribute("bgcolor").string().isEmpty())
                        bgcolor = parsecolor(cols.getAttribute("bgcolor").string());

                    pushNewState();
                    QRect colrect = cols.getRect();
                    state()->frameset = _writer->createTableCell(tableno, nrow, ncol, 1, colrect);
                    state()->frameset.firstChild().toElement().setAttribute("bkRed", bgcolor.red());
                    state()->frameset.firstChild().toElement().setAttribute("bkGreen", bgcolor.green());
                    state()->frameset.firstChild().toElement().setAttribute("bkBlue", bgcolor.blue());
                    if (has_borders) {
                        state()->frameset.firstChild().toElement().setAttribute("lWidth", 1);
                        state()->frameset.firstChild().toElement().setAttribute("rWidth", 1);
                        state()->frameset.firstChild().toElement().setAttribute("bWidth", 1);
                        state()->frameset.firstChild().toElement().setAttribute("tWidth", 1);
                    }

                    // fixme don't guess. get it right.
                    state()->paragraph = _writer->addParagraph(state()->frameset);
                    parseNode(cols);
                    _writer->cleanUpParagraph(state()->paragraph);
                    popState();
                    ncol++;
                    bgcolor = bbgcolor;
                }
            }
            nrow++;
            bgcolor = obgcolor;
        }
    }
    _writer->finishTable(tableno/*,0,0,r.right()-r.left(),r.bottom()-r.top()*/); // FIXME find something better.
    startNewParagraph(false, false);
    _writer->createInline(state()->paragraph, _writer->fetchTableCell(tableno, 0, 0));
    startNewParagraph(false, false);
    return false; // we do our own recursion
}
Exemplo n.º 12
0
MoveNodeCommand::MoveNodeCommand(const DOM::Node &node, const DOM::Node &parent, const DOM::Node &after)
: _node(node), new_parent(parent), new_after(after)
{
  old_parent = node.parentNode();
  old_after = node.nextSibling();
}