Query the document

Querying the document can be done using several of the Fonto Editor APIs, this document highlights some of them.

When adding functionality to the editor it is often required to query the document for nodes and information on the nodes. The Fonto Editor API provides several different methods of doing so.

Getting the document node

The document node can be acquired by using the getDocumentNode function of the DocumentsManager.

This function accepts a DocumentId as its only argument which is used to get the corresponding document node. The DocumentId can be acquired using the focusedDocumentId property of the SelectionManager.


var documentId = selectionManager.focusedDocumentId,
	documentNode = documentsManager.getDocumentNode(documentId);

Getting the selected element

The selected element can be acquired using the getSelectedElement function of the SelectionManager.

This function gets the currently selected element, being the element fully contained in the selection, or the common ancestor of the selection.


var selectedElement = selectionManager.getSelectedElement();

Getting a specific node or a set of nodes

Querying the document to acquire a specific node or set of nodes can be done using the evaluateXPathToFirstNode and evaluateXPathToNodes functions.

These functions require an XPath query, a context Node and a Blueprint.

When no Blueprint is available for use a ReadOnlyBlueprint can be used.


// Example: Getting a direct child node of the selected element.
var selectedElement = selectionManager.getSelectedElement(),
	childNode = evaluateXPathToFirstNode('child::CHILD_NODE_NAME', selectedElement, readOnlyBlueprint);

// Example: Getting all paragraphs in a document
var documentId = selectionManager.focusedDocumentId,
	documentNode = documentsManager.getDocumentNode(documentId),
	paragraphNodes = evaluateXPathToNodes('descendant::PARAGRAPH_NODE_NAME', documentNode, readOnlyBlueprint);