HTMLDocument::addInlineElementAt(3) Wrap existing text in an inline element


ElementTree addInlineElementAt( ElementTree block, InlineElement inline, Int startpos, Int endpos )


block The element to add to

inline The inline element to add

startpos The start position for the inline element

endpos The end position for the inline element


Encloses existing text within an element within an inline element. The characters from startpos to endpos inclusive (with a starting index of zero) will be included in the inline element.

 p = addParagraph(parent,"abcdefghijklmnopqrstuvwxyz");
 // <p>abcde<em>f<strong>ghi</strong>jk</em>lmnopqrstuvwxyz</p>

An HTMLDocument.InvalidNesting(3kaya) Exception will be thrown if the element being added to cannot contain inline elements, or an impossible nesting situation would be created. An HTMLDocument.InvalidRange(3kaya) Exception will be thrown if the startpos or endpos are outside the text contents of the block.

 p = addParagraph(parent,"abcdefghijklmnopqrstuvwxyz");
 // exception thrown

The firstOccurs function is useful for highlighting a particular word within a string.

 haystack = getAllText(element);
 npos = firstOccurs(needle,haystack);
 if (npos < length(haystack)) {
     try {
         added = addInlineElementAt(element,Emphasis,npos,npos+length(needle)+1);
     } catch(InvalidNesting(details)) {
         // probably overlapping elements


Kaya standard library by Edwin Brady, Chris Morris and others ([email protected]). For further information see


The Kaya standard library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (version 2.1 or any later version) as published by the Free Software Foundation.