Browse Source

fromSelector: => at:

Herbert Vojčík 9 years ago
parent
commit
c5eaa72752
4 changed files with 60 additions and 56 deletions
  1. 19 19
      src/DOMite-Tests.js
  2. 7 7
      src/DOMite-Tests.st
  3. 26 26
      src/DOMite.js
  4. 8 4
      src/DOMite.st

+ 19 - 19
src/DOMite-Tests.js

@@ -193,18 +193,18 @@ function $Domite(){return $globals.Domite||(typeof Domite=="undefined"?nil:Domit
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
 var $1,$2;
-d=$recv($Domite())._fromSelector_("#goo");
+d=$recv($Domite())._at_("#goo");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-$ctx1.sendIdx["fromSelector:"]=1;
+$ctx1.sendIdx["at:"]=1;
 //>>excludeEnd("ctx");
 $1=$recv(d)._isInvalid();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["isInvalid"]=1;
 //>>excludeEnd("ctx");
 self._assert_($1);
-d=$recv($Domite())._fromSelector_("body");
+d=$recv($Domite())._at_("body");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-$ctx1.sendIdx["fromSelector:"]=2;
+$ctx1.sendIdx["at:"]=2;
 //>>excludeEnd("ctx");
 $2=$recv(d)._isInvalid();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
@@ -214,7 +214,7 @@ self._deny_($2);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["deny:"]=1;
 //>>excludeEnd("ctx");
-d=$recv($Domite())._fromSelector_("html body div#fixture");
+d=$recv($Domite())._at_("html body div#fixture");
 self._deny_($recv(d)._isInvalid());
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
@@ -223,10 +223,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "testFromBadSelectorIsInvalid\x0a\x09| d |\x0a\x09d := Domite fromSelector: '#goo'.\x0a\x09self assert: d isInvalid.\x0a\x09d := Domite fromSelector: 'body'.\x0a\x09self deny: d isInvalid.\x0a\x09d := Domite fromSelector: 'html body div#fixture'.\x0a\x09self deny: d isInvalid",
+source: "testFromBadSelectorIsInvalid\x0a\x09| d |\x0a\x09d := Domite at: '#goo'.\x0a\x09self assert: d isInvalid.\x0a\x09d := Domite at: 'body'.\x0a\x09self deny: d isInvalid.\x0a\x09d := Domite at: 'html body div#fixture'.\x0a\x09self deny: d isInvalid",
 referencedClasses: ["Domite"],
 //>>excludeEnd("ide");
-messageSends: ["fromSelector:", "assert:", "isInvalid", "deny:"]
+messageSends: ["at:", "assert:", "isInvalid", "deny:"]
 }),
 $globals.DOMiteTest);
 
@@ -241,7 +241,7 @@ function $Domite(){return $globals.Domite||(typeof Domite=="undefined"?nil:Domit
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-d=$recv($Domite())._fromSelector_("html body div#fixture");
+d=$recv($Domite())._at_("html body div#fixture");
 $recv(d).__lt_lt($recv($Domite())._newElement_("p"));
 self._assert_($recv($recv($recv(document)._body())._innerHTML())._endsWith_(">sentinel<p></p></div>"));
 return self;
@@ -251,10 +251,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "testInsertElement\x0a\x09| d |\x0a\x09d := Domite fromSelector: 'html body div#fixture'.\x0a\x09d << (Domite newElement: 'p').\x0a\x09self assert: (document body innerHTML endsWith: '>sentinel<p></p></div>')",
+source: "testInsertElement\x0a\x09| d |\x0a\x09d := Domite at: 'html body div#fixture'.\x0a\x09d << (Domite newElement: 'p').\x0a\x09self assert: (document body innerHTML endsWith: '>sentinel<p></p></div>')",
 referencedClasses: ["Domite"],
 //>>excludeEnd("ide");
-messageSends: ["fromSelector:", "<<", "newElement:", "assert:", "endsWith:", "innerHTML", "body"]
+messageSends: ["at:", "<<", "newElement:", "assert:", "endsWith:", "innerHTML", "body"]
 }),
 $globals.DOMiteTest);
 
