Skip to content

Commit 5e11e5f

Browse files
committed
implement selectorText setter
1 parent e89e86c commit 5e11e5f

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/main/java/com/gargoylesoftware/css/dom/AbstractCSSRuleImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public AbstractCSSRuleImpl(final CSSStyleSheetImpl parentStyleSheet, final Abstr
4444

4545
/**
4646
* Sets the css text.
47-
* @param text the new css text
47+
* @param cssText the new css text
4848
*/
49-
public abstract void setCssText(String text);
49+
public abstract void setCssText(String cssText);
5050

5151
/**
5252
* Sets the parent style sheet.

src/main/java/com/gargoylesoftware/css/dom/CSSPageRuleImpl.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void setCssText(final String cssText) throws DOMException {
108108
}
109109

110110
/**
111-
* @return the selector text
111+
* @return the current selector text
112112
*/
113113
public String getSelectorText() {
114114
if (null == pseudoPage_) {
@@ -117,6 +117,39 @@ public String getSelectorText() {
117117
return pseudoPage_;
118118
}
119119

120+
/**
121+
* Sets the selector text.
122+
* @param selectorText the new selector text
123+
*/
124+
public void setSelectorText(final String selectorText) throws DOMException {
125+
try {
126+
final CSSOMParser parser = new CSSOMParser();
127+
final AbstractCSSRuleImpl r = parser.parseRule("@page " + selectorText + " {}");
128+
129+
// The rule must be a page rule
130+
if (r instanceof CSSPageRuleImpl) {
131+
pseudoPage_ = ((CSSPageRuleImpl) r).pseudoPage_;
132+
}
133+
else {
134+
throw new DOMExceptionImpl(
135+
DOMException.INVALID_MODIFICATION_ERR,
136+
DOMExceptionImpl.EXPECTING_PAGE_RULE);
137+
}
138+
}
139+
catch (final CSSException e) {
140+
throw new DOMExceptionImpl(
141+
DOMException.SYNTAX_ERR,
142+
DOMExceptionImpl.SYNTAX_ERROR,
143+
e.getMessage());
144+
}
145+
catch (final IOException e) {
146+
throw new DOMExceptionImpl(
147+
DOMException.SYNTAX_ERR,
148+
DOMExceptionImpl.SYNTAX_ERROR,
149+
e.getMessage());
150+
}
151+
}
152+
120153
/**
121154
* @return the style
122155
*/

src/test/java/com/gargoylesoftware/css/dom/CSSPageRuleImplTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ public void getSelectorText() throws Exception {
6969
assertEquals(":pseudo", value.getSelectorText());
7070
}
7171

72+
/**
73+
* @throws Exception if any error occurs
74+
*/
75+
@Test
76+
public void setSelectorText() throws Exception {
77+
final CSSPageRuleImpl value = parsePageRule("@page { color: blue; }");
78+
assertEquals("", value.getSelectorText());
79+
80+
value.setSelectorText(":pseudo");
81+
assertEquals("@page :pseudo { color: blue; }", value.getCssText());
82+
assertEquals("@page :pseudo { color: blue; }", value.toString());
83+
84+
value.setSelectorText("");
85+
assertEquals("@page { color: blue; }", value.getCssText());
86+
assertEquals("@page { color: blue; }", value.toString());
87+
}
88+
7289
/**
7390
* @throws Exception if any error occurs
7491
*/

0 commit comments

Comments
 (0)