6 #include "TDocument.hh"
10 DOM::DOMImplementation di;
12 doc = di.createDocument(TML_NS_URI, "tml:tex", dt);
13 DOM::Element root = doc.get_documentElement();
15 root.setAttributeNS(XMLNS_NS_URI, "xmlns:tml", TML_NS_URI);
17 DOM::EventTarget et(doc);
19 et.addEventListener("DOMSubtreeModified", *this, false);
22 TDocument::~TDocument()
24 //DOM::Element root = doc.get_documentElement();
25 DOM::EventTarget et(doc);
27 et.removeEventListener("DOMSubtreeModified", *this, false);
33 DOM::Element root = doc.createElementNS(TML_NS_URI, "tml:tex");
34 root.setAttributeNS(XMLNS_NS_URI, "xmlns:tml", TML_NS_URI);
35 doc.replaceChild(root, doc.get_documentElement());
40 TDocument::serialize(const char* filename) const
42 DOM::DOMImplementation di;
43 di.saveDocumentToFile(doc, filename, GDOME_SAVE_LIBXML_INDENT);
47 TDocument::makeId(unsigned id)
55 TDocument::create(const std::string& name, unsigned id) const
57 DOM::Element elem = doc.createElementNS(TML_NS_URI, "tml:" + name);
58 if (id > 0) elem.setAttribute("id", makeId(id));
63 TDocument::createC(const std::string& name, unsigned id) const
65 TNode m = create("c", id);
71 TDocument::createT(const std::string& name, const std::string& text, unsigned id) const
73 TNode t = create(name, id);
79 TDocument::nodeDepth(const DOM::Node& node)
87 n = n.get_parentNode();
93 TDocument::findCommonAncestor(const DOM::Node& node1, const DOM::Node& node2)
98 unsigned d1 = nodeDepth(n1);
99 unsigned d2 = nodeDepth(n2);
101 // cout << "finding common ancestor " << d1 << " " << d2 << endl;
106 n2 = n2.get_parentNode();
113 n1 = n1.get_parentNode();
121 n1 = n1.get_parentNode();
122 n2 = n2.get_parentNode();
129 TDocument::findIdNode(const DOM::Node& node)
134 if (n.get_nodeType() == DOM::Node::ELEMENT_NODE)
137 if (el.hasAttribute("id")) return el;
139 n = n.get_parentNode();
146 TDocument::getNodeByIdAux(const TNode& node, const std::string& id)
148 if (node.hasId(id)) return node;
150 for (TNode p = node.first(); p; p = p.next())
151 if (TNode res = getNodeByIdAux(p, id)) return res;
156 TDocument::getNodeById(unsigned id) const
158 DOM::Element root = doc.get_documentElement();
160 return getNodeByIdAux(root, makeId(id));
164 TDocument::handleEvent(const DOM::Event& ev)
166 DOM::MutationEvent me(ev);
171 cout << "TDocument::handleEvent DIRTY BEFORE = " << dirty.getAttribute("id") << endl;
173 cout << "TDocument::handleEvent DIRTY BEFORE = (nil)" << endl;
176 if (DOM::Node node = me.get_target())
178 dirty = findIdNode(findCommonAncestor(dirty, node));
180 dirty = findIdNode(node);
185 cout << "TDocument::handleEvent target = " << DOM::Node(me.get_target()).get_nodeName() << " DIRTY AFTER = "
186 << dirty.getAttribute("id") << " ME = " << DOM::Node(me.get_target()).get_nodeName() << endl;