Skip to content

Commit 5c40cf3

Browse files
duonglaiquangrbri
authored andcommitted
HTMLEmbedElement: add getter and setter for src and type
1 parent 2e3ecf1 commit 5c40cf3

4 files changed

Lines changed: 152 additions & 8 deletions

File tree

src/main/java/org/htmlunit/javascript/host/html/HTMLEmbedElement.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
*/
1515
package org.htmlunit.javascript.host.html;
1616

17+
import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
18+
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
1722
import org.htmlunit.html.DomElement;
1823
import org.htmlunit.html.HtmlEmbed;
24+
import org.htmlunit.html.HtmlPage;
1925
import org.htmlunit.javascript.configuration.JsxClass;
2026
import org.htmlunit.javascript.configuration.JsxConstructor;
2127
import org.htmlunit.javascript.configuration.JsxGetter;
@@ -95,6 +101,54 @@ public void setWidth_js(final String width) {
95101
getDomNodeOrDie().setAttribute("width", width);
96102
}
97103

104+
/**
105+
* Returns the value of the {@code src} property.
106+
* @return the value of the {@code src} property
107+
*/
108+
@JsxGetter
109+
public String getSrc() {
110+
final HtmlEmbed embed = (HtmlEmbed) getDomNodeOrDie();
111+
String src = embed.getAttributeDirect("src");
112+
if (ATTRIBUTE_NOT_DEFINED == src) {
113+
return src;
114+
}
115+
try {
116+
final URL expandedSrc = ((HtmlPage) embed.getPage()).getFullyQualifiedUrl(src);
117+
src = expandedSrc.toString();
118+
}
119+
catch (final MalformedURLException ignored) {
120+
// ignore
121+
}
122+
return src;
123+
}
124+
125+
/**
126+
* Sets the value of the {@code src} property.
127+
* @param src the value of the {@code src} property
128+
*/
129+
@JsxSetter
130+
public void setSrc(final String src) {
131+
getDomNodeOrDie().setAttribute("src", src);
132+
}
133+
134+
/**
135+
* Returns the value of the {@code type} property.
136+
* @return the value of the {@code type} property
137+
*/
138+
@JsxGetter
139+
public String getType() {
140+
return getDomNodeOrDie().getAttributeDirect("type");
141+
}
142+
143+
/**
144+
* Sets the value of the {@code type} property.
145+
* @param type the value of the {@code type} property
146+
*/
147+
@JsxSetter
148+
public void setType(final String type) {
149+
getDomNodeOrDie().setAttribute("type", type);
150+
}
151+
98152
/**
99153
* {@inheritDoc}
100154
*/

src/test/java/org/htmlunit/general/ElementOwnPropertiesTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5266,10 +5266,10 @@ public void dt() throws Exception {
52665266
+ "width[GSCE]",
52675267
FF_ESR = "align[GSCE],constructor(),getSVGDocument(),height[GSCE],name[GSCE],src[GSCE],type[GSCE],"
52685268
+ "width[GSCE]")
5269-
@HtmlUnitNYI(CHROME = "align[GSCE],constructor(),height[GSCE],name[GSCE],width[GSCE]",
5270-
EDGE = "align[GSCE],constructor(),height[GSCE],name[GSCE],width[GSCE]",
5271-
FF_ESR = "align[GSCE],constructor(),height[GSCE],name[GSCE],width[GSCE]",
5272-
FF = "align[GSCE],constructor(),height[GSCE],name[GSCE],width[GSCE]")
5269+
@HtmlUnitNYI(CHROME = "align[GSCE],constructor(),height[GSCE],name[GSCE],src[GSCE],type[GSCE],width[GSCE]",
5270+
EDGE = "align[GSCE],constructor(),height[GSCE],name[GSCE],src[GSCE],type[GSCE],width[GSCE]",
5271+
FF_ESR = "align[GSCE],constructor(),height[GSCE],name[GSCE],src[GSCE],type[GSCE],width[GSCE]",
5272+
FF = "align[GSCE],constructor(),height[GSCE],name[GSCE],src[GSCE],type[GSCE],width[GSCE]")
52735273
public void embed() throws Exception {
52745274
test("embed");
52755275
}

