Create some content in the context of a given document

@return <ul>
<li>A {@link DocumentFragment} if <code>text</code> is
well-formed.</li>
<li><code>null</code>, if <code>text</code> is plain text or not
well formed</li>
</ul>
552826
static final DocumentFragment createContent(Document doc, String text) {

        // [#150] Text might hold XML content, which can be leniently identified by the presence
        //        of either < or & characters (other entities, like >, ", ' are not stricly XML content)
        if (text != null && (text.contains("<") || text.contains("&"))) {
            DocumentBuilder builder = JOOX.builder();

            try {

                // [#128] Trimming will get rid of leading and trailing whitespace, which would
                // otherwise cause a HIERARCHY_REQUEST_ERR raised by the parser
                text = text.trim();

                // There is a processing instruction. We can safely assume
                // valid XML and parse it as such
                if (text.startsWith("<?xml")) {
                    Document parsed = builder.parse(new InputSource(new StringReader(text)));
                    DocumentFragment fragment = parsed.createDocumentFragment();
                    fragment.appendChild(parsed.getDocumentElement());

                    return (DocumentFragment) doc.importNode(fragment, true);
                }

                // Any XML document fragment. To be on the safe side, fragments
                // are wrapped in a dummy root node
                else {
                    String wrapped = "<dummy>" + text + "</dummy>";
                    Document parsed = builder.parse(new InputSource(new StringReader(wrapped)));
                    DocumentFragment fragment = parsed.createDocumentFragment();
                    NodeList children = parsed.getDocumentElement().getChildNodes();

                    // appendChild removes children also from NodeList!
                    while (children.getLength() > 0) {
                        fragment.appendChild(children.item(0));
                    }

                    return (DocumentFragment) doc.importNode(fragment, true);
                }
            }

            // This does not occur
            catch (IOException ignore) {}

            // The XML content is invalid
            catch (SAXException ignore) {}
        }

        // Plain text or invalid XML
        return null;
    }
417907
public final Element getDocumentElement()
  {
		int dochandle=dtm.getDocument();
		int elementhandle=DTM.NULL;
		for(int kidhandle=dtm.getFirstChild(dochandle);
				kidhandle!=DTM.NULL;
				kidhandle=dtm.getNextSibling(kidhandle))
		{
			switch(dtm.getNodeType(kidhandle))
			{
			case Node.ELEMENT_NODE:
				if(elementhandle!=DTM.NULL) 
				{
					elementhandle=DTM.NULL; // More than one; ill-formed.
					kidhandle=dtm.getLastChild(dochandle); // End loop
				}
				else
					elementhandle=kidhandle;
				break;
				
			// These are harmless; document is still wellformed
			case Node.COMMENT_NODE:
			case Node.PROCESSING_INSTRUCTION_NODE:
			case Node.DOCUMENT_TYPE_NODE:
				break;
					
			default:
				elementhandle=DTM.NULL; // ill-formed
				kidhandle=dtm.getLastChild(dochandle); // End loop
				break;
			}
		}
		if(elementhandle==DTM.NULL)
			throw new DTMDOMException(DOMException.NOT_SUPPORTED_ERR);
		else
			return (Element)(dtm.getNode(elementhandle));
  }
