1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import java.io.IOException;
18
19 import org.htmlunit.StringWebResponse;
20 import org.htmlunit.WebClient;
21 import org.htmlunit.WebResponse;
22 import org.htmlunit.WebWindow;
23 import org.htmlunit.html.HtmlPage;
24 import org.htmlunit.html.parser.HTMLParser;
25 import org.htmlunit.javascript.HtmlUnitScriptable;
26 import org.htmlunit.javascript.JavaScriptEngine;
27 import org.htmlunit.javascript.configuration.JsxClass;
28 import org.htmlunit.javascript.configuration.JsxConstructor;
29 import org.htmlunit.javascript.configuration.JsxFunction;
30 import org.htmlunit.javascript.host.Window;
31 import org.htmlunit.javascript.host.html.HTMLDocument;
32 import org.htmlunit.javascript.host.xml.XMLDocument;
33 import org.htmlunit.util.MimeType;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @JsxClass
49 public class DOMParser extends HtmlUnitScriptable {
50
51
52
53
54 @JsxConstructor
55 public void jsConstructor() {
56
57 }
58
59
60
61
62
63
64
65
66
67 @JsxFunction
68 public Document parseFromString(final String str, final Object type) {
69 try {
70 final Document document = parseFromString(this, str, type);
71 if (document == null) {
72 throw JavaScriptEngine.typeError("Invalid 'type' parameter: " + type);
73 }
74 return document;
75 }
76 catch (final IOException e) {
77 throw JavaScriptEngine.syntaxError("Parsing failed" + e.getMessage());
78 }
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static Document parseFromString(final HtmlUnitScriptable scriptable, final String str, final Object type)
94 throws IOException {
95 if (type == null || JavaScriptEngine.isUndefined(type)) {
96 throw JavaScriptEngine.typeError("Missing 'type' parameter");
97 }
98
99 if (MimeType.TEXT_XML.equals(type)
100 || MimeType.APPLICATION_XML.equals(type)
101 || MimeType.APPLICATION_XHTML.equals(type)
102 || "image/svg+xml".equals(type)) {
103 final XMLDocument document = new XMLDocument();
104 document.setParentScope(scriptable.getParentScope());
105 document.setPrototype(scriptable.getPrototype(XMLDocument.class));
106 document.loadXML(str);
107 return document;
108 }
109
110 if (MimeType.TEXT_HTML.equals(type)) {
111 final WebWindow webWindow = scriptable.getWindow().getWebWindow();
112 final WebResponse webResponse = new StringWebResponse(str, webWindow.getEnclosedPage().getUrl());
113
114 return parseHtmlDocument(scriptable, webResponse, webWindow);
115 }
116
117 return null;
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public static Document parseHtmlDocument(final HtmlUnitScriptable scriptable, final WebResponse webResponse,
131 final WebWindow webWindow)
132 throws IOException {
133
134
135 final HtmlPage page = new HtmlPage(webResponse, webWindow);
136 page.setEnclosingWindow(null);
137 final Window window = webWindow.getScriptableObject();
138
139
140 final HTMLDocument document = new HTMLDocument();
141 document.setParentScope(window);
142 document.setPrototype(window.getPrototype(document.getClass()));
143
144 document.setDomNode(page);
145
146 final WebClient webClient = webWindow.getWebClient();
147 final HTMLParser htmlParser = webClient.getPageCreator().getHtmlParser();
148 htmlParser.parse(webResponse, page, false, true);
149 return page.getScriptableObject();
150 }
151 }