summaryrefslogtreecommitdiffstats
path: root/quanta/parts/kafka/htmlenhancer.cpp
blob: 3ea0b78e8252ae6984c5bc81d983390255741959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/***************************************************************************
                              htmltranslator.cpp
                             -------------------

    copyright            : (C) 2003, 2004 - Nicolas Deschildre
    email                : ndeschildre@tdewebdev.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <dom/dom_node.h>
#include <dom/dom_string.h>
#include <dom/dom_exception.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <tdeconfig.h>

#include "quantacommon.h"
#include "document.h"
#include "tag.h"
#include "node.h"
#include "wkafkapart.h"
#include "nodeproperties.h"
#include "kafkacommon.h"
#include "qextfileinfo.h"
#include "viewmanager.h"

#include "htmlenhancer.h"

HTMLEnhancer::HTMLEnhancer(KafkaDocument *_wkafkapart)
	: NodeEnhancer()
{
	m_showIconForScripts = true;
	m_wkafkapart = _wkafkapart;
	m_stddirs = new TDEStandardDirs();
}

HTMLEnhancer::~HTMLEnhancer()
{
	delete m_stddirs;
}

bool HTMLEnhancer::enhanceNode(Node *node, DOM::Node parentDNode, DOM::Node nextDNode)
{
	DOM::Node domNode, domNode2, attr, *ptDomNode;
	bool tbody, goUp;
	Node *n;
	TQString script, filename, text, oldName;
	KURL url, baseURL;
	int oldType;

	//FIRST update the src attr with the baseURL
	if(node->rootNode())
	{
		domNode = node->rootNode()->attributes().getNamedItem("src");
		if(!domNode.isNull())
		{
			baseURL.setPath(ViewManager::ref()->activeDocument()->url().directory());
			QuantaCommon::setUrl(url, domNode.nodeValue().string());
			url = QExtFileInfo::toAbsolute(url, baseURL);
			domNode.setNodeValue(url.url());
#ifdef HEAVY_DEBUG
			kdDebug(25001)<< "HTMLTranslator::translateNode() - new src : " << url.url() << endl;
#endif
		}
	}
        
        //THEN update the href attr of the LINK node with the baseURL
        if(node->tag->name.lower() == "link" && node->rootNode())
        {
          domNode = node->rootNode()->attributes().getNamedItem("href");
          if(!domNode.isNull())
          {
            baseURL.setPath(ViewManager::ref()->activeDocument()->url().directory());
            QuantaCommon::setUrl(url, domNode.nodeValue().string());
            url = QExtFileInfo::toAbsolute(url, baseURL);
            domNode.setNodeValue(url.url());
#ifdef HEAVY_DEBUG
            kdDebug(25001)<< "HTMLTranslator::translateNode() - new href : " << url.url() << endl;
#endif        
          }
        }

	//THEN if it is the style element, add a DOM::Node::TEXT_NODE child gathering all the CSS
	//by default, the parser parse it as a script, which can't be translated in DOM::Nodes.
	if((node->tag->type == Tag::XmlTag && node->tag->name.lower() == "style") ||
		(node->tag->type == Tag::ScriptTag && node->tag->name.lower().contains("style") != 0))
	{
		//If the style Node doesn't exists, create it
		if(!node->rootNode())
		{
			oldType = node->tag->type;
			node->tag->type = Tag::XmlTag;
			oldName = node->tag->name;
			node->tag->name = "style";
			m_wkafkapart->buildKafkaNodeFromNode(node);
			node->tag->type = oldType;
			node->tag->name = oldName;
		}

		if(node->rootNode())
		{
			domNode = *node->rootNode();
			n = node->child;
			text = "";
			goUp = false;
			while(n)
			{
				text += n->tag->tagStr();
				n = kafkaCommon::getNextNode(n, goUp, node);
			}
#ifdef HEAVY_DEBUG
			kdDebug(25001)<< "HTMLTranslator::translateNode() - CSS code : " << text << endl;
#endif
			domNode2 = kafkaCommon::createTextDomNode(text, m_wkafkapart->getKafkaWidget()->document());
			if(!kafkaCommon::insertDomNode(domNode2, domNode))
				return false;
			m_wkafkapart->connectDomNodeToQuantaNode(domNode2, node);
		}
	}
    
    TQTag* qTag = QuantaCommon::tagFromDTD(m_wkafkapart->getCurrentDoc()->defaultDTD(),
                                          parentDNode.nodeName().string());

	//THEN replace, if asked, scripts by a little icon.
    if(node->tag->type == Tag::ScriptTag && m_showIconForScripts && qTag->isChild("IMG", false))
	{
		script = node->tag->name.left(node->tag->name.find("block", 0, false) - 1).lower();
#ifdef LIGHT_DEBUG
		kdDebug(25001)<< "HTMLTranslator::translateNode() - BLOCK:" << script << ":" << endl;
#endif
		filename = m_stddirs->findResource("data", "kafkapart/pics/" + script + ".png" );
		if(!filename.isEmpty())
		{
			//FIXME DTD!
			domNode = kafkaCommon::createDomNode("IMG", m_wkafkapart->defaultDTD(),
				m_wkafkapart->getKafkaWidget()->document());
				
			kafkaCommon::editDomNodeAttribute(domNode, "IMG", m_wkafkapart->defaultDTD(), "src",
				filename, m_wkafkapart->getKafkaWidget()->document());

			//Add a tooltip indicating the content of the script
			n = node->child;
			text = "";
			goUp = false;
			while(n && n != node)
			{
				text += n->tag->tagStr();
 				n = kafkaCommon::getNextNode(n, goUp, node);
			}
			//if(text == "")
			//	text = i18n("Empty")
			kafkaCommon::editDomNodeAttribute(domNode, "img", m_wkafkapart->defaultDTD(),
				"title", text, m_wkafkapart->getKafkaWidget()->document());

			if(!kafkaCommon::insertDomNode(domNode, parentDNode, nextDNode))
				return false;
			m_wkafkapart->connectDomNodeToQuantaNode(domNode, node);
		}
	}
	
	//THEN if it is a comment, add a little icon ;o)
    if(node->tag->type == Tag::Comment && m_showIconForScripts && qTag->isChild("IMG", false))
	{
#ifdef LIGHT_DEBUG
		kdDebug(25001)<< "HTMLTranslator::translateNode() - Comment" << endl;
#endif
            
        filename = m_stddirs->findResource("data", "kafkapart/pics/comment.png" );
        if(!filename.isEmpty())
        {
            //FIXME DTD!
            domNode = kafkaCommon::createDomNode("IMG", m_wkafkapart->defaultDTD(),
                m_wkafkapart->getKafkaWidget()->document());
            kafkaCommon::editDomNodeAttribute(domNode, "IMG", m_wkafkapart->defaultDTD(), "src",
                filename, m_wkafkapart->getKafkaWidget()->document());

            //Add a tooltip indicating the content of the script
            n = node->child;
            text = "";
            goUp = false;
            while(n && n != node)
            {
                text += n->tag->tagStr();
                n = kafkaCommon::getNextNode(n, goUp, node);
            }
            //if(text == "")
            //	text = i18n("Empty")
            kafkaCommon::editDomNodeAttribute(domNode, "img", m_wkafkapart->defaultDTD(),
                "title", text, m_wkafkapart->getKafkaWidget()->document());

            if(!kafkaCommon::insertDomNode(domNode, parentDNode, nextDNode))
                return false;
            m_wkafkapart->connectDomNodeToQuantaNode(domNode, node);
        }
    }

	//THEN add a TBODY tag if necessary
	if(node->rootNode() && node->rootNode()->nodeName().string().lower() == "table")
	{
		tbody = false;
		n = node->child;
		while(n)
		{
			if(n->tag->name.lower() == "tbody")
				tbody = true;
			n = n->next;
		}
		if(!tbody)
		{
			domNode = kafkaCommon::createDomNode("TBODY", m_wkafkapart->defaultDTD(),
				m_wkafkapart->getKafkaWidget()->htmlDocument());
			if(!kafkaCommon::insertDomNode(domNode, *node->rootNode()))
				return false;
			m_wkafkapart->connectDomNodeToQuantaNode(domNode, node);
			ptDomNode = new DOM::Node(domNode);
			node->setLeafNode(ptDomNode);
		}
	}

	//THEN add a red dotted border to FORM tags.
	if(node->rootNode() && node->rootNode()->nodeName().string().lower() == "form")
	{
		kafkaCommon::editDomNodeAttribute(*node->rootNode(), node, "style", "border: 1px dotted red",
			m_wkafkapart->getKafkaWidget()->document());
	}

    // THEN add a tooltip indicating the content of the name attribute
    if(node->rootNode() && node->rootNode()->nodeName().string().lower() == "input")
    {
        domNode = *(node->rootNode());
        TQString text = node->tag->attributeValue("name");
        kafkaCommon::editDomNodeAttribute(domNode, "input", m_wkafkapart->defaultDTD(),
                                          "title", text, m_wkafkapart->getKafkaWidget()->document());
    }

	//THEN add a blue dotted border to DL, OL, UL tags
	if(node->rootNode())
	{
		text = node->rootNode()->nodeName().string().lower();
		if(text == "dl" || text == "ol" || text == "ul")
		{
			kafkaCommon::editDomNodeAttribute(*node->rootNode(), node, "style", "border: 1px dotted blue",
				m_wkafkapart->getKafkaWidget()->document());
		}
	}
    
	//THEN add a minimal border for borderless tables
	//TODO: make it configurable, and look if CSS hasn't defined a border first
	if(node->rootNode() && node->rootNode()->nodeName().string().lower() == "table")
	{
		attr = node->rootNode()->attributes().getNamedItem("border");
		if(attr.isNull() || (!attr.isNull() && attr.nodeValue().string() == "0"))
		{
			kafkaCommon::editDomNodeAttribute(*node->rootNode(), node, "border", "1",
				m_wkafkapart->getKafkaWidget()->document());
		}
	}

    //THEN add a blue dotted border to DIV tags
    if(node->rootNode())
    {
        text = node->rootNode()->nodeName().string().lower();
        if(text == "div")
        {
            kafkaCommon::editDomNodeAttribute(*node->rootNode(), node, "style", "border: 1px dotted green",
                                               m_wkafkapart->getKafkaWidget()->document());
        }
    }
    
    return true;
}

void HTMLEnhancer::postEnhanceNode(DOM::Node domNode)
{
	DOM::Node textNode;
	kNodeAttrs *props;
	TQTag *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);
	}
}

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());
	}
}

void HTMLEnhancer::readConfig(TDEConfig *m_config)
{
	m_config->setGroup("HTML Enhancer");
	m_showIconForScripts = m_config->readBoolEntry("Show Scripts Icons", true);
}