Can we get element name where cursor is placed

Having trouble installing Oxygen? Got a bug to report? Post it all here.
SmitaPatil
Posts: 93
Joined: Mon Aug 08, 2022 2:32 pm

Can we get element name where cursor is placed

Post by SmitaPatil »

Hi Team,
I want to know that if there is any JavaScript function which you have to know the element name where cursor is placed instead of calling java method every time to check it. I would call that JavaScript method. I have tried following one but couldn't able to get method which will return me element name instead of id.
this.editor.getSelectionManager().getSelection().getCaretPositionInformation()
If you need any more clarification please let me know.

Thanks & Regards,
Smita
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: Can we get element name where cursor is placed

Post by cristi_talau »

Hello,
You can obtain the selection as you mentioned:

Code: Select all

var sel = this.editor.getSelectionManager().getSelection()
Then use one of the methods here: https://www.oxygenxml.com/maven/com/oxy ... ction.html, for example:

Code: Select all

sel.getNodeAtSelection().getTagName()
Best,
Cristian
SmitaPatil
Posts: 93
Joined: Mon Aug 08, 2022 2:32 pm

Re: Can we get element name where cursor is placed

Post by SmitaPatil »

Hi Cristian,
Thank you so much!!.
just one more question, can you please tell the method for just checking if the given element has child element with some "xyz" name.

Thanks & Regards,
Smita
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: Can we get element name where cursor is placed

Post by cristi_talau »

Hello,
The getNodeAtSelection() method returns a node which implements the DOM level 3 API. So, you ca use properties like childNodes on it.
Or do you want to check whether that node can have a child "xyz"?
Best,
Cristian
SmitaPatil
Posts: 93
Joined: Mon Aug 08, 2022 2:32 pm

Re: Can we get element name where cursor is placed

Post by SmitaPatil »

Hi Cristian,
I want to check whether that node can have a child "xyz"?

Thanks & Regards,
Smita
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: Can we get element name where cursor is placed

Post by cristi_talau »

Hello,
There is no JS API for that. You have two options:
1. Use a hard-coded list of possible children / possible parents.
2. Use a Java API: https://www.oxygenxml.com/InstData/Edit ... reContext- .

Best,
Cristian
SmitaPatil
Posts: 93
Joined: Mon Aug 08, 2022 2:32 pm

Re: Can we get element name where cursor is placed

Post by SmitaPatil »

Hi Cristian,
Thank you.
Can you tell me how we can differentiate between other elements or tag and comment tag.
just wanted to know if there any method to get to know whether element is comment or not where cursor has placed.

Thanks & Regards,
Smita
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: Can we get element name where cursor is placed

Post by cristi_talau »

Hello,
You can use the "nodeType" property of the node: https://developer.mozilla.org/en-US/doc ... e/nodeType .
Best,
Cristian
SmitaPatil
Posts: 93
Joined: Mon Aug 08, 2022 2:32 pm

Re: Can we get element name where cursor is placed

Post by SmitaPatil »

Hi Cristian,
Thank you.
I tried unwrapping <#comment> element but its not working.
Can you please tell how we can just remove comment tag and just keep the text which is present inside comment.

Thanks & Regards,
Smita
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: Can we get element name where cursor is placed

Post by cristi_talau »

Hello,
If you talk about using UnwrapTagsOperation, it does not work for comments. What you can do instead is to use a custom AuthorOperation that reads the content of an XML comment, delete the XML comment node and insert the content back.
Best,
Cristian
Post Reply