130364
public static Content content(String somePlainText) {
		Content c = null;
		try {
			c = new Content("text/plain; charset=UTF-8", somePlainText.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) { /* UTF-8 is never unsupported */
		}
		return c;
	}
1342923
public Document literal(String text) {
        try {
            return parseString(text);        
        } catch (SAXException e) {
            throw new RuntimeException(text, e);
        }
    }
787648
function(tag) {
		if (!tag) {
			// create a document fragment to hold multiple-root elements
			if (document.createDocumentFragment) {
				return document.createDocumentFragment();
			}

			tag = '';

		} else if (tag.charAt(0) === '!') {
			return document.createComment(tag === '!' ? '' : tag.substr(1)+' ');
		}

		if (tag.toLowerCase() === 'style' && document.createStyleSheet) {
			// IE requires this interface for styles
			return document.createStyleSheet();
		}

		return document.createElement(tag);
	}
417059
public AnnotatedText build() {
    int plainTextPosition = 0;
    int totalPosition = 0;
    Map<Integer,Integer> mapping = new HashMap<>();
    mapping.put(0, 0);
    for (TextPart part : parts) {
      if (part.getType() == TextPart.Type.TEXT) {
        plainTextPosition += part.getPart().length();
        totalPosition += part.getPart().length();
      } else if (part.getType() == TextPart.Type.MARKUP) {
        totalPosition += part.getPart().length();
      } else if (part.getType() == TextPart.Type.FAKE_CONTENT) {
        plainTextPosition += part.getPart().length();
      }
      mapping.put(plainTextPosition, totalPosition);
    }
    return new AnnotatedText(parts, mapping, metaData, customMetaData);
  }
894467
public Document create(@NonNull String content, @NonNull Language language, @NonNull Map<AttributeType, ?> attributeMap) {
      return create("", content, language, attributeMap);
   }
693641
public static DocumentBuilder createDocumentBuilder() {
		final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		disableXXE(dbf);
		DocumentBuilder builder = null;
		try {
			builder = dbf.newDocumentBuilder();
		} catch (Exception e) {
			throw new UtilException(e, "Create xml document error!");
		}
		return builder;
	}
694282
public Document create(@NonNull String content, @NonNull Language language) {
      return create(StringUtils.EMPTY, content, language, Collections.emptyMap());
   }
271453
function getDocumentFragmentFromString(html) {
  // return document.createRange().createContextualFragment(html); // FU safari
  var range = document.createRange();
  range.selectNode(document.body);// safari
  return range.createContextualFragment(html);
}
467952
public JQMContent createContent() {
        JQMContent content = new JQMContent();
        Element elt = getElement();
        add(content, elt);
        return content;
    }
195634
function text(text) {
  if (typeof text === 'undefined') {
    if (this[0]) {
      return this[0].textContent.trim();
    }
    return null;
  }

  for (let i = 0; i < this.length; i += 1) {
    this[i].textContent = text;
  }
  return this;
}
164493
protected function documentFragmentCreate($source, $charset = null)
    {
        $fake              = new DOMDocumentWrapper();
        $fake->contentType = $this->contentType;
        $fake->isXML       = $this->isXML;
        $fake->isHTML      = $this->isHTML;
        $fake->isXHTML     = $this->isXHTML;
        $fake->root        = $fake->document;
        if (!$charset) {
            $charset = $this->charset;
        }
        //	$fake->documentCreate($this->charset);
        if ($source instanceof DOMNODE && !($source instanceof DOMNODELIST)) {
            $source = array($source);
        }
        if (is_array($source) || $source instanceof DOMNODELIST) {
            // dom nodes
            // load fake document
            if (!$this->documentFragmentLoadMarkup($fake, $charset)) {
                return false;
            }
            $nodes = $fake->import($source);
            foreach ($nodes as $node)
                $fake->root->appendChild($node);
        } else {
            // string markup
            $this->documentFragmentLoadMarkup($fake, $charset, $source);
        }

        return $fake;
    }
1021771
public String tokenizeToText() {

    final StringBuilder sb = new StringBuilder();
    if (isNoSeg) {
      String[] sentences = text.toArray(new String[text.size()]);
      final List<List<Token>> tokens = tokenizer.tokenize(sentences);
      for (final List<Token> tokSentence : tokens) {
        for (final Token tok : tokSentence) {
          String tokenValue = tok.getTokenValue();
          sb.append(tokenValue.trim()).append(DELIMITER);
        }
        sb.append(LINE_BREAK);
      }
    } else {
      final String[] sentences = segmenter.segmentSentence();
      final List<List<Token>> tokens = tokenizer.tokenize(sentences);
      for (final List<Token> tokSentence : tokens) {
        for (final Token token : tokSentence) {
          String tokenValue = token.getTokenValue();
          if (tokenValue.equals(RuleBasedSegmenter.PARAGRAPH)) {
            sb.append(DEFAULT_TOKEN_VALUE).append(LINE_BREAK);
          } else {
            sb.append(tokenValue.trim()).append(DELIMITER);
          }
        }
        sb.append(LINE_BREAK);
      }
    }
    return sb.toString().trim();
  }
1038008
public static Text createTextElement(String tapLine) {

        Matcher m = Patterns.TEXT_PATTERN.matcher(tapLine);

        if (m.matches()) {
            Text result = new Text(tapLine);
            result.setIndentationString(m.group(1));
            result.indentation = m.group(1).length();
            return result;
        }

        return null;
    }
307644
function( document ){
			var newNode,
				newDocument = null;

			if( this.canCloneDocument( document )){
				newDocument = document.implementation.createHTMLDocument('');
				newNode = newDocument.importNode( document.documentElement, true );
				newDocument.replaceChild(newNode, newDocument.querySelector('html'));
			}
			return (newNode && newNode.nodeType && newNode.nodeType === 1)? newDocument : document;
		}
640229
private static String getText(Document document)
	{
		try
		{
			return document.getText(0, document.getLength());
		}
		catch (BadLocationException exception)
		{
			return null;
		}
	}