@@ -311,7 +311,7 @@ function $Domite(){return $globals.Domite||(typeof Domite=="undefined"?nil:Domit
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
 var $1,$3,$2,$4,$6,$5;
-d=$recv($Domite())._fromSelector_("#fixture");
+d=$recv($Domite())._at_("#fixture");
 p=$recv($Domite())._newElement_("p");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["newElement:"]=1;
@@ -355,10 +355,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "testInsertElementTwoLevelViaFragments\x0a\x09| d p s |\x0a\x09d := Domite fromSelector: '#fixture'.\x0a\x09p := Domite newElement: 'p'.\x0a\x09d << (Domite newStream << p).\x0a\x09s := Domite newElement: 'span'.\x0a\x09p << (Domite newStream << s).\x0a\x09s << (Domite newStream << 'dom-mite').\x0a\x09self assert: (document body innerHTML endsWith: '>sentinel<p><span>dom-mite</span></p></div>')",
+source: "testInsertElementTwoLevelViaFragments\x0a\x09| d p s |\x0a\x09d := Domite at: '#fixture'.\x0a\x09p := Domite newElement: 'p'.\x0a\x09d << (Domite newStream << p).\x0a\x09s := Domite newElement: 'span'.\x0a\x09p << (Domite newStream << s).\x0a\x09s << (Domite newStream << 'dom-mite').\x0a\x09self assert: (document body innerHTML endsWith: '>sentinel<p><span>dom-mite</span></p></div>')",
 referencedClasses: ["Domite"],
 //>>excludeEnd("ide");
-messageSends: ["fromSelector:", "newElement:", "<<", "newStream", "assert:", "endsWith:", "innerHTML", "body"]
+messageSends: ["at:", "newElement:", "<<", "newStream", "assert:", "endsWith:", "innerHTML", "body"]
 }),
 $globals.DOMiteTest);
 
@@ -489,7 +489,7 @@ function $Domite(){return $globals.Domite||(typeof Domite=="undefined"?nil:Domit
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-d=$recv($Domite())._fromSelector_("#fixture");
+d=$recv($Domite())._at_("#fixture");
 $recv(d).__lt_lt($recv($recv($Domite())._newStream()).__lt_lt("a foo in a bar"));
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["<<"]=1;
@@ -502,10 +502,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "testInsertStringInFragment\x0a\x09| d |\x0a\x09d := Domite fromSelector: '#fixture'.\x0a\x09d << (Domite newStream << 'a foo in a bar').\x0a\x09self assert: (document body innerHTML endsWith: '>sentinela foo in a bar</div>')",
+source: "testInsertStringInFragment\x0a\x09| d |\x0a\x09d := Domite at: '#fixture'.\x0a\x09d << (Domite newStream << 'a foo in a bar').\x0a\x09self assert: (document body innerHTML endsWith: '>sentinela foo in a bar</div>')",
 referencedClasses: ["Domite"],
 //>>excludeEnd("ide");
-messageSends: ["fromSelector:", "<<", "newStream", "assert:", "endsWith:", "innerHTML", "body"]
+messageSends: ["at:", "<<", "newStream", "assert:", "endsWith:", "innerHTML", "body"]
 }),
 $globals.DOMiteTest);
 
@@ -561,7 +561,7 @@ function $Domite(){return $globals.Domite||(typeof Domite=="undefined"?nil:Domit
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-d=$recv($Domite())._fromSelector_("#fixture");
+d=$recv($Domite())._at_("#fixture");
 p=$recv($Domite())._newElement_("p");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["newElement:"]=1;
@@ -593,10 +593,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "testMultipleInsertsWithClear\x0a\x09| d p s |\x0a\x09d := Domite fromSelector: '#fixture'.\x0a\x09p := Domite newElement: 'p'.\x0a\x09d << p.\x0a\x09s := Domite newElement: 'span'.\x0a\x09d << s.\x0a\x09d resetContents.\x0a\x09d << 'dom-mite'.\x0a\x09d resetContents.\x0a\x09d resetContents.\x0a\x09self assert: (document body innerHTML endsWith: '></div>')",
+source: "testMultipleInsertsWithClear\x0a\x09| d p s |\x0a\x09d := Domite at: '#fixture'.\x0a\x09p := Domite newElement: 'p'.\x0a\x09d << p.\x0a\x09s := Domite newElement: 'span'.\x0a\x09d << s.\x0a\x09d resetContents.\x0a\x09d << 'dom-mite'.\x0a\x09d resetContents.\x0a\x09d resetContents.\x0a\x09self assert: (document body innerHTML endsWith: '></div>')",
 referencedClasses: ["Domite"],
 //>>excludeEnd("ide");
-messageSends: ["fromSelector:", "newElement:", "<<", "resetContents", "assert:", "endsWith:", "innerHTML", "body"]
+messageSends: ["at:", "newElement:", "<<", "resetContents", "assert:", "endsWith:", "innerHTML", "body"]
 }),
 $globals.DOMiteTest);
 

+ 7 - 7
src/DOMite-Tests.st

@@ -41,17 +41,17 @@ testDefaultInsertStringInFragment
 
 testFromBadSelectorIsInvalid
 	| d |
-	d := Domite fromSelector: '#goo'.
+	d := Domite at: '#goo'.
 	self assert: d isInvalid.
-	d := Domite fromSelector: 'body'.
+	d := Domite at: 'body'.
 	self deny: d isInvalid.
-	d := Domite fromSelector: 'html body div#fixture'.
+	d := Domite at: 'html body div#fixture'.
 	self deny: d isInvalid
 !
 
 testInsertElement
 	| d |
-	d := Domite fromSelector: 'html body div#fixture'.
+	d := Domite at: 'html body div#fixture'.
 	d << (Domite newElement: 'p').
 	self assert: (document body innerHTML endsWith: '>sentinel<p></p></div>')
 !
@@ -69,7 +69,7 @@ testInsertElementTwoLevel
 
 testInsertElementTwoLevelViaFragments
 	| d p s |
-	d := Domite fromSelector: '#fixture'.
+	d := Domite at: '#fixture'.
 	p := Domite newElement: 'p'.
 	d << (Domite newStream << p).
 	s := Domite newElement: 'span'.
@@ -98,7 +98,7 @@ testInsertString
 
 testInsertStringInFragment
 	| d |
-	d := Domite fromSelector: '#fixture'.
+	d := Domite at: '#fixture'.
 	d << (Domite newStream << 'a foo in a bar').
 	self assert: (document body innerHTML endsWith: '>sentinela foo in a bar</div>')
 !
@@ -116,7 +116,7 @@ testMultipleInserts
 
 testMultipleInsertsWithClear
 	| d p s |
-	d := Domite fromSelector: '#fixture'.
+	d := Domite at: '#fixture'.
 	p := Domite newElement: 'p'.
 	d << p.
 	s := Domite newElement: 'span'.

+ 26 - 26
src/DOMite.js

@@ -6,7 +6,7 @@ $core.packages["DOMite"].transport = {"type":"amd","amdNamespace":"domite"};
 
 $core.addClass('Domite', $globals.ProtoStream, ['element', 'reference'], 'DOMite');
 //>>excludeStart("ide", pragmas.excludeIdeData);
