summaryrefslogtreecommitdiffstats
path: root/src/xml/simple-node.cpp
blob: 82cac3a727cdece5292d04387afbb88fbec386a6 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 *  Garbage collected XML node implementation
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2018 Authors
 * Copyright 2003-2005 MenTaLguY <mental@rydia.net>
 * Copyright 2003 Nathan Hurst
 * Copyright 1999-2003 Lauris Kaplinski
 * Copyright 2000-2002 Ximian Inc.
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

// clang-format off
#include "xml/node.h"
#include "xml/simple-node.h"
// clang-format on

#include <algorithm>
#include <cstring>
#include <string>

#include <glib.h>

#include "preferences.h"

#include "xml/node-event-vector.h"
#include "xml/node-fns.h"
#include "debug/event-tracker.h"
#include "debug/simple-event.h"
#include "util/format.h"

#include "attribute-rel-util.h"

namespace Inkscape {

namespace XML {

namespace {

std::shared_ptr<std::string> stringify_node(Node const &node) {
    gchar *string;
    switch (node.type()) {
    case NodeType::ELEMENT_NODE: {
        char const *id=node.attribute("id");
        if (id) {
            string = g_strdup_printf("element(%p)=%s(#%s)", &node, node.name(), id);
        } else {
            string = g_strdup_printf("element(%p)=%s", &node, node.name());
        }
    } break;
    case NodeType::TEXT_NODE:
        string = g_strdup_printf("text(%p)=%s", &node, node.content());
        break;
    case NodeType::COMMENT_NODE:
        string = g_strdup_printf("comment(%p)=<!--%s-->", &node, node.content());
        break;
    case NodeType::DOCUMENT_NODE:
        string = g_strdup_printf("document(%p)", &node);
        break;
    default:
        string = g_strdup_printf("unknown(%p)", &node);
    }
    std::shared_ptr<std::string> result = std::make_shared<std::string>(string);
    g_free(string);
    return result;
}

typedef Debug::SimpleEvent<Debug::Event::XML> DebugXML;

class DebugXMLNode : public DebugXML {
public:
    DebugXMLNode(Node const &node, char const *name)
    : DebugXML(name)
    {
        _addProperty("node", stringify_node(node));
    }
};

class DebugAddChild : public DebugXMLNode {
public:
    DebugAddChild(Node const &node, Node const &child, Node const *prev)
    : DebugXMLNode(node, "add-child")
    {
        _addProperty("child", stringify_node(child));
        _addProperty("position", prev ? prev->position() + 1 : 0 );
    }
};

class DebugRemoveChild : public DebugXMLNode {
public:
    DebugRemoveChild(Node const &node, Node const &child)
    : DebugXMLNode(node, "remove-child")
    {
        _addProperty("child", stringify_node(child));
    }
};

class DebugSetChildPosition : public DebugXMLNode {
public:
    DebugSetChildPosition(Node const &node, Node const &child,
                          Node const *old_prev, Node const *new_prev)
    : DebugXMLNode(node, "set-child-position")
    {
        _addProperty("child", stringify_node(child));

        unsigned old_position = ( old_prev ? old_prev->position() : 0 );
        unsigned position = ( new_prev ? new_prev->position() : 0 );
        if ( position > old_position ) {
            --position;
        }

        _addProperty("position", position);
    }
};

class DebugSetContent : public DebugXMLNode {
public:
    DebugSetContent(Node const &node,
                    Util::ptr_shared content)
    : DebugXMLNode(node, "set-content")
    {
        _addProperty("content", content.pointer());
    }
};

class DebugClearContent : public DebugXMLNode {
public:
    DebugClearContent(Node const &node)
    : DebugXMLNode(node, "clear-content")
    {}
};

class DebugSetAttribute : public DebugXMLNode {
public:
    DebugSetAttribute(Node const &node,
                      GQuark name,
                      Util::ptr_shared value)
    : DebugXMLNode(node, "set-attribute")
    {
        _addProperty("name", g_quark_to_string(name));
        _addProperty("value", value.pointer());
    }
};

class DebugClearAttribute : public DebugXMLNode {
public:
    DebugClearAttribute(Node const &node, GQuark name)
    : DebugXMLNode(node, "clear-attribute")
    {
        _addProperty("name", g_quark_to_string(name));
    }
};

class DebugSetElementName : public DebugXMLNode {
public:
    DebugSetElementName(Node const& node, GQuark name)
    : DebugXMLNode(node, "set-name")
    {
        _addProperty("name", g_quark_to_string(name));
    }
};

}

using Util::ptr_shared;
using Util::share_string;
using Util::share_unsafe;

SimpleNode::SimpleNode(int code, Document *document)
: Node(), _name(code), _attributes(), _child_count(0),
  _cached_positions_valid(false)
{
    g_assert(document != nullptr);

    this->_document = document;
    this->_parent = this->_next = this->_prev = nullptr;
    this->_first_child = this->_last_child = nullptr;

    _observers.add(_subtree_observers);
}

SimpleNode::SimpleNode(SimpleNode const &node, Document *document)
: Node(),
  _cached_position(node._cached_position),
  _name(node._name), _attributes(), _content(node._content),
  _child_count(node._child_count),
  _cached_positions_valid(node._cached_positions_valid)
{
    g_assert(document != nullptr);

    _document = document;
    _parent = _next = _prev = nullptr;
    _first_child = _last_child = nullptr;

    for ( SimpleNode *child = node._first_child ;
          child != nullptr ; child = child->_next )
    {
        SimpleNode *child_copy=dynamic_cast<SimpleNode *>(child->duplicate(document));

        child_copy->_setParent(this);
        if (_last_child) { // not the first iteration
            _last_child->_next = child_copy;
            child_copy->_prev = _last_child;
        } else {
            _first_child = child_copy;
        }
        _last_child = child_copy;

        child_copy->release(); // release to avoid a leak
    }

    _attributes = node._attributes;

    _observers.add(_subtree_observers);
}

gchar const *SimpleNode::name() const {
    return g_quark_to_string(_name);
}

gchar const *SimpleNode::content() const {
    return this->_content;
}

gchar const *SimpleNode::attribute(gchar const *name) const {
    g_return_val_if_fail(name != nullptr, NULL);

    GQuark const key = g_quark_from_string(name);

    for (const auto & iter : _attributes)
    {
        if ( iter.key == key ) {
            return iter.value;
        }
    }

    return nullptr;
}

unsigned SimpleNode::position() const {
    g_return_val_if_fail(_parent != nullptr, 0);
    return _parent->_childPosition(*this);
}

unsigned SimpleNode::_childPosition(SimpleNode const &child) const {
    if (!_cached_positions_valid) {
        unsigned position=0;
        for ( SimpleNode *sibling = _first_child ;
              sibling ; sibling = sibling->_next )
        {
            sibling->_cached_position = position;
            position++;
        }
        _cached_positions_valid = true;
    }
    return child._cached_position;
}

Node *SimpleNode::nthChild(unsigned index) {
    SimpleNode *child = _first_child;
    for ( ; index > 0 && child ; child = child->_next ) {
        index--;
    }
    return child;
}

bool SimpleNode::matchAttributeName(gchar const *partial_name) const {
    g_return_val_if_fail(partial_name != nullptr, false);

    for ( const auto & iter : _attributes )
    {
        gchar const *name = g_quark_to_string(iter.key);
        if (std::strstr(name, partial_name)) {
            return true;
        }
    }

    return false;
}

void SimpleNode::_setParent(SimpleNode *parent) {
    if (_parent) {
        _subtree_observers.remove(_parent->_subtree_observers);
    }
    _parent = parent;
    if (parent) {
        _subtree_observers.add(parent->_subtree_observers);
    }
}

void SimpleNode::setContent(gchar const *content) {
    ptr_shared old_content=_content;
    ptr_shared new_content = ( content ? share_string(content) : ptr_shared() );

    Debug::EventTracker<> tracker;
    if (new_content) {
        tracker.set<DebugSetContent>(*this, new_content);
    } else {
        tracker.set<DebugClearContent>(*this);
    }

    _content = new_content;

    if ( _content != old_content ) {
        _document->logger()->notifyContentChanged(*this, old_content, _content);
        _observers.notifyContentChanged(*this, old_content, _content);
    }
}

void
SimpleNode::setAttributeImpl(gchar const *name, gchar const *value)
{
    g_return_if_fail(name && *name);

    // sanity check: `name` must not contain whitespace
    g_assert(std::none_of(name, name + strlen(name), [](char c) { return g_ascii_isspace(c); }));

    // Check usefulness of attributes on elements in the svg namespace, optionally don't add them to tree.
    Glib::ustring element = g_quark_to_string(_name);
    //g_message("setAttribute:  %s: %s: %s", element.c_str(), name, value);
    gchar* cleaned_value = g_strdup( value );

    // Only check elements in SVG name space and don't block setting attribute to NULL.
    if( element.substr(0,4) == "svg:" && value != nullptr) {

        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        if( prefs->getBool("/options/svgoutput/check_on_editing") ) {

            gchar const *id_char = attribute("id");
            Glib::ustring id = (id_char == nullptr ? "" : id_char );
            unsigned int flags = sp_attribute_clean_get_prefs();
            bool attr_warn   = flags & SP_ATTRCLEAN_ATTR_WARN;
            bool attr_remove = flags & SP_ATTRCLEAN_ATTR_REMOVE;

            // Check attributes
            if( (attr_warn || attr_remove) && value != nullptr ) {
                bool is_useful = sp_attribute_check_attribute( element, id, name, attr_warn );
                if( !is_useful && attr_remove ) {
                    g_free( cleaned_value );
                    return; // Don't add to tree.
                }
            }

            // Check style properties -- Note: if element is not yet inserted into
            // tree (and thus has no parent), default values will not be tested.
            if( !strcmp( name, "style" ) && (flags >= SP_ATTRCLEAN_STYLE_WARN) ) {
                g_free( cleaned_value );
                cleaned_value = g_strdup( sp_attribute_clean_style( this, value, flags ).c_str() );
                // if( g_strcmp0( value, cleaned_value ) ) {
                //     g_warning( "SimpleNode::setAttribute: %s", id.c_str() );
                //     g_warning( "     original: %s", value);
                //     g_warning( "      cleaned: %s", cleaned_value);
                // }
            }
        }
    }

    GQuark const key = g_quark_from_string(name);

    AttributeRecord *ref = nullptr;
    for ( auto & existing : _attributes ) {
        if ( existing.key == key ) {
            ref = &existing;
            break;
        }
    }
    Debug::EventTracker<> tracker;

    ptr_shared old_value=( ref ? ref->value : ptr_shared() );

    ptr_shared new_value=ptr_shared();
    if (cleaned_value) { // set value of attribute
        new_value = share_string(cleaned_value);
        tracker.set<DebugSetAttribute>(*this, key, new_value);
        if (!ref) {
	    _attributes.emplace_back(key, new_value);
        } else {
            ref->value = new_value;
        }
    } else { //clearing attribute
        tracker.set<DebugClearAttribute>(*this, key);
        if (ref) {
	    _attributes.erase(std::find(_attributes.begin(),_attributes.end(),(*ref)));
        }
    }

    if ( new_value != old_value && (!old_value || !new_value || strcmp(old_value, new_value))) {
        _document->logger()->notifyAttributeChanged(*this, key, old_value, new_value);
        _observers.notifyAttributeChanged(*this, key, old_value, new_value);
        //g_warning( "setAttribute notified: %s: %s: %s: %s", name, element.c_str(), old_value, new_value ); 
    }
    g_free( cleaned_value );
}

void SimpleNode::setCodeUnsafe(int code) {
    GQuark old_code = static_cast<GQuark>(_name);
    GQuark new_code = static_cast<GQuark>(code);

    Debug::EventTracker<> tracker;
    tracker.set<DebugSetElementName>(*this, new_code);

    _name = static_cast<int>(new_code);

    if (new_code != old_code) {
        _document->logger()->notifyElementNameChanged(*this, old_code, new_code);
        _observers.notifyElementNameChanged(*this, old_code, new_code);
    }
}

void SimpleNode::addChild(Node *generic_child, Node *generic_ref) {
    g_assert(generic_child);
    g_assert(generic_child->document() == _document);
    g_assert(!generic_ref || generic_ref->document() == _document);

    SimpleNode *child=dynamic_cast<SimpleNode *>(generic_child);
    SimpleNode *ref=dynamic_cast<SimpleNode *>(generic_ref);

    g_assert(!ref || ref->_parent == this);
    g_assert(!child->_parent);

    Debug::EventTracker<DebugAddChild> tracker(*this, *child, ref);

    SimpleNode *next;
    if (ref) {
        next = ref->_next;
        ref->_next = child;

        child->_prev = ref;
    } else {
        if(_first_child) _first_child->_prev = child;
        next = _first_child;
        _first_child = child;
    }

    if (!next) { // appending?
        _last_child = child;
        // set cached position if possible when appending
        if (!ref) {
            // if !next && !ref, child is sole child
            child->_cached_position = 0;
            _cached_positions_valid = true;
        } else if (_cached_positions_valid) {
            child->_cached_position = ref->_cached_position + 1;
        }
    } else {
        next->_prev = child;
        // invalidate cached positions otherwise
        _cached_positions_valid = false;
    }

    child->_setParent(this);
    child->_next = next;
    _child_count++;

    _document->logger()->notifyChildAdded(*this, *child, ref);
    _observers.notifyChildAdded(*this, *child, ref);
}

void SimpleNode::removeChild(Node *generic_child) {
    g_assert(generic_child);
    g_assert(generic_child->document() == _document);

    SimpleNode *child=dynamic_cast<SimpleNode *>(generic_child);
    SimpleNode *ref=child->_prev;
    SimpleNode *next = child->_next;

    g_assert(child->_parent == this);

    Debug::EventTracker<DebugRemoveChild> tracker(*this, *child);

    if (ref) {
        ref->_next = next;
    } else {
        _first_child = next;
    }
    if (next) { // removing the last child?
        next->_prev = ref;
    } else {
        // removing any other child invalidates the cached positions
        _last_child = ref;
        _cached_positions_valid = false;
    }

    child->_next = nullptr;
    child->_prev = nullptr;
    child->_setParent(nullptr);
    _child_count--;

    _document->logger()->notifyChildRemoved(*this, *child, ref);
    _observers.notifyChildRemoved(*this, *child, ref);
}

void SimpleNode::changeOrder(Node *generic_child, Node *generic_ref) {
    g_assert(generic_child);
    g_assert(generic_child->document() == this->_document);
    g_assert(!generic_ref || generic_ref->document() == this->_document);

    SimpleNode *const child=dynamic_cast<SimpleNode *>(generic_child);
    SimpleNode *const ref=dynamic_cast<SimpleNode *>(generic_ref);

    g_return_if_fail(child->parent() == this);
    g_return_if_fail(child != ref);
    g_return_if_fail(!ref || ref->parent() == this);

    SimpleNode *const prev= child->_prev;

    Debug::EventTracker<DebugSetChildPosition> tracker(*this, *child, prev, ref);

    if (prev == ref) { return; }

    SimpleNode *next;

    /* Remove from old position. */
    next = child->_next;
    if (prev) {
        prev->_next = next;
    } else {
        _first_child = next;
    }
    if (next) {
        next->_prev = prev;
    } else {
        _last_child = prev;
    }

    /* Insert at new position. */
    if (ref) {
        next = ref->_next;
        ref->_next = child;
    } else {
        next = _first_child;
        _first_child = child;
    }

    child->_prev = ref;
    child->_next = next;
    
    if (next) {
        next->_prev = child;
    } else {
        _last_child = child;
    }

    _cached_positions_valid = false;

    _document->logger()->notifyChildOrderChanged(*this, *child, prev, ref);
    _observers.notifyChildOrderChanged(*this, *child, prev, ref);
}

void SimpleNode::setPosition(int pos) {
    g_return_if_fail(_parent != nullptr);

    // a position beyond the end of the list means the end of the list;
    // a negative position is the same as an infinitely large position

    SimpleNode *ref=nullptr;
    for ( SimpleNode *sibling = _parent->_first_child ;
          sibling && pos ; sibling = sibling->_next )
    {
        if ( sibling != this ) {
            ref = sibling;
            pos--;
        }
    }

    _parent->changeOrder(this, ref);
}

namespace {

void child_added(Node *node, Node *child, Node *ref, void *data) {
    reinterpret_cast<NodeObserver *>(data)->notifyChildAdded(*node, *child, ref);
}

void child_removed(Node *node, Node *child, Node *ref, void *data) {
    reinterpret_cast<NodeObserver *>(data)->notifyChildRemoved(*node, *child, ref);
}

void content_changed(Node *node, gchar const *old_content, gchar const *new_content, void *data) {
    reinterpret_cast<NodeObserver *>(data)->notifyContentChanged(*node, Util::share_unsafe((const char *)old_content), Util::share_unsafe((const char *)new_content));
}

void attr_changed(Node *node, gchar const *name, gchar const *old_value, gchar const *new_value, bool /*is_interactive*/, void *data) {
    reinterpret_cast<NodeObserver *>(data)->notifyAttributeChanged(*node, g_quark_from_string(name), Util::share_unsafe((const char *)old_value), Util::share_unsafe((const char *)new_value));
}

void order_changed(Node *node, Node *child, Node *old_ref, Node *new_ref, void *data) {
    reinterpret_cast<NodeObserver *>(data)->notifyChildOrderChanged(*node, *child, old_ref, new_ref);
}

const NodeEventVector OBSERVER_EVENT_VECTOR = {
    &child_added,
    &child_removed,
    &attr_changed,
    &content_changed,
    &order_changed
};

};

void SimpleNode::synthesizeEvents(NodeEventVector const *vector, void *data) {
    if (vector->attr_changed) {
        for ( const auto & iter : _attributes )
        {
            vector->attr_changed(this, g_quark_to_string(iter.key), nullptr, iter.value, false, data);
        }
    }
    if (vector->child_added) {
        SimpleNode *ref = nullptr;
        for ( SimpleNode *child = this->_first_child ;
              child ; child = child->_next )
        {
            vector->child_added(this, child, ref, data);
            ref = child;
        }
    }
    if (vector->content_changed) {
        vector->content_changed(this, nullptr, this->_content, data);
    }
}

void SimpleNode::synthesizeEvents(NodeObserver &observer) {
    synthesizeEvents(&OBSERVER_EVENT_VECTOR, &observer);
}

void SimpleNode::recursivePrintTree(unsigned level) {

    if (level == 0) {
        std::cout << "XML Node Tree" << std::endl;
    }
    std::cout << "XML: ";
    for (unsigned i = 0; i < level; ++i) {
        std::cout << "  ";
    }
    char const *id=attribute("id");
    if (id) {
        std::cout << id << std::endl;
    } else {
        std::cout << name() << std::endl;
    }
    for (SimpleNode *child = _first_child; child != nullptr; child = child->_next) {
        child->recursivePrintTree( level+1 );
    }
}

Node *SimpleNode::root() {
    Node *parent=this;
    while (parent->parent()) {
        parent = parent->parent();
    }

    if ( parent->type() == NodeType::DOCUMENT_NODE ) {
        for ( Node *child = _document->firstChild() ;
              child ; child = child->next() )
        {
            if ( child->type() == NodeType::ELEMENT_NODE ) {
                return child;
            }
        }
        return nullptr;
    } else if ( parent->type() == NodeType::ELEMENT_NODE ) {
        return parent;
    } else {
        return nullptr;
    }
}

void SimpleNode::cleanOriginal(Node *src, gchar const *key){
    std::vector<Node *> to_delete;
    for ( Node *child = this->firstChild() ; child != nullptr ; child = child->next() )
    {
        gchar const *id = child->attribute(key);
        if (id) {
            Node *rch = sp_repr_lookup_child(src, key, id);
            if (rch) {
                child->cleanOriginal(rch, key);
            } else {
                to_delete.push_back(child);
            }
        } else {
            to_delete.push_back(child);
        }
    }
    for (auto & i : to_delete) {
        removeChild(i);
    }
}

bool SimpleNode::equal(Node const *other, bool recursive) {
    if(strcmp(name(),other->name())!= 0){
        return false;
    }
    if (!(strcmp("sodipodi:namedview", name()))) {
        return true;
    }
    guint orig_length = 0;
    guint other_length = 0;

    if(content() && other->content() && strcmp(content(), other->content()) != 0){
        return false;
    }
    for (auto orig_attr : attributeList()) {
        for (auto other_attr : other->attributeList()) {
            const gchar * key_orig = g_quark_to_string(orig_attr.key);
            const gchar * key_other = g_quark_to_string(other_attr.key);
            if (!strcmp(key_orig, key_other) && 
                !strcmp(orig_attr.value, other_attr.value)) 
            {
                other_length++;
                break;
            }
        }
        orig_length++;
    }
    if (orig_length != other_length) {
        return false;
    }
    if (recursive) {
        //NOTE: for faster the childs need to be in the same order
        Node const *other_child = other->firstChild();
        for ( Node *child = firstChild();
              child; 
              child = child->next())
        {
            if (!child->equal(other_child, recursive)) {
                return false;
            }
            other_child = other_child->next();
            if(!other_child) {
                return false;
            }
        }
    }
    return true;
}

void SimpleNode::mergeFrom(Node const *src, gchar const *key, bool extension, bool clean) {
    g_return_if_fail(src != nullptr);
    g_return_if_fail(key != nullptr);
    g_assert(src != this);

    setContent(src->content());
    if(_parent) {
        setPosition(src->position());
    }

    if (clean) {
        Node * srcp = const_cast<Node *>(src);
        cleanOriginal(srcp, key);
    }

    for ( Node const *child = src->firstChild() ; child != nullptr ; child = child->next() )
    {
        gchar const *id = child->attribute(key);
        if (id) {
            Node *rch=sp_repr_lookup_child(this, key, id);
            if (rch && (!extension || rch->equal(child, false))) {
                rch->mergeFrom(child, key, extension);
                continue;
            } else {
                if(rch) {
                    removeChild(rch);
                }
            }
        }
        {
            guint pos = child->position();
            Node *rch=child->duplicate(_document);
            addChildAtPos(rch, pos);
            rch->release();
        }
    }

    for ( const auto & iter : src->attributeList() )
    {
        setAttribute(g_quark_to_string(iter.key), iter.value);
    }
}

}

}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :