Skip to content

Commit

Permalink
reducing the core-js dependencies (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 9, 2023
1 parent 7b87556 commit c35a38b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/htmlunit/html/impl/SimpleRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.htmlunit.SgmlPage;
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.html.DomDocumentFragment;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.DomNodeList;
Expand Down Expand Up @@ -153,7 +152,7 @@ else if (end == e) {

// Remove everything following the selection end from the clones.
if (endClone == null) {
throw Context.reportRuntimeError("Unable to find end node clone.");
throw new IllegalStateException("Unable to find end node clone.");
}
deleteAfter(endClone, endOffset_);
for (DomNode n = endClone; n != null; n = n.getParentNode()) {
Expand All @@ -164,7 +163,7 @@ else if (end == e) {

// Remove everything prior to the selection start from the clones.
if (startClone == null) {
throw Context.reportRuntimeError("Unable to find start node clone.");
throw new IllegalStateException("Unable to find start node clone.");
}
deleteBefore(startClone, startOffset_);
for (DomNode n = startClone; n != null; n = n.getParentNode()) {
Expand Down
101 changes: 73 additions & 28 deletions src/main/java/org/htmlunit/javascript/host/dom/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,12 @@ public Object createContextualFragment(final String valueAsString) {
*/
@JsxFunction
public Object extractContents() {
return getSimpleRange().extractContents().getScriptableObject();
try {
return getSimpleRange().extractContents().getScriptableObject();
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand Down Expand Up @@ -388,15 +393,25 @@ else if ((nodeComparision & Node.DOCUMENT_POSITION_PRECEDING) != 0) {
*/
@JsxFunction
public Object cloneContents() {
return getSimpleRange().cloneContents().getScriptableObject();
try {
return getSimpleRange().cloneContents().getScriptableObject();
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
* Deletes the contents of the range.
*/
@JsxFunction
public void deleteContents() {
getSimpleRange().deleteContents();
try {
getSimpleRange().deleteContents();
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand All @@ -406,7 +421,12 @@ public void deleteContents() {
*/
@JsxFunction
public void insertNode(final Node newNode) {
getSimpleRange().insertNode(newNode.getDomNodeOrDie());
try {
getSimpleRange().insertNode(newNode.getDomNodeOrDie());
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand All @@ -415,7 +435,12 @@ public void insertNode(final Node newNode) {
*/
@JsxFunction
public void surroundContents(final Node newNode) {
getSimpleRange().surroundContents(newNode.getDomNodeOrDie());
try {
getSimpleRange().surroundContents(newNode.getDomNodeOrDie());
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand All @@ -424,7 +449,12 @@ public void surroundContents(final Node newNode) {
*/
@JsxFunction
public Object cloneRange() {
return new Range(getSimpleRange().cloneRange());
try {
return new Range(getSimpleRange().cloneRange());
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand All @@ -441,7 +471,12 @@ public void detach() {
*/
@JsxFunction(functionName = "toString")
public String jsToString() {
return getSimpleRange().toString();
try {
return getSimpleRange().toString();
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand All @@ -456,18 +491,23 @@ public ClientRectList getClientRects() {
rectList.setParentScope(w);
rectList.setPrototype(getPrototype(rectList.getClass()));

// simple impl for now
for (final DomNode node : getSimpleRange().containedNodes()) {
final ScriptableObject scriptable = node.getScriptableObject();
if (scriptable instanceof HTMLElement) {
final ClientRect rect = new ClientRect(0, 0, 1, 1);
rect.setParentScope(w);
rect.setPrototype(getPrototype(rect.getClass()));
rectList.add(rect);
try {
// simple impl for now
for (final DomNode node : getSimpleRange().containedNodes()) {
final ScriptableObject scriptable = node.getScriptableObject();
if (scriptable instanceof HTMLElement) {
final ClientRect rect = new ClientRect(0, 0, 1, 1);
rect.setParentScope(w);
rect.setPrototype(getPrototype(rect.getClass()));
rectList.add(rect);
}
}
}

return rectList;
return rectList;
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}

/**
Expand All @@ -481,18 +521,23 @@ public ClientRect getBoundingClientRect() {
rect.setParentScope(getWindow());
rect.setPrototype(getPrototype(rect.getClass()));

// simple impl for now
for (final DomNode node : getSimpleRange().containedNodes()) {
final ScriptableObject scriptable = node.getScriptableObject();
if (scriptable instanceof HTMLElement) {
final ClientRect childRect = ((HTMLElement) scriptable).getBoundingClientRect();
rect.setTop(Math.min(rect.getTop(), childRect.getTop()));
rect.setLeft(Math.min(rect.getLeft(), childRect.getLeft()));
rect.setRight(Math.max(rect.getRight(), childRect.getRight()));
rect.setBottom(Math.max(rect.getBottom(), childRect.getBottom()));
try {
// simple impl for now
for (final DomNode node : getSimpleRange().containedNodes()) {
final ScriptableObject scriptable = node.getScriptableObject();
if (scriptable instanceof HTMLElement) {
final ClientRect childRect = ((HTMLElement) scriptable).getBoundingClientRect();
rect.setTop(Math.min(rect.getTop(), childRect.getTop()));
rect.setLeft(Math.min(rect.getLeft(), childRect.getLeft()));
rect.setRight(Math.max(rect.getRight(), childRect.getRight()));
rect.setBottom(Math.max(rect.getBottom(), childRect.getBottom()));
}
}
}

return rect;
return rect;
}
catch (final IllegalStateException e) {
throw Context.reportRuntimeError(e.getMessage());
}
}
}
1 change: 0 additions & 1 deletion src/test/java/org/htmlunit/archunit/ArchitectureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ public void check(final JavaMethod method, final ConditionEvents events) {
.and().doNotHaveFullyQualifiedName("org.htmlunit.html.HtmlDialog$1")
.and().doNotHaveFullyQualifiedName("org.htmlunit.html.HtmlInput")
.and().doNotHaveFullyQualifiedName("org.htmlunit.html.HtmlPage")
.and().doNotHaveFullyQualifiedName("org.htmlunit.html.impl.SimpleRange")
.and().doNotHaveFullyQualifiedName("org.htmlunit.util.DebuggingWebConnection")
.and().doNotHaveFullyQualifiedName("org.htmlunit.util.WebClientUtils")
.and().doNotHaveFullyQualifiedName("org.htmlunit.websocket.JettyWebSocketAdapter")
Expand Down

0 comments on commit c35a38b

Please sign in to comment.