-$globals.Domite.comment="I am (hopefully thin) wrapper around the notion of \x22cursor in a page\x22.\x0aI represent a DOM node _and_ a point where\x0ato insert new content into it.\x0a\x0aSo I play both the role of a container that inserts\x0aas well as the role of an element being inserted.\x0a\x0aI inherit from `ProtoStream`.\x0a\x0aCreation API:\x0a\x0a - `Domite new` creates an insertion point at the bottom of `<body>`.\x0a - `Domite newStream` is unique way to create pieces of content. It creates an instance \x22floating in thin air\x22 (wrapper around DOM DocumentFragment) that can be filled with any contents and then inserted in a page.\x0a - `Domite fromElement: aDomElement` wraps an element and set the cursor to its end.\x0a\x0aManipulation API:\x0a\x0a - `aDomite << obj` inserts obj at the insertion point.\x0a - `aDomite resetContents` deletes contents of the wrapped element.\x0a\x0aCursor moving API:\x0a\x0aTake this sample HTML, where `[n]` are just markers, not real content:\x0a\x0a```\x0a<body>\x0a  <h1>header</h1>\x0a  [4]<p>[2]Hello[1]world[3]</p>[5]\x0a  <small>footer</small>\x0a</body>\x0a```\x0a\x0aIf `d` is a `Domite` representing `[1]`, then:\x0a\x0a - `d setToStart` would move `d` to be at `[2]`,\x0a - `d setToEnd` would move `d` to be at `[3]`,\x0a - `d setToBefore` would move `d` to be at `[4]`, and\x0a - `d setToAfter` would move `d` to be at `[5]`.\x0a\x0aIt is not presumed one would use `setToXxx`\x0ato actually move around in a single instance.\x0aIt is envisioned this API will be used mostly\x0ain combination with `copy`, like\x0a`afterMe := self copy setToAfter`.";
+$globals.Domite.comment="I am (hopefully thin) wrapper around the notion of \x22cursor in a page\x22.\x0aI represent a DOM node _and_ a point where\x0ato insert new content into it.\x0a\x0aSo I play both the role of a container that inserts\x0aas well as the role of an element being inserted.\x0a\x0aI inherit from `ProtoStream`.\x0a\x0aCreation API:\x0a\x0a - `Domite new` creates an insertion point at the bottom of `<body>`.\x0a - `Domite newStream` is unique way to create pieces of content. It creates an instance \x22floating in thin air\x22 (wrapper around DOM DocumentFragment) that can be filled with any contents and then inserted in a page.\x0a - `Domite fromElement: aDomElement` wraps an element and set the cursor to its end.\x0a\x0aCSS selector API:\x0a\x0a - `Domite at: aSelector` wraps an element found by `document.querySelector(aSelector)`.\x0a\x0aManipulation API:\x0a\x0a - `aDomite << obj` inserts obj at the insertion point.\x0a - `aDomite resetContents` deletes contents of the wrapped element.\x0a\x0aCursor moving API:\x0a\x0aTake this sample HTML, where `[n]` are just markers, not real content:\x0a\x0a```\x0a<body>\x0a  <h1>header</h1>\x0a  [4]<p>[2]Hello[1]world[3]</p>[5]\x0a  <small>footer</small>\x0a</body>\x0a```\x0a\x0aIf `d` is a `Domite` representing `[1]`, then:\x0a\x0a - `d setToStart` would move `d` to be at `[2]`,\x0a - `d setToEnd` would move `d` to be at `[3]`,\x0a - `d setToBefore` would move `d` to be at `[4]`, and\x0a - `d setToAfter` would move `d` to be at `[5]`.\x0a\x0aIt is not presumed one would use `setToXxx`\x0ato actually move around in a single instance.\x0aIt is envisioned this API will be used mostly\x0ain combination with `copy`, like\x0a`afterMe := self copy setToAfter`.";
 //>>excludeEnd("ide");
 $core.addMethod(
 $core.method({
@@ -605,6 +605,31 @@ messageSends: ["reference:", "asJSON"]
 $globals.Domite);
 
 
+$core.addMethod(
+$core.method({
+selector: "at:",
+protocol: 'instance creation',
+fn: function (aString){
+var self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+$1=self._fromElement_($recv(document)._querySelector_(aString));
+return $1;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"at:",{aString:aString},$globals.Domite.klass)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aString"],
+source: "at: aString\x0a\x09^ self fromElement: (document querySelector: aString)",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["fromElement:", "querySelector:"]
+}),
+$globals.Domite.klass);
+
 $core.addMethod(
 $core.method({
 selector: "fromElement:",
@@ -662,31 +687,6 @@ messageSends: ["element:", "new", "referenceElement:", "yourself"]
 }),
 $globals.Domite.klass);
 
-$core.addMethod(
-$core.method({
-selector: "fromSelector:",
-protocol: 'instance creation',
-fn: function (aString){
-var self=this;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx1) {
-//>>excludeEnd("ctx");
-var $1;
-$1=self._fromElement_($recv(document)._querySelector_(aString));
-return $1;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"fromSelector:",{aString:aString},$globals.Domite.klass)});
-//>>excludeEnd("ctx");
-},
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: ["aString"],
-source: "fromSelector: aString\x0a\x09^ self fromElement: (document querySelector: aString)",
-referencedClasses: [],
-//>>excludeEnd("ide");
-messageSends: ["fromElement:", "querySelector:"]
-}),
-$globals.Domite.klass);
-
 $core.addMethod(
 $core.method({
 selector: "newElement:",

+ 8 - 4
src/DOMite.st

@@ -18,6 +18,10 @@ Creation API:
  - `Domite newStream` is unique way to create pieces of content. It creates an instance "floating in thin air" (wrapper around DOM DocumentFragment) that can be filled with any contents and then inserted in a page.
  - `Domite fromElement: aDomElement` wraps an element and set the cursor to its end.
 
+CSS selector API:
+
+ - `Domite at: aSelector` wraps an element found by `document.querySelector(aSelector)`.
+
 Manipulation API:
 
  - `aDomite << obj` inserts obj at the insertion point.
@@ -177,6 +181,10 @@ isInvalid
 
 !Domite class methodsFor: 'instance creation'!
 
+at: aString
+	^ self fromElement: (document querySelector: aString)
+!
+
 fromElement: aDomElement
 	^ self new
 		element: aDomElement;
@@ -190,10 +198,6 @@ fromElement: aDomElement cursorBefore: anotherDomElement
 		yourself
 !
 
-fromSelector: aString
-	^ self fromElement: (document querySelector: aString)
-!
-
 newElement: aString
 	^ self fromElement: (document createElement: aString)
 !