src/test/java/org/htmlunit/general/ElementPropertiesTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,10 +1837,10 @@ public void dt() throws Exception {
18371837
*/
18381838
@Test
18391839
@Alerts("align,getSVGDocument(),height,name,src,type,width")
1840-
@HtmlUnitNYI(CHROME = "align,height,name,width",
1841-
EDGE = "align,height,name,width",
1842-
FF_ESR = "align,height,name,width",
1843-
FF = "align,height,name,width")
1840+
@HtmlUnitNYI(CHROME = "align,height,name,src,type,width",
1841+
EDGE = "align,height,name,src,type,width",
1842+
FF_ESR = "align,height,name,src,type,width",
1843+
FF = "align,height,name,src,type,width")
18441844
public void embed() throws Exception {
18451845
test("embed");
18461846
}

src/test/java/org/htmlunit/javascript/host/html/HTMLEmbedElementTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,94 @@ public void setWidth() throws Exception {
221221

222222
loadPageVerifyTitle2(html);
223223
}
224+
225+
/**
226+
* @throws Exception if an error occurs
227+
*/
228+
@Test
229+
@Alerts({"§§URL§§foo.pdf", ""})
230+
public void getSrc() throws Exception {
231+
final String html = DOCTYPE_HTML
232+
+ "<html><body>\n"
233+
+ " <embed id='e1' src='foo.pdf' ></embed>\n"
234+
+ " <embed id='e2' ></embed>\n"
235+
236+
+ "<script>\n"
237+
+ LOG_TITLE_FUNCTION
238+
+ " for (var i = 1; i <= 2; i++) {\n"
239+
+ " log(document.getElementById('e' + i).src);\n"
240+
+ " }\n"
241+
+ "</script>\n"
242+
+ "</body></html>";
243+
244+
expandExpectedAlertsVariables(URL_FIRST);
245+
loadPageVerifyTitle2(html);
246+
}
247+
248+
/**
249+
* @throws Exception if an error occurs
250+
*/
251+
@Test
252+
@Alerts({"§§URL§§foo.pdf", "§§URL§§bar.pdf"})
253+
public void setSrc() throws Exception {
254+
final String html = DOCTYPE_HTML
255+
+ "<html><body>\n"
256+
+ " <embed id='e1' src='foo.pdf' ></embed>\n"
257+
258+
+ "<script>\n"
259+
+ LOG_TITLE_FUNCTION
260+
+ " var elem = document.getElementById('e1');\n"
261+
+ " log(elem.src);\n"
262+
+ " elem.src = 'bar.pdf';\n"
263+
+ " log(elem.src);\n"
264+
+ "</script>\n"
265+
+ "</body></html>";
266+
267+
expandExpectedAlertsVariables(URL_FIRST);
268+
loadPageVerifyTitle2(html);
269+
}
270+
271+
/**
272+
* @throws Exception if an error occurs
273+
*/
274+
@Test
275+
@Alerts({"application/pdf", ""})
276+
public void getType() throws Exception {
277+
final String html = DOCTYPE_HTML
278+
+ "<html><body>\n"
279+
+ " <embed id='e1' type='application/pdf' ></embed>\n"
280+
+ " <embed id='e2' ></embed>\n"
281+
282+
+ "<script>\n"
283+
+ LOG_TITLE_FUNCTION
284+
+ " for (var i = 1; i <= 2; i++) {\n"
285+
+ " log(document.getElementById('e' + i).type);\n"
286+
+ " }\n"
287+
+ "</script>\n"
288+
+ "</body></html>";
289+
290+
loadPageVerifyTitle2(html);
291+
}
292+
293+
/**
294+
* @throws Exception if an error occurs
295+
*/
296+
@Test
297+
@Alerts({"application/pdf", "text/plain"})
298+
public void setType() throws Exception {
299+
final String html = DOCTYPE_HTML
300+
+ "<html><body>\n"
301+
+ " <embed id='e1' type='application/pdf' ></embed>\n"
302+
303+
+ "<script>\n"
304+
+ LOG_TITLE_FUNCTION
305+
+ " var elem = document.getElementById('e1');\n"
306+
+ " log(elem.type);\n"
307+
+ " elem.type = 'text/plain';\n"
308+
+ " log(elem.type);\n"
309+
+ "</script>\n"
310+
+ "</body></html>";
311+
312+
loadPageVerifyTitle2(html);
313+
}
224314
}

0 commit comments

Comments
